View | Details | Raw Unified | Return to bug 60514
Collapse All | Expand All

(-)src/core/org/apache/jmeter/gui/GuiPackage.java (+25 lines)
Lines 37-42 Link Here
37
import org.apache.jmeter.engine.util.ValueReplacer;
37
import org.apache.jmeter.engine.util.ValueReplacer;
38
import org.apache.jmeter.exceptions.IllegalUserActionException;
38
import org.apache.jmeter.exceptions.IllegalUserActionException;
39
import org.apache.jmeter.gui.UndoHistory.HistoryListener;
39
import org.apache.jmeter.gui.UndoHistory.HistoryListener;
40
import org.apache.jmeter.gui.action.TreeNodeNamingPolicy;
41
import org.apache.jmeter.gui.action.impl.DefaultTreeNodeNamingPolicy;
40
import org.apache.jmeter.gui.tree.JMeterTreeListener;
42
import org.apache.jmeter.gui.tree.JMeterTreeListener;
41
import org.apache.jmeter.gui.tree.JMeterTreeModel;
43
import org.apache.jmeter.gui.tree.JMeterTreeModel;
42
import org.apache.jmeter.gui.tree.JMeterTreeNode;
44
import org.apache.jmeter.gui.tree.JMeterTreeNode;
Lines 69-74 Link Here
69
    /** Logging. */
71
    /** Logging. */
70
    private static final Logger log = LoggingManager.getLoggerForClass();
72
    private static final Logger log = LoggingManager.getLoggerForClass();
71
73
74
    private static final String NAMING_POLICY_IMPLEMENTATION = 
75
            JMeterUtils.getPropDefault("naming_policy_impl", //$NON-NLS-1$ 
76
                    DefaultTreeNodeNamingPolicy.class.getName());
77
72
    /** Singleton instance. */
78
    /** Singleton instance. */
73
    private static GuiPackage guiPack;
79
    private static GuiPackage guiPack;
74
80
Lines 653-658 Link Here
653
659
654
    private final List<Stoppable> stoppables = Collections.synchronizedList(new ArrayList<Stoppable>());
660
    private final List<Stoppable> stoppables = Collections.synchronizedList(new ArrayList<Stoppable>());
655
661
662
    private TreeNodeNamingPolicy namingPolicy;
663
656
    /**
664
    /**
657
     * Sets the filepath of the current test plan. It's shown in the main frame
665
     * Sets the filepath of the current test plan. It's shown in the main frame
658
     * title and used on saving.
666
     * title and used on saving.
Lines 856-859 Link Here
856
        ((JMeterToolBar)toolbar).updateUndoRedoIcons(history.canUndo(), history.canRedo());
864
        ((JMeterToolBar)toolbar).updateUndoRedoIcons(history.canUndo(), history.canRedo());
857
    }
865
    }
858
866
867
    /**
868
     * @return {@link TreeNodeNamingPolicy}
869
     */
870
    public TreeNodeNamingPolicy getNamingPolicy() {
871
        if(namingPolicy == null) {
872
            try {
873
                Class<?> implementationClass = Class.forName(NAMING_POLICY_IMPLEMENTATION);
874
                this.namingPolicy = (TreeNodeNamingPolicy) implementationClass.newInstance();
875
                
876
            } catch (Exception ex) {
877
                log.error("Failed to create configured naming policy:"+NAMING_POLICY_IMPLEMENTATION+", will use default one", ex);
878
                this.namingPolicy = new DefaultTreeNodeNamingPolicy();
879
            }
880
        }
881
        return namingPolicy;
882
    }
883
859
}
884
}
(-)src/core/org/apache/jmeter/gui/action/ActionNames.java (+1 lines)
Lines 34-39 Link Here
34
    public static final String ADD_ALL          = "add_all"; // $NON-NLS-1$
34
    public static final String ADD_ALL          = "add_all"; // $NON-NLS-1$
35
    public static final String ADD_PARENT       = "Add Parent"; // $NON-NLS-1$
35
    public static final String ADD_PARENT       = "Add Parent"; // $NON-NLS-1$
36
    public static final String ANALYZE_FILE     = "Analyze File"; // $NON-NLS-1$
36
    public static final String ANALYZE_FILE     = "Analyze File"; // $NON-NLS-1$
37
    public static final String APPLY_NAMING_CONVENTION    = "Apply Naming Convention"; // $NON-NLS-1$
37
    public static final String CHANGE_LANGUAGE  = "change_language"; // $NON-NLS-1$
38
    public static final String CHANGE_LANGUAGE  = "change_language"; // $NON-NLS-1$
38
    public static final String CHANGE_PARENT    = "Change Parent"; // $NON-NLS-1$
39
    public static final String CHANGE_PARENT    = "Change Parent"; // $NON-NLS-1$
39
    public static final String CHECK_DIRTY      = "check_dirty"; // $NON-NLS-1$
40
    public static final String CHECK_DIRTY      = "check_dirty"; // $NON-NLS-1$
(-)src/core/org/apache/jmeter/gui/action/AddToTree.java (+1 lines)
Lines 68-73 Link Here
68
            TestElement testElement = guiPackage.createTestElement(((JComponent) e.getSource()).getName());
68
            TestElement testElement = guiPackage.createTestElement(((JComponent) e.getSource()).getName());
69
            JMeterTreeNode parentNode = guiPackage.getCurrentNode();
69
            JMeterTreeNode parentNode = guiPackage.getCurrentNode();
70
            JMeterTreeNode node = guiPackage.getTreeModel().addComponent(testElement, parentNode);
70
            JMeterTreeNode node = guiPackage.getTreeModel().addComponent(testElement, parentNode);
71
            guiPackage.getNamingPolicy().nameOnCreation(node);
71
            guiPackage.getMainFrame().getTree().setSelectionPath(new TreePath(node.getPath()));
72
            guiPackage.getMainFrame().getTree().setSelectionPath(new TreePath(node.getPath()));
72
        } catch (Exception err) {
73
        } catch (Exception err) {
73
            log.error("", err); // $NON-NLS-1$
74
            log.error("", err); // $NON-NLS-1$
(-)src/core/org/apache/jmeter/gui/action/ApplyNamingConvention.java (+92 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
19
package org.apache.jmeter.gui.action;
20
21
import java.awt.Toolkit;
22
import java.awt.event.ActionEvent;
23
import java.util.Enumeration;
24
import java.util.HashSet;
25
import java.util.Set;
26
27
import org.apache.jmeter.control.Controller;
28
import org.apache.jmeter.gui.GuiPackage;
29
import org.apache.jmeter.gui.tree.JMeterTreeNode;
30
import org.apache.jmeter.util.JMeterUtils;
31
import org.apache.jorphan.logging.LoggingManager;
32
import org.apache.log.Logger;
33
34
/**
35
 * Allows to apply naming convention on nodes
36
 * @since 3.2
37
 */
38
public class ApplyNamingConvention extends AbstractAction {
39
    private static final Logger log = LoggingManager.getLoggerForClass();
40
41
    private static final Set<String> commands = new HashSet<>();
42
    
43
44
    static {
45
        commands.add(ActionNames.APPLY_NAMING_CONVENTION);
46
    }
47
48
    public ApplyNamingConvention() {
49
    }
50
51
    @Override
52
    public void doAction(ActionEvent e) {
53
        GuiPackage guiPackage = GuiPackage.getInstance();
54
        JMeterTreeNode currentNode = guiPackage.getTreeListener().getCurrentNode();
55
        if (!(currentNode.getUserObject() instanceof Controller)) {
56
            Toolkit.getDefaultToolkit().beep();
57
            return;
58
        }
59
        try {
60
            applyNamingPolicyToCurrentNode(guiPackage, currentNode);            
61
        } catch (Exception err) {
62
            Toolkit.getDefaultToolkit().beep();
63
            log.error("Failed to apply naming policy", err);
64
            JMeterUtils.reportErrorToUser("Failed to apply naming policy", err);
65
        }
66
67
    }
68
69
    /**
70
     * Apply the naming policy of currentNode children
71
     * @param guiPackage {@link GuiPackage}
72
     * @param currentNode Parent node of elements on which we apply naming policy
73
     */
74
    private void applyNamingPolicyToCurrentNode(GuiPackage guiPackage, 
75
            JMeterTreeNode currentNode) {
76
        TreeNodeNamingPolicy namingPolicy = guiPackage.getNamingPolicy();
77
        guiPackage.updateCurrentNode();
78
        Enumeration<JMeterTreeNode> enumeration = currentNode.children();
79
        int index = 0;
80
        namingPolicy.resetState(currentNode);
81
        while(enumeration.hasMoreElements()) {
82
            JMeterTreeNode childNode = enumeration.nextElement();
83
            namingPolicy.rename(currentNode, childNode, index);
84
            index++;
85
        }        
86
    }
87
88
    @Override
89
    public Set<String> getActionNames() {
90
        return commands;
91
    }
92
}
(-)src/core/org/apache/jmeter/gui/action/TreeNodeNamingPolicy.java (+53 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
package org.apache.jmeter.gui.action;
19
20
import org.apache.jmeter.gui.tree.JMeterTreeNode;
21
22
/**
23
 * Naming policy applied to JMeter Tree nodes :
24
 * <ul>
25
 * <li>on creation through {@link TreeNodeNamingPolicy#nameOnCreation(JMeterTreeNode)}</li>
26
 * <li>By applying naming policy on Controller child nodes through {@link TreeNodeNamingPolicy#resetState(JMeterTreeNode)}
27
    and {@link TreeNodeNamingPolicy#rename(JMeterTreeNode, JMeterTreeNode, int)}</li>
28
 * </ul> 
29
 * @since 3.2
30
 */
31
public interface TreeNodeNamingPolicy {
32
33
    /**
34
     * Called by Apply Naming Policy popup menu on TransactionController nodes
35
     * Rename childNode based on custom policy 
36
     * @param parentNode Parent node
37
     * @param childNode Child node
38
     * @param index index of child node
39
     */
40
    void rename(JMeterTreeNode parentNode, JMeterTreeNode childNode, int index);
41
42
    /**
43
     * Called within Apply Naming Policy popup menu on TransactionController nodes to 
44
     * init the naming process. 
45
     * @param parentNode {@link JMeterTreeNode} Parent of nodes that will be renamed
46
     */
47
    void resetState(JMeterTreeNode parentNode);
48
    
49
    /**
50
     * @param node {@link JMeterTreeNode} node that has been added to JMeter Tree node
51
     */
52
    void nameOnCreation(JMeterTreeNode node);
53
}
(-)src/core/org/apache/jmeter/gui/action/impl/DefaultTreeNodeNamingPolicy.java (+76 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
19
package org.apache.jmeter.gui.action.impl;
20
21
import java.text.DecimalFormat;
22
23
import org.apache.jmeter.control.TransactionController;
24
import org.apache.jmeter.gui.action.TreeNodeNamingPolicy;
25
import org.apache.jmeter.gui.tree.JMeterTreeNode;
26
import org.apache.jmeter.samplers.Sampler;
27
import org.apache.jmeter.testelement.TestElement;
28
import org.apache.jmeter.util.JMeterUtils;
29
30
/**
31
 * Default implementation of {@link TreeNodeNamingPolicy}
32
 * @since 3.2
33
 */
34
public class DefaultTreeNodeNamingPolicy implements TreeNodeNamingPolicy {
35
    private static final String PREFIX = JMeterUtils.getPropDefault("naming_policy.prefix", ""); 
36
    private static final String SUFFIX = JMeterUtils.getPropDefault("naming_policy.suffix", ""); 
37
    private int numberOfChildren;
38
    private int index;
39
    private DecimalFormat formatter;
40
41
42
    /**
43
     * @see org.apache.jmeter.gui.action.TreeNodeNamingPolicy#rename(org.apache.jmeter.gui.tree.JMeterTreeNode, org.apache.jmeter.gui.tree.JMeterTreeNode, int)
44
     */
45
    @Override
46
    public void rename(JMeterTreeNode parentNode, JMeterTreeNode childNode, int iterationIndex) {
47
        if(childNode.getUserObject() instanceof TransactionController ||
48
                childNode.getUserObject() instanceof Sampler) {
49
            childNode.setName(parentNode.getName()+"-"+formatter.format(index));
50
            index++;
51
        }
52
    }
53
54
    /**
55
     * @see org.apache.jmeter.gui.action.TreeNodeNamingPolicy#resetState(org.apache.jmeter.gui.tree.JMeterTreeNode)
56
     */
57
    @Override
58
    public void resetState(JMeterTreeNode rootNode) {
59
        this.numberOfChildren = rootNode.getChildCount();
60
        this.index = 0;
61
        int numberOfDigits = String.valueOf(numberOfChildren).length();
62
        StringBuilder formatSB = new StringBuilder(numberOfDigits);
63
        for (int i=0; i<numberOfDigits;i++) {
64
            formatSB.append("0");
65
        }
66
        this.formatter = new DecimalFormat(formatSB.toString());
67
    }
68
69
    @Override
70
    public void nameOnCreation(JMeterTreeNode node) {
71
        if(node.getName().isEmpty()) {
72
            node.setName(((TestElement)node.getUserObject()).getClass().getSimpleName());
73
        }
74
        node.setName(PREFIX+node.getName()+SUFFIX);
75
    }
76
}
(-)src/core/org/apache/jmeter/gui/util/MenuFactory.java (+2 lines)
Lines 293-298 Link Here
293
        pop.add(makeMenus(MENU_PARENT_CONTROLLER,
293
        pop.add(makeMenus(MENU_PARENT_CONTROLLER,
294
                JMeterUtils.getResString("change_parent"),// $NON-NLS-1$
294
                JMeterUtils.getResString("change_parent"),// $NON-NLS-1$
295
                ActionNames.CHANGE_PARENT));
295
                ActionNames.CHANGE_PARENT));
296
        pop.add(MenuFactory.makeMenuItemRes("apply_naming",// $NON-NLS-1$
297
                ActionNames.APPLY_NAMING_CONVENTION));
296
        MenuFactory.addEditMenu(pop, true);
298
        MenuFactory.addEditMenu(pop, true);
297
        MenuFactory.addFileMenu(pop);
299
        MenuFactory.addFileMenu(pop);
298
        return pop;
300
        return pop;
(-)src/core/org/apache/jmeter/resources/messages.properties (+1 lines)
Lines 99-104 Link Here
99
analyze=Analyze Data File...
99
analyze=Analyze Data File...
100
anchor_modifier_title=HTML Link Parser
100
anchor_modifier_title=HTML Link Parser
101
appearance=Look and Feel
101
appearance=Look and Feel
102
apply_naming=Apply Naming Policy
102
argument_must_not_be_negative=The Argument must not be negative\!
103
argument_must_not_be_negative=The Argument must not be negative\!
103
arguments_panel_title=Command parameters
104
arguments_panel_title=Command parameters
104
assertion_assume_success=Ignore Status
105
assertion_assume_success=Ignore Status
(-)src/core/org/apache/jmeter/resources/messages_fr.properties (+1 lines)
Lines 92-97 Link Here
92
analyze=En train d'analyser le fichier de donn\u00E9es
92
analyze=En train d'analyser le fichier de donn\u00E9es
93
anchor_modifier_title=Analyseur de lien HTML
93
anchor_modifier_title=Analyseur de lien HTML
94
appearance=Apparence
94
appearance=Apparence
95
apply_naming=Appliquer Convention Nommage
95
argument_must_not_be_negative=L'argument ne peut pas \u00EAtre n\u00E9gatif \!
96
argument_must_not_be_negative=L'argument ne peut pas \u00EAtre n\u00E9gatif \!
96
arguments_panel_title=Param\u00E8tres de commande
97
arguments_panel_title=Param\u00E8tres de commande
97
assertion_assume_success=Ignorer le statut
98
assertion_assume_success=Ignorer le statut

Return to bug 60514