KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/LoopController.java,v 1.20 2004/02/13 02:40:53 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.BooleanProperty;
28 import org.apache.jmeter.testelement.property.IntegerProperty;
29 import org.apache.jmeter.testelement.property.StringProperty;
30 //NOTUSED import org.apache.jorphan.logging.LoggingManager;
31
//NOTUSED import org.apache.log.Logger;
32

33 /**
34  * @author Michael Stover
35  * @author Thad Smith
36  * @version $Revision: 1.20 $
37  */

38 public class LoopController extends GenericController implements Serializable JavaDoc
39 {
40     //NOTUSED private static Logger log = LoggingManager.getLoggerForClass();
41

42     private final static String JavaDoc LOOPS = "LoopController.loops";
43     private final static String JavaDoc CONTINUE_FOREVER =
44         "LoopController.continue_forever";
45     private int loopCount = 0;
46
47     public LoopController()
48     {
49         setContinueForever(true);
50     }
51
52     public void setLoops(int loops)
53     {
54         setProperty(new IntegerProperty(LOOPS, loops));
55     }
56
57     public void setLoops(String JavaDoc loopValue)
58     {
59         setProperty(new StringProperty(LOOPS, loopValue));
60     }
61
62     public int getLoops()
63     {
64         try
65         {
66             return Integer.parseInt(getPropertyAsString(LOOPS));
67         }
68         catch (NumberFormatException JavaDoc e)
69         {
70             return 0;
71         }
72     }
73
74     public String JavaDoc getLoopString()
75     {
76         return getPropertyAsString(LOOPS);
77     }
78
79     /**
80      * Determines whether the loop will return any samples if it is rerun.
81      *
82      * @param forever true if the loop must be reset after ending a run
83      */

84     public void setContinueForever(boolean forever)
85     {
86         setProperty(new BooleanProperty(CONTINUE_FOREVER, forever));
87     }
88
89     public boolean getContinueForever()
90     {
91         return getPropertyAsBoolean(CONTINUE_FOREVER);
92     }
93
94     /* (non-Javadoc)
95      * @see org.apache.jmeter.control.Controller#isDone()
96      */

97     public boolean isDone()
98     {
99         if (getLoops() != 0)
100         {
101             return super.isDone();
102         }
103         else
104         {
105             return true;
106         }
107     }
108
109     private boolean endOfLoop()
110     {
111         return (getLoops() > -1) && loopCount >= getLoops();
112     }
113
114     /* (non-Javadoc)
115      * @see org.apache.jmeter.control.GenericController#nextIsNull()
116      */

117     protected Sampler nextIsNull() throws NextIsNullException
118     {
119         reInitialize();
120         if (endOfLoop())
121         {
122             if (!getContinueForever())
123             {
124                 setDone(true);
125             }
126             else
127             {
128                 resetLoopCount();
129             }
130             return null;
131         }
132         else
133         {
134             return next();
135         }
136     }
137
138     protected void incrementLoopCount()
139     {
140         loopCount++;
141     }
142
143     protected void resetLoopCount()
144     {
145         loopCount = 0;
146     }
147
148     /* (non-Javadoc)
149      * @see org.apache.jmeter.control.GenericController#getIterCount()
150      */

151     protected int getIterCount()
152     {
153         return loopCount + 1;
154     }
155
156     /* (non-Javadoc)
157      * @see org.apache.jmeter.control.GenericController#reInitialize()
158      */

159     protected void reInitialize()
160     {
161         setFirst(true);
162         resetCurrent();
163         incrementLoopCount();
164     }
165
166 ///////////////////////// Start of Test code ///////////////////////////////
167

168     public static class Test extends JMeterTestCase
169     {
170         public Test(String JavaDoc name)
171         {
172             super(name);
173         }
174
175         public void testProcessing() throws Exception JavaDoc
176         {
177             GenericController controller = new GenericController();
178             GenericController sub_1 = new GenericController();
179             sub_1.addTestElement(new TestSampler("one"));
180             sub_1.addTestElement(new TestSampler("two"));
181             controller.addTestElement(sub_1);
182             controller.addTestElement(new TestSampler("three"));
183             LoopController sub_2 = new LoopController();
184             sub_2.setLoops(3);
185             GenericController sub_3 = new GenericController();
186             sub_2.addTestElement(new TestSampler("four"));
187             sub_3.addTestElement(new TestSampler("five"));
188             sub_3.addTestElement(new TestSampler("six"));
189             sub_2.addTestElement(sub_3);
190             sub_2.addTestElement(new TestSampler("seven"));
191             controller.addTestElement(sub_2);
192             String JavaDoc[] order =
193                 new String JavaDoc[] {
194                     "one",
195                     "two",
196                     "three",
197                     "four",
198                     "five",
199                     "six",
200                     "seven",
201                     "four",
202                     "five",
203                     "six",
204                     "seven",
205                     "four",
206                     "five",
207                     "six",
208                     "seven" };
209             int counter = 15;
210             controller.initialize();
211             for (int i = 0; i < 2; i++)
212             {
213                 assertEquals(15, counter);
214                 counter = 0;
215                 TestElement sampler = null;
216                 while ((sampler = controller.next()) != null)
217                 {
218                     assertEquals(
219                         order[counter++],
220                         sampler.getPropertyAsString(TestElement.NAME));
221                 }
222             }
223         }
224         
225         public void testLoopZeroTimes() throws Exception JavaDoc
226         {
227             LoopController loop = new LoopController();
228             loop.setLoops(0);
229             loop.addTestElement(new TestSampler("never run"));
230             loop.initialize();
231             assertNull(loop.next());
232         }
233
234         public void testInfiniteLoop() throws Exception JavaDoc
235         {
236             LoopController loop = new LoopController();
237             loop.setLoops(-1);
238             loop.addTestElement(new TestSampler("never run"));
239             loop.initialize();
240             for (int i=0; i<42; i++)
241             {
242                 assertNotNull(loop.next());
243             }
244         }
245
246     }
247 }
Popular Tags