Search in sources :

Example 11 with TestPlan

use of org.apache.jmeter.testelement.TestPlan in project jmeter by apache.

the class TestTreeCloner method testCloning.

@Test
public void testCloning() throws Exception {
    ListedHashTree original = new ListedHashTree();
    GenericController controller = new GenericController();
    controller.setName("controller");
    Arguments args = new Arguments();
    args.setName("args");
    TestPlan plan = new TestPlan();
    plan.addParameter("server", "jakarta");
    original.add(controller, args);
    original.add(plan);
    ResultCollector listener = new ResultCollector();
    listener.setName("Collector");
    original.add(controller, listener);
    TreeCloner cloner = new TreeCloner();
    original.traverse(cloner);
    ListedHashTree newTree = cloner.getClonedTree();
    assertTrue(original != newTree);
    assertEquals(original.size(), newTree.size());
    assertEquals(original.getTree(original.getArray()[0]).size(), newTree.getTree(newTree.getArray()[0]).size());
    assertTrue(original.getArray()[0] != newTree.getArray()[0]);
    assertEquals(((GenericController) original.getArray()[0]).getName(), ((GenericController) newTree.getArray()[0]).getName());
    assertSame(original.getTree(original.getArray()[0]).getArray()[1], newTree.getTree(newTree.getArray()[0]).getArray()[1]);
    TestPlan clonedTestPlan = (TestPlan) newTree.getArray()[1];
    clonedTestPlan.setRunningVersion(true);
    clonedTestPlan.recoverRunningVersion();
    assertTrue(!plan.getUserDefinedVariablesAsProperty().isRunningVersion());
    assertTrue(clonedTestPlan.getUserDefinedVariablesAsProperty().isRunningVersion());
    Arguments vars = (Arguments) plan.getUserDefinedVariablesAsProperty().getObjectValue();
    PropertyIterator iter = ((CollectionProperty) vars.getProperty(Arguments.ARGUMENTS)).iterator();
    while (iter.hasNext()) {
        JMeterProperty argProp = iter.next();
        assertTrue(!argProp.isRunningVersion());
        assertTrue(argProp.getObjectValue() instanceof Argument);
        Argument arg = (Argument) argProp.getObjectValue();
        arg.setValue("yahoo");
        assertEquals("yahoo", arg.getValue());
    }
    vars = (Arguments) clonedTestPlan.getUserDefinedVariablesAsProperty().getObjectValue();
    iter = vars.propertyIterator();
    while (iter.hasNext()) {
        assertTrue(iter.next().isRunningVersion());
    }
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) TestPlan(org.apache.jmeter.testelement.TestPlan) Arguments(org.apache.jmeter.config.Arguments) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) GenericController(org.apache.jmeter.control.GenericController) ResultCollector(org.apache.jmeter.reporters.ResultCollector) Test(org.junit.Test)

Example 12 with TestPlan

use of org.apache.jmeter.testelement.TestPlan 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
     */
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);
    Enumeration<JMeterTreeNode> children = mySelf.children();
    while (children.hasMoreElements()) {
        JMeterTreeNode templateNode = 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", 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)

Example 13 with TestPlan

use of org.apache.jmeter.testelement.TestPlan in project jmeter by apache.

the class ProxyControl method findApplicableElements.

/**
     * Finds all configuration objects of the given class applicable to the
     * recorded samplers, that is:
     * <ul>
     * <li>All such elements directly within the HTTP(S) Test Script Recorder (these have
     * the highest priority).
     * <li>All such elements directly within the target controller (higher
     * priority) or directly within any containing controller (lower priority),
     * including the Test Plan itself (lowest priority).
     * </ul>
     *
     * @param myTarget
     *            tree node for the recording target controller.
     * @param myClass
     *            Class of the elements to be found.
     * @param ascending
     *            true if returned elements should be ordered in ascending
     *            priority, false if they should be in descending priority.
     *
     * @return a collection of applicable objects of the given class.
     */
// TODO - could be converted to generic class?
private Collection<?> findApplicableElements(JMeterTreeNode myTarget, Class<? extends TestElement> myClass, boolean ascending) {
    JMeterTreeModel treeModel = getJmeterTreeModel();
    LinkedList<TestElement> elements = new LinkedList<>();
    // Look for elements directly within the HTTP proxy:
    Enumeration<?> kids = treeModel.getNodeOf(this).children();
    while (kids.hasMoreElements()) {
        JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
        if (subNode.isEnabled()) {
            TestElement element = (TestElement) subNode.getUserObject();
            if (myClass.isInstance(element)) {
                if (ascending) {
                    elements.addFirst(element);
                } else {
                    elements.add(element);
                }
            }
        }
    }
    // Look for arguments elements in the target controller or higher up:
    for (JMeterTreeNode controller = myTarget; controller != null; controller = (JMeterTreeNode) controller.getParent()) {
        kids = controller.children();
        while (kids.hasMoreElements()) {
            JMeterTreeNode subNode = (JMeterTreeNode) kids.nextElement();
            if (subNode.isEnabled()) {
                TestElement element = (TestElement) subNode.getUserObject();
                if (myClass.isInstance(element)) {
                    log.debug("Applicable: " + element.getName());
                    if (ascending) {
                        elements.addFirst(element);
                    } else {
                        elements.add(element);
                    }
                }
                // Special case for the TestPlan's Arguments sub-element:
                if (element instanceof TestPlan) {
                    TestPlan tp = (TestPlan) element;
                    Arguments args = tp.getArguments();
                    if (myClass.isInstance(args)) {
                        if (ascending) {
                            elements.addFirst(args);
                        } else {
                            elements.add(args);
                        }
                    }
                }
            }
        }
    }
    return elements;
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) TestPlan(org.apache.jmeter.testelement.TestPlan) Arguments(org.apache.jmeter.config.Arguments) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) LinkedList(java.util.LinkedList)

Example 14 with TestPlan

use of org.apache.jmeter.testelement.TestPlan in project jmeter by apache.

the class NonGuiProxySample method main.

public static void main(String[] args) throws IllegalUserActionException, IOException {
    // Or wherever you put it.
    JMeterUtils.setJMeterHome("./");
    JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
    JMeterUtils.initLocale();
    TestPlan testPlan = new TestPlan();
    ThreadGroup threadGroup = new ThreadGroup();
    ListedHashTree testPlanTree = new ListedHashTree();
    testPlanTree.add(testPlan);
    testPlanTree.add(threadGroup, testPlan);
    // deliberate use of deprecated ctor
    @SuppressWarnings("deprecation") JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
    JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
    treeModel.addSubTree(testPlanTree, root);
    ProxyControl proxy = new ProxyControl();
    proxy.setNonGuiTreeModel(treeModel);
    proxy.setTarget(treeModel.getNodeOf(threadGroup));
    proxy.setPort(8282);
    treeModel.addComponent(proxy, (JMeterTreeNode) root.getChildAt(1));
    proxy.startProxy();
    HttpHost proxyHost = new HttpHost("localhost", 8282);
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
    CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();
    try {
        httpclient.execute(new HttpGet("http://example.invalid"));
    } catch (Exception e) {
    //
    }
    proxy.stopProxy();
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        SaveService.saveTree(treeModel.getTestPlan(), out);
        out.close();
        System.out.println(out.toString());
    }
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TestPlan(org.apache.jmeter.testelement.TestPlan) HttpGet(org.apache.http.client.methods.HttpGet) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) HttpHost(org.apache.http.HttpHost) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) ThreadGroup(org.apache.jmeter.threads.ThreadGroup)

Example 15 with TestPlan

use of org.apache.jmeter.testelement.TestPlan in project jmeter by apache.

the class TestHTTPSamplersAgainstHttpMirrorServer method testPostRequest_UrlEncoded.

private void testPostRequest_UrlEncoded(int samplerType, String samplerDefaultEncoding, int test) throws Exception {
    String titleField = "title";
    String titleValue = "mytitle";
    String descriptionField = "description";
    String descriptionValue = "mydescription";
    HTTPSamplerBase sampler = createHttpSampler(samplerType);
    HTTPSampleResult res;
    String contentEncoding;
    switch(test) {
        case 0:
            // Test sending data with default encoding
            contentEncoding = "";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 1:
            // Test sending data as ISO-8859-1
            contentEncoding = ISO_8859_1;
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 2:
            // Test sending data as UTF-8
            contentEncoding = "UTF-8";
            titleValue = "mytitle2œ₡ĕÅ";
            descriptionValue = "mydescription2œ₡ĕÅ";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 3:
            // Test sending data as UTF-8, with values that will change when urlencoded
            contentEncoding = "UTF-8";
            titleValue = "mytitle3/=";
            descriptionValue = "mydescription3   /\\";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 4:
            // Test sending data as UTF-8, with values that have been urlencoded
            contentEncoding = "UTF-8";
            titleValue = "mytitle4%2F%3D";
            descriptionValue = "mydescription4+++%2F%5C";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true);
            break;
        case 5:
            // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
            contentEncoding = "UTF-8";
            titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
            descriptionValue = "mydescription5";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            res = executeSampler(sampler);
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
            break;
        case 6:
            // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses,
            // with values urlencoded, but the always encode set to false for the arguments
            // This is how the HTTP Proxy server adds arguments to the sampler
            contentEncoding = "UTF-8";
            titleValue = "%2FwEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ%2FrA%2B8DZ2dnZ2dnZ2d%2FGNDar6OshPwdJc%3D";
            descriptionValue = "mydescription6";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            ((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
            ((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
            res = executeSampler(sampler);
            assertFalse(((HTTPArgument) sampler.getArguments().getArgument(0)).isAlwaysEncoded());
            assertFalse(((HTTPArgument) sampler.getArguments().getArgument(1)).isAlwaysEncoded());
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true);
            break;
        case 7:
            // 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Å");
            vars.put("description_suffix", "the_end");
            JMeterContextService.getContext().setVariables(vars);
            JMeterContextService.getContext().setSamplingStarted(true);
            ValueReplacer replacer = new ValueReplacer();
            replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
            contentEncoding = "UTF-8";
            titleValue = "${title_prefix}mytitle7œ₡ĕÅ";
            descriptionValue = "mydescription7œ₡ĕÅ${description_suffix}";
            setupUrl(sampler, contentEncoding);
            setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
            // Replace the variables in the sampler
            replacer.replaceValues(sampler);
            res = executeSampler(sampler);
            String expectedTitleValue = "a testÅmytitle7œ₡ĕÅ";
            String expectedDescriptionValue = "mydescription7œ₡ĕÅthe_end";
            checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, expectedTitleValue, descriptionField, expectedDescriptionValue, false);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            break;
        default:
            fail("Unexpected switch value: " + test);
    }
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) TestPlan(org.apache.jmeter.testelement.TestPlan) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer)

Aggregations

TestPlan (org.apache.jmeter.testelement.TestPlan)23 TestElement (org.apache.jmeter.testelement.TestElement)12 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)7 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)6 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)6 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)5 WorkBench (org.apache.jmeter.testelement.WorkBench)4 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)4 StringProperty (org.apache.jmeter.testelement.property.StringProperty)4 Test (org.junit.Test)4 Arguments (org.apache.jmeter.config.Arguments)3 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)3 CollectionProperty (org.apache.jmeter.testelement.property.CollectionProperty)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Controller (org.apache.jmeter.control.Controller)2 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)2 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)2 HTTPArgument (org.apache.jmeter.protocol.http.util.HTTPArgument)2 ResultCollector (org.apache.jmeter.reporters.ResultCollector)2