Search in sources :

Example 1 with ValueReplacer

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

the class ProxyControl method addTimers.

/**
 * Helper method to replicate any timers found within the Proxy Controller
 * into the provided sampler, while replacing any occurrences of string _T_
 * in the timer's configuration with the provided deltaT.
 * Called from AWT Event thread
 *
 * @param model  Test component tree model
 * @param node   Sampler node in where we will add the timers
 * @param deltaT Time interval from the previous request
 */
@SuppressWarnings("JdkObsolete")
private void addTimers(JMeterTreeModel model, JMeterTreeNode node, long deltaT) {
    TestPlan variables = new TestPlan();
    // $NON-NLS-1$
    variables.addParameter("T", Long.toString(deltaT));
    ValueReplacer replacer = new ValueReplacer(variables);
    JMeterTreeNode mySelf = model.getNodeOf(this);
    if (mySelf != null) {
        Enumeration<?> children = mySelf.children();
        while (children.hasMoreElements()) {
            JMeterTreeNode templateNode = (JMeterTreeNode) children.nextElement();
            if (templateNode.isEnabled()) {
                TestElement template = templateNode.getTestElement();
                if (template instanceof Timer) {
                    TestElement timer = (TestElement) template.clone();
                    try {
                        timer.setComment("Recorded:" + Long.toString(deltaT) + "ms");
                        replacer.undoReverseReplace(timer);
                        model.addComponent(timer, node);
                    } catch (InvalidVariableException | IllegalUserActionException e) {
                        // Not 100% sure, but I believe this can't happen, so
                        // I'll log and throw an error:
                        log.error("Program error adding timers", e);
                        throw new Error(e);
                    }
                }
            }
        }
    }
}
Also used : InvalidVariableException(org.apache.jmeter.functions.InvalidVariableException) Timer(org.apache.jmeter.timers.Timer) TestPlan(org.apache.jmeter.testelement.TestPlan) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) NonTestElement(org.apache.jmeter.testelement.NonTestElement)

Example 2 with ValueReplacer

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

the class TestHTTPSamplersAgainstHttpMirrorServer method testGetRequest_Parameters.

private void testGetRequest_Parameters(int samplerType, int test) throws Exception {
    String titleField = "title";
    String titleValue = "mytitle";
    String descriptionField = "description";
    String descriptionValue = "mydescription";
    HTTPSamplerBase sampler = createHttpSampler(samplerType);
    String contentEncoding;
    HTTPSampleResult res;
    URL executedUrl;
    switch(test) {
        case 0:
            // Test sending simple HTTP get
            // Test sending data with default encoding
            contentEncoding = "";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 1:
            // Test sending data with ISO-8859-1 encoding
            sampler = createHttpSampler(samplerType);
            contentEncoding = ISO_8859_1;
            titleValue = "mytitle1\uc385";
            descriptionValue = "mydescription1\uc385";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 2:
            // Test sending data with UTF-8 encoding
            sampler = createHttpSampler(samplerType);
            contentEncoding = "UTF-8";
            titleValue = "mytitle2\u0153\u20a1\u0115\u00c5";
            descriptionValue = "mydescription2\u0153\u20a1\u0115\u00c5";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 3:
            // Test sending data as UTF-8, with values that changes when urlencoded
            sampler = createHttpSampler(samplerType);
            contentEncoding = "UTF-8";
            titleValue = "mytitle3\u0153+\u20a1 \u0115&yes\u00c5";
            descriptionValue = "mydescription3 \u0153 \u20a1 \u0115 \u00c5";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 4:
            // Test sending data as UTF-8, with values that have been urlencoded
            sampler = createHttpSampler(samplerType);
            contentEncoding = "UTF-8";
            titleValue = "mytitle4%2F%3D";
            descriptionValue = "mydescription4+++%2F%5C";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, true);
            break;
        case 5:
            // Test sending data as UTF-8, where user defined variables are used
            // to set the value for form data
            JMeterUtils.setLocale(Locale.ENGLISH);
            TestPlan testPlan = new TestPlan();
            JMeterVariables vars = new JMeterVariables();
            vars.put("title_prefix", "a test\u00c5");
            vars.put("description_suffix", "the_end");
            JMeterContextService.getContext().setVariables(vars);
            JMeterContextService.getContext().setSamplingStarted(true);
            ValueReplacer replacer = new ValueReplacer();
            replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
            sampler = createHttpSampler(samplerType);
            contentEncoding = "UTF-8";
            titleValue = "${title_prefix}mytitle5\u0153\u20a1\u0115\u00c5";
            descriptionValue = "mydescription5\u0153\u20a1\u0115\u00c5${description_suffix}";
            setupUrl(sampler, contentEncoding);
            sampler.setMethod(HTTPConstants.GET);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            // Replace the variables in the sampler
            replacer.replaceValues(sampler);
            res = executeSampler(sampler);
            String expectedTitleValue = "a test\u00c5mytitle5\u0153\u20a1\u0115\u00c5";
            String expectedDescriptionValue = "mydescription5\u0153\u20a1\u0115\u00c5the_end";
            sampler.setRunningVersion(true);
            executedUrl = sampler.getUrl();
            sampler.setRunningVersion(false);
            checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, expectedTitleValue, descriptionField, expectedDescriptionValue, false);
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            break;
        default:
            fail("Unexpected switch value: " + test);
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) TestPlan(org.apache.jmeter.testelement.TestPlan) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) URL(java.net.URL)

Example 3 with ValueReplacer

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

the class TestHTTPSamplersAgainstHttpMirrorServer method testPostRequest_FormMultipart.

private void testPostRequest_FormMultipart(int samplerType, String samplerDefaultEncoding) throws Exception {
    String titleField = "title";
    String titleValue = "mytitle";
    String descriptionField = "description";
    String descriptionValue = "mydescription";
    // Test sending data with default encoding
    HTTPSamplerBase sampler = createHttpSampler(samplerType);
    String contentEncoding = "";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    HTTPSampleResult res = executeSampler(sampler);
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue);
    // Test sending data as ISO-8859-1
    sampler = createHttpSampler(samplerType);
    contentEncoding = ISO_8859_1;
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    res = executeSampler(sampler);
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue);
    // Test sending data as UTF-8
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
    descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    res = executeSampler(sampler);
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue);
    // Test sending data as UTF-8, with values that would have been urlencoded
    // if it was not sent as multipart
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle/=";
    descriptionValue = "mydescription   /\\";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    res = executeSampler(sampler);
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue);
    // Test sending data as UTF-8, with values that have been urlencoded
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "mytitle%2F%3D";
    descriptionValue = "mydescription+++%2F%5C";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    res = executeSampler(sampler);
    String expectedTitleValue = "mytitle/=";
    String expectedDescriptionValue = "mydescription   /\\";
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, expectedTitleValue, descriptionField, expectedDescriptionValue);
    // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
    descriptionValue = "mydescription";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    res = executeSampler(sampler);
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue);
    // Test sending data as UTF-8, where user defined variables are used
    // to set the value for form data
    JMeterUtils.setLocale(Locale.ENGLISH);
    TestPlan testPlan = new TestPlan();
    JMeterVariables vars = new JMeterVariables();
    vars.put("title_prefix", "a test\u00c5");
    vars.put("description_suffix", "the_end");
    JMeterContextService.getContext().setVariables(vars);
    JMeterContextService.getContext().setSamplingStarted(true);
    ValueReplacer replacer = new ValueReplacer();
    replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
    sampler = createHttpSampler(samplerType);
    contentEncoding = "UTF-8";
    titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
    descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
    setupUrl(sampler, contentEncoding);
    setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
    sampler.setDoMultipart(true);
    // Replace the variables in the sampler
    replacer.replaceValues(sampler);
    res = executeSampler(sampler);
    expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
    expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
    checkPostRequestFormMultipart(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, expectedTitleValue, descriptionField, expectedDescriptionValue);
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) TestPlan(org.apache.jmeter.testelement.TestPlan) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer)

Example 4 with ValueReplacer

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

the class TestWhileController method testVariable2.

// Test with SimpleController as first item
@Test
public void testVariable2() throws Exception {
    GenericController controller = new GenericController();
    WhileController while_cont = new WhileController();
    setLastSampleStatus(false);
    while_cont.setCondition("${VAR}");
    jmvars.put("VAR", "");
    ValueReplacer vr = new ValueReplacer();
    vr.replaceValues(while_cont);
    setRunning(while_cont);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(while_cont);
    GenericController simple = new GenericController();
    while_cont.addTestElement(simple);
    simple.addTestElement(new TestSampler("one"));
    simple.addTestElement(new TestSampler("two"));
    while_cont.addTestElement(new TestSampler("three"));
    while_cont.addTestElement(new TestSampler("four"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        assertEquals("Loop: " + i, "one", nextName(controller));
        assertEquals("Loop: " + i, "two", nextName(controller));
        assertEquals("Loop: " + i, "three", nextName(controller));
        assertEquals("Loop: " + i, "four", nextName(controller));
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
    // Should not enter the loop
    jmvars.put("VAR", "LAST");
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
    jmvars.put("VAR", "");
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop: " + i, "before", nextName(controller));
        if (i == 1) {
            assertEquals("Loop: " + i, "one", nextName(controller));
            assertEquals("Loop: " + i, "two", nextName(controller));
            // Should not enter the loop next time
            jmvars.put("VAR", "LAST");
            // But should continue to the end of the loop
            assertEquals("Loop: " + i, "three", nextName(controller));
            assertEquals("Loop: " + i, "four", nextName(controller));
        }
        assertEquals("Loop: " + i, "after", nextName(controller));
        assertNull("Loop: " + i, nextName(controller));
    }
}
Also used : ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.jupiter.api.Test)

Example 5 with ValueReplacer

use of org.apache.jmeter.engine.util.ValueReplacer in project jmeter-plugins by undera.

the class ParameterizedControllerTest method testNext.

@Test
public void testNext() throws InvalidVariableException {
    System.out.println("next");
    Arguments args = new Arguments();
    args.addArgument("var2", "${var1}");
    args.addArgument("var3", "${var2}");
    instance.setUserDefinedVariables(args);
    ValueReplacer replacer = new ValueReplacer();
    replacer.replaceValues(instance);
    args.setRunningVersion(true);
    instance.next();
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    assertEquals("${var2}", vars.get("var3"));
    assertEquals("val1", vars.get("var2"));
    instance.next();
    assertEquals("val1", vars.get("var3"));
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Arguments(org.apache.jmeter.config.Arguments) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) Test(org.junit.Test)

Aggregations

ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)10 TestPlan (org.apache.jmeter.testelement.TestPlan)5 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)5 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)3 Arguments (org.apache.jmeter.config.Arguments)2 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)2 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)2 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)2 NonTestElement (org.apache.jmeter.testelement.NonTestElement)2 TestElement (org.apache.jmeter.testelement.TestElement)2 Test (org.junit.jupiter.api.Test)2 Cursor (java.awt.Cursor)1 Window (java.awt.Window)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1 URL (java.net.URL)1 JLabel (javax.swing.JLabel)1 Timer (javax.swing.Timer)1 TreeNodeWrapper (org.apache.jmeter.control.gui.TreeNodeWrapper)1