use of org.apache.jorphan.collections.HashTree in project jmeter by apache.
the class CheckDirty method doAction.
/**
* @see Command#doAction(ActionEvent)
*/
@Override
public void doAction(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals(ActionNames.SUB_TREE_SAVED)) {
HashTree subTree = (HashTree) e.getSource();
subTree.traverse(this);
} else if (action.equals(ActionNames.SUB_TREE_LOADED)) {
ListedHashTree addTree = (ListedHashTree) e.getSource();
addTree.traverse(this);
} else if (action.equals(ActionNames.ADD_ALL)) {
previousGuiItems.clear();
GuiPackage.getInstance().getTreeModel().getTestPlan().traverse(this);
if (isWorkbenchSaveable()) {
GuiPackage.getInstance().getTreeModel().getWorkBench().traverse(this);
}
} else if (action.equals(ActionNames.CHECK_REMOVE)) {
GuiPackage guiPackage = GuiPackage.getInstance();
JMeterTreeNode[] nodes = guiPackage.getTreeListener().getSelectedNodes();
removeMode = true;
try {
for (int i = nodes.length - 1; i >= 0; i--) {
guiPackage.getTreeModel().getCurrentSubTree(nodes[i]).traverse(this);
}
} finally {
removeMode = false;
}
}
// If we are merging in another test plan, we know the test plan is dirty now
if (action.equals(ActionNames.SUB_TREE_MERGED)) {
dirty = true;
} else {
dirty = false;
checkMode = true;
try {
HashTree wholeTree = GuiPackage.getInstance().getTreeModel().getTestPlan();
wholeTree.traverse(this);
// check the workbench for modification
if (!dirty) {
// NOSONAR
if (isWorkbenchSaveable()) {
HashTree workbench = GuiPackage.getInstance().getTreeModel().getWorkBench();
workbench.traverse(this);
}
}
} finally {
checkMode = false;
}
}
GuiPackage.getInstance().setDirty(dirty);
}
use of org.apache.jorphan.collections.HashTree in project jmeter by apache.
the class UndoHistory method loadHistoricalTree.
/**
* Load the undo item into acceptorModel tree
*
* @param acceptorModel tree to accept the data
* @param guiInstance {@link GuiPackage} to be used
*/
private void loadHistoricalTree(JMeterTreeModel acceptorModel, GuiPackage guiInstance) {
HashTree newModel = history.get(position).getTree();
acceptorModel.removeTreeModelListener(this);
working = true;
try {
guiInstance.getTreeModel().clearTestPlan();
guiInstance.addSubTree(newModel);
} catch (Exception ex) {
log.error("Failed to load from history", ex);
}
acceptorModel.addTreeModelListener(this);
working = false;
}
use of org.apache.jorphan.collections.HashTree in project jmeter by apache.
the class TestSaveService method loadAndSave.
private boolean loadAndSave(File testFile, String fileName, boolean checkSize, File savedFile) throws Exception {
boolean failed = false;
final FileStats origStats = getFileStats(testFile);
final FileStats savedStats = getFileStats(savedFile);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000000);
try {
HashTree tree = SaveService.loadTree(testFile);
SaveService.saveTree(tree, out);
} finally {
// Make sure all the data is flushed out
out.close();
}
final FileStats compareStats = savedStats == FileStats.NO_STATS ? origStats : savedStats;
final FileStats outputStats;
try (ByteArrayInputStream ins = new ByteArrayInputStream(out.toByteArray());
Reader insReader = new InputStreamReader(ins);
BufferedReader bufferedReader = new BufferedReader(insReader)) {
outputStats = computeFileStats(bufferedReader);
}
// enough to detect most problem cases...
if ((checkSize && !compareStats.isSameSize(outputStats)) || !compareStats.hasSameLinesCount(outputStats)) {
failed = true;
System.out.println();
System.out.println("Loading file testfiles/" + fileName + " and " + "saving it back changes its size from " + compareStats.size + " to " + outputStats.size + ".");
if (!origStats.hasSameLinesCount(outputStats)) {
System.out.println("Number of lines changes from " + compareStats.lines + " to " + outputStats.lines);
}
if (saveOut) {
final File outFile = findTestFile("testfiles/" + fileName + ".out");
System.out.println("Write " + outFile);
try (FileOutputStream outf = new FileOutputStream(outFile)) {
outf.write(out.toByteArray());
}
System.out.println("Wrote " + outFile);
}
}
// the test file.
return failed;
}
use of org.apache.jorphan.collections.HashTree 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.jorphan.collections.HashTree in project jmeter by apache.
the class Load method loadProjectFile.
/**
* Loads or merges a file into the current GUI, reporting any errors to the user.
* If the file is a complete test plan, sets the GUI test plan file name
*
* @param e the event that triggered the action
* @param f the file to load
* @param merging if true, then try to merge the file into the current GUI.
* @param setDetails if true, then set the file details (if not merging)
*/
static void loadProjectFile(final ActionEvent e, final File f, final boolean merging, final boolean setDetails) {
ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.STOP_THREAD));
final GuiPackage guiPackage = GuiPackage.getInstance();
if (f != null) {
try {
if (merging) {
log.info("Merging file: {}", f);
} else {
log.info("Loading file: {}", f);
// and what if load fails?
if (setDetails) {
FileServer.getFileServer().setBaseForScript(f);
}
}
final HashTree tree = SaveService.loadTree(f);
final boolean isTestPlan = insertLoadedTree(e.getID(), tree, merging);
// don't change name if merging
if (!merging && isTestPlan && setDetails) {
// TODO should setBaseForScript be called here rather than
// above?
guiPackage.setTestPlanFile(f.getAbsolutePath());
}
} catch (NoClassDefFoundError ex) {
// Allow for missing optional jars
reportError("Missing jar file. {}", ex, true);
} catch (ConversionException ex) {
if (log.isWarnEnabled()) {
log.warn("Could not convert file. {}", ex.toString());
}
JMeterUtils.reportErrorToUser(SaveService.CEtoString(ex));
} catch (IOException ex) {
reportError("Error reading file. {}", ex, false);
} catch (Exception ex) {
reportError("Unexpected error. {}", ex, true);
}
FileDialoger.setLastJFCDirectory(f.getParentFile().getAbsolutePath());
guiPackage.updateCurrentGui();
guiPackage.getMainFrame().repaint();
}
}
Aggregations