Search in sources :

Example 41 with JMeterTreeNode

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

use of org.apache.jmeter.gui.tree.JMeterTreeNode 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)

Example 43 with JMeterTreeNode

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

the class What method doAction.

@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
    JMeterTreeNode node = GuiPackage.getInstance().getTreeListener().getCurrentNode();
    TestElement te = (TestElement) node.getUserObject();
    if (ActionNames.WHAT_CLASS.equals(e.getActionCommand())) {
        String guiClassName = te.getPropertyAsString(TestElement.GUI_CLASS);
        System.out.println(te.getClass().getName());
        System.out.println(guiClassName);
        if (log.isInfoEnabled()) {
            log.info("TestElement: {}, guiClassName: {}", te.getClass(), guiClassName);
        }
    } else if (ActionNames.DEBUG_ON.equals(e.getActionCommand())) {
        final String loggerName = te.getClass().getName();
        Configurator.setAllLevels(loggerName, Level.DEBUG);
        log.info("Log level set to DEBUG for {}", loggerName);
    } else if (ActionNames.DEBUG_OFF.equals(e.getActionCommand())) {
        final String loggerName = te.getClass().getName();
        Configurator.setAllLevels(loggerName, Level.INFO);
        log.info("Log level set to INFO for {}", loggerName);
    } else if (ActionNames.HEAP_DUMP.equals(e.getActionCommand())) {
        try {
            String s = HeapDumper.dumpHeap();
            JOptionPane.showMessageDialog(null, "Created " + s, "HeapDump", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            // NOSONAR We show cause in message
            JOptionPane.showMessageDialog(null, ex.toString(), "HeapDump", JOptionPane.ERROR_MESSAGE);
        }
    } else if (ActionNames.THREAD_DUMP.equals(e.getActionCommand())) {
        try {
            String s = ThreadDumper.threadDump();
            JOptionPane.showMessageDialog(null, "Created " + s, "ThreadDump", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception ex) {
            // NOSONAR We show cause in message
            JOptionPane.showMessageDialog(null, ex.toString(), "ThreadDump", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException)

Example 44 with JMeterTreeNode

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

the class Move method getParentNode.

private JMeterTreeNode getParentNode(JMeterTreeNode currentNode) {
    JMeterTreeNode parentNode = (JMeterTreeNode) currentNode.getParent();
    TestElement te = currentNode.getTestElement();
    if (te instanceof TestPlan || te instanceof WorkBench) {
        // So elements can only be added as children
        parentNode = null;
    }
    return parentNode;
}
Also used : TestPlan(org.apache.jmeter.testelement.TestPlan) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) TestElement(org.apache.jmeter.testelement.TestElement) WorkBench(org.apache.jmeter.testelement.WorkBench)

Example 45 with JMeterTreeNode

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

the class Paste method doAction.

/**
     * @see Command#doAction(ActionEvent)
     */
@Override
public void doAction(ActionEvent e) {
    JMeterTreeNode[] draggedNodes = Copy.getCopiedNodes();
    if (draggedNodes == null) {
        Toolkit.getDefaultToolkit().beep();
        return;
    }
    JMeterTreeListener treeListener = GuiPackage.getInstance().getTreeListener();
    JMeterTreeNode currentNode = treeListener.getCurrentNode();
    if (MenuFactory.canAddTo(currentNode, draggedNodes)) {
        for (JMeterTreeNode draggedNode : draggedNodes) {
            if (draggedNode != null) {
                addNode(currentNode, draggedNode);
            }
        }
    } else {
        Toolkit.getDefaultToolkit().beep();
    }
    GuiPackage.getInstance().getMainFrame().repaint();
}
Also used : JMeterTreeListener(org.apache.jmeter.gui.tree.JMeterTreeListener) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode)

Aggregations

JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)68 GuiPackage (org.apache.jmeter.gui.GuiPackage)21 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)18 TestElement (org.apache.jmeter.testelement.TestElement)18 TreePath (javax.swing.tree.TreePath)10 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)10 IOException (java.io.IOException)7 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)7 TestPlan (org.apache.jmeter.testelement.TestPlan)7 HashTree (org.apache.jorphan.collections.HashTree)7 ActionEvent (java.awt.event.ActionEvent)6 JTree (javax.swing.JTree)6 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)6 Controller (org.apache.jmeter.control.Controller)6 JMeterTreeListener (org.apache.jmeter.gui.tree.JMeterTreeListener)6 TreeNode (javax.swing.tree.TreeNode)5 LinkedList (java.util.LinkedList)4 JMeterGUIComponent (org.apache.jmeter.gui.JMeterGUIComponent)4 WorkBench (org.apache.jmeter.testelement.WorkBench)4 ListedHashTree (org.apache.jorphan.collections.ListedHashTree)4