You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2022/01/08 14:12:54 UTC

[jmeter] branch master updated: Split function test refactor with failure and different success scenarios separately

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new af7fc4d  Split function test refactor with failure and different success scenarios separately
af7fc4d is described below

commit af7fc4d56bffab352843cc87514ab9ed19bdad32
Author: Sampath Kumar Krishnasamy <sa...@aexp.com>
AuthorDate: Thu Jan 6 14:49:05 2022 +0000

    Split function test refactor with failure and different success scenarios separately
    
    Closes #690
---
 .../apache/jmeter/functions/SplitFunctionTest.java | 56 +++++++++++++++-------
 xdocs/changes.xml                                  |  1 +
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/src/functions/src/test/java/org/apache/jmeter/functions/SplitFunctionTest.java b/src/functions/src/test/java/org/apache/jmeter/functions/SplitFunctionTest.java
index 177b85e..69169bb 100644
--- a/src/functions/src/test/java/org/apache/jmeter/functions/SplitFunctionTest.java
+++ b/src/functions/src/test/java/org/apache/jmeter/functions/SplitFunctionTest.java
@@ -17,15 +17,16 @@
 
 package org.apache.jmeter.functions;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.util.Collection;
 import java.util.LinkedList;
 
 import org.apache.jmeter.engine.util.CompoundVariable;
 import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.threads.JMeterContext;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.apache.jmeter.threads.JMeterVariables;
@@ -34,27 +35,30 @@ import org.junit.jupiter.api.Test;
 
 public class SplitFunctionTest extends JMeterTestCase {
 
-    private JMeterContext jmctx = null;
     private JMeterVariables vars = null;
 
     @BeforeEach
     public void setUp() {
-        jmctx = JMeterContextService.getContext();
-        jmctx.setVariables(new JMeterVariables());
-        vars = jmctx.getVariables();
+        JMeterContext jmeterContext = JMeterContextService.getContext();
+        jmeterContext.setVariables(new JMeterVariables());
+        vars = jmeterContext.getVariables();
     }
 
     @Test
-    public void splitTest1() throws Exception {
-        String src = "";
-
-        try {
-            splitParams("a,b,c", null, null);
-            fail("Expected InvalidVariableException (wrong number of parameters)");
-        } catch (InvalidVariableException e) {
-            // OK
-        }
-        src = "a,b,c";
+    public void shouldThrowExceptionWhenParameterCountIsInvalid() {
+        InvalidVariableException invalidVariableException = assertThrows(InvalidVariableException.class, () ->
+                splitParams("a,b,c", null, null),
+            ""
+        );
+        assertEquals(
+            "__split called with wrong number of parameters. Actual: 1. Expected: >= 2 and <= 3",
+            invalidVariableException.getMessage()
+        );
+    }
+
+    @Test
+    public void shouldSplitWithoutAnyArguments() throws Exception {
+        String src = "a,b,c";
         SplitFunction split;
         split = splitParams(src, "VAR1", null);
         assertEquals(src, split.execute());
@@ -134,6 +138,24 @@ public class SplitFunctionTest extends JMeterTestCase {
         assertNull(vars.get("VAR5_5"));
     }
 
+    @Test
+    public void shouldSplitWithPreviousResultOnly() throws Exception {
+        String src = "a,,c,";
+        vars.put("VAR", src);
+        SplitFunction split = splitParams("${VAR}", "VAR5", null);
+
+        SampleResult previousResult = new SampleResult();
+        previousResult.setResponseData("Some data", null);
+
+        assertEquals(src, split.execute(previousResult, null));
+        assertEquals("4", vars.get("VAR5_n"));
+        assertEquals("a", vars.get("VAR5_1"));
+        assertEquals("?", vars.get("VAR5_2"));
+        assertEquals("c", vars.get("VAR5_3"));
+        assertEquals("?", vars.get("VAR5_4"));
+        assertNull(vars.get("VAR5_5"));
+    }
+
     // Create the SplitFile function and set its parameters.
     private static SplitFunction splitParams(String p1, String p2, String p3) throws Exception {
         SplitFunction split = new SplitFunction();
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 79c70d7..e80edad 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -195,6 +195,7 @@ however, the profile can't be updated while the test is running.
   <li><pr>672</pr>Add more details to documentation for timeShift function. Contributed by Mariusz (mawasak at gmail.com)</li>
   <li>Updated Gradle to 7.3 (from 7.2)</li>
   <li><pr>689</pr>Code clean up in StringFromFile. Contributed by Sampath Kumar Krishnasamy (sampathkumar.krishnasamykuppusamy at aexp.com)</li>
+  <li><pr>690</pr>Refactor a few unit tests. Contributed by Sampath Kumar Krishnasamy (sampathkumar.krishnasamykuppusamy at aexp.com)</li>
 </ul>
 
  <!-- =================== Bug fixes =================== -->