KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/InterleaveControl.java,v 1.24 2004/02/13 01:31:55 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
23 import org.apache.jmeter.junit.JMeterTestCase;
24 import org.apache.jmeter.junit.stubs.TestSampler;
25 import org.apache.jmeter.samplers.Sampler;
26 import org.apache.jmeter.testelement.TestElement;
27 import org.apache.jmeter.testelement.property.IntegerProperty;
28
29 /**
30  * @author Michael Stover
31  * Created March 13, 2001
32  * @version $Revision: 1.24 $ Last updated: $Date: 2004/02/13 01:31:55 $
33  */

34 public class InterleaveControl extends GenericController implements Serializable JavaDoc
35 {
36     private static final String JavaDoc STYLE = "InterleaveControl.style";
37     public static final int IGNORE_SUB_CONTROLLERS = 0;
38     public static final int USE_SUB_CONTROLLERS = 1;
39     private boolean skipNext;
40     private TestElement searchStart = null;
41     private boolean currentReturnedAtLeastOne;
42     private boolean stillSame = true;
43
44     /****************************************
45      * Constructor for the InterleaveControl object
46      ***************************************/

47     public InterleaveControl()
48     {}
49
50     /* (non-Javadoc)
51      * @see org.apache.jmeter.control.GenericController#reInitialize()
52      */

53     public void reInitialize()
54     {
55         setFirst(true);
56         currentReturnedAtLeastOne = false;
57         searchStart = null;
58         stillSame = true;
59         skipNext = false;
60         incrementIterCount();
61     }
62
63     public void setStyle(int style)
64     {
65         setProperty(new IntegerProperty(STYLE, style));
66     }
67
68     public int getStyle()
69     {
70         return getPropertyAsInt(STYLE);
71     }
72     
73     /* (non-Javadoc)
74      * @see org.apache.jmeter.control.Controller#next()
75      */

76     public Sampler next()
77     {
78         if(isSkipNext())
79         {
80             reInitialize();
81             return null;
82         }
83         return super.next();
84     }
85
86     /* (non-Javadoc)
87      * @see GenericController#nextIsAController(Controller)
88      */

89     protected Sampler nextIsAController(Controller controller)
90         throws NextIsNullException
91     {
92         Sampler sampler = controller.next();
93         if (sampler == null)
94         {
95             currentReturnedNull(controller);
96             return next();
97         }
98         else
99         {
100             currentReturnedAtLeastOne = true;
101             if (getStyle() == IGNORE_SUB_CONTROLLERS)
102             {
103                 incrementCurrent();
104                 skipNext = true;
105             }
106             else
107             {
108                 searchStart = null;
109             }
110             return sampler;
111         }
112     }
113
114     /* (non-Javadoc)
115      * @see org.apache.jmeter.control.GenericController#nextIsASampler(Sampler)
116      */

117     protected Sampler nextIsASampler(Sampler element) throws NextIsNullException
118     {
119         skipNext = true;
120         incrementCurrent();
121         return element;
122     }
123
124     /**
125      * If the current is null, reset and continue searching. The
126      * searchStart attribute will break us off when we start a repeat.
127      *
128      * @see org.apache.jmeter.testelement.AbstractTestElement#nextIsNull()
129      */

130     protected Sampler nextIsNull()
131     {
132         resetCurrent();
133         return next();
134     }
135
136     /* (non-Javadoc)
137      * @see GenericController#setCurrentElement(TestElement)
138      */

139     protected void setCurrentElement(TestElement currentElement)
140         throws NextIsNullException
141     {
142         // Set the position when next is first called, and don't overwrite
143
// until reInitialize is called.
144
if (searchStart == null)
145         {
146             searchStart = currentElement;
147         }
148         else if (searchStart == currentElement && !stillSame)
149         {
150             // We've gone through the whole list and are now back at the start
151
// point of our search.
152
reInitialize();
153             throw new NextIsNullException();
154         }
155     }
156     
157     /* (non-Javadoc)
158      * @see GenericController#currentReturnedNull(Controller)
159      */

160     protected void currentReturnedNull(Controller c)
161     {
162         if (c.isDone())
163         {
164             removeCurrentElement();
165         }
166         else if(getStyle() == USE_SUB_CONTROLLERS)
167         {
168             incrementCurrent();
169         }
170     }
171
172     /**
173      * @return skipNext
174      */

175     protected boolean isSkipNext()
176     {
177         return skipNext;
178     }
179
180     /**
181      * @param skipNext
182      */

183     protected void setSkipNext(boolean skipNext)
184     {
185         this.skipNext = skipNext;
186     }
187     
188     
189     /* (non-Javadoc)
190      * @see org.apache.jmeter.control.GenericController#incrementCurrent()
191      */

192     protected void incrementCurrent()
193     {
194         if (currentReturnedAtLeastOne)
195         {
196             skipNext = true;
197         }
198         stillSame = false;
199         super.incrementCurrent();
200     }
201
202 /////////////// Start of Test Code ////////////////////////////////
203

204     public static class Test extends JMeterTestCase
205     {
206         public Test(String JavaDoc name)
207         {
208             super(name);
209         }
210
211         public void testProcessing() throws Exception JavaDoc
212         {
213             testLog.debug("Testing Interleave Controller 1");
214             GenericController controller = new GenericController();
215             InterleaveControl sub_1 = new InterleaveControl();
216             sub_1.setStyle(IGNORE_SUB_CONTROLLERS);
217             sub_1.addTestElement(new TestSampler("one"));
218             sub_1.addTestElement(new TestSampler("two"));
219             controller.addTestElement(sub_1);
220             controller.addTestElement(new TestSampler("three"));
221             LoopController sub_2 = new LoopController();
222             sub_2.setLoops(3);
223             GenericController sub_3 = new GenericController();
224             sub_2.addTestElement(new TestSampler("four"));
225             sub_3.addTestElement(new TestSampler("five"));
226             sub_3.addTestElement(new TestSampler("six"));
227             sub_2.addTestElement(sub_3);
228             sub_2.addTestElement(new TestSampler("seven"));
229             controller.addTestElement(sub_2);
230             String JavaDoc[] interleaveOrder = new String JavaDoc[] { "one", "two" };
231             String JavaDoc[] order =
232                 new String JavaDoc[] {
233                     "dummy",
234                     "three",
235                     "four",
236                     "five",
237                     "six",
238                     "seven",
239                     "four",
240                     "five",
241                     "six",
242                     "seven",
243                     "four",
244                     "five",
245                     "six",
246                     "seven" };
247             int counter = 14;
248             controller.initialize();
249             for (int i = 0; i < 4; i++)
250             {
251                 assertEquals(14, counter);
252                 counter = 0;
253                 TestElement sampler = null;
254                 while ((sampler = controller.next()) != null)
255                 {
256                     if (counter == 0)
257                     {
258                         assertEquals(
259                             interleaveOrder[i % 2],
260                             sampler.getPropertyAsString(TestElement.NAME));
261                     }
262                     else
263                     {
264                         assertEquals(
265                             order[counter],
266                             sampler.getPropertyAsString(TestElement.NAME));
267                     }
268                     counter++;
269                 }
270             }
271         }
272         
273         public void testProcessing6() throws Exception JavaDoc
274         {
275             testLog.debug("Testing Interleave Controller 6");
276             GenericController controller = new GenericController();
277             InterleaveControl sub_1 = new InterleaveControl();
278             controller.addTestElement(new TestSampler("one"));
279             sub_1.setStyle(IGNORE_SUB_CONTROLLERS);
280             controller.addTestElement(sub_1);
281             LoopController sub_2 = new LoopController();
282             sub_1.addTestElement(sub_2);
283             sub_2.setLoops(3);
284             int counter = 1;
285             controller.initialize();
286             for (int i = 0; i < 4; i++)
287             {
288                 assertEquals(1, counter);
289                 counter = 0;
290                 TestElement sampler = null;
291                 while ((sampler = controller.next()) != null)
292                 {
293                     assertEquals(
294                         "one",
295                         sampler.getPropertyAsString(TestElement.NAME));
296                     counter++;
297                 }
298             }
299         }
300
301         public void testProcessing2() throws Exception JavaDoc
302         {
303             testLog.debug("Testing Interleave Controller 2");
304             GenericController controller = new GenericController();
305             InterleaveControl sub_1 = new InterleaveControl();
306             sub_1.setStyle(IGNORE_SUB_CONTROLLERS);
307             sub_1.addTestElement(new TestSampler("one"));
308             sub_1.addTestElement(new TestSampler("two"));
309             controller.addTestElement(sub_1);
310             controller.addTestElement(new TestSampler("three"));
311             LoopController sub_2 = new LoopController();
312             sub_2.setLoops(3);
313             GenericController sub_3 = new GenericController();
314             sub_2.addTestElement(new TestSampler("four"));
315             sub_3.addTestElement(new TestSampler("five"));
316             sub_3.addTestElement(new TestSampler("six"));
317             sub_2.addTestElement(sub_3);
318             sub_2.addTestElement(new TestSampler("seven"));
319             sub_1.addTestElement(sub_2);
320             String JavaDoc[] order =
321                 new String JavaDoc[] {
322                     "one",
323                     "three",
324                     "two",
325                     "three",
326                     "four",
327                     "three",
328                     "one",
329                     "three",
330                     "two",
331                     "three",
332                     "five",
333                     "three",
334                     "one",
335                     "three",
336                     "two",
337                     "three",
338                     "six",
339                     "three",
340                     "one",
341                     "three" };
342             int counter = 0;
343             controller.initialize();
344             while (counter < order.length)
345             {
346                 TestElement sampler = null;
347                 while ((sampler = controller.next()) != null)
348                 {
349                     assertEquals(
350                         "failed on " + counter,
351                         order[counter],
352                         sampler.getPropertyAsString(TestElement.NAME));
353                     counter++;
354                 }
355             }
356         }
357
358         public void testProcessing3() throws Exception JavaDoc
359         {
360             testLog.debug("Testing Interleave Controller 3");
361             GenericController controller = new GenericController();
362             InterleaveControl sub_1 = new InterleaveControl();
363             sub_1.setStyle(USE_SUB_CONTROLLERS);
364             sub_1.addTestElement(new TestSampler("one"));
365             sub_1.addTestElement(new TestSampler("two"));
366             controller.addTestElement(sub_1);
367             controller.addTestElement(new TestSampler("three"));
368             LoopController sub_2 = new LoopController();
369             sub_2.setLoops(3);
370             GenericController sub_3 = new GenericController();
371             sub_2.addTestElement(new TestSampler("four"));
372             sub_3.addTestElement(new TestSampler("five"));
373             sub_3.addTestElement(new TestSampler("six"));
374             sub_2.addTestElement(sub_3);
375             sub_2.addTestElement(new TestSampler("seven"));
376             sub_1.addTestElement(sub_2);
377             String JavaDoc[] order =
378                 new String JavaDoc[] {
379                     "one",
380                     "three",
381                     "two",
382                     "three",
383                     "four",
384                     "five",
385                     "six",
386                     "seven",
387                     "four",
388                     "five",
389                     "six",
390                     "seven",
391                     "four",
392                     "five",
393                     "six",
394                     "seven",
395                     "three",
396                     "one",
397                     "three",
398                     "two",
399                     "three" };
400             int counter = 0;
401             controller.initialize();
402             while (counter < order.length)
403             {
404                 TestElement sampler = null;
405                 while ((sampler = controller.next()) != null)
406                 {
407                     assertEquals(
408                         "failed on" + counter,
409                         order[counter],
410                         sampler.getPropertyAsString(TestElement.NAME));
411                     counter++;
412                 }
413             }
414         }
415
416         public void testProcessing4() throws Exception JavaDoc
417         {
418             testLog.debug("Testing Interleave Controller 4");
419             GenericController controller = new GenericController();
420             InterleaveControl sub_1 = new InterleaveControl();
421             sub_1.setStyle(IGNORE_SUB_CONTROLLERS);
422             controller.addTestElement(sub_1);
423             GenericController sub_2 = new GenericController();
424             sub_2.addTestElement(new TestSampler("one"));
425             sub_2.addTestElement(new TestSampler("two"));
426             sub_1.addTestElement(sub_2);
427             GenericController sub_3 = new GenericController();
428             sub_3.addTestElement(new TestSampler("three"));
429             sub_3.addTestElement(new TestSampler("four"));
430             sub_1.addTestElement(sub_3);
431             String JavaDoc[] order = new String JavaDoc[] { "one", "three", "two", "four" };
432             int counter = 0;
433             controller.initialize();
434             while (counter < order.length)
435             {
436                 TestElement sampler = null;
437                 while ((sampler = controller.next()) != null)
438                 {
439                     assertEquals(
440                         "failed on" + counter,
441                         order[counter],
442                         sampler.getPropertyAsString(TestElement.NAME));
443                     counter++;
444                 }
445             }
446         }
447
448         public void testProcessing5() throws Exception JavaDoc
449         {
450             testLog.debug("Testing Interleave Controller 5");
451             GenericController controller = new GenericController();
452             InterleaveControl sub_1 = new InterleaveControl();
453             sub_1.setStyle(USE_SUB_CONTROLLERS);
454             controller.addTestElement(sub_1);
455             GenericController sub_2 = new GenericController();
456             sub_2.addTestElement(new TestSampler("one"));
457             sub_2.addTestElement(new TestSampler("two"));
458             sub_1.addTestElement(sub_2);
459             GenericController sub_3 = new GenericController();
460             sub_3.addTestElement(new TestSampler("three"));
461             sub_3.addTestElement(new TestSampler("four"));
462             sub_1.addTestElement(sub_3);
463             String JavaDoc[] order = new String JavaDoc[] { "one", "two", "three", "four" };
464             int counter = 0;
465             controller.initialize();
466             while (counter < order.length)
467             {
468                 TestElement sampler = null;
469                 while ((sampler = controller.next()) != null)
470                 {
471                     assertEquals(
472                         "failed on" + counter,
473                         order[counter],
474                         sampler.getPropertyAsString(TestElement.NAME));
475                     counter++;
476                 }
477             }
478         }
479     }
480 }
481
Popular Tags