Search in sources :

Example 1 with ReplaceStringWithFunctions

use of org.apache.jmeter.engine.util.ReplaceStringWithFunctions in project jmeter by apache.

the class TestLoopController method testBug54467.

@Test
public void testBug54467() throws Exception {
    JMeterContext jmctx = JMeterContextService.getContext();
    String lcName = "LC";
    LoopController loop = new LoopController();
    loop.setName(lcName);
    Map<String, String> variables = new HashMap<>();
    ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
    jmctx.setVariables(new JMeterVariables());
    StringProperty prop = new StringProperty(LoopController.LOOPS, "${__Random(1,12,)}");
    JMeterProperty newProp = transformer.transformValue(prop);
    newProp.setRunningVersion(true);
    loop.setProperty(newProp);
    loop.addTestElement(new TestSampler("random run"));
    loop.setRunningVersion(true);
    loop.initialize();
    int loops = loop.getLoops();
    for (int i = 0; i < loops; i++) {
        Sampler s = loop.next();
        assertNotNull(s);
        Integer value = (Integer) jmctx.getVariables().getObject(JMeterUtils.formatJMeterExportedVariableName(lcName + LoopController.INDEX_VAR_NAME_SUFFIX));
        assertNotNull(value);
        assertEquals(Integer.valueOf(i), value);
    }
    assertNull(loop.next());
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.jupiter.api.Test)

Example 2 with ReplaceStringWithFunctions

use of org.apache.jmeter.engine.util.ReplaceStringWithFunctions in project jmeter by apache.

the class TestSwitchController method testFunction.

/*
     * N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
     * otherwise the function cannot be resolved.
     */
@Test
public void testFunction() throws Exception {
    JMeterContext jmctx = JMeterContextService.getContext();
    Map<String, String> variables = new HashMap<>();
    ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
    jmctx.setVariables(new JMeterVariables());
    JMeterVariables jmvars = jmctx.getVariables();
    jmvars.put("VAR", "100");
    StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
    JMeterProperty newProp = transformer.transformValue(prop);
    newProp.setRunningVersion(true);
    GenericController controller = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setProperty(newProp);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    switch_cont.addTestElement(new TestSampler("0"));
    switch_cont.addTestElement(new TestSampler("1"));
    switch_cont.addTestElement(new TestSampler("2"));
    switch_cont.addTestElement(new TestSampler("3"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    assertEquals("100", jmvars.get("VAR"));
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop " + i, "before", nextName(controller));
        assertEquals("Loop " + i, "" + i, nextName(controller));
        assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
        assertEquals("Loop " + i, "after", nextName(controller));
        assertNull(nextName(controller));
    }
    int i = 4;
    assertEquals("Loop " + i, "before", nextName(controller));
    assertEquals("Loop " + i, "0", nextName(controller));
    assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
    assertEquals("Loop " + i, "after", nextName(controller));
    assertNull(nextName(controller));
    assertEquals("4", jmvars.get("VAR"));
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 JMeterContext (org.apache.jmeter.threads.JMeterContext)2 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)2 Test (org.junit.jupiter.api.Test)2 Sampler (org.apache.jmeter.samplers.Sampler)1