Skip to content

Commit 830b1c8

Browse files
KavabungozIlya Ulizko
and
Ilya Ulizko
authoredFeb 26, 2025··
Now it makes it possible to interpret a given path by evaluating any (#696)
* Now it makes it possible to interpret a given path by evaluating any embedded expressions. * Backed to Java version to 1.8; Backed to standard default delimiter for CSV; Changed plugin version from 0.3 to 0.2 * updated the wiki * fixed markdown * Reverted formatting changes to the old 'GoogleCode' format. * Now the dialog opens in the directory where the current .jmx file is located * Imports fixed * Reverted BrowseAction --------- Co-authored-by: Ilya Ulizko <[email protected]>
1 parent 8f8bd3c commit 830b1c8

File tree

7 files changed

+98
-40
lines changed

7 files changed

+98
-40
lines changed
 

‎infra/common/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>kg.apc</groupId>
66
<artifactId>jmeter-plugins-cmn-jmeter</artifactId>
7-
<version>0.7</version>
7+
<version>0.8</version>
88

99
<name>JMeter Plugins Common Classes</name>
1010
<description>Various utility classes to ease development of plugins</description>
@@ -45,8 +45,8 @@
4545
</build>
4646
<properties>
4747
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
48-
<maven.compiler.source>1.7</maven.compiler.source>
49-
<maven.compiler.target>1.7</maven.compiler.target>
48+
<maven.compiler.source>1.8</maven.compiler.source>
49+
<maven.compiler.target>1.8</maven.compiler.target>
5050
</properties>
5151
<dependencies>
5252
<dependency>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package kg.apc.jmeter.gui;
22

3-
import java.awt.event.ActionEvent;
4-
import java.awt.event.ActionListener;
3+
import org.apache.jmeter.services.FileServer;
4+
55
import javax.swing.JFileChooser;
66
import javax.swing.JTextField;
7+
import java.awt.event.ActionEvent;
8+
import java.awt.event.ActionListener;
9+
import java.io.File;
710
import org.apache.jmeter.gui.GuiPackage;
811

912
public class BrowseAction implements ActionListener {
@@ -13,34 +16,36 @@ public class BrowseAction implements ActionListener {
1316
private String lastPath = ".";
1417

1518
public BrowseAction(JTextField filename) {
16-
control = filename;
19+
this.control = filename;
1720
}
1821

1922
public BrowseAction(JTextField filename, boolean isDirectoryBrowse) {
20-
control = filename;
23+
this.control = filename;
2124
this.isDirectoryBrowse = isDirectoryBrowse;
2225
}
2326

24-
@Override
27+
@Override
2528
public void actionPerformed(ActionEvent e) {
2629
JFileChooser chooser = getFileChooser();
27-
if (chooser != null) {
28-
if(GuiPackage.getInstance() != null) {
29-
int returnVal = chooser.showOpenDialog(GuiPackage.getInstance().getMainFrame());
30-
if(returnVal == JFileChooser.APPROVE_OPTION) {
31-
control.setText(chooser.getSelectedFile().getPath());
32-
}
33-
lastPath = chooser.getCurrentDirectory().getPath();
30+
if (chooser != null && GuiPackage.getInstance() != null) {
31+
int returnVal = chooser.showOpenDialog(GuiPackage.getInstance().getMainFrame());
32+
if (returnVal == JFileChooser.APPROVE_OPTION) {
33+
control.setText(chooser.getSelectedFile().getPath());
3434
}
35+
lastPath = chooser.getCurrentDirectory().getPath();
3536
}
3637
}
3738

3839
protected JFileChooser getFileChooser() {
39-
JFileChooser ret = new JFileChooser(lastPath);
40-
if(isDirectoryBrowse) {
40+
String baseDir = FileServer.getFileServer().getBaseDir();
41+
File initialDirectory = baseDir != null && !baseDir.isEmpty()
42+
? new File(baseDir)
43+
: new File(lastPath);
44+
45+
JFileChooser ret = new JFileChooser(initialDirectory);
46+
if (isDirectoryBrowse) {
4147
ret.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
4248
}
43-
4449
return ret;
4550
}
46-
}
51+
}

‎plugins/csvars/pom.xml

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>kg.apc</groupId>
88
<artifactId>jmeter-plugins-csvars</artifactId>
9-
<version>0.1</version>
9+
<version>0.2</version>
1010

1111
<name>Variables from CSV</name>
1212
<description>Variables from CSV</description>
@@ -33,14 +33,14 @@
3333

3434
<properties>
3535
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36-
<maven.compiler.source>1.7</maven.compiler.source>
37-
<maven.compiler.target>1.7</maven.compiler.target>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<maven.compiler.target>1.8</maven.compiler.target>
3838
</properties>
3939
<dependencies>
4040
<dependency>
4141
<groupId>kg.apc</groupId>
4242
<artifactId>jmeter-plugins-cmn-jmeter</artifactId>
43-
<version>0.3</version>
43+
<version>0.7</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>org.powermock</groupId>
@@ -58,6 +58,12 @@
5858
<groupId>kg.apc</groupId>
5959
<artifactId>jmeter-plugins-emulators</artifactId>
6060
<version>0.2</version>
61+
<exclusions>
62+
<exclusion>
63+
<groupId>org.slf4j</groupId>
64+
<artifactId>slf4j-nop</artifactId>
65+
</exclusion>
66+
</exclusions>
6167
<scope>test</scope>
6268
</dependency>
6369
</dependencies>

‎plugins/csvars/src/main/java/kg/apc/jmeter/config/VariablesFromCSV.java

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package kg.apc.jmeter.config;
22

33
import org.apache.jmeter.config.Arguments;
4+
import org.apache.jmeter.engine.util.CompoundVariable;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
47

58
import java.util.Map;
69

@@ -12,25 +15,54 @@ public class VariablesFromCSV extends Arguments {
1215
public static final String SKIP_LINES = "skipLines";
1316
public static final int SKIP_LINES_DEFAULT = 0;
1417
public static final String STORE_SYS_PROP = "storeSysProp";
18+
private static final Logger log = LoggerFactory.getLogger(VariablesFromCSV.class);
1519

16-
// It seems org.apache.jmeter.engine.Precompiler requires only this
17-
// https://groups.google.com/forum/#!topic/jmeter-plugins/gWn7MTgvTfE method
1820
@Override
1921
public Map<String, String> getArgumentsAsMap() {
20-
Map<String, String> variables = new VariableFromCsvFileReader(getFileName()).getDataAsMap(getVariablePrefix(), getSeparator(), getSkipLines());
21-
//store in System Properties also
22+
String fileName = getFileName();
23+
String interpretedPath = interpretPath(fileName);
24+
25+
Map<String, String> variables = new VariableFromCsvFileReader(interpretedPath)
26+
.getDataAsMap(getVariablePrefix(), getSeparator(), getSkipLines());
27+
2228
if (isStoreAsSystemProperty()) {
23-
for (Map.Entry<String, String> element : variables.entrySet()) {
24-
String variable = element.getKey();
29+
for (Map.Entry<String, String> entry : variables.entrySet()) {
30+
String variable = entry.getKey();
2531
if (System.getProperty(variable) == null) {
26-
System.setProperty(variable, element.getValue());
32+
System.setProperty(variable, entry.getValue());
2733
}
2834
}
2935
}
3036

3137
return variables;
3238
}
3339

40+
/**
41+
* Interprets the given path by evaluating any embedded expressions.
42+
*
43+
* @param rawPath the raw path string that may contain expressions
44+
* @return the interpreted path
45+
*/
46+
public String interpretPath(String rawPath) {
47+
if (rawPath == null || rawPath.isEmpty()) {
48+
return rawPath;
49+
}
50+
51+
try {
52+
CompoundVariable compoundVariable = new CompoundVariable(rawPath);
53+
String interpretedPath = compoundVariable.execute().trim();
54+
55+
if (!interpretedPath.isEmpty() && !new java.io.File(interpretedPath).exists()) {
56+
throw new IllegalArgumentException("Interpreted path does not exist: " + interpretedPath);
57+
}
58+
59+
return interpretedPath;
60+
} catch (Exception ex) {
61+
log.error("Error interpreting path: " + rawPath, ex);
62+
throw new RuntimeException("Failed to interpret path: " + rawPath, ex);
63+
}
64+
}
65+
3466
public String getVariablePrefix() {
3567
return getPropertyAsString(VARIABLE_PREFIX);
3668
}

‎plugins/csvars/src/main/java/kg/apc/jmeter/config/VariablesFromCSVGui.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
import kg.apc.jmeter.gui.IntegerInputVerifier;
1919
import org.apache.jmeter.config.gui.AbstractConfigGui;
2020
import org.apache.jmeter.testelement.TestElement;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2123

2224
public class VariablesFromCSVGui extends AbstractConfigGui {
2325

2426
public static final String WIKIPAGE = "VariablesFromCSV";
2527

28+
private static final Logger log = LoggerFactory.getLogger(VariablesFromCSVGui.class);
29+
2630
private JTextField fileName;
2731
private JTextField variablePrefix;
2832
private JTextField separator;
2933
private JTextField skipLines;
3034
private JCheckBox storeSysProp;
31-
35+
3236
private JButton browseButton;
3337
private JButton checkButton;
3438
private JTextArea checkInfo;
@@ -39,11 +43,13 @@ public VariablesFromCSVGui() {
3943
initFields();
4044
}
4145

46+
4247
@Override
4348
public String getStaticLabel() {
4449
return JMeterPluginsUtils.prefixLabel("Variables From CSV File");
4550
}
4651

52+
4753
@Override
4854
public String getLabelResource() {
4955
return getClass().getCanonicalName();
@@ -53,7 +59,7 @@ public String getLabelResource() {
5359
public void configure(TestElement element) {
5460
super.configure(element);
5561
if (element instanceof VariablesFromCSV) {
56-
VariablesFromCSV varsCsv = (VariablesFromCSV)element;
62+
VariablesFromCSV varsCsv = (VariablesFromCSV) element;
5763
fileName.setText(varsCsv.getFileName());
5864
variablePrefix.setText(varsCsv.getVariablePrefix());
5965
separator.setText(varsCsv.getSeparator());
@@ -107,6 +113,7 @@ private void init() {
107113

108114
addToPanel(mainPanel, labelConstraints, 0, 0, new JLabel("CSV File: ", JLabel.RIGHT));
109115
addToPanel(mainPanel, editConstraints, 1, 0, fileName = new JTextField(20));
116+
fileName.setToolTipText("You can use expressions like ${__groovy(...)} for dynamic paths.");
110117
addToPanel(mainPanel, labelConstraints, 2, 0, browseButton = new JButton("Browse..."));
111118

112119
GuiBuilderHelper.strechItemToComponent(fileName, browseButton);

‎site/dat/wiki/VariablesFromCSV.wiki

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
<span class=''>[/?search=jpgc-csvars <i class='fa fa-download'></i> Download]</span>
44
5-
This Config item allows you to load variable values from CSV file. Variable names and values taken from CSV file, name from first column, value from second column, respectively.
5+
This Config item allows you to load variable values from CSV file. Variable names and values taken from CSV file, name from first column, value from second column, respectively. In the <i>CSV File:</i> window, you can specify the absolute path to your csv file. However, to make your test plans more portable, you can also use dynamic path by evaluating ${} expressions.
66
77
== Tutorial ==
8-
Let's create a text file _c:\temp\testvariables.csv_ with following content:
8+
Let's create a text file <i>c:\temp\testvariables.csv</i> with following content:
99
1010
{{{
1111
First_Variable,here comes value 1
1212
Second_Variable,and here value 2
13-
}}}
13+
}}}
1414
15-
Let's create test plan with _Variables From CSV File_, _Thread Group_, _Debug Sampler_ and _View Results Tree_. Then configure _Variables From CSV File_, give it filename _testvariables.csv_, optional variable prefix `MyVar_` and column separator ','. Now your plan should look like this:
15+
1. To use the absolute path, let's create test plan with <i>Variables From CSV File</i>, <i>Thread Group</i>, <i>Debug Sampler</i> and <i>View Results Tree</i>. Then configure <i>Variables From CSV File</i>, specify the absolute path to <i>testvariables.csv</i>, optional variable prefix `MyVar_` and column separator ','. Now your plan should look like this:
1616
17-
[/img/wiki/varsfromcsv1.png]
17+
[/site/img/wiki/varsfromcsv1.png]
1818
19-
Save this test plan to _c:\temp\test1.jmx_.
19+
Save this test plan to <i>c:\temp\test1.jmx</i>.
2020
21-
Now, run the test, go to _View Results Tree_ and click on _Debug Sampler_, then click on _Response data_ tab. You'll see that your variables from _testvariables.csv_ are defined in JMeter, e.g.:
21+
Now, run the test, go to <i>View Results Tree</i> and click on <i>Debug Sampler</i>, then click on <i>Response data</i> tab. You'll see that your variables from <i>testvariables.csv</i> are defined in JMeter, e.g.:
2222
2323
{{{
2424
JMeterVariables:
@@ -30,4 +30,12 @@ START.HMS=153028
3030
START.MS=1264671028968
3131
START.YMD=20100128
3232
TESTSTART.MS=1264671163750
33-
}}}
33+
}}}
34+
35+
2. To use dynamic path you should specify expressions enclosed in ${}, e.g.:
36+
37+
{{{${__groovy(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();,)}${__groovy(File.separator;,)}resources${__groovy(File.separator;,)}properties.csv}}}
38+
39+
This expression dynamically resolves the path (like <i>D:\JAVA_pet_projects\resources\properties.csv</i>) relative to the directory of the current <i>.jmx</i> file.
40+
41+
[/site/img/wiki/varsfromcsv2.png]

‎site/img/wiki/varsfromcsv2.png

89.8 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.