Skip to content

Commit 5678314

Browse files
Refactors over response file saver contribution to fix styling, consistency, more explicit naming and add jmx conversion
1 parent d837858 commit 5678314

File tree

2 files changed

+79
-54
lines changed

2 files changed

+79
-54
lines changed

jmeter-java-dsl/src/main/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaver.java

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import org.apache.jmeter.testelement.TestElement;
88
import us.abstracta.jmeter.javadsl.codegeneration.MethodCall;
99
import us.abstracta.jmeter.javadsl.codegeneration.MethodCallContext;
10+
import us.abstracta.jmeter.javadsl.codegeneration.MethodParam;
1011
import us.abstracta.jmeter.javadsl.codegeneration.SingleTestElementCallBuilder;
1112
import us.abstracta.jmeter.javadsl.codegeneration.TestElementParamBuilder;
13+
import us.abstracta.jmeter.javadsl.codegeneration.params.BoolParam;
1214

1315
/**
1416
* Generates one file for each response of a sample/request.
@@ -19,17 +21,22 @@
1921
* generate files only for the associated sampler.
2022
* <p>
2123
* By default, it will generate one file for each response using the given (which might include the
22-
* directory location) prefix to create the files and adding an incremental number to each response
23-
* and an extension according to the response mime type. Both the incremental number and the
24-
* extension can be set manually if skipAutoNumber and skipSuffix are set to true respectively.
24+
* directory location) prefix to create the files and adding an incremental number and an extension
25+
* according to the response mime type.
26+
* <p>
27+
* Eg: <pre>{@code responseFileSaver("responses/resp")}</pre> will generate files like
28+
* "responses/resp1.json".
29+
* <p>
30+
* Both the incremental number and the file extension can be disabled setting
31+
* {@link #autoNumber(boolean)} and {@link #autoFileExtension(boolean)} to false.
2532
*
2633
* @since 0.13
2734
*/
2835
public class ResponseFileSaver extends BaseListener {
2936

3037
protected String fileNamePrefix;
31-
protected boolean skipAutoNumber = false;
32-
protected boolean skipSuffix = false;
38+
protected boolean autoNumber = true;
39+
protected boolean autoFileExtension = true;
3340

3441
public ResponseFileSaver(String fileNamePrefix) {
3542
super("Save Responses to a file", ResultSaverGui.class);
@@ -40,38 +47,40 @@ public ResponseFileSaver(String fileNamePrefix) {
4047
protected TestElement buildTestElement() {
4148
ResultSaver ret = new ResultSaver();
4249
ret.setFilename(fileNamePrefix);
43-
ret.setSkipAutoNumber(skipAutoNumber);
44-
ret.setSkipSuffix(skipSuffix);
50+
ret.setSkipAutoNumber(!autoNumber);
51+
ret.setSkipSuffix(!autoFileExtension);
4552
return ret;
4653
}
4754

48-
4955
/**
50-
* Allows specifying whether the ResponseFileSaver appends a number to the end of the generated file.
56+
* Specifies whether, or not, to append an auto incremental number to each generated response file
57+
* name.
5158
* <p>
52-
* By default, the ResponseFileSaver will add a number based on the samplers in the scope of the
53-
* ResponseFileSaver test element. If set to true then no number will be appended.
59+
* <b>WARNING:</b> if you disable this feature you might not get the files for all generated
60+
* responses (due to potential file name collision and file rewrite). Consider using some jmeter
61+
* expression in file name to avoid file name collisions and overrides (eg:
62+
* "responses/${__threadNum}-${__jm__Thread Group__idx}").
5463
*
55-
* @param skipAutoNumber Boolean determining whether the number is added.
64+
* @param autoNumber specifies to add the auto incremental numbers to the file when set to true.
65+
* By default, this is set to true.
5666
* @return the ResponseFileSaver for further configuration or usage.
5767
*/
58-
public ResponseFileSaver setSkipAutoNumber(boolean skipAutoNumber) {
59-
this.skipAutoNumber = skipAutoNumber;
68+
public ResponseFileSaver autoNumber(boolean autoNumber) {
69+
this.autoNumber = autoNumber;
6070
return this;
6171
}
6272

63-
6473
/**
65-
* Allows specifying whether the ResponseFileSaver will append the file type to the file name.
74+
* Specifies whether, or not, to append an automatic file extension to the file name.
6675
* <p>
67-
* By default, the ResponseFileSaver will use the MIME type to append the file type to the end of the
68-
* generated file. If this is set to true then no file type will be appended.
69-
*
70-
* @param skipSuffix Boolean determining whether a file type is added.
76+
* The automatic file extension is solved according to the response MIME type.
77+
*
78+
* @param autoFileExtension specifies to use the automatic file type extension when set to true.
79+
* By default, is set ti true.
7180
* @return the ResponseFileSaver for further configuration or usage.
7281
*/
73-
public ResponseFileSaver setSkipSuffix(boolean skipSuffix) {
74-
this.skipSuffix = skipSuffix;
82+
public ResponseFileSaver autoFileExtension(boolean autoFileExtension) {
83+
this.autoFileExtension = autoFileExtension;
7584
return this;
7685
}
7786

@@ -83,8 +92,17 @@ public CodeBuilder(List<Method> builderMethods) {
8392

8493
@Override
8594
protected MethodCall buildMethodCall(ResultSaver testElement, MethodCallContext context) {
86-
return buildMethodCall(
87-
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
95+
TestElementParamBuilder paramBuilder = new TestElementParamBuilder(testElement);
96+
MethodCall ret = buildMethodCall(paramBuilder.stringParam(ResultSaver.FILENAME));
97+
MethodParam skipAutoNumber = paramBuilder.boolParam(ResultSaver.SKIP_AUTO_NUMBER, false);
98+
if (!skipAutoNumber.isDefault()) {
99+
ret.chain("autoNumber", new BoolParam(false, true));
100+
}
101+
MethodParam skipSuffix = paramBuilder.boolParam(ResultSaver.SKIP_SUFFIX, false);
102+
if (!skipSuffix.isDefault()) {
103+
ret.chain("autoFileExtension", new BoolParam(false, true));
104+
}
105+
return ret;
88106
}
89107

90108
}

jmeter-java-dsl/src/test/java/us/abstracta/jmeter/javadsl/core/listeners/ResponseFileSaverTest.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static us.abstracta.jmeter.javadsl.JmeterDsl.testPlan;
1111
import static us.abstracta.jmeter.javadsl.JmeterDsl.threadGroup;
1212

13+
import java.io.IOException;
1314
import java.nio.file.Path;
1415
import org.junit.jupiter.api.Nested;
1516
import org.junit.jupiter.api.Test;
@@ -23,56 +24,51 @@ public class ResponseFileSaverTest extends JmeterDslTest {
2324
private static final String RESPONSE_FILE_PREFIX = "response";
2425

2526
@Test
26-
public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
27+
public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempDir Path tempDir)
28+
throws Exception {
29+
checkGeneratedResponseFile(buildResponseFileSaver(tempDir),
30+
tempDir.resolve("response1.unknown"));
31+
}
32+
33+
private ResponseFileSaver buildResponseFileSaver(Path tempDir) {
34+
return responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
35+
}
36+
37+
private void checkGeneratedResponseFile(ResponseFileSaver responseFileSaver, Path filePath)
38+
throws IOException {
2739
String body = "TEST BODY";
2840
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
2941
testPlan(
3042
threadGroup(1, 1,
3143
httpSampler(wiremockUri)
3244
),
33-
responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString())
45+
responseFileSaver
3446
).run();
35-
assertThat(tempDir.resolve("response1.unknown")).hasContent(body);
47+
assertThat(filePath).hasContent(body);
3648
}
3749

3850
@Test
39-
public void shouldWriteFileWithNoAddedNumberWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
40-
String body = "TEST BODY";
41-
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
42-
fileSaver.setSkipAutoNumber(true);
43-
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
44-
testPlan(
45-
threadGroup(1, 1,
46-
httpSampler(wiremockUri)
47-
),
48-
fileSaver
49-
).run();
50-
assertThat(tempDir.resolve("response.unknown")).hasContent(body);
51+
public void shouldWriteFileWithNoNumberWhenResponseFileSaverWithoutAutoNumber(
52+
@TempDir Path tempDir) throws Exception {
53+
checkGeneratedResponseFile(buildResponseFileSaver(tempDir).autoNumber(false),
54+
tempDir.resolve("response.unknown"));
5155
}
5256

5357
@Test
54-
public void shouldWriteFileWithNoAddedFileExtensionWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
55-
String body = "TEST BODY";
56-
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
57-
fileSaver.setSkipSuffix(true);
58-
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
59-
testPlan(
60-
threadGroup(1, 1,
61-
httpSampler(wiremockUri)
62-
),
63-
fileSaver
64-
).run();
65-
assertThat(tempDir.resolve("response1")).hasContent(body);
58+
public void shouldWriteFileWithNoExtensionWhenResponseFileSaverWithoutAutoExtension(
59+
@TempDir Path tempDir) throws Exception {
60+
checkGeneratedResponseFile(buildResponseFileSaver(tempDir).autoFileExtension(false),
61+
tempDir.resolve("response1"));
6662
}
6763

68-
6964
@Test
70-
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
65+
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir)
66+
throws Exception {
7167
testPlan(
7268
threadGroup(1, TEST_ITERATIONS,
7369
httpSampler(wiremockUri)
7470
),
75-
responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString())
71+
buildResponseFileSaver(tempDir)
7672
).run();
7773
String[] responseFiles = tempDir.toFile().list((dir, name) -> name.startsWith(
7874
RESPONSE_FILE_PREFIX));
@@ -91,6 +87,17 @@ public DslTestPlan testPlanWithResponseFileSaver() {
9187
);
9288
}
9389

90+
public DslTestPlan testPlanWithResponseFileSaverAndNonDefaultProperties() {
91+
return testPlan(
92+
threadGroup(1, 1,
93+
httpSampler("http://localhost"),
94+
responseFileSaver("response")
95+
.autoNumber(false)
96+
.autoFileExtension(false)
97+
)
98+
);
99+
}
100+
94101
}
95102

96103
}

0 commit comments

Comments
 (0)