KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > threads > TestCompiler


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/threads/TestCompiler.java,v 1.38.2.1 2004/12/17 23:21:02 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.threads;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import org.apache.jmeter.assertions.Assertion;
32 import org.apache.jmeter.config.ConfigTestElement;
33 import org.apache.jmeter.control.Controller;
34 import org.apache.jmeter.control.GenericController;
35 import org.apache.jmeter.engine.event.LoopIterationListener;
36 import org.apache.jmeter.processor.PostProcessor;
37 import org.apache.jmeter.processor.PreProcessor;
38 import org.apache.jmeter.samplers.AbstractSampler;
39 import org.apache.jmeter.samplers.SampleEvent;
40 import org.apache.jmeter.samplers.SampleListener;
41 import org.apache.jmeter.samplers.SampleResult;
42 import org.apache.jmeter.samplers.Sampler;
43 import org.apache.jmeter.testbeans.TestBean;
44 import org.apache.jmeter.testelement.AbstractTestElement;
45 import org.apache.jmeter.testelement.TestElement;
46 import org.apache.jmeter.timers.Timer;
47 import org.apache.jorphan.collections.HashTree;
48 import org.apache.jorphan.collections.HashTreeTraverser;
49 import org.apache.jorphan.collections.ListedHashTree;
50 import org.apache.jorphan.logging.LoggingManager;
51 import org.apache.log.Logger;
52
53 /**
54  * @author unascribed
55  * @version $Revision: 1.38.2.1 $
56  */

57 public class TestCompiler implements HashTreeTraverser, SampleListener
58 {
59     transient private static Logger log = LoggingManager.getLoggerForClass();
60     
61     //TODO - should these variables be made private?
62
LinkedList JavaDoc stack = new LinkedList JavaDoc();
63     Map JavaDoc samplerConfigMap = new HashMap JavaDoc();
64     //Set objectsWithFunctions = new HashSet();
65
HashTree testTree;
66     SampleResult previousResult;//TODO is this needed?
67
Sampler currentSampler;//TODO is this needed?
68
JMeterVariables threadVars;//TODO is this needed?
69

70     /*
71      * This set keeps track of which ObjectPairs have been seen
72      * Its purpose is not entirely clear (please document if you know!)
73      * but it is needed,..
74      */

75     private static Set JavaDoc pairing = new HashSet JavaDoc();
76
77     List JavaDoc loopIterListeners = new ArrayList JavaDoc();
78
79     public TestCompiler(HashTree testTree, JMeterVariables vars)
80     {
81         threadVars = vars;
82         this.testTree = testTree;
83     }
84     
85     /**
86      * Clears the pairing Set
87      * Called by StandardJmeterEngine at the start of a test run.
88      */

89     public static void initialize()
90     {
91         // synch is probably not needed as only called before run starts
92
synchronized(pairing){
93             pairing.clear();
94         }
95     }
96
97     public void sampleOccurred(SampleEvent e)
98     {
99         previousResult = e.getResult();
100     }
101
102     public void sampleStarted(SampleEvent e)
103     {
104     }
105
106     public void sampleStopped(SampleEvent e)
107     {
108     }
109
110     public SamplePackage configureSampler(Sampler sampler)
111     {
112         currentSampler = sampler;
113         SamplePackage pack = (SamplePackage) samplerConfigMap.get(sampler);
114         pack.setSampler(sampler);
115         configureWithConfigElements(sampler, pack.getConfigs());
116         runPreProcessors(pack.getPreProcessors());
117         //replaceStatics(ret);
118
return pack;
119     }
120
121     private void runPreProcessors(List JavaDoc preProcessors)
122     {
123         Iterator JavaDoc iter = preProcessors.iterator();
124         while (iter.hasNext())
125         {
126             PreProcessor ex = (PreProcessor) iter.next();
127             if (log.isDebugEnabled())
128             {
129                 log.debug(
130                     "Running preprocessor: "
131                         + ((AbstractTestElement) ex).getName());
132             }
133              if (ex instanceof TestBean) ((TestBean)ex).prepare();
134             ex.process();
135         }
136     }
137
138     public void done(SamplePackage pack)
139     {
140         pack.recoverRunningVersion();
141     }
142
143     public void addNode(Object JavaDoc node, HashTree subTree)
144     {
145         stack.addLast(node);
146     }
147
148     public void subtractNode()
149     {
150         log.debug("Subtracting node, stack size = " + stack.size());
151         TestElement child = (TestElement) stack.getLast();
152         trackIterationListeners(stack);
153         if (child instanceof Sampler)
154         {
155             saveSamplerConfigs((Sampler) child);
156         }
157         stack.removeLast();
158         if (stack.size() > 0)
159         {
160             ObjectPair pair =
161                 new ObjectPair(
162                     (TestElement) child,
163                     (TestElement) stack.getLast());
164             synchronized (pairing){//Called from multiple threads
165
if (!pairing.contains(pair))
166                 {
167                     pair.addTestElements();
168                     pairing.add(pair);
169                 }
170             }
171         }
172     }
173
174     private void trackIterationListeners(LinkedList JavaDoc stack)
175     {
176         TestElement child = (TestElement) stack.getLast();
177         if (child instanceof LoopIterationListener)
178         {
179             ListIterator JavaDoc iter = stack.listIterator(stack.size());
180             while (iter.hasPrevious())
181             {
182                 TestElement item = (TestElement) iter.previous();
183                 if (item == child)
184                 {
185                     continue;
186                 }
187                 else
188                 {
189                     if (item instanceof Controller)
190                     {
191                         ((Controller) item).addIterationListener(
192                             (LoopIterationListener) child);
193                         break;
194                     }
195                 }
196             }
197         }
198     }
199
200     public void processPath()
201     {
202     }
203
204     private void saveSamplerConfigs(Sampler sam)
205     {
206         List JavaDoc configs = new LinkedList JavaDoc();
207         List JavaDoc modifiers = new LinkedList JavaDoc();
208         List JavaDoc responseModifiers = new LinkedList JavaDoc();
209         List JavaDoc listeners = new LinkedList JavaDoc();
210         List JavaDoc timers = new LinkedList JavaDoc();
211         List JavaDoc assertions = new LinkedList JavaDoc();
212         LinkedList JavaDoc posts = new LinkedList JavaDoc();
213         LinkedList JavaDoc pres = new LinkedList JavaDoc();
214         for (int i = stack.size(); i > 0; i--)
215         {
216             Iterator JavaDoc iter = testTree.list(stack.subList(0, i)).iterator();
217             List JavaDoc tempPre = new LinkedList JavaDoc();
218             List JavaDoc tempPost = new LinkedList JavaDoc();
219             while (iter.hasNext())
220             {
221                 TestElement item = (TestElement) iter.next();
222                 if ((item instanceof ConfigTestElement))
223                 {
224                     configs.add(item);
225                 }
226                 if (item instanceof SampleListener)
227                 {
228                     listeners.add(item);
229                 }
230                 if (item instanceof Timer)
231                 {
232                     timers.add(item);
233                 }
234                 if (item instanceof Assertion)
235                 {
236                     assertions.add(item);
237                 }
238                 if (item instanceof PostProcessor)
239                 {
240                     tempPost.add(item);
241                 }
242                 if (item instanceof PreProcessor)
243                 {
244                     tempPre.add(item);
245                 }
246             }
247             pres.addAll(0,tempPre);
248             posts.addAll(0,tempPost);
249         }
250
251         SamplePackage pack =
252             new SamplePackage(
253                 configs,
254                 modifiers,
255                 responseModifiers,
256                 listeners,
257                 timers,
258                 assertions,
259                 posts,
260                 pres);
261         pack.setSampler(sam);
262         pack.setRunningVersion(true);
263         samplerConfigMap.put(sam, pack);
264     }
265
266     /**
267      * @version $Revision: 1.38.2.1 $
268      */

269     public static class Test extends junit.framework.TestCase
270     {
271         public Test(String JavaDoc name)
272         {
273             super(name);
274         }
275
276         public void testConfigGathering() throws Exception JavaDoc
277         {
278             ListedHashTree testing = new ListedHashTree();
279             GenericController controller = new GenericController();
280             ConfigTestElement config1 = new ConfigTestElement();
281             config1.setName("config1");
282             config1.setProperty("test.property", "A test value");
283             TestSampler sampler = new TestSampler();
284             sampler.setName("sampler");
285             testing.add(controller, config1);
286             testing.add(controller, sampler);
287             TestCompiler.initialize();
288
289             TestCompiler compiler =
290                 new TestCompiler(testing, new JMeterVariables());
291             testing.traverse(compiler);
292             sampler =
293                 (TestSampler) compiler.configureSampler(sampler).getSampler();
294             assertEquals(
295                 "A test value",
296                 sampler.getPropertyAsString("test.property"));
297         }
298
299         class TestSampler extends AbstractSampler
300         {
301             public SampleResult sample(org.apache.jmeter.samplers.Entry e)
302             {
303                 return null;
304             }
305             public Object JavaDoc clone()
306             {
307                 return new TestSampler();
308             }
309         }
310     }
311
312     private class ObjectPair
313     {
314         TestElement child, parent;
315
316         public ObjectPair(TestElement one, TestElement two)
317         {
318             this.child = one;
319             this.parent = two;
320         }
321
322         public void addTestElements()
323         {
324             if (parent instanceof Controller
325                 && (child instanceof Sampler || child instanceof Controller))
326             {
327                 parent.addTestElement(child);
328             }
329         }
330
331         public int hashCode()
332         {
333             return child.hashCode() + parent.hashCode();
334         }
335
336         public boolean equals(Object JavaDoc o)
337         {
338             if (o instanceof ObjectPair)
339             {
340                 return child == ((ObjectPair) o).child
341                     && parent == ((ObjectPair) o).parent;
342             }
343             return false;
344         }
345     }
346
347     private void configureWithConfigElements(Sampler sam, List JavaDoc configs)
348     {
349         Iterator JavaDoc iter = configs.iterator();
350         while (iter.hasNext())
351         {
352             ConfigTestElement config = (ConfigTestElement)iter.next();
353             sam.addTestElement(config);
354         }
355     }
356 }
357
Popular Tags