KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > engine > TreeCloner


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/engine/TreeCloner.java,v 1.8 2004/02/14 03:34:29 sebb Exp $
2
/*
3  * Copyright 2001-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.engine;
20
21 import java.util.LinkedList JavaDoc;
22
23 import org.apache.jmeter.config.Argument;
24 import org.apache.jmeter.config.Arguments;
25 import org.apache.jmeter.control.GenericController;
26 import org.apache.jmeter.engine.util.NoThreadClone;
27 import org.apache.jmeter.reporters.ResultCollector;
28 import org.apache.jmeter.testelement.TestElement;
29 import org.apache.jmeter.testelement.TestPlan;
30 import org.apache.jmeter.testelement.property.CollectionProperty;
31 import org.apache.jmeter.testelement.property.JMeterProperty;
32 import org.apache.jmeter.testelement.property.PropertyIterator;
33 import org.apache.jorphan.collections.HashTree;
34 import org.apache.jorphan.collections.HashTreeTraverser;
35 import org.apache.jorphan.collections.ListedHashTree;
36 import org.apache.jorphan.logging.LoggingManager;
37 import org.apache.log.Logger;
38
39 /**
40  * @version $Revision: 1.8 $
41  */

42 public class TreeCloner implements HashTreeTraverser
43 {
44     Logger log = LoggingManager.getLoggerForClass();
45     ListedHashTree newTree;
46     LinkedList JavaDoc objects = new LinkedList JavaDoc();
47     boolean forThread = true;
48
49     public TreeCloner()
50     {
51         this(true);
52     }
53
54     public TreeCloner(boolean forThread)
55     {
56         newTree = new ListedHashTree();
57         this.forThread = forThread;
58     }
59     public void addNode(Object JavaDoc node, HashTree subTree)
60     {
61         if ((!forThread || !(node instanceof NoThreadClone))
62             && (node instanceof TestElement))
63         {
64             node = ((TestElement) node).clone();
65             newTree.add(objects, node);
66         }
67         else
68         {
69             newTree.add(objects, node);
70         }
71         objects.addLast(node);
72     }
73
74     public void subtractNode()
75     {
76         objects.removeLast();
77     }
78
79     public ListedHashTree getClonedTree()
80     {
81         return newTree;
82     }
83
84     public void processPath()
85     {
86     }
87
88     public static class Test extends junit.framework.TestCase
89     {
90         public Test(String JavaDoc name)
91         {
92             super(name);
93         }
94
95         public void testCloning() throws Exception JavaDoc
96         {
97             ListedHashTree original = new ListedHashTree();
98             GenericController controller = new GenericController();
99             controller.setName("controller");
100             Arguments args = new Arguments();
101             args.setName("args");
102             TestPlan plan = new TestPlan();
103             plan.addParameter("server", "jakarta");
104             original.add(controller, args);
105             original.add(plan);
106             ResultCollector listener = new ResultCollector();
107             listener.setName("Collector");
108             original.add(controller, listener);
109             TreeCloner cloner = new TreeCloner();
110             original.traverse(cloner);
111             ListedHashTree newTree = cloner.getClonedTree();
112             assertTrue(original != newTree);
113             assertEquals(original.size(), newTree.size());
114             assertEquals(
115                 original.getTree(original.getArray()[0]).size(),
116                 newTree.getTree(newTree.getArray()[0]).size());
117             assertTrue(original.getArray()[0] != newTree.getArray()[0]);
118             assertEquals(
119                 ((GenericController) original.getArray()[0]).getName(),
120                 ((GenericController) newTree.getArray()[0]).getName());
121             assertSame(
122                 original.getTree(original.getArray()[0]).getArray()[1],
123                 newTree.getTree(newTree.getArray()[0]).getArray()[1]);
124             TestPlan clonedTestPlan = (TestPlan) newTree.getArray()[1];
125             clonedTestPlan.setRunningVersion(true);
126             clonedTestPlan.recoverRunningVersion();
127             assertTrue(
128                 !plan
129                     .getProperty(TestPlan.USER_DEFINED_VARIABLES)
130                     .isRunningVersion());
131             assertTrue(
132                 clonedTestPlan
133                     .getProperty(TestPlan.USER_DEFINED_VARIABLES)
134                     .isRunningVersion());
135             Arguments vars =
136                 (Arguments) plan
137                     .getProperty(TestPlan.USER_DEFINED_VARIABLES)
138                     .getObjectValue();
139             PropertyIterator iter =
140                 ((CollectionProperty) vars.getProperty(Arguments.ARGUMENTS))
141                     .iterator();
142             while (iter.hasNext())
143             {
144                 JMeterProperty argProp = iter.next();
145                 assertTrue(!argProp.isRunningVersion());
146                 assertTrue(argProp.getObjectValue() instanceof Argument);
147                 Argument arg = (Argument)argProp.getObjectValue();
148                 arg.setValue("yahoo");
149                 assertEquals("yahoo",arg.getValue());
150             }
151             vars =
152                 (Arguments) clonedTestPlan
153                     .getProperty(TestPlan.USER_DEFINED_VARIABLES)
154                     .getObjectValue();
155             iter = vars.propertyIterator();
156             while (iter.hasNext())
157             {
158                 assertTrue(iter.next().isRunningVersion());
159             }
160         }
161
162     }
163 }
Popular Tags