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());
}
}
}
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();
}
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;
}
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();
}
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();
}
Aggregations