KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/OnceOnlyController.java,v 1.11.2.2 2005/03/13 21:13:40 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.engine.event.LoopIterationEvent;
24 import org.apache.jmeter.engine.event.LoopIterationListener;
25 import org.apache.jmeter.junit.JMeterTestCase;
26 import org.apache.jmeter.junit.stubs.TestSampler;
27 import org.apache.jmeter.samplers.Sampler;
28 import org.apache.jmeter.testelement.TestElement;
29
30 /**
31  * @author Michael Stover
32  * Created March 13, 2001
33  * @version $Revision: 1.11.2.2 $ Last updated: $Date: 2005/03/13 21:13:40 $
34  */

35 public class OnceOnlyController
36     extends GenericController
37     implements Serializable JavaDoc, LoopIterationListener
38 {
39
40     /**
41      * Constructor for the OnceOnlyController object.
42      */

43     public OnceOnlyController()
44     {
45     }
46
47     /**
48      * @see LoopIterationListener#iterationStart(LoopIterationEvent)
49      */

50     public void iterationStart(LoopIterationEvent event)
51     {
52         if (event.getIteration() == 1)
53         {
54             reInitialize();
55         }
56     }
57
58     protected Sampler nextIsNull() throws NextIsNullException
59     {
60         return null;
61     }
62
63 /////////////////////////// Start of Test Code ///////////////////////////
64

65     public static class Test extends JMeterTestCase
66     {
67         public Test(String JavaDoc name)
68         {
69             super(name);
70         }
71
72         public void testProcessing() throws Exception JavaDoc
73         {
74             GenericController controller = new GenericController();
75             GenericController sub_1 = new OnceOnlyController();
76             sub_1.addTestElement(new TestSampler("one"));
77             sub_1.addTestElement(new TestSampler("two"));
78             controller.addTestElement(sub_1);
79             controller.addTestElement(new TestSampler("three"));
80             LoopController sub_2 = new LoopController();
81             sub_2.setLoops(3);
82             GenericController sub_3 = new GenericController();
83             sub_2.addTestElement(new TestSampler("four"));
84             sub_3.addTestElement(new TestSampler("five"));
85             sub_3.addTestElement(new TestSampler("six"));
86             sub_2.addTestElement(sub_3);
87             sub_2.addTestElement(new TestSampler("seven"));
88             controller.addTestElement(sub_2);
89             String JavaDoc[] interleaveOrder = new String JavaDoc[] { "one", "two" };
90             String JavaDoc[] order =
91                 new String JavaDoc[] {
92                     "",
93                     "",
94                     "three",
95                     "four",
96                     "five",
97                     "six",
98                     "seven",
99                     "four",
100                     "five",
101                     "six",
102                     "seven",
103                     "four",
104                     "five",
105                     "six",
106                     "seven" };
107             int counter = 15;
108             controller.initialize();
109             for (int i = 0; i < 4; i++)
110             {
111                 assertEquals(15, counter);
112                 counter = 0;
113                 if (i > 0)
114                 {
115                     counter = 2;
116                 }
117                 TestElement sampler = null;
118                 while ((sampler = controller.next()) != null)
119                 {
120                     if (i == 0 && counter < 2)
121                     {
122                         assertEquals(
123                             interleaveOrder[counter],
124                             sampler.getPropertyAsString(TestElement.NAME));
125                     }
126                     else
127                     {
128                         assertEquals(
129                             order[counter],
130                             sampler.getPropertyAsString(TestElement.NAME));
131                     }
132                     counter++;
133                 }
134             }
135         }
136
137         public void testProcessing2() throws Exception JavaDoc
138         {
139             GenericController controller = new GenericController();
140             GenericController sub_1 = new OnceOnlyController();
141             sub_1.addTestElement(new TestSampler("one"));
142             sub_1.addTestElement(new TestSampler("two"));
143             controller.addTestElement(sub_1);
144             controller.addTestElement(new TestSampler("three"));
145             LoopController sub_2 = new LoopController();
146             sub_2.setLoops(3);
147             OnceOnlyController sub_3 = new OnceOnlyController();
148             sub_2.addTestElement(new TestSampler("four"));
149             sub_3.addTestElement(new TestSampler("five"));
150             sub_3.addTestElement(new TestSampler("six"));
151             sub_2.addTestElement(sub_3);
152             sub_2.addIterationListener(sub_3);
153             sub_2.addTestElement(new TestSampler("seven"));
154             controller.addTestElement(sub_2);
155             String JavaDoc[] interleaveOrder = new String JavaDoc[] { "one", "two" };
156             String JavaDoc[] order =
157                 new String JavaDoc[] {
158                     "",
159                     "",
160                     "three",
161                     "four",
162                     "five",
163                     "six",
164                     "seven",
165                     "four",
166                     "seven",
167                     "four",
168                     "seven" };
169             int counter = 11;
170             controller.initialize();
171             for (int i = 0; i < 4; i++)
172             {
173                 assertEquals(11, counter);
174                 counter = 0;
175                 if (i > 0)
176                 {
177                     counter = 2;
178                 }
179                 TestElement sampler = null;
180                 while ((sampler = controller.next()) != null)
181                 {
182                     if (i == 0 && counter < 2)
183                     {
184                         assertEquals(
185                             interleaveOrder[counter],
186                             sampler.getPropertyAsString(TestElement.NAME));
187                     }
188                     else
189                     {
190                         assertEquals(
191                             order[counter],
192                             sampler.getPropertyAsString(TestElement.NAME));
193                     }
194                     counter++;
195                 }
196             }
197         }
198     }
199 }
200
Popular Tags