Search in sources :

Example 1 with TreeCloner

use of org.apache.jmeter.engine.TreeCloner in project jmeter by apache.

the class Start method startEngine.

/**
     * Start JMeter engine
     * @param ignoreTimer flag to ignore timers
     * @param isValidationShot 
     * @param threadGroupsToRun Array of AbstractThreadGroup to run
     */
private void startEngine(boolean ignoreTimer, boolean isValidationShot, AbstractThreadGroup[] threadGroupsToRun) {
    GuiPackage gui = GuiPackage.getInstance();
    HashTree testTree = gui.getTreeModel().getTestPlan();
    JMeter.convertSubTree(testTree);
    if (threadGroupsToRun != null && threadGroupsToRun.length > 0) {
        removeThreadGroupsFromHashTree(testTree, threadGroupsToRun);
    }
    testTree.add(testTree.getArray()[0], gui.getMainFrame());
    if (log.isDebugEnabled()) {
        log.debug("test plan before cloning is running version: {}", ((TestPlan) testTree.getArray()[0]).isRunningVersion());
    }
    ListedHashTree clonedTree = null;
    if (isValidationShot) {
        TreeCloner cloner = createTreeClonerForValidation();
        testTree.traverse(cloner);
        clonedTree = cloner.getClonedTree();
    } else {
        TreeCloner cloner = cloneTree(testTree, ignoreTimer);
        clonedTree = cloner.getClonedTree();
    }
    if (popupCheckExistingFileListener(testTree)) {
        engine = new StandardJMeterEngine();
        engine.configure(clonedTree);
        try {
            engine.runTest();
        } catch (JMeterEngineException e) {
            JOptionPane.showMessageDialog(gui.getMainFrame(), e.getMessage(), JMeterUtils.getResString("error_occurred"), //$NON-NLS-1$
            JOptionPane.ERROR_MESSAGE);
        }
        if (log.isDebugEnabled()) {
            log.debug("test plan after cloning and running test is running version: {}", ((TestPlan) testTree.getArray()[0]).isRunningVersion());
        }
    }
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) HashTree(org.apache.jorphan.collections.HashTree) ListedHashTree(org.apache.jorphan.collections.ListedHashTree) StandardJMeterEngine(org.apache.jmeter.engine.StandardJMeterEngine) GuiPackage(org.apache.jmeter.gui.GuiPackage) TreeCloner(org.apache.jmeter.engine.TreeCloner) JMeterEngineException(org.apache.jmeter.engine.JMeterEngineException)

Example 2 with TreeCloner

use of org.apache.jmeter.engine.TreeCloner 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 3 with TreeCloner

use of org.apache.jmeter.engine.TreeCloner in project jmeter by apache.

the class Start method cloneTree.

/**
     * Create a Cloner that ignores {@link Timer} if removeTimers is true
     * @param testTree {@link HashTree}
     * @param removeTimers boolean remove timers 
     * @return {@link TreeCloner}
     */
private TreeCloner cloneTree(HashTree testTree, boolean removeTimers) {
    TreeCloner cloner = null;
    if (removeTimers) {
        cloner = new TreeClonerNoTimer(false);
    } else {
        cloner = new TreeCloner(false);
    }
    testTree.traverse(cloner);
    return cloner;
}
Also used : TreeCloner(org.apache.jmeter.engine.TreeCloner) TreeClonerNoTimer(org.apache.jmeter.engine.TreeClonerNoTimer)

Example 4 with TreeCloner

use of org.apache.jmeter.engine.TreeCloner in project jmeter by apache.

the class UndoCommand method convertAndCloneSubTree.

/**
     * wrapper to use package-visible method
     * and clone tree for saving
     *
     * @param tree to be converted and cloned
     * @return converted and cloned tree
     */
public static HashTree convertAndCloneSubTree(HashTree tree) {
    Save executor = new Save();
    executor.convertSubTree(tree);
    // convert before clone
    TreeCloner cloner = new TreeCloner(false);
    tree.traverse(cloner);
    return cloner.getClonedTree();
}
Also used : TreeCloner(org.apache.jmeter.engine.TreeCloner)

Example 5 with TreeCloner

use of org.apache.jmeter.engine.TreeCloner in project jmeter by apache.

the class ThreadGroup method cloneTree.

/**
     * @param tree {@link ListedHashTree}
     * @return a clone of tree
     */
private ListedHashTree cloneTree(ListedHashTree tree) {
    TreeCloner cloner = new TreeCloner(true);
    tree.traverse(cloner);
    return cloner.getClonedTree();
}
Also used : TreeCloner(org.apache.jmeter.engine.TreeCloner)

Aggregations

TreeCloner (org.apache.jmeter.engine.TreeCloner)5 HashTree (org.apache.jorphan.collections.HashTree)2 ListedHashTree (org.apache.jorphan.collections.ListedHashTree)2 ActionEvent (java.awt.event.ActionEvent)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 JFileChooser (javax.swing.JFileChooser)1 JMeterEngineException (org.apache.jmeter.engine.JMeterEngineException)1 StandardJMeterEngine (org.apache.jmeter.engine.StandardJMeterEngine)1 TreeClonerNoTimer (org.apache.jmeter.engine.TreeClonerNoTimer)1 IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)1 GuiPackage (org.apache.jmeter.gui.GuiPackage)1 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)1 TestElement (org.apache.jmeter.testelement.TestElement)1