Coverage Report - kg.apc.jmeter.reporters.FlexibleFileWriterGui
 
Classes in this File Line Coverage Branch Coverage Complexity
FlexibleFileWriterGui
100%
112/112
75%
3/4
1.25
FlexibleFileWriterGui$1
N/A
N/A
1.25
FlexibleFileWriterGui$CopyAction
20%
1/5
N/A
1.25
FlexibleFileWriterGui$CopyAction$1
0%
0/6
0%
0/2
1.25
 
 1  
 package kg.apc.jmeter.reporters;
 2  
 
 3  
 import java.awt.BorderLayout;
 4  
 import java.awt.GridBagConstraints;
 5  
 import java.awt.GridBagLayout;
 6  
 import java.awt.Insets;
 7  
 import java.awt.datatransfer.Clipboard;
 8  
 import java.awt.datatransfer.ClipboardOwner;
 9  
 import java.awt.datatransfer.DataFlavor;
 10  
 import java.awt.datatransfer.Transferable;
 11  
 import java.awt.event.ActionEvent;
 12  
 import java.awt.event.ActionListener;
 13  
 import javax.swing.JButton;
 14  
 import javax.swing.JCheckBox;
 15  
 import javax.swing.JComponent;
 16  
 import javax.swing.JLabel;
 17  
 import javax.swing.JPanel;
 18  
 import javax.swing.JTextArea;
 19  
 import javax.swing.JTextField;
 20  
 
 21  
 import kg.apc.jmeter.JMeterPluginsUtils;
 22  
 import kg.apc.jmeter.gui.BrowseAction;
 23  
 import kg.apc.jmeter.gui.GuiBuilderHelper;
 24  
 import org.apache.jmeter.testelement.TestElement;
 25  
 import org.apache.jmeter.visualizers.gui.AbstractListenerGui;
 26  
 
 27  
 public class FlexibleFileWriterGui extends AbstractListenerGui implements ClipboardOwner {
 28  
 
 29  
     public static final String WIKIPAGE = "FlexibleFileWriter";
 30  
     private JTextField filename;
 31  
     private JTextField columns;
 32  
     private JCheckBox overwrite;
 33  
     private JTextArea header;
 34  
     private JTextArea footer;
 35  7
     private String[] fields = {
 36  
             "endTime", "Epoch time when the request was ended",
 37  
             "endTimeMillis", "Same as endTime, but divided by 1000 (surrogate field, eg. 1311122631.104)",
 38  
             "isFailed", "If response was marked as failed (surrogate field)",
 39  
             "isSuccsessful", "If response was marked as successful",
 40  
             "latency", "Latency, time to first response byte received (if available)",
 41  
             "latencyMicros", "Same as latency, but multiplied by 1000 (surrogate field)",
 42  
             "connectTime", "Time to establish connection",
 43  
             "receivedBytes", "Number of request bytes received (if available)",
 44  
             "requestData", "Request data from sample",
 45  
             "responseCode", "Response code (eg. 200, 404, etc.)",
 46  
             "responseData", "Response data",
 47  
             "responseHeaders", "Response headers (if present in sample)",
 48  
             "responseMessage", "Response message (eg. OK, Not Found, etc.)",
 49  
             "responseTime", "Response time, time to full response loaded",
 50  
             "responseTimeMicros", "Same as responseTime, but multiplied by 1000 (surrogate field)",
 51  
             "sampleLabel", "Name of the sampler that made the request",
 52  
             "sentBytes", "Number of request bytes sent (if available)",
 53  
             "startTime", "Epoch time when the request was started",
 54  
             "startTimeMillis", "Same as startTime, but divided by 1000 (surrogate field, eg. 1311121131.062)",
 55  
             "threadName", "Name of thread in Thread Group that processed the request",
 56  
             "variable#<N>", "Sample variable with index N (eg. variable#2), see help for details"
 57  
     };
 58  
 
 59  
     public FlexibleFileWriterGui() {
 60  7
         super();
 61  7
         init();
 62  7
         initFields();
 63  7
     }
 64  
 
 65  
     @Override
 66  
     public String getStaticLabel() {
 67  16
         return JMeterPluginsUtils.prefixLabel("Flexible File Writer");
 68  
     }
 69  
 
 70  
     @Override
 71  
     public String getLabelResource() {
 72  1
         return getClass().getCanonicalName();
 73  
     }
 74  
 
 75  
     @Override
 76  
     public TestElement createTestElement() {
 77  1
         TestElement te = new FlexibleFileWriter();
 78  1
         modifyTestElement(te);
 79  1
         te.setComment(JMeterPluginsUtils.getWikiLinkText(WIKIPAGE));
 80  1
         return te;
 81  
     }
 82  
 
 83  
     @Override
 84  
     public void modifyTestElement(TestElement te) {
 85  2
         super.configureTestElement(te);
 86  2
         if (te instanceof FlexibleFileWriter) {
 87  2
             FlexibleFileWriter fw = (FlexibleFileWriter) te;
 88  2
             fw.setFilename(filename.getText());
 89  2
             fw.setColumns(columns.getText());
 90  2
             fw.setOverwrite(overwrite.isSelected());
 91  2
             fw.setFileHeader(header.getText());
 92  2
             fw.setFileFooter(footer.getText());
 93  
         }
 94  2
     }
 95  
 
 96  
     @Override
 97  
     public void clearGui() {
 98  1
         super.clearGui();
 99  1
         initFields();
 100  1
     }
 101  
 
 102  
     private void initFields() {
 103  8
         filename.setText("testResults.txt");
 104  8
         columns.setText("endTimeMillis|\\t|"
 105  
                 + "responseTime|\\t|latency|\\t|"
 106  
                 + "sentBytes|\\t|receivedBytes|\\t|"
 107  
                 + "isSuccessful|\\t|responseCode|\\r\\n");
 108  8
         overwrite.setSelected(false);
 109  8
         header.setText("endTimeMillis\tresponseTime\tlatency\tsentBytes\t"
 110  
                 + "receivedBytes\tisSuccessful\tresponseCode\n");
 111  8
         footer.setText("");
 112  8
     }
 113  
 
 114  
     @Override
 115  
     public void configure(TestElement element) {
 116  1
         super.configure(element);
 117  1
         FlexibleFileWriter fw = (FlexibleFileWriter) element;
 118  1
         filename.setText(fw.getFilename());
 119  1
         columns.setText(fw.getColumns());
 120  1
         overwrite.setSelected(fw.isOverwrite());
 121  1
         header.setText(fw.getFileHeader());
 122  1
         footer.setText(fw.getFileFooter());
 123  1
     }
 124  
 
 125  
     private void init() {
 126  7
         setLayout(new BorderLayout(0, 5));
 127  7
         setBorder(makeBorder());
 128  
 
 129  7
         add(JMeterPluginsUtils.addHelpLinkToPanel(makeTitlePanel(), WIKIPAGE), BorderLayout.NORTH);
 130  
 
 131  7
         JPanel mainPanel = new JPanel(new GridBagLayout());
 132  
 
 133  7
         GridBagConstraints labelConstraints = new GridBagConstraints();
 134  7
         labelConstraints.anchor = GridBagConstraints.FIRST_LINE_END;
 135  
 
 136  7
         GridBagConstraints editConstraints = new GridBagConstraints();
 137  7
         editConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
 138  7
         editConstraints.weightx = 1.0;
 139  7
         editConstraints.fill = GridBagConstraints.HORIZONTAL;
 140  
 
 141  7
         addToPanel(mainPanel, labelConstraints, 0, 1, new JLabel("Filename: ", JLabel.RIGHT));
 142  7
         addToPanel(mainPanel, editConstraints, 1, 1, filename = new JTextField(20));
 143  7
         JButton browseButton = new JButton("Browse...");
 144  7
         addToPanel(mainPanel, labelConstraints, 2, 1, browseButton);
 145  7
         GuiBuilderHelper.strechItemToComponent(filename, browseButton);
 146  7
         browseButton.addActionListener(new BrowseAction(filename));
 147  
 
 148  7
         addToPanel(mainPanel, labelConstraints, 0, 2, new JLabel("Overwrite existing file: ", JLabel.RIGHT));
 149  7
         addToPanel(mainPanel, editConstraints, 1, 2, overwrite = new JCheckBox());
 150  
 
 151  7
         addToPanel(mainPanel, labelConstraints, 0, 3, new JLabel("Write File Header: ", JLabel.RIGHT));
 152  7
         header = new JTextArea();
 153  7
         header.setLineWrap(true);
 154  7
         addToPanel(mainPanel, editConstraints, 1, 3, GuiBuilderHelper.getTextAreaScrollPaneContainer(header, 3));
 155  
 
 156  7
         editConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
 157  7
         labelConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
 158  7
         addToPanel(mainPanel, labelConstraints, 0, 4, new JLabel("Record each sample as: ", JLabel.RIGHT));
 159  7
         addToPanel(mainPanel, editConstraints, 1, 4, columns = new JTextField(20));
 160  
 
 161  7
         editConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
 162  7
         labelConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
 163  7
         addToPanel(mainPanel, labelConstraints, 0, 5, new JLabel("Write File Footer: ", JLabel.RIGHT));
 164  7
         footer = new JTextArea();
 165  7
         footer.setLineWrap(true);
 166  7
         addToPanel(mainPanel, editConstraints, 1, 5, GuiBuilderHelper.getTextAreaScrollPaneContainer(footer, 3));
 167  
 
 168  7
         JPanel container = new JPanel(new BorderLayout());
 169  7
         container.add(mainPanel, BorderLayout.NORTH);
 170  7
         add(container, BorderLayout.CENTER);
 171  
 
 172  7
         add(createHelperPanel(), BorderLayout.SOUTH);
 173  7
     }
 174  
 
 175  
     private JPanel createHelperPanel() {
 176  7
         JPanel ret = new JPanel(new GridBagLayout());
 177  
 
 178  7
         GridBagConstraints labelConstraints = new GridBagConstraints();
 179  7
         labelConstraints.insets = new Insets(0, 0, 10, 0);
 180  7
         labelConstraints.gridx = 0;
 181  7
         labelConstraints.fill = GridBagConstraints.HORIZONTAL;
 182  7
         labelConstraints.gridwidth = 2;
 183  
 
 184  7
         ret.add(new JLabel("Available sample fields (click any button to copy the field to clipboard):"), labelConstraints);
 185  
 
 186  7
         GridBagConstraints buttonConstraints = new GridBagConstraints();
 187  7
         buttonConstraints.insets = new Insets(4, 0, 0, 0);
 188  7
         buttonConstraints.gridx = 0;
 189  7
         buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
 190  
 
 191  7
         GridBagConstraints detailConstraints = new GridBagConstraints();
 192  7
         detailConstraints.insets = new Insets(4, 10, 0, 0);
 193  7
         detailConstraints.weightx = 1.0;
 194  7
         detailConstraints.fill = GridBagConstraints.HORIZONTAL;
 195  7
         detailConstraints.gridx = 1;
 196  7
         detailConstraints.anchor = GridBagConstraints.WEST;
 197  
 
 198  7
         int line = 1;
 199  
 
 200  7
         CopyAction copyAction = new CopyAction();
 201  
 
 202  154
         for (int i = 0; i < fields.length / 2; i++) {
 203  147
             JButton fieldButton = new JButton(fields[2 * i]);
 204  147
             fieldButton.addActionListener(copyAction);
 205  
 
 206  147
             JTextField fieldDescription = new JTextField(fields[2 * i + 1]);
 207  147
             fieldDescription.setEditable(false);
 208  147
             fieldDescription.setBorder(null);
 209  147
             fieldDescription.setOpaque(false);
 210  
 
 211  147
             GuiBuilderHelper.strechItemToComponent(fieldDescription, fieldButton);
 212  
 
 213  147
             buttonConstraints.gridy = line;
 214  147
             detailConstraints.gridy = line;
 215  
 
 216  147
             ret.add(fieldButton, buttonConstraints);
 217  147
             ret.add(fieldDescription, detailConstraints);
 218  
 
 219  147
             line++;
 220  
         }
 221  7
         return ret;
 222  
     }
 223  
 
 224  
     private void addToPanel(JPanel panel, GridBagConstraints constraints, int col, int row, JComponent component) {
 225  77
         constraints.gridx = col;
 226  77
         constraints.gridy = row;
 227  77
         panel.add(component, constraints);
 228  77
     }
 229  
 
 230  
     @Override
 231  
     public void lostOwnership(Clipboard clipboard, Transferable contents) {
 232  
         // do nothing
 233  1
     }
 234  
 
 235  14
     private class CopyAction
 236  
             implements ActionListener {
 237  
 
 238  
         @Override
 239  
         public void actionPerformed(final ActionEvent e) {
 240  0
             Clipboard clipboard = getToolkit().getSystemClipboard();
 241  0
             Transferable transferable = new Transferable() {
 242  
 
 243  
                 @Override
 244  
                 public Object getTransferData(DataFlavor flavor) {
 245  0
                     if (isDataFlavorSupported(flavor)) {
 246  0
                         return "|" + ((JButton) e.getSource()).getText() + "|";
 247  
                     }
 248  0
                     return null;
 249  
                 }
 250  
 
 251  
                 @Override
 252  
                 public DataFlavor[] getTransferDataFlavors() {
 253  0
                     return new DataFlavor[]{
 254  
                             DataFlavor.stringFlavor
 255  
                     };
 256  
                 }
 257  
 
 258  
                 @Override
 259  
                 public boolean isDataFlavorSupported(DataFlavor flavor) {
 260  0
                     return DataFlavor.stringFlavor.equals(flavor);
 261  
                 }
 262  
             };
 263  0
             clipboard.setContents(transferable, FlexibleFileWriterGui.this);
 264  0
         }
 265  
     }
 266  
 }