Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

enhancements to VariablesFromCSV plugin #5

Merged
merged 7 commits into from
May 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@
<artifactId>qpid-client</artifactId>
<version>0.20</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -319,6 +325,15 @@
</executions>

</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- several of the unit tests require a large heap -->
<argLine>-Xmx784m</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
70 changes: 31 additions & 39 deletions src/kg/apc/jmeter/config/TestCsvFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,66 @@
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TestCsvFileAction implements ActionListener {

private final JTextField filename;
private final JTextField prefix;
private final JTextField separator;
private final JTextArea infoArea;
private final VariablesFromCSVGui variablesCsvUi;

public TestCsvFileAction(JTextField fileName, JTextField prefix, JTextField separator, JTextArea infoArea) {
this.filename = fileName;
this.prefix = prefix;
this.separator = separator;
this.infoArea = infoArea;
public TestCsvFileAction(VariablesFromCSVGui variablesCsvUi) {
this.variablesCsvUi = variablesCsvUi;
}

@Override
public void actionPerformed(ActionEvent e) {
JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
infoArea.setText("");
infoArea.setForeground(Color.black);

VariablesFromCSV testElem = (VariablesFromCSV)variablesCsvUi.createTestElement();

boolean noValues = true;
String msgVars = "";
int count = 0;

File f = new File(filename.getText());
if (!f.exists()) {
reportError("File '" + filename.getText() + "' was not found...");
return;
} else {
try {
VariableFromCsvFileReader reader = new VariableFromCsvFileReader(filename.getText());
Map<String, String> vars = reader.getDataAsMap(prefix.getText(), separator.getText());
Iterator<String> iter = vars.keySet().iterator();
while (iter.hasNext()) {
String var = iter.next();
String value = vars.get(var);
if (!"".equals(value)) {
noValues = false;
}
msgVars = msgVars + "${" + var + "} = " + vars.get(var) + "\n";
count++;
try {
Map<String, String> vars = testElem.getArgumentsAsMap();
Iterator<String> iter = vars.keySet().iterator();

while (iter.hasNext()) {
String var = iter.next();
String value = vars.get(var);
if (!"".equals(value)) {
noValues = false;
}
} catch (Exception ex) {
reportError("Error processing file: " + ex.toString());
msgVars = msgVars + "${" + var + "} = " + vars.get(var) + "\n";
count++;
}
}
if (count == 0) {
reportError("File parsed, but no variable found.");
} else if (noValues) {
reportOk("WARNING: File parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found, but no variable have value!");
reportOk(msgVars);
} else {
reportOk("File successfuly parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found:");
reportOk(msgVars);

if (count == 0) {
reportError("File parsed, but no variable found.");
} else if (noValues) {
reportOk("WARNING: File parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found, but no variable have value!");
reportOk(msgVars);
} else {
reportOk("File successfuly parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found:");
reportOk(msgVars);
}
} catch (Exception ex) {
reportError("Error processing file: " + ex.toString());
}
}

private void reportError(String msg) {
JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
infoArea.setText(infoArea.getText() + "Problem detected: " + msg + "\n");
infoArea.setForeground(Color.red);
}

private void reportOk(String string) {
JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
infoArea.setText(infoArea.getText() + string + "\n");
}
}
82 changes: 58 additions & 24 deletions src/kg/apc/jmeter/config/VariableFromCsvFileReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kg.apc.jmeter.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
Expand All @@ -18,46 +17,81 @@
public class VariableFromCsvFileReader {

private static final Logger log = LoggingManager.getLoggerForClass();
private File file;
private BufferedReader input;

/**
* Initialize a new CSV reader for the named file.
*
* @param csvFileName name of the CSV input file
*/
public VariableFromCsvFileReader(String csvFileName) {
file = new File(csvFileName);
try {
input = new BufferedReader(new FileReader(csvFileName));
} catch (FileNotFoundException ex) {
log.error("File not found: " + ex.getMessage());
}
}

/**
* Initialize a new CSV reader with a BufferedReader as input.
*
* @param input the CSV input
*/
public VariableFromCsvFileReader(BufferedReader input) {
this.input = input;
}

/**
* Parses (name, value) pairs from the input and returns the result as a Map. The name is taken from the first column and
* value from the second column. If an input line contains only one column its value is defaulted to an empty string.
* Any extra columns are ignored.
*
* @param prefix a prefix to apply to the mapped variable names
* @param separator the field delimiter
* @return a map of (name, value) pairs
*/
public Map<String, String> getDataAsMap(String prefix, String separator) {
return getDataAsMap(prefix, separator, 0);
}

/**
* Parses (name, value) pairs from the input and returns the result as a Map, with the option to skip the first line.
* The name is taken from the first column and value from the second column. If an input line contains only one
* column its value is defaulted to an empty string. Any extra columns are ignored.
*
* If the input contains headers, call with skipLines equal to the number of lines of headers.
*
* @param prefix a prefix to apply to the mapped variable names
* @param separator the field delimiter
* @param skipLines the number of lines at the beginning of the input to skip
* @return a map of (name, value) pairs
*/
public Map<String, String> getDataAsMap(String prefix, String separator, int skipLines) {
if (separator.isEmpty()) {
throw new IllegalArgumentException("CSV separator cannot be empty");
}

HashMap ret = new HashMap<String, String>();
if (file.exists()) {
Map<String, String> variables = new HashMap<String, String>();
if (input != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
String[] lineValues = JOrphanUtils.split(line, separator, false);
String line;
int lineNum = 0;
while ((line = input.readLine()) != null) {
if (++lineNum > skipLines) {
String[] lineValues = JOrphanUtils.split(line, separator, false);

switch (lineValues.length) {
case 1:
if (lineValues.length == 1) {
log.warn("Less than 2 columns at line: " + line);
ret.put(prefix + lineValues[0], "");
break;
case 2:
ret.put(prefix + lineValues[0], lineValues[1]);
break;
default:
log.warn("Bad format for line: " + line);
break;
variables.put(prefix + lineValues[0], "");
} else if (lineValues.length >= 2) {
variables.put(prefix + lineValues[0], lineValues[1]);
}
}

line = reader.readLine();
}
} catch (FileNotFoundException ex) {
log.error("File not found: " + ex.getMessage());
} catch (IOException ex) {
log.error("Error while reading: " + ex.getMessage());
}
}
return ret;
return variables;
}
}
13 changes: 11 additions & 2 deletions src/kg/apc/jmeter/config/VariablesFromCSV.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public class VariablesFromCSV extends Arguments{
public static final String VARIABLE_PREFIX = "variablesPrefix";
public static final String FILENAME = "filename";
public static final String SEPARATOR = "delimiter";
public static final String SKIP_LINES = "skipLines";
public static final String STORE_SYS_PROP = "storeSysProp";

//It seems org.apache.jmeter.engine.Precompiler requires only this method
//It seems org.apache.jmeter.engine.Precompiler requires only thishttps://groups.google.com/forum/#!topic/jmeter-plugins/gWn7MTgvTfE method
@Override
public Map<String, String> getArgumentsAsMap() {
Map<String, String> variables = new VariableFromCsvFileReader(getFileName()).getDataAsMap(getVariablePrefix(), getSeparator());
Map<String, String> variables = new VariableFromCsvFileReader(getFileName()).getDataAsMap(getVariablePrefix(), getSeparator(), getSkipLines());
//store in System Properties also
if(isStoreAsSystemProperty()) {
Iterator<String> iter = variables.keySet().iterator();
Expand Down Expand Up @@ -56,6 +57,14 @@ public void setSeparator(String separator) {
setProperty(SEPARATOR, separator);
}

public int getSkipLines() {
return getPropertyAsInt(SKIP_LINES, 0);
}

public void setSkipLines(int skipLines) {
setProperty(SKIP_LINES, skipLines);
}

public boolean isStoreAsSystemProperty() {
return getPropertyAsBoolean(STORE_SYS_PROP);
}
Expand Down
21 changes: 16 additions & 5 deletions src/kg/apc/jmeter/config/VariablesFromCSVGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class VariablesFromCSVGui extends AbstractConfigGui {
private JTextField fileName;
private JTextField variablePrefix;
private JTextField separator;
private JTextField skipLines;
private JCheckBox storeSysProp;

private JButton browseButton;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void configure(TestElement element) {
fileName.setText(element.getPropertyAsString(VariablesFromCSV.FILENAME));
variablePrefix.setText(element.getPropertyAsString(VariablesFromCSV.VARIABLE_PREFIX));
separator.setText(element.getPropertyAsString(VariablesFromCSV.SEPARATOR));
skipLines.setText(element.getPropertyAsString(VariablesFromCSV.SKIP_LINES));
storeSysProp.setSelected(element.getPropertyAsBoolean(VariablesFromCSV.STORE_SYS_PROP));
}

Expand All @@ -74,6 +76,7 @@ public void modifyTestElement(TestElement te) {
varsCsv.setFileName(fileName.getText());
varsCsv.setVariablePrefix(variablePrefix.getText());
varsCsv.setSeparator(separator.getText());
varsCsv.setSkipLines(Integer.parseInt(skipLines.getText()));
varsCsv.setStoreAsSystemProperty(storeSysProp.isSelected());
}
}
Expand Down Expand Up @@ -117,19 +120,22 @@ private void init() {
addToPanel(mainPanel, labelConstraints, 0, 2, new JLabel("Separator (use '\\t' for tab): ", JLabel.RIGHT));
addToPanel(mainPanel, editConstraints, 1, 2, separator = new JTextField(20));

addToPanel(mainPanel, labelConstraints, 0, 3, new JLabel("Store variables also in System Properties: ", JLabel.RIGHT));
addToPanel(mainPanel, editConstraints, 1, 3, storeSysProp = new JCheckBox());
addToPanel(mainPanel, labelConstraints, 0, 3, new JLabel("Skip initial lines: ", JLabel.RIGHT));
addToPanel(mainPanel, editConstraints, 1, 3, skipLines = new JTextField(20));

addToPanel(mainPanel, labelConstraints, 0, 4, new JLabel("Store variables also in System Properties: ", JLabel.RIGHT));
addToPanel(mainPanel, editConstraints, 1, 4, storeSysProp = new JCheckBox());

editConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
labelConstraints.insets = new java.awt.Insets(4, 0, 0, 2);

addToPanel(mainPanel, labelConstraints, 0, 4, checkButton = new JButton("Test CSV File"));
addToPanel(mainPanel, labelConstraints, 0, 5, checkButton = new JButton("Test CSV File"));

labelConstraints.insets = new java.awt.Insets(4, 0, 0, 0);

checkInfo = new JTextArea();
addToPanel(mainPanel, editConstraints, 1, 4, GuiBuilderHelper.getTextAreaScrollPaneContainer(checkInfo, 10));
checkButton.addActionListener(new TestCsvFileAction(fileName, variablePrefix, separator, checkInfo));
addToPanel(mainPanel, editConstraints, 1, 5, GuiBuilderHelper.getTextAreaScrollPaneContainer(checkInfo, 10));
checkButton.addActionListener(new TestCsvFileAction(this));
checkInfo.setEditable(false);
checkInfo.setOpaque(false);

Expand All @@ -138,6 +144,10 @@ private void init() {
add(container, BorderLayout.CENTER);
}

public JTextArea getCheckInfoTextArea() {
return checkInfo;
}

private void addToPanel(JPanel panel, GridBagConstraints constraints, int col, int row, JComponent component) {
constraints.gridx = col;
constraints.gridy = row;
Expand All @@ -149,6 +159,7 @@ private void initFields() {
fileName.setText("");
checkInfo.setText("");
separator.setText(";");
skipLines.setText("0");
storeSysProp.setSelected(false);
}
}
Loading