Skip to content

Commit

Permalink
Expose Additional JMeter Result Saver Properties in the DSL (#263)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Dunlop <[email protected]>
  • Loading branch information
Pyrobow and Will Dunlop authored Apr 30, 2024
1 parent 39c960b commit d837858
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
* <p>
* By default, it will generate one file for each response using the given (which might include the
* directory location) prefix to create the files and adding an incremental number to each response
* and an extension according to the response mime type.
* and an extension according to the response mime type. Both the incremental number and the
* extension can be set manually if skipAutoNumber and skipSuffix are set to true respectively.
*
* @since 0.13
*/
public class ResponseFileSaver extends BaseListener {

protected String fileNamePrefix;
protected boolean skipAutoNumber = false;
protected boolean skipSuffix = false;

public ResponseFileSaver(String fileNamePrefix) {
super("Save Responses to a file", ResultSaverGui.class);
Expand All @@ -37,9 +40,41 @@ public ResponseFileSaver(String fileNamePrefix) {
protected TestElement buildTestElement() {
ResultSaver ret = new ResultSaver();
ret.setFilename(fileNamePrefix);
ret.setSkipAutoNumber(skipAutoNumber);
ret.setSkipSuffix(skipSuffix);
return ret;
}


/**
* Allows specifying whether the ResponseFileSaver appends a number to the end of the generated file.
* <p>
* By default, the ResponseFileSaver will add a number based on the samplers in the scope of the
* ResponseFileSaver test element. If set to true then no number will be appended.
*
* @param skipAutoNumber Boolean determining whether the number is added.
* @return the ResponseFileSaver for further configuration or usage.
*/
public ResponseFileSaver setSkipAutoNumber(boolean skipAutoNumber) {
this.skipAutoNumber = skipAutoNumber;
return this;
}


/**
* Allows specifying whether the ResponseFileSaver will append the file type to the file name.
* <p>
* By default, the ResponseFileSaver will use the MIME type to append the file type to the end of the
* generated file. If this is set to true then no file type will be appended.
*
* @param skipSuffix Boolean determining whether a file type is added.
* @return the ResponseFileSaver for further configuration or usage.
*/
public ResponseFileSaver setSkipSuffix(boolean skipSuffix) {
this.skipSuffix = skipSuffix;
return this;
}

public static class CodeBuilder extends SingleTestElementCallBuilder<ResultSaver> {

public CodeBuilder(List<Method> builderMethods) {
Expand All @@ -49,7 +84,7 @@ public CodeBuilder(List<Method> builderMethods) {
@Override
protected MethodCall buildMethodCall(ResultSaver testElement, MethodCallContext context) {
return buildMethodCall(
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
new TestElementParamBuilder(testElement).stringParam(ResultSaver.FILENAME));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ public void shouldWriteFileWithResponseContentWhenResponseFileSaverInPlan(@TempD
assertThat(tempDir.resolve("response1.unknown")).hasContent(body);
}

@Test
public void shouldWriteFileWithNoAddedNumberWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
String body = "TEST BODY";
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
fileSaver.setSkipAutoNumber(true);
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
testPlan(
threadGroup(1, 1,
httpSampler(wiremockUri)
),
fileSaver
).run();
assertThat(tempDir.resolve("response.unknown")).hasContent(body);
}

@Test
public void shouldWriteFileWithNoAddedFileExtensionWithResponseContentWhenResponseFileSaverInPlanAndSkipAutoNumberTrue(@TempDir Path tempDir) throws Exception {
String body = "TEST BODY";
ResponseFileSaver fileSaver = responseFileSaver(tempDir.resolve(RESPONSE_FILE_PREFIX).toString());
fileSaver.setSkipSuffix(true);
stubFor(any(anyUrl()).willReturn(aResponse().withBody(body)));
testPlan(
threadGroup(1, 1,
httpSampler(wiremockUri)
),
fileSaver
).run();
assertThat(tempDir.resolve("response1")).hasContent(body);
}


@Test
public void shouldWriteOneFileForEachResponseWhenResponseFileSaverInPlan(@TempDir Path tempDir) throws Exception {
testPlan(
Expand Down

0 comments on commit d837858

Please sign in to comment.