Search in sources :

Example 6 with ListedHashTree

use of org.apache.jorphan.collections.ListedHashTree in project jmeter by apache.

the class TestTransactionController method testIssue57958.

/**
     * @see "http://bz.apache.org/bugzilla/show_bug.cgi?id=57958"
     */
@Test
public void testIssue57958() throws Exception {
    JMeterContextService.getContext().setVariables(new JMeterVariables());
    TestSampleListener listener = new TestSampleListener();
    TransactionController transactionController = new TransactionController();
    transactionController.setGenerateParentSample(true);
    ResponseAssertion assertion = new ResponseAssertion();
    assertion.setTestFieldResponseCode();
    assertion.setToEqualsType();
    assertion.addTestString("201");
    DebugSampler debugSampler = new DebugSampler();
    debugSampler.addTestElement(assertion);
    LoopController loop = new LoopController();
    loop.setLoops(1);
    loop.setContinueForever(false);
    ListedHashTree hashTree = new ListedHashTree();
    hashTree.add(loop);
    hashTree.add(loop, transactionController);
    hashTree.add(transactionController, debugSampler);
    hashTree.add(transactionController, listener);
    hashTree.add(debugSampler, assertion);
    TestCompiler compiler = new TestCompiler(hashTree);
    hashTree.traverse(compiler);
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setNumThreads(1);
    ListenerNotifier notifier = new ListenerNotifier();
    JMeterThread thread = new JMeterThread(hashTree, threadGroup, notifier);
    thread.setThreadGroup(threadGroup);
    thread.setOnErrorStopThread(true);
    thread.run();
    assertEquals("Must one transaction samples with parent debug sample", 1, listener.events.size());
    assertEquals("Number of samples in transaction : 1, number of failing samples : 1", listener.events.get(0).getResult().getResponseMessage());
}
Also used : JMeterVariables(org.apache.jmeter.threads.JMeterVariables) ListedHashTree(org.apache.jorphan.collections.ListedHashTree) ResponseAssertion(org.apache.jmeter.assertions.ResponseAssertion) DebugSampler(org.apache.jmeter.sampler.DebugSampler) TestCompiler(org.apache.jmeter.threads.TestCompiler) ThreadGroup(org.apache.jmeter.threads.ThreadGroup) JMeterThread(org.apache.jmeter.threads.JMeterThread) ListenerNotifier(org.apache.jmeter.threads.ListenerNotifier) Test(org.junit.Test)

Example 7 with ListedHashTree

use of org.apache.jorphan.collections.ListedHashTree 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 8 with ListedHashTree

use of org.apache.jorphan.collections.ListedHashTree in project jmeter by apache.

the class CheckDirty method doAction.

/**
     * @see Command#doAction(ActionEvent)
     */
@Override
public void doAction(ActionEvent e) {
    String action = e.getActionCommand();
    if (action.equals(ActionNames.SUB_TREE_SAVED)) {
        HashTree subTree = (HashTree) e.getSource();
        subTree.traverse(this);
    } else if (action.equals(ActionNames.SUB_TREE_LOADED)) {
        ListedHashTree addTree = (ListedHashTree) e.getSource();
        addTree.traverse(this);
    } else if (action.equals(ActionNames.ADD_ALL)) {
        previousGuiItems.clear();
        GuiPackage.getInstance().getTreeModel().getTestPlan().traverse(this);
        if (isWorkbenchSaveable()) {
            GuiPackage.getInstance().getTreeModel().getWorkBench().traverse(this);
        }
    } else if (action.equals(ActionNames.CHECK_REMOVE)) {
        GuiPackage guiPackage = GuiPackage.getInstance();
        JMeterTreeNode[] nodes = guiPackage.getTreeListener().getSelectedNodes();
        removeMode = true;
        try {
            for (int i = nodes.length - 1; i >= 0; i--) {
                guiPackage.getTreeModel().getCurrentSubTree(nodes[i]).traverse(this);
            }
        } finally {
            removeMode = false;
        }
    }
    // If we are merging in another test plan, we know the test plan is dirty now
    if (action.equals(ActionNames.SUB_TREE_MERGED)) {
        dirty = true;
    } else {
        dirty = false;
        checkMode = true;
        try {
            HashTree wholeTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
            wholeTree.traverse(this);
            // check the workbench for modification
            if (!dirty) {
                // NOSONAR
                if (isWorkbenchSaveable()) {
                    HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
                    workbench.traverse(this);
                }
            }
        } finally {
            checkMode = false;
        }
    }
    GuiPackage.getInstance().setDirty(dirty);
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) HashTree(org.apache.jorphan.collections.HashTree) ListedHashTree(org.apache.jorphan.collections.ListedHashTree) GuiPackage(org.apache.jmeter.gui.GuiPackage)

Example 9 with ListedHashTree

use of org.apache.jorphan.collections.ListedHashTree 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 10 with ListedHashTree

use of org.apache.jorphan.collections.ListedHashTree in project jmeter by apache.

the class Save method doAction.

@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
    HashTree subTree;
    // are we saving the whole tree?
    boolean fullSave = false;
    if (!commands.contains(e.getActionCommand())) {
        throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
    }
    if (e.getActionCommand().equals(ActionNames.SAVE_AS)) {
        JMeterTreeNode[] nodes = GuiPackage.getInstance().getTreeListener().getSelectedNodes();
        if (nodes.length > 1) {
            JMeterUtils.reportErrorToUser(// $NON-NLS-1$
            JMeterUtils.getResString("save_as_error"), // $NON-NLS-1$
            JMeterUtils.getResString("save_as"));
            return;
        }
        subTree = GuiPackage.getInstance().getCurrentSubTree();
    } else if (e.getActionCommand().equals(ActionNames.SAVE_AS_TEST_FRAGMENT)) {
        JMeterTreeNode[] nodes = GuiPackage.getInstance().getTreeListener().getSelectedNodes();
        if (checkAcceptableForTestFragment(nodes)) {
            subTree = GuiPackage.getInstance().getCurrentSubTree();
            // Create Test Fragment node
            TestElement element = GuiPackage.getInstance().createTestElement(TestFragmentControllerGui.class.getName());
            HashTree hashTree = new ListedHashTree();
            HashTree tfTree = hashTree.add(new JMeterTreeNode(element, null));
            for (JMeterTreeNode node : nodes) {
                // Clone deeply current node
                TreeCloner cloner = new TreeCloner(false);
                GuiPackage.getInstance().getTreeModel().getCurrentSubTree(node).traverse(cloner);
                // Add clone to tfTree
                tfTree.add(cloner.getClonedTree());
            }
            subTree = hashTree;
        } else {
            JMeterUtils.reportErrorToUser(// $NON-NLS-1$
            JMeterUtils.getResString("save_as_test_fragment_error"), // $NON-NLS-1$
            JMeterUtils.getResString("save_as_test_fragment"));
            return;
        }
    } else {
        fullSave = true;
        HashTree testPlan = GuiPackage.getInstance().getTreeModel().getTestPlan();
        // If saveWorkBench 
        if (isWorkbenchSaveable()) {
            HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
            testPlan.add(workbench);
        }
        subTree = testPlan;
    }
    String updateFile = GuiPackage.getInstance().getTestPlanFile();
    if (!ActionNames.SAVE.equals(e.getActionCommand()) || updateFile == null) {
        JFileChooser chooser = FileDialoger.promptToSaveFile(updateFile == null ? GuiPackage.getInstance().getTreeListener().getCurrentNode().getName() + JMX_FILE_EXTENSION : updateFile);
        if (chooser == null) {
            return;
        }
        updateFile = chooser.getSelectedFile().getAbsolutePath();
        // Make sure the file ends with proper extension
        if (FilenameUtils.getExtension(updateFile).isEmpty()) {
            updateFile = updateFile + JMX_FILE_EXTENSION;
        }
        // Check if the user is trying to save to an existing file
        File f = new File(updateFile);
        if (f.exists()) {
            int response = JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(), // $NON-NLS-1$
            JMeterUtils.getResString("save_overwrite_existing_file"), // $NON-NLS-1$
            JMeterUtils.getResString("save?"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.NO_OPTION) {
                // Do not save, user does not want to overwrite
                return;
            }
        }
        if (!e.getActionCommand().equals(ActionNames.SAVE_AS)) {
            GuiPackage.getInstance().setTestPlanFile(updateFile);
        }
    }
    // backup existing file according to jmeter/user.properties settings
    List<File> expiredBackupFiles = EMPTY_FILE_LIST;
    File fileToBackup = new File(updateFile);
    try {
        expiredBackupFiles = createBackupFile(fileToBackup);
    } catch (Exception ex) {
        //$NON-NLS-1$
        log.error("Failed to create a backup for {}", fileToBackup, ex);
    }
    try {
        convertSubTree(subTree);
    } catch (Exception err) {
        if (log.isWarnEnabled()) {
            log.warn("Error converting subtree. {}", err.toString());
        }
    }
    try (FileOutputStream ostream = new FileOutputStream(updateFile)) {
        SaveService.saveTree(subTree, ostream);
        if (fullSave) {
            // Only update the stored copy of the tree for a full save
            // refetch, because convertSubTree affects it
            subTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
            if (isWorkbenchSaveable()) {
                HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
                subTree.add(workbench);
            }
            ActionRouter.getInstance().doActionNow(new ActionEvent(subTree, e.getID(), ActionNames.SUB_TREE_SAVED));
        }
        // proceed to deletion
        for (File expiredBackupFile : expiredBackupFiles) {
            try {
                FileUtils.deleteQuietly(expiredBackupFile);
            } catch (Exception ex) {
                //$NON-NLS-1$
                log.warn("Failed to delete backup file, {}", expiredBackupFile);
            }
        }
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        log.error("Error saving tree.", ex);
        throw new IllegalUserActionException("Couldn't save test plan to file: " + updateFile, ex);
    }
    GuiPackage.getInstance().updateCurrentGui();
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) HashTree(org.apache.jorphan.collections.HashTree) ListedHashTree(org.apache.jorphan.collections.ListedHashTree) ActionEvent(java.awt.event.ActionEvent) TreeCloner(org.apache.jmeter.engine.TreeCloner) TestElement(org.apache.jmeter.testelement.TestElement) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) File(java.io.File)

Aggregations

ListedHashTree (org.apache.jorphan.collections.ListedHashTree)11 HashTree (org.apache.jorphan.collections.HashTree)5 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 Arguments (org.apache.jmeter.config.Arguments)2 GenericController (org.apache.jmeter.control.GenericController)2 TreeCloner (org.apache.jmeter.engine.TreeCloner)2 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)2 GuiPackage (org.apache.jmeter.gui.GuiPackage)2 TestPlan (org.apache.jmeter.testelement.TestPlan)2 ThreadGroup (org.apache.jmeter.threads.ThreadGroup)2 ActionEvent (java.awt.event.ActionEvent)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 JFileChooser (javax.swing.JFileChooser)1 HttpHost (org.apache.http.HttpHost)1 HttpGet (org.apache.http.client.methods.HttpGet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1