77import org .apache .jmeter .testelement .TestElement ;
88import us .abstracta .jmeter .javadsl .codegeneration .MethodCall ;
99import us .abstracta .jmeter .javadsl .codegeneration .MethodCallContext ;
10+ import us .abstracta .jmeter .javadsl .codegeneration .MethodParam ;
1011import us .abstracta .jmeter .javadsl .codegeneration .SingleTestElementCallBuilder ;
1112import 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.
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 */
2835public 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 }
0 commit comments