Skip to content

Commit d837858

Browse files
Pyrobowwill-dunlop
andauthored
Expose Additional JMeter Result Saver Properties in the DSL (#263)
Co-authored-by: Will Dunlop <[email protected]>
1 parent 39c960b commit d837858

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
* <p>
2121
* By default, it will generate one file for each response using the given (which might include the
2222
* 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.
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.
2425
*
2526
* @since 0.13
2627
*/
2728
public class ResponseFileSaver extends BaseListener {
2829

2930
protected String fileNamePrefix;
31+
protected boolean skipAutoNumber = false;
32+
protected boolean skipSuffix = false;
3033

3134
public ResponseFileSaver(String fileNamePrefix) {
3235
super("Save Responses to a file", ResultSaverGui.class);
@@ -37,9 +40,41 @@ public ResponseFileSaver(String fileNamePrefix) {
3740
protected TestElement buildTestElement() {
3841
ResultSaver ret = new ResultSaver();
3942
ret.setFilename(fileNamePrefix);
43+
ret.setSkipAutoNumber(skipAutoNumber);
44+
ret.setSkipSuffix(skipSuffix);
4045
return ret;
4146
}
4247

48+
49+
/**
50+
* Allows specifying whether the ResponseFileSaver appends a number to the end of the generated file.
51+
* <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.
54+
*
55+
* @param skipAutoNumber Boolean determining whether the number is added.
56+
* @return the ResponseFileSaver for further configuration or usage.
57+
*/
58+
public ResponseFileSaver setSkipAutoNumber(boolean skipAutoNumber) {
59+
this.skipAutoNumber = skipAutoNumber;
60+
return this;
61+
}
62+
63+
64+
/**
65+
* Allows specifying whether the ResponseFileSaver will append the file type to the file name.
66+
* <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.
71+
* @return the ResponseFileSaver for further configuration or usage.
72+
*/
73+
public ResponseFileSaver setSkipSuffix(boolean skipSuffix) {
74+
this.skipSuffix = skipSuffix;
75+
return this;
76+
}
77+
4378
public static class CodeBuilder extends SingleTestElementCallBuilder<ResultSaver> {
4479

4580
public CodeBuilder(List<Method> builderMethods) {
@@ -49,7 +84,7 @@ public CodeBuilder(List<Method> builderMethods) {
4984
@Override
5085
protected MethodCall buildMethodCall(ResultSaver testElement, MethodCallContext context) {
5186
return buildMethodCall(
52-
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
87+
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
5388
}
5489

5590
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,37 @@ public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempD
3535
assertThat(tempDir.resolve("response1.unknown")).hasContent(body);
3636
}
3737

38+
@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+
}
52+
53+
@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);
66+
}
67+
68+
3869
@Test
3970
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
4071
testPlan(

0 commit comments

Comments
 (0)