Search in sources :

Example 41 with TestElement

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

the class LdapConfigGui method configure.

/**
     * A newly created component can be initialized with the contents of a Test
     * Element object by calling this method. The component is responsible for
     * querying the Test Element object for the relevant information to display
     * in its GUI.
     *
     * @param element
     *            the TestElement to configure
     */
@Override
public void configure(TestElement element) {
    super.configure(element);
    servername.setText(element.getPropertyAsString(LDAPSampler.SERVERNAME));
    port.setText(element.getPropertyAsString(LDAPSampler.PORT));
    rootdn.setText(element.getPropertyAsString(LDAPSampler.ROOTDN));
    CardLayout cl = (CardLayout) (cards.getLayout());
    final String testType = element.getPropertyAsString(LDAPSampler.TEST);
    if (testType.equals(LDAPSampler.ADD)) {
        addTest.setSelected(true);
        add.setText(element.getPropertyAsString(LDAPSampler.BASE_ENTRY_DN));
        tableAddPanel.configure((TestElement) element.getProperty(LDAPSampler.ARGUMENTS).getObjectValue());
        cl.show(cards, "Add");
    } else if (testType.equals(LDAPSampler.MODIFY)) {
        modifyTest.setSelected(true);
        modify.setText(element.getPropertyAsString(LDAPSampler.BASE_ENTRY_DN));
        tableModifyPanel.configure((TestElement) element.getProperty(LDAPSampler.ARGUMENTS).getObjectValue());
        cl.show(cards, "Modify");
    } else if (testType.equals(LDAPSampler.DELETE)) {
        deleteTest.setSelected(true);
        delete.setText(element.getPropertyAsString(LDAPSampler.DELETE));
        cl.show(cards, "Delete");
    } else if (testType.equals(LDAPSampler.SEARCHBASE)) {
        searchTest.setSelected(true);
        searchbase.setText(element.getPropertyAsString(LDAPSampler.SEARCHBASE));
        searchfilter.setText(element.getPropertyAsString(LDAPSampler.SEARCHFILTER));
        cl.show(cards, "Search");
    }
    if (element.getPropertyAsBoolean(LDAPSampler.USER_DEFINED)) {
        userDefined.setSelected(true);
    } else {
        userDefined.setSelected(false);
        // $NON-NLS-1$
        cl.show(cards, "");
    }
}
Also used : CardLayout(java.awt.CardLayout) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) TestElement(org.apache.jmeter.testelement.TestElement)

Example 42 with TestElement

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

the class JMeterTreeModel method clearTestPlan.

/**
     * Clear the test plan, and use default node for test plan and workbench.
     *
     * N.B. Should only be called by {@link GuiPackage#clearTestPlan()}
     */
public void clearTestPlan() {
    TestElement tp = new TestPlanGui().createTestElement();
    clearTestPlan(tp);
}
Also used : TestPlanGui(org.apache.jmeter.control.gui.TestPlanGui) TestElement(org.apache.jmeter.testelement.TestElement)

Example 43 with TestElement

use of org.apache.jmeter.testelement.TestElement 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 44 with TestElement

use of org.apache.jmeter.testelement.TestElement 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 45 with TestElement

use of org.apache.jmeter.testelement.TestElement 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)

Aggregations

TestElement (org.apache.jmeter.testelement.TestElement)83 Test (org.junit.Test)27 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)26 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)21 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)18 TestPlan (org.apache.jmeter.testelement.TestPlan)12 ArrayList (java.util.ArrayList)9 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)9 StringProperty (org.apache.jmeter.testelement.property.StringProperty)7 Controller (org.apache.jmeter.control.Controller)6 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)6 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)6 TreePath (javax.swing.tree.TreePath)5 GuiPackage (org.apache.jmeter.gui.GuiPackage)5 ActionEvent (java.awt.event.ActionEvent)4 IOException (java.io.IOException)4 LinkedList (java.util.LinkedList)4 Arguments (org.apache.jmeter.config.Arguments)4 WorkBench (org.apache.jmeter.testelement.WorkBench)4 TestElementProperty (org.apache.jmeter.testelement.property.TestElementProperty)4