From 9d2919bb55fda66e3263424ba624840bbb110b01 Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Fri, 25 Oct 2024 18:39:35 +0200 Subject: [PATCH] Use same spelling sampleResult in all dynamic samplers, functions, assertions and listeners --- .../jmeter/assertions/BSFAssertion.java | 2 +- .../jmeter/assertions/BeanShellAssertion.java | 1 + .../jmeter/assertions/JSR223Assertion.java | 1 + .../apache/jmeter/functions/BeanShell.java | 1 + .../protocol/java/sampler/BSFSampler.java | 1 + .../java/sampler/BeanShellSampler.java | 1 + .../protocol/java/sampler/JSR223Sampler.java | 1 + .../java/sampler/JSR223SamplerTest.java | 9 ++-- xdocs/changes.xml | 1 + xdocs/usermanual/component_reference.xml | 48 ++++++++++--------- xdocs/usermanual/functions.xml | 2 +- 11 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java index 57ea8ae84cb..a8cd865e842 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/BSFAssertion.java @@ -37,7 +37,7 @@ public AssertionResult getResult(SampleResult response) { BSFManager mgr =null; try { mgr = getManager(); - mgr.declareBean("SampleResult", response, SampleResult.class); + mgr.declareBean("sampleResult", response, SampleResult.class); mgr.declareBean("AssertionResult", result, AssertionResult.class); processFileOrScript(mgr); result.setError(false); diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java index e80d2c724f3..5cc35551578 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/BeanShellAssertion.java @@ -88,6 +88,7 @@ public AssertionResult getResult(SampleResult response) { // Add SamplerData for consistency with BeanShell Sampler bshInterpreter.set("SampleResult", response); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", response); //$NON-NLS-1$ bshInterpreter.set("Response", response); //$NON-NLS-1$ bshInterpreter.set("ResponseData", response.getResponseData());//$NON-NLS-1$ bshInterpreter.set("ResponseCode", response.getResponseCode());//$NON-NLS-1$ diff --git a/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java b/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java index ffffe0de725..26c93735b97 100644 --- a/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java +++ b/src/components/src/main/java/org/apache/jmeter/assertions/JSR223Assertion.java @@ -46,6 +46,7 @@ public AssertionResult getResult(SampleResult response) { ScriptEngine scriptEngine = getScriptEngine(); Bindings bindings = scriptEngine.createBindings(); bindings.put("SampleResult", response); + bindings.put("sampleResult", response); bindings.put("AssertionResult", result); processFileOrScript(scriptEngine, bindings); result.setError(false); diff --git a/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java b/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java index f72284c877b..bea72f3d639 100644 --- a/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java +++ b/src/functions/src/main/java/org/apache/jmeter/functions/BeanShell.java @@ -90,6 +90,7 @@ public synchronized String execute(SampleResult previousResult, Sampler currentS if (previousResult != null) { bshInterpreter.set("SampleResult", previousResult); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", previousResult); //$NON-NLS-1$ } // Allow access to context and variables directly diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java index 16154997b3a..ffe1eebe182 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java @@ -82,6 +82,7 @@ public SampleResult sample(Entry e)// Entry tends to be ignored ... try { initManager(mgr); mgr.declareBean("SampleResult", res, res.getClass()); // $NON-NLS-1$ + mgr.declareBean("sampleResult", res, res.getClass()); // $NON-NLS-1$ // N.B. some engines (e.g. Javascript) cannot handle certain declareBean() calls // after the engine has been initialised, so create the engine last diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java index 3607a55692e..3006ae9c179 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java @@ -114,6 +114,7 @@ public SampleResult sample(Entry e)// Entry tends to be ignored ... } try { bshInterpreter.set("SampleResult", res); //$NON-NLS-1$ + bshInterpreter.set("sampleResult", res); //$NON-NLS-1$ // Set default values bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$ diff --git a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java index 22ffd43dee1..c12edb10203 100644 --- a/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java +++ b/src/protocol/java/src/main/java/org/apache/jmeter/protocol/java/sampler/JSR223Sampler.java @@ -69,6 +69,7 @@ public SampleResult sample(Entry entry) { ScriptEngine scriptEngine = getScriptEngine(); Bindings bindings = scriptEngine.createBindings(); bindings.put("SampleResult",result); + bindings.put("sampleResult",result); Object ret = processFileOrScript(scriptEngine, bindings); if (ret != null && (result.getResponseData() == null || result.getResponseData().length==0)){ result.setResponseData(ret.toString(), null); diff --git a/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java b/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java index f2478c7e4d6..f94e42f6454 100644 --- a/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java +++ b/src/protocol/java/src/test/java/org/apache/jmeter/protocol/java/sampler/JSR223SamplerTest.java @@ -21,14 +21,17 @@ import org.apache.jmeter.samplers.SampleResult; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; class JSR223SamplerTest { - @Test - void sampleWithEndTimeSet() { + @ParameterizedTest + @ValueSource(strings={"sampleResult", "SampleResult"}) + void sampleWithEndTimeSet(String sampleResultVariableName) { JSR223Sampler sampler = new JSR223Sampler(); sampler.setName("jsr223Test"); - sampler.setScript("SampleResult.setEndTime(42); 'OK'"); + sampler.setScript(sampleResultVariableName + ".setEndTime(42); 'OK'"); sampler.setScriptLanguage("groovy"); SampleResult sampleResult = sampler.sample(null); assertEquals(42, sampleResult.getEndTime()); diff --git a/xdocs/changes.xml b/xdocs/changes.xml index f50ac08b508..b2028a21901 100644 --- a/xdocs/changes.xml +++ b/xdocs/changes.xml @@ -68,6 +68,7 @@ Summary
  • 6352 Calculate delays in Open Model Thread Group and Precise Throughput Timer relative to start of Thread Group instead of the start of the test.
  • 63576358 Ensure writable directories when copying template files while report generation.
  • +
  • 6368 Use same spelling sampleResult to reference SampleResult in all JSR223/BeanShell/JavaScript listeners, samplers and assertions.
  • diff --git a/xdocs/usermanual/component_reference.xml b/xdocs/usermanual/component_reference.xml index 213a62758ed..a2b662f4d75 100644 --- a/xdocs/usermanual/component_reference.xml +++ b/xdocs/usermanual/component_reference.xml @@ -1094,7 +1094,7 @@ BeanShell does not currently support Java 5 syntax such as generics and the enha
  • FileName - the file name, if any
  • Parameters - text from the Parameters field
  • bsh.args - the parameters, split as described above
  • -
  • SampleResult - pointer to the current SampleResult
  • +
  • sampleResult - pointer to the current SampleResult
  • ResponseCode defaults to 200
  • ResponseMessage defaults to "OK"
  • IsSuccess defaults to true
  • @@ -1109,7 +1109,7 @@ vars.putObject("OBJ1",new Object()); props.put("PROP1","1234");

    When the script completes, control is returned to the Sampler, and it copies the contents - of the following script variables into the corresponding variables in the SampleResult:

    + of the following script variables into the corresponding variables in the sampleResult:

    The SampleResult ResponseData is set from the return value of the script. If the script returns null, it can set the response directly, by using the method - SampleResult.setResponseData(data), where data is either a String or a byte array. + sampleResult.setResponseData(data), where data is either a String or a byte array. The data type defaults to "text", but can be set to binary by using the method - SampleResult.setDataType(SampleResult.BINARY). + sampleResult.setDataType(SampleResult.BINARY).

    -

    The SampleResult variable gives the script full access to all the fields and +

    The sampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods - setStopThread(boolean) and setStopTest(boolean). + setStopThread(boolean) and setStopTest(boolean).

    - Here is a simple (not very useful!) example script:

    +

    Here is a simple (not very useful!) example script:

    if (bsh.args[0].equalsIgnoreCase("StopThread")) { log.info("Stop Thread detected!"); - SampleResult.setStopThread(true); + sampleResult.setStopThread(true); } return "Data from sample with Label "+Label; -//or -SampleResult.setResponseData("My data"); + +or + +sampleResult.setResponseData("My data"); return null;

    Another example:

    ensure that the property beanshell.sampler.init=BeanShellSampler.bshrc is defined in jmeter.properties. @@ -1155,12 +1157,12 @@ Beware however that misuse of any methods can cause subtle faults that may be di The JSR223 Sampler allows JSR223 script code to be used to perform a sample or some computation required to create/update variables. If you don't want to generate a SampleResult when this sampler is run, call the following method: -SampleResult.setIgnore(); +sampleResult.setIgnore(); This call will have the following impact:

    @@ -1227,7 +1229,7 @@ Note that these are JSR223 variables - i.e. they can be used directly in the scr
  • FileName - the file name, if any
  • Parameters - text from the Parameters field
  • args - the parameters, split as described above
  • -
  • SampleResult - pointer to the current SampleResult
  • +
  • sampleResult - pointer to the current SampleResult
  • sampler - (Sampler) - pointer to current Sampler
  • ctx - JMeterContext
  • vars - JMeterVariables - e.g. @@ -1243,12 +1245,12 @@ props.put("PROP1","1234");
  • The SampleResult ResponseData is set from the return value of the script. If the script returns null, it can set the response directly, by using the method -SampleResult.setResponseData(data), where data is either a String or a byte array. +sampleResult.setResponseData(data), where data is either a String or a byte array. The data type defaults to "text", but can be set to binary by using the method -SampleResult.setDataType(SampleResult.BINARY). +sampleResult.setDataType(SampleResult.BINARY).

    -The SampleResult variable gives the script full access to all the fields and +The sampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods setStopThread(boolean) and setStopTest(boolean).

    @@ -1257,9 +1259,9 @@ Unlike the BeanShell Sampler, the JSR223 Sampler does not set the Response Currently the only way to changes these is via the SampleResult methods:

    @@ -4798,7 +4800,7 @@ These are strings unless otherwise noted:

    (*) means that this is set before the init file, if any, is processed. Other variables vary from invocation to invocation.