use of org.apache.jorphan.collections.ListedHashTree in project jmeter by apache.
the class TestTransactionController method testIssue57958.
/**
* @see "http://bz.apache.org/bugzilla/show_bug.cgi?id=57958"
*/
@Test
public void testIssue57958() throws Exception {
JMeterContextService.getContext().setVariables(new JMeterVariables());
TestSampleListener listener = new TestSampleListener();
TransactionController transactionController = new TransactionController();
transactionController.setGenerateParentSample(true);
ResponseAssertion assertion = new ResponseAssertion();
assertion.setTestFieldResponseCode();
assertion.setToEqualsType();
assertion.addTestString("201");
DebugSampler debugSampler = new DebugSampler();
debugSampler.addTestElement(assertion);
LoopController loop = new LoopController();
loop.setLoops(1);
loop.setContinueForever(false);
ListedHashTree hashTree = new ListedHashTree();
hashTree.add(loop);
hashTree.add(loop, transactionController);
hashTree.add(transactionController, debugSampler);
hashTree.add(transactionController, listener);
hashTree.add(debugSampler, assertion);
TestCompiler compiler = new TestCompiler(hashTree);
hashTree.traverse(compiler);
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
ListenerNotifier notifier = new ListenerNotifier();
JMeterThread thread = new JMeterThread(hashTree, threadGroup, notifier);
thread.setThreadGroup(threadGroup);
thread.setOnErrorStopThread(true);
thread.run();
assertEquals("Must one transaction samples with parent debug sample", 1, listener.events.size());
assertEquals("Number of samples in transaction : 1, number of failing samples : 1", listener.events.get(0).getResult().getResponseMessage());
}
use of org.apache.jorphan.collections.ListedHashTree in project jmeter by apache.
the class TestTreeCloner method testCloning.
@Test
public void testCloning() throws Exception {
ListedHashTree original = new ListedHashTree();
GenericController controller = new GenericController();
controller.setName("controller");
Arguments args = new Arguments();
args.setName("args");
TestPlan plan = new TestPlan();
plan.addParameter("server", "jakarta");
original.add(controller, args);
original.add(plan);
ResultCollector listener = new ResultCollector();
listener.setName("Collector");
original.add(controller, listener);
TreeCloner cloner = new TreeCloner();
original.traverse(cloner);
ListedHashTree newTree = cloner.getClonedTree();
assertTrue(original != newTree);
assertEquals(original.size(), newTree.size());
assertEquals(original.getTree(original.getArray()[0]).size(), newTree.getTree(newTree.getArray()[0]).size());
assertTrue(original.getArray()[0] != newTree.getArray()[0]);
assertEquals(((GenericController) original.getArray()[0]).getName(), ((GenericController) newTree.getArray()[0]).getName());
assertSame(original.getTree(original.getArray()[0]).getArray()[1], newTree.getTree(newTree.getArray()[0]).getArray()[1]);
TestPlan clonedTestPlan = (TestPlan) newTree.getArray()[1];
clonedTestPlan.setRunningVersion(true);
clonedTestPlan.recoverRunningVersion();
assertTrue(!plan.getUserDefinedVariablesAsProperty().isRunningVersion());
assertTrue(clonedTestPlan.getUserDefinedVariablesAsProperty().isRunningVersion());
Arguments vars = (Arguments) plan.getUserDefinedVariablesAsProperty().getObjectValue();
PropertyIterator iter = ((CollectionProperty) vars.getProperty(Arguments.ARGUMENTS)).iterator();
while (iter.hasNext()) {
JMeterProperty argProp = iter.next();
assertTrue(!argProp.isRunningVersion());
assertTrue(argProp.getObjectValue() instanceof Argument);
Argument arg = (Argument) argProp.getObjectValue();
arg.setValue("yahoo");
assertEquals("yahoo", arg.getValue());
}
vars = (Arguments) clonedTestPlan.getUserDefinedVariablesAsProperty().getObjectValue();
iter = vars.propertyIterator();
while (iter.hasNext()) {
assertTrue(iter.next().isRunningVersion());
}
}
use of org.apache.jorphan.collections.ListedHashTree 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.ListedHashTree in project jmeter by apache.
the class NonGuiProxySample method main.
public static void main(String[] args) throws IllegalUserActionException, IOException {
// Or wherever you put it.
JMeterUtils.setJMeterHome("./");
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir() + "/jmeter.properties");
JMeterUtils.initLocale();
TestPlan testPlan = new TestPlan();
ThreadGroup threadGroup = new ThreadGroup();
ListedHashTree testPlanTree = new ListedHashTree();
testPlanTree.add(testPlan);
testPlanTree.add(threadGroup, testPlan);
// deliberate use of deprecated ctor
@SuppressWarnings("deprecation") JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(testPlanTree, root);
ProxyControl proxy = new ProxyControl();
proxy.setNonGuiTreeModel(treeModel);
proxy.setTarget(treeModel.getNodeOf(threadGroup));
proxy.setPort(8282);
treeModel.addComponent(proxy, (JMeterTreeNode) root.getChildAt(1));
proxy.startProxy();
HttpHost proxyHost = new HttpHost("localhost", 8282);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();
try {
httpclient.execute(new HttpGet("http://example.invalid"));
} catch (Exception e) {
//
}
proxy.stopProxy();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
SaveService.saveTree(treeModel.getTestPlan(), out);
out.close();
System.out.println(out.toString());
}
}
use of org.apache.jorphan.collections.ListedHashTree 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();
}
Aggregations