KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/GenericController.java,v 1.23.2.1 2004/10/13 00:32:22 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.control;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.jmeter.engine.event.LoopIterationEvent;
28 import org.apache.jmeter.engine.event.LoopIterationListener;
29 import org.apache.jmeter.junit.JMeterTestCase;
30 import org.apache.jmeter.junit.stubs.TestSampler;
31 import org.apache.jmeter.samplers.Sampler;
32 import org.apache.jmeter.testelement.AbstractTestElement;
33 import org.apache.jmeter.testelement.TestElement;
34 import org.apache.jorphan.logging.LoggingManager;
35 import org.apache.log.Logger;
36
37 /**
38  * @author Michael Stover
39  * @author Thad Smith
40  * @version $Revision: 1.23.2.1 $
41  */

42 public class GenericController
43     extends AbstractTestElement
44     implements Controller, Serializable JavaDoc
45 {
46     private static final Logger log = LoggingManager.getLoggerForClass();
47
48     protected LinkedList JavaDoc iterationListeners = new LinkedList JavaDoc();
49     protected List JavaDoc subControllersAndSamplers = new ArrayList JavaDoc();
50
51     protected int current;
52     private int iterCount;
53     private boolean done, first;
54
55     /**
56      * Creates a Generic Controller
57      */

58     public GenericController(){}
59
60     public void initialize()
61     {
62         resetCurrent();
63         resetIterCount();
64         done = false;
65         first = true;
66         TestElement elem;
67         for (int i = 0; i < subControllersAndSamplers.size(); i++)
68         {
69             elem = (TestElement) subControllersAndSamplers.get(i);
70             if (elem instanceof Controller)
71             {
72                 ((Controller) elem).initialize();
73             }
74         }
75     }
76
77     protected void reInitialize()
78     {
79         resetCurrent();
80         incrementIterCount();
81         setFirst(true);
82     }
83
84     /**
85      * @see org.apache.jmeter.control.Controller#next()
86      */

87     public Sampler next()
88     {
89         fireIterEvents();
90         log.debug("Calling next on: " + this.getClass().getName());
91         if (isDone()) return null;
92         Sampler returnValue = null;
93         TestElement currentElement = null;
94         try
95         {
96             currentElement = getCurrentElement();
97             setCurrentElement(currentElement);
98             if (currentElement == null)
99             {
100                 //incrementCurrent();
101
returnValue = nextIsNull();
102             }
103             else
104             {
105                 if (currentElement instanceof Sampler)
106                 {
107                     returnValue = nextIsASampler((Sampler) currentElement);
108                 }
109                 else
110                 {
111                     returnValue =
112                         nextIsAController((Controller) currentElement);
113                 }
114             }
115         }
116         catch (NextIsNullException e)
117         {
118             returnValue = null;
119         }
120         return returnValue;
121     }
122
123     /**
124      * @see org.apache.jmeter.control.Controller#isDone()
125      */

126     public boolean isDone()
127     {
128         return done;
129     }
130
131     protected void setDone(boolean done)
132     {
133         this.done = done;
134     }
135
136     protected boolean isFirst()
137     {
138         return first;
139     }
140
141     public void setFirst(boolean b)
142     {
143         first = b;
144     }
145
146     protected Sampler nextIsAController(Controller controller)
147         throws NextIsNullException
148     {
149         Sampler returnValue;
150         Sampler sampler = controller.next();
151         if (sampler == null)
152         {
153             currentReturnedNull(controller);
154             returnValue = next();
155         }
156         else
157         {
158             returnValue = sampler;
159         }
160         return returnValue;
161     }
162
163     protected Sampler nextIsASampler(Sampler element) throws NextIsNullException
164     {
165         incrementCurrent();
166         return element;
167     }
168
169     protected Sampler nextIsNull() throws NextIsNullException
170     {
171         reInitialize();
172         return null;
173     }
174
175     protected void currentReturnedNull(Controller c)
176     {
177         if (c.isDone())
178         {
179             removeCurrentElement();
180         }
181         else
182         {
183             incrementCurrent();
184         }
185     }
186
187     /**
188      * Gets the SubControllers attribute of the
189      * GenericController object
190      *
191      * @return the SubControllers value
192      */

193     protected List JavaDoc getSubControllers()
194     {
195         return subControllersAndSamplers;
196     }
197
198     private void addElement(TestElement child)
199     {
200         subControllersAndSamplers.add(child);
201     }
202
203     protected void setCurrentElement(TestElement currentElement)
204         throws NextIsNullException
205     {
206     }
207
208     protected TestElement getCurrentElement() throws NextIsNullException
209     {
210         if (current < subControllersAndSamplers.size())
211         {
212             return (TestElement) subControllersAndSamplers.get(current);
213         }
214         else
215         {
216             if (subControllersAndSamplers.size() == 0)
217             {
218                 setDone(true);
219                 throw new NextIsNullException();
220             }
221             return null;
222         }
223     }
224
225     protected void removeCurrentElement()
226     {
227         subControllersAndSamplers.remove(current);
228     }
229
230     protected void incrementCurrent()
231     {
232         current++;
233     }
234
235     protected void resetCurrent()
236     {
237         current = 0;
238     }
239
240     public void addTestElement(TestElement child)
241     {
242         if (child instanceof Controller || child instanceof Sampler)
243         {
244             addElement(child);
245         }
246     }
247
248     public void addIterationListener(LoopIterationListener lis)
249     {
250         /* A little hack - add each listener to the start of the list - this
251          * ensures that the thread running the show is the first listener and
252          * can modify certain values before other listeners are called.
253          */

254         iterationListeners.addFirst(lis);
255     }
256
257     protected void fireIterEvents()
258     {
259         if (isFirst())
260         {
261             fireIterationStart();
262             first = false;
263         }
264     }
265
266     protected void fireIterationStart()
267     {
268         Iterator JavaDoc iter = iterationListeners.iterator();
269         LoopIterationEvent event = new LoopIterationEvent(this, getIterCount());
270         while (iter.hasNext())
271         {
272             LoopIterationListener item = (LoopIterationListener) iter.next();
273             item.iterationStart(event);
274         }
275     }
276
277     protected int getIterCount()
278     {
279         return iterCount;
280     }
281
282     protected void incrementIterCount()
283     {
284         iterCount++;
285     }
286
287     protected void resetIterCount()
288     {
289         iterCount = 0;
290     }
291
292     public static class Test extends JMeterTestCase
293     {
294         public Test(String JavaDoc name)
295         {
296             super(name);
297         }
298
299         public void testProcessing() throws Exception JavaDoc
300         {
301             testLog.debug("Testing Generic Controller");
302             GenericController controller = new GenericController();
303             GenericController sub_1 = new GenericController();
304             sub_1.addTestElement(new TestSampler("one"));
305             sub_1.addTestElement(new TestSampler("two"));
306             controller.addTestElement(sub_1);
307             controller.addTestElement(new TestSampler("three"));
308             GenericController sub_2 = new GenericController();
309             GenericController sub_3 = new GenericController();
310             sub_2.addTestElement(new TestSampler("four"));
311             sub_3.addTestElement(new TestSampler("five"));
312             sub_3.addTestElement(new TestSampler("six"));
313             sub_2.addTestElement(sub_3);
314             sub_2.addTestElement(new TestSampler("seven"));
315             controller.addTestElement(sub_2);
316             String JavaDoc[] order =
317                 new String JavaDoc[] {
318                     "one",
319                     "two",
320                     "three",
321                     "four",
322                     "five",
323                     "six",
324                     "seven" };
325             int counter = 7;
326             controller.initialize();
327             for (int i = 0; i < 2; i++)
328             {
329                 assertEquals(7, counter);
330                 counter = 0;
331                 TestElement sampler = null;
332                 while ((sampler = controller.next()) != null)
333                 {
334                     assertEquals(
335                         order[counter++],
336                         sampler.getPropertyAsString(TestElement.NAME));
337                 }
338             }
339         }
340     }
341 }
342
Popular Tags