Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]
    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.control;
   20   
   21   import java.util.ArrayList;
   22   import java.util.Enumeration;
   23   import java.util.List;
   24   
   25   import javax.swing.tree.TreeNode;
   26   
   27   import org.apache.jmeter.gui.GuiPackage;
   28   import org.apache.jmeter.gui.tree.JMeterTreeNode;
   29   import org.apache.jmeter.testelement.TestElement;
   30   import org.apache.jmeter.testelement.property.CollectionProperty;
   31   import org.apache.jmeter.testelement.property.JMeterProperty;
   32   import org.apache.jmeter.testelement.property.NullProperty;
   33   import org.apache.jorphan.collections.HashTree;
   34   import org.apache.jorphan.collections.ListedHashTree;
   35   
   36   /**
   37    * The goal of ModuleController is to add modularity to JMeter. The general idea
   38    * is that web applications consist of small units of functionality (i.e. Logon,
   39    * Create Account, Logoff...) which consist of requests that implement the
   40    * functionality. These small units of functionality can be stored in
   41    * SimpleControllers as modules that can be linked together quickly to form
   42    * tests. ModuleController facilitates this by acting as a pointer to any
   43    * controller that sits under the WorkBench. The controller and it's subelements
   44    * will be substituted in place of the ModuleController at runtime. Config
   45    * elements can be attached to the ModuleController to alter the functionality
   46    * (which user logs in, which account is created, etc.) of the module.
   47    *
   48    */
   49   public class ModuleController extends GenericController implements ReplaceableController {
   50   
   51       private static final String NODE_PATH = "ModuleController.node_path";// $NON-NLS-1$
   52   
   53       private JMeterTreeNode selectedNode = null;
   54   
   55       /**
   56        * No-arg constructor
   57        *
   58        * @see java.lang.Object#Object()
   59        */
   60       public ModuleController() {
   61           super();
   62       }
   63   
   64       public Object clone() {
   65           ModuleController clone = (ModuleController) super.clone();
   66           if (selectedNode == null) {
   67               this.restoreSelected();
   68           }
   69           clone.selectedNode = selectedNode; // TODO ?? (JMeterTreeNode) selectedNode.clone();
   70           return clone;
   71       }
   72   
   73       /**
   74        * Sets the (@link JMeterTreeNode) which represents the controller which
   75        * this object is pointing to. Used for building the test case upon
   76        * execution.
   77        *
   78        * @param tn
   79        *            JMeterTreeNode
   80        * @see org.apache.jmeter.gui.tree.JMeterTreeNode
   81        */
   82       public void setSelectedNode(JMeterTreeNode tn) {
   83           selectedNode = tn;
   84           setNodePath();
   85       }
   86   
   87       /**
   88        * Gets the (@link JMeterTreeNode) for the Controller
   89        *
   90        * @return JMeterTreeNode
   91        */
   92       public JMeterTreeNode getSelectedNode() {
   93           if (selectedNode == null){
   94               restoreSelected();
   95           }
   96           return selectedNode;
   97       }
   98   
   99       private void setNodePath() {
  100           List nodePath = new ArrayList();
  101           if (selectedNode != null) {
  102               TreeNode[] path = selectedNode.getPath();
  103               for (int i = 0; i < path.length; i++) {
  104                   nodePath.add(((JMeterTreeNode) path[i]).getName());
  105               }
  106               // nodePath.add(selectedNode.getName());
  107           }
  108           setProperty(new CollectionProperty(NODE_PATH, nodePath));
  109       }
  110   
  111       public List getNodePath() {
  112           JMeterProperty prop = getProperty(NODE_PATH);
  113           if (!(prop instanceof NullProperty)) {
  114               return (List) ((CollectionProperty) prop).getObjectValue();
  115           }
  116           return null;
  117       }
  118   
  119       private void restoreSelected() {
  120           GuiPackage gp = GuiPackage.getInstance();
  121           if (gp != null) {
  122               JMeterTreeNode root = (JMeterTreeNode) gp.getTreeModel().getRoot();
  123               resolveReplacementSubTree(root);
  124           }
  125       }
  126   
  127       /**
  128        * Compute the replacement tree.
  129        * @param context
  130        */
  131       public void resolveReplacementSubTree(JMeterTreeNode context) {
  132           if (selectedNode == null) {
  133               List nodePathList = getNodePath();
  134               if (nodePathList != null && nodePathList.size() > 0) {
  135                   traverse(context, nodePathList, 1);
  136               }
  137           }
  138       }
  139   
  140       private void traverse(JMeterTreeNode node, List nodePath, int level) {
  141           if (node != null && nodePath.size() > level) {
  142               for (int i = 0; i < node.getChildCount(); i++) {
  143                   JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i);
  144                   if (cur.getName().equals(nodePath.get(level).toString())) {
  145                       if (nodePath.size() == (level + 1)) {
  146                           selectedNode = cur;
  147                       }
  148                       traverse(cur, nodePath, level + 1);
  149                   }
  150               }
  151           }
  152       }
  153   
  154       /**
  155        * Copies the controller's subelements into the execution tree
  156        *
  157        */
  158       public HashTree getReplacementSubTree() {
  159           HashTree tree = new ListedHashTree();
  160           if (selectedNode != null) {
  161               if (!selectedNode.isEnabled()) {
  162                   selectedNode = cloneTreeNode(selectedNode);
  163                   selectedNode.setEnabled(true);
  164               }
  165               tree.add(selectedNode);
  166               createSubTree(tree, selectedNode);
  167           }
  168           return tree;
  169       }
  170   
  171       private void createSubTree(HashTree tree, JMeterTreeNode node) {
  172           Enumeration e = node.children();
  173           while (e.hasMoreElements()) {
  174               JMeterTreeNode subNode = (JMeterTreeNode) e.nextElement();
  175               tree.add(subNode);
  176               createSubTree(tree.getTree(subNode), subNode);
  177           }
  178       }
  179   
  180       private static JMeterTreeNode cloneTreeNode(JMeterTreeNode node) {
  181           JMeterTreeNode treeNode = (JMeterTreeNode) node.clone();
  182           treeNode.setUserObject(((TestElement) node.getUserObject()).clone());
  183           cloneChildren(treeNode, node);
  184           return treeNode;
  185       }
  186   
  187       private static void cloneChildren(JMeterTreeNode to, JMeterTreeNode from) {
  188           Enumeration enumr = from.children();
  189           while (enumr.hasMoreElements()) {
  190               JMeterTreeNode child = (JMeterTreeNode) enumr.nextElement();
  191               JMeterTreeNode childClone = (JMeterTreeNode) child.clone();
  192               childClone.setUserObject(((TestElement) child.getUserObject()).clone());
  193               to.add(childClone);
  194               cloneChildren((JMeterTreeNode) to.getLastChild(), child);
  195           }
  196       }
  197   
  198   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.control » [javadoc | source]