Skip to content

Commit

Permalink
Split function test refactor with failure and different success scena…
Browse files Browse the repository at this point in the history
…rios separately

Closes #690
  • Loading branch information
SampathKumarAmex authored and FSchumacher committed Jan 8, 2022
1 parent 37be72f commit af7fc4d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,27 +35,30 @@

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());
Expand Down Expand Up @@ -134,6 +138,24 @@ public void splitTest1() throws Exception {
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();
Expand Down
1 change: 1 addition & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =================== -->
Expand Down

0 comments on commit af7fc4d

Please sign in to comment.