Search in sources :

Example 6 with IllegalUserActionException

use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.

the class ProxyControl method addTimers.

/**
     * Helper method to replicate any timers found within the Proxy Controller
     * into the provided sampler, while replacing any occurrences of string _T_
     * in the timer's configuration with the provided deltaT.
     * Called from AWT Event thread
     * @param model
     *            Test component tree model
     * @param node
     *            Sampler node in where we will add the timers
     * @param deltaT
     *            Time interval from the previous request
     */
private void addTimers(JMeterTreeModel model, JMeterTreeNode node, long deltaT) {
    TestPlan variables = new TestPlan();
    // $NON-NLS-1$
    variables.addParameter("T", Long.toString(deltaT));
    ValueReplacer replacer = new ValueReplacer(variables);
    JMeterTreeNode mySelf = model.getNodeOf(this);
    Enumeration<JMeterTreeNode> children = mySelf.children();
    while (children.hasMoreElements()) {
        JMeterTreeNode templateNode = children.nextElement();
        if (templateNode.isEnabled()) {
            TestElement template = templateNode.getTestElement();
            if (template instanceof Timer) {
                TestElement timer = (TestElement) template.clone();
                try {
                    timer.setComment("Recorded:" + Long.toString(deltaT) + "ms");
                    replacer.undoReverseReplace(timer);
                    model.addComponent(timer, node);
                } catch (InvalidVariableException | IllegalUserActionException e) {
                    // Not 100% sure, but I believe this can't happen, so
                    // I'll log and throw an error:
                    log.error("Program error", e);
                    throw new Error(e);
                }
            }
        }
    }
}
Also used : InvalidVariableException(org.apache.jmeter.functions.InvalidVariableException) Timer(org.apache.jmeter.timers.Timer) TestPlan(org.apache.jmeter.testelement.TestPlan) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) ValueReplacer(org.apache.jmeter.engine.util.ValueReplacer) TestElement(org.apache.jmeter.testelement.TestElement) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement)

Example 7 with IllegalUserActionException

use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.

the class ProxyControl method setAuthorization.

/**
     * Find if there is any AuthManager in JMeterTreeModel
     * If there is no one, create and add it to tree
     * Add authorization object to AuthManager
     * @param authorization {@link Authorization}
     * @param target {@link JMeterTreeNode}
     */
private void setAuthorization(Authorization authorization, JMeterTreeNode target) {
    JMeterTreeModel jmeterTreeModel = getJmeterTreeModel();
    List<JMeterTreeNode> authManagerNodes = jmeterTreeModel.getNodesOfType(AuthManager.class);
    if (authManagerNodes.isEmpty()) {
        try {
            log.debug("Creating HTTP Authentication manager for authorization:" + authorization);
            AuthManager authManager = newAuthorizationManager(authorization);
            jmeterTreeModel.addComponent(authManager, target);
        } catch (IllegalUserActionException e) {
            log.error("Failed to add Authorization Manager to target node:" + target.getName(), e);
        }
    } else {
        AuthManager authManager = (AuthManager) authManagerNodes.get(0).getTestElement();
        authManager.addAuth(authorization);
    }
}
Also used : JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException)

Example 8 with IllegalUserActionException

use of org.apache.jmeter.exceptions.IllegalUserActionException 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());
    }
}
Also used : ListedHashTree(org.apache.jorphan.collections.ListedHashTree) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TestPlan(org.apache.jmeter.testelement.TestPlan) HttpGet(org.apache.http.client.methods.HttpGet) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JMeterTreeModel(org.apache.jmeter.gui.tree.JMeterTreeModel) HttpHost(org.apache.http.HttpHost) JMeterTreeNode(org.apache.jmeter.gui.tree.JMeterTreeNode) ThreadGroup(org.apache.jmeter.threads.ThreadGroup)

Example 9 with IllegalUserActionException

use of org.apache.jmeter.exceptions.IllegalUserActionException in project jmeter by apache.

the class SaveGraphics method doAction.

@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
    if (!commands.contains(e.getActionCommand())) {
        throw new IllegalUserActionException("Invalid user command:" + e.getActionCommand());
    }
    if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS)) {
        JMeterGUIComponent component = GuiPackage.getInstance().getCurrentGui();
        // get the JComponent from the visualizer
        if (component instanceof Printable) {
            JComponent comp = ((Printable) component).getPrintableComponent();
            saveImage(comp);
        }
    }
    if (e.getActionCommand().equals(ActionNames.SAVE_GRAPHICS_ALL)) {
        JMeterGUIComponent component = GuiPackage.getInstance().getCurrentGui();
        JComponent comp = ((JComponent) component).getRootPane();
        saveImage(comp);
    }
}
Also used : JComponent(javax.swing.JComponent) IllegalUserActionException(org.apache.jmeter.exceptions.IllegalUserActionException) JMeterGUIComponent(org.apache.jmeter.gui.JMeterGUIComponent) Printable(org.apache.jmeter.visualizers.Printable)

Example 10 with IllegalUserActionException

use of org.apache.jmeter.exceptions.IllegalUserActionException 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)

Aggregations

IllegalUserActionException (org.apache.jmeter.exceptions.IllegalUserActionException)12 JMeterTreeNode (org.apache.jmeter.gui.tree.JMeterTreeNode)8 TestElement (org.apache.jmeter.testelement.TestElement)5 IOException (java.io.IOException)4 JMeterTreeModel (org.apache.jmeter.gui.tree.JMeterTreeModel)3 TestPlan (org.apache.jmeter.testelement.TestPlan)3 ActionEvent (java.awt.event.ActionEvent)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ConfigTestElement (org.apache.jmeter.config.ConfigTestElement)2 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)2 GuiPackage (org.apache.jmeter.gui.GuiPackage)2 JMeterGUIComponent (org.apache.jmeter.gui.JMeterGUIComponent)2 HashTree (org.apache.jorphan.collections.HashTree)2 ListedHashTree (org.apache.jorphan.collections.ListedHashTree)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1