KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > ModuleController


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/ModuleController.java,v 1.7 2004/02/20 01:03:07 jsalvata Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.Enumeration;
22 import java.util.Vector;
23
24 import javax.swing.tree.TreeNode;
25
26 import org.apache.jmeter.gui.GuiPackage;
27 import org.apache.jmeter.gui.tree.JMeterTreeNode;
28 import org.apache.jmeter.testelement.TestElement;
29 import org.apache.jmeter.testelement.property.CollectionProperty;
30 import org.apache.jmeter.testelement.property.JMeterProperty;
31 import org.apache.jmeter.testelement.property.NullProperty;
32 import org.apache.jorphan.collections.HashTree;
33
34 /**
35  * The goal of ModuleController is to add modularity to JMeter. The general idea
36  * is that web applications consist of small units of functionality (i.e.
37  * Logon, Create Account, Logoff...) which consist of requests that implement
38  * the functionality. These small units of functionality can be stored in
39  * SimpleControllers as modules that can be linked together quickly to form
40  * tests. ModuleController facilitates this by acting as a pointer to any
41  * controller that sits under the WorkBench. The controller and it's subelements
42  * will be substituted in place of the ModuleController at runtime. Config
43  * elements can be attached to the ModuleController to alter the functionality
44  * (which user logs in, which account is created, etc.) of the module.
45  *
46  * @author Thad Smith
47  * @version $Revision: 1.7 $
48  */

49 public class ModuleController
50     extends GenericController
51     implements ReplaceableController
52 {
53
54     private static final String NODE_PATH = "ModuleController.node_path";
55     private JMeterTreeNode selectedNode = null;
56
57     /**
58      * No-arg constructor
59      *
60      * @see java.lang.Object#Object()
61      */

62     public ModuleController()
63     {
64         super();
65     }
66
67     public Object clone()
68     {
69         ModuleController clone = (ModuleController) super.clone();
70         if (selectedNode == null)
71         {
72             this.restoreSelected();
73         }
74         clone.selectedNode = selectedNode;
75         return clone;
76     }
77
78     /**
79      * Get the controller which this object is "pointing" to.
80      *
81      * @return the controller which this node points to
82      * @see org.apache.jmeter.testelement.TestElement
83      * @see org.apache.jmeter.control.ReplaceableController#getReplacement()
84      */

85     public TestElement getReplacement()
86     {
87         if (selectedNode != null)
88         {
89             return selectedNode.getTestElement();
90         }
91         else
92         {
93             return this;
94         }
95     }
96
97     /**
98      * Sets the (@link JMeterTreeNode) which represents the controller which
99      * this object is pointing to. Used for building the test case upon
100      * execution.
101      *
102      * @param tn JMeterTreeNode
103      * @see org.apache.jmeter.gui.tree.JMeterTreeNode
104      */

105     public void setSelectedNode(JMeterTreeNode tn)
106     {
107         selectedNode = tn;
108         setNodePath();
109     }
110
111     /**
112      * Gets the (@link JMeterTreeNode) for the Controller
113      *
114      * @return JMeterTreeNode
115      */

116     public JMeterTreeNode getSelectedNode()
117     {
118         return selectedNode;
119     }
120
121     private void setNodePath()
122     {
123         Vector nodePath = new Vector();
124         if (selectedNode != null)
125         {
126             TreeNode[] path = selectedNode.getPath();
127             for (int i = 0; i < path.length; i++)
128             {
129                 nodePath.add(((JMeterTreeNode) path[i]).getName());
130             }
131             nodePath.add(selectedNode.getName());
132         }
133         setProperty(new CollectionProperty(NODE_PATH, nodePath));
134     }
135
136     private Vector getNodePath()
137     {
138         JMeterProperty prop = getProperty(NODE_PATH);
139         if (!(prop instanceof NullProperty))
140         {
141             return (Vector) ((CollectionProperty) prop).getObjectValue();
142         }
143         else
144         {
145             return null;
146         }
147     }
148
149     private void restoreSelected()
150     {
151         if (selectedNode == null)
152         {
153             Vector nodePath = getNodePath();
154             if (nodePath != null && nodePath.size() > 0)
155             {
156                 GuiPackage gp = GuiPackage.getInstance();
157                 if (gp != null)
158                 {
159                     JMeterTreeNode root =
160                         (JMeterTreeNode) gp.getTreeModel().getRoot();
161                     nodePath.remove(0);
162                     traverse(root, nodePath);
163                 }
164             }
165         }
166     }
167
168     private void traverse(JMeterTreeNode node, Vector nodePath)
169     {
170         if (node != null && nodePath.size() > 0)
171         {
172             for (int i = 0; i < node.getChildCount(); i++)
173             {
174                 JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i);
175                 if (cur.getName().equals(nodePath.elementAt(0).toString()))
176                 {
177                     selectedNode = cur;
178                     nodePath.remove(0);
179                     traverse(cur, nodePath);
180                 }
181             }
182         }
183     }
184
185     /**
186      * Copies the controller's subelements into the execution tree
187      *
188      * @param tree - The current tree under which the nodes will be added
189      */

190     public void replace(HashTree tree)
191     {
192         if (!selectedNode.isEnabled())
193         {
194             selectedNode = cloneTreeNode(selectedNode);
195             selectedNode.setEnabled(true);
196         }
197         createSubTree(tree, selectedNode);
198     }
199
200     private void createSubTree(HashTree tree, JMeterTreeNode node)
201     {
202         Enumeration e = node.children();
203         while (e.hasMoreElements())
204         {
205             JMeterTreeNode subNode = (JMeterTreeNode) e.nextElement();
206             tree.add(subNode);
207             createSubTree(tree.getTree(subNode), subNode);
208         }
209     }
210
211     private static JMeterTreeNode cloneTreeNode(JMeterTreeNode node)
212     {
213         JMeterTreeNode treeNode = (JMeterTreeNode) node.clone();
214         treeNode.setUserObject(((TestElement) node.getUserObject()).clone());
215         cloneChildren(treeNode, node);
216         return treeNode;
217     }
218
219     private static void cloneChildren(JMeterTreeNode to, JMeterTreeNode from)
220     {
221         Enumeration enum = from.children();
222         while (enum.hasMoreElements())
223         {
224             JMeterTreeNode child = (JMeterTreeNode) enum.nextElement();
225             JMeterTreeNode childClone = (JMeterTreeNode) child.clone();
226             childClone.setUserObject(
227                 ((TestElement) child.getUserObject()).clone());
228             to.add(childClone);
229             cloneChildren((JMeterTreeNode) to.getLastChild(), child);
230         }
231     }
232
233 }
234
Popular Tags