Search in sources :

Example 11 with JMeterTreeModel

use of org.apache.jmeter.gui.tree.JMeterTreeModel 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 12 with JMeterTreeModel

use of org.apache.jmeter.gui.tree.JMeterTreeModel 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 13 with JMeterTreeModel

use of org.apache.jmeter.gui.tree.JMeterTreeModel in project jmeter by apache.

the class SearchTreeDialog method doReplaceAll.

/**
     * Replace all occurrences in nodes that contain {@link Replaceable} Test Elements
     * @param e {@link ActionEvent}
     */
private void doReplaceAll(ActionEvent e) {
    String wordToSearch = searchTF.getText();
    String wordToReplace = replaceTF.getText();
    if (StringUtils.isEmpty(wordToReplace)) {
        return;
    }
    // Save any change to current node
    GuiPackage.getInstance().updateCurrentNode();
    // reset previous result
    ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.SEARCH_RESET));
    Searcher searcher;
    String regex;
    if (isRegexpCB.isSelected()) {
        regex = wordToSearch;
        searcher = new RegexpSearcher(isCaseSensitiveCB.isSelected(), wordToSearch);
    } else {
        regex = Pattern.quote(wordToSearch);
        searcher = new RawTextSearcher(isCaseSensitiveCB.isSelected(), wordToSearch);
    }
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeModel jMeterTreeModel = guiPackage.getTreeModel();
    Set<JMeterTreeNode> nodes = new HashSet<>();
    boolean caseSensitiveReplacement = isCaseSensitiveCB.isSelected();
    int totalReplaced = 0;
    for (JMeterTreeNode jMeterTreeNode : jMeterTreeModel.getNodesOfType(Searchable.class)) {
        try {
            Searchable searchable = (Searchable) jMeterTreeNode.getUserObject();
            List<String> searchableTokens = searchable.getSearchableTokens();
            boolean result = searcher.search(searchableTokens);
            if (result && jMeterTreeNode.getUserObject() instanceof Replaceable) {
                Replaceable replaceable = (Replaceable) jMeterTreeNode.getUserObject();
                int numberOfReplacements = replaceable.replace(regex, wordToReplace, caseSensitiveReplacement);
                if (logger.isInfoEnabled()) {
                    logger.info("Replaced " + numberOfReplacements + " in element:" + ((TestElement) jMeterTreeNode.getUserObject()).getName());
                }
                totalReplaced += numberOfReplacements;
                if (numberOfReplacements > 0) {
                    List<JMeterTreeNode> matchingNodes = jMeterTreeNode.getPathToThreadGroup();
                    nodes.addAll(matchingNodes);
                }
            }
        } catch (Exception ex) {
            logger.error("Error occurred replacing data in node:" + jMeterTreeNode.getName(), ex);
        }
    }
    statusLabel.setText(MessageFormat.format("Replaced {0} occurrences", new Object[] { totalReplaced }));
    GuiPackage guiInstance = GuiPackage.getInstance();
    JTree jTree = guiInstance.getMainFrame().getTree();
    for (JMeterTreeNode jMeterTreeNode : nodes) {
        jMeterTreeNode.setMarkedBySearch(true);
        jTree.expandPath(new TreePath(jMeterTreeNode.getPath()));
    }
    // Update GUI as current node may be concerned by changes
    if (totalReplaced > 0) {
        GuiPackage.getInstance().refreshCurrentGui();
    }
    GuiPackage.getInstance().getMainFrame().repaint();
    searchTF.requestFocusInWindow();
}
Also used : ActionEvent(java.awt.event.ActionEvent) TestElement(org.apache.jmeter.testelement.TestElement) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JTree(javax.swing.JTree) TreePath(javax.swing.tree.TreePath) GuiPackage(org.apache.jmeter.gui.GuiPackage) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) Searchable(org.apache.jmeter.gui.Searchable) Replaceable(org.apache.jmeter.gui.Replaceable) HashSet(java.util.HashSet)

Example 14 with JMeterTreeModel

use of org.apache.jmeter.gui.tree.JMeterTreeModel in project jmeter by apache.

the class ProxyControl method findFirstNodeOfType.

/**
     * Finds the first enabled node of a given type in the tree.
     *
     * @param type
     *            class of the node to be found
     *
     * @return the first node of the given type in the test component tree, or
     *         <code>null</code> if none was found.
     */
private JMeterTreeNode findFirstNodeOfType(Class<?> type) {
    JMeterTreeModel treeModel = getJmeterTreeModel();
    List<JMeterTreeNode> nodes = treeModel.getNodesOfType(type);
    for (JMeterTreeNode node : nodes) {
        if (node.isEnabled()) {
            return node;
        }
    }
    return null;
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode)

Example 15 with JMeterTreeModel

use of org.apache.jmeter.gui.tree.JMeterTreeModel in project jmeter by apache.

the class ProxyControl method notifySampleListeners.

/**
     * This will notify sample listeners directly within the Proxy of the
     * sampling that just occurred -- so that we have a means to record the
     * server's responses as we go.
     *
     * @param event
     *            sampling event to be delivered
     */
private void notifySampleListeners(SampleEvent event) {
    JMeterTreeModel treeModel = getJmeterTreeModel();
    JMeterTreeNode myNode = treeModel.getNodeOf(this);
    Enumeration<JMeterTreeNode> kids = myNode.children();
    while (kids.hasMoreElements()) {
        JMeterTreeNode subNode = kids.nextElement();
        if (subNode.isEnabled()) {
            TestElement testElement = subNode.getTestElement();
            if (testElement instanceof SampleListener) {
                ((SampleListener) testElement).sampleOccurred(event);
            }
        }
    }
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) SampleListener(org.apache.jmeter.samplers.SampleListener)

Aggregations

JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)19 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)18 GuiPackage (org.apache.jmeter.gui.GuiPackage)6 TestElement (org.apache.jmeter.testelement.TestElement)6 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)5 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)5 IOException (java.io.IOException)4 JTree (javax.swing.JTree)4 TreePath (javax.swing.tree.TreePath)4 ActionEvent (java.awt.event.ActionEvent)3 MalformedURLException (java.net.MalformedURLException)3 Searchable (org.apache.jmeter.gui.Searchable)3 ConversionException (com.thoughtworks.xstream.converters.ConversionException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 SocketException (java.net.SocketException)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 JMeterTreeListener (org.apache.jmeter.gui.tree.JMeterTreeListener)2 ConfigurationException (org.apache.jmeter.report.config.ConfigurationException)2