KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/WhileController.java,v 1.1.2.9 2005/03/17 00:31:58 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.StringProperty;
28 import org.apache.jmeter.threads.JMeterContextService;
29 import org.apache.jmeter.threads.JMeterThread;
30 import org.apache.jmeter.threads.JMeterVariables;
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33
34 /**
35  * @version $Revision: 1.1.2.9 $
36  */

37 public class WhileController extends GenericController implements Serializable JavaDoc
38 {
39     private static Logger log = LoggingManager.getLoggerForClass();
40     private final static String JavaDoc CONDITION = "WhileController.condition"; // $NON-NLS-1$
41

42     private static boolean testMode=false; // To make testing easier
43
private static boolean testModeResult=false; // dummy sample result
44

45     public WhileController()
46     {
47     }
48
49
50     /* (non-Javadoc)
51      * @see org.apache.jmeter.control.Controller#isDone()
52      */

53     public boolean isDone()
54     {
55         if (getSubControllers().size() == 0) // Nothing left to run
56
{
57             return true;
58         }
59         return false;// Never want to remove the controller from the tree
60
}
61
62     /*
63      * Evaluate the condition, which can be:
64      * blank or LAST = was the last sampler OK?
65      * otherwise, evaluate the condition to see if it is not "false"
66      * If blank, only evaluate at the end of the loop
67      *
68      * Must only be called at start and end of loop
69      *
70      * @param loopEnd - are we at loop end?
71      * @return true means OK to continue
72      */

73     private boolean endOfLoop(boolean loopEnd)
74     {
75 // clear cached condition
76
getProperty(CONDITION).recoverRunningVersion(null);
77         String JavaDoc cnd = getCondition();
78         log.debug("Condition string:"+cnd);
79         boolean res;
80         // If blank, only check previous sample when at end of loop
81
if ((loopEnd && cnd.length() == 0)
82                 || "LAST".equalsIgnoreCase(cnd)) {// $NON-NLS-1$
83
if (testMode) {
84                 res=!testModeResult;
85             } else {
86                 JMeterVariables threadVars =
87                     JMeterContextService.getContext().getVariables();
88                 // Use !false rather than true, so that null is treated as true
89
res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK));// $NON-NLS-1$
90
}
91         } else {
92             // cnd may be blank if next() called us
93
res = "false".equalsIgnoreCase(cnd);// $NON-NLS-1$
94
}
95         log.debug("Condition value: "+res);
96         return res;
97     }
98
99     /* (non-Javadoc)
100      * Only called at End of Loop
101      * @see org.apache.jmeter.control.GenericController#nextIsNull()
102      */

103     protected Sampler nextIsNull() throws NextIsNullException
104     {
105         reInitialize();
106         if (!endOfLoop(true))
107         {
108             return super.next();
109         }
110         else
111         {
112             setDone(true);
113             return null;
114         }
115     }
116
117     public Sampler next()
118     {
119         if (current!=0){ // in the middle of the loop
120
return super.next();
121         }
122         // Must be start of loop
123
if(!endOfLoop(false))
124         {
125             return super.next(); // OK to continue
126
}
127         else
128         {
129             reInitialize(); // Don't even start the loop
130
return null;
131         }
132     }
133
134     /**
135      * @param string the condition to save
136      */

137     public void setCondition(String JavaDoc string) {
138         log.debug("setCondition("+ string+")");
139         setProperty(new StringProperty(CONDITION, string));
140     }
141
142     /**
143      * @return the condition
144      */

145     public String JavaDoc getCondition() {
146         String JavaDoc cnd;
147         cnd=getPropertyAsString(CONDITION);
148         log.debug("getCondition() => "+cnd);
149         return cnd;
150     }
151     public static class Test extends JMeterTestCase
152     {
153         static{
154             //LoggingManager.setPriority("DEBUG","jmeter");
155
//LoggingManager.setTarget(new java.io.PrintWriter(System.out));
156
}
157
158         public Test(String JavaDoc name)
159         {
160             super(name);
161         }
162
163         // Get next sample and its name
164
private String JavaDoc nextName(GenericController c){
165             Sampler s = c.next();
166             if (s==null){
167                 return null;
168             } else {
169                 return s.getPropertyAsString(TestElement.NAME);
170             }
171         }
172         
173         // While (blank), previous sample OK - should loop until false
174
public void testBlankPrevOK() throws Exception JavaDoc
175         {
176             log.info("testBlankPrevOK");
177             runtestPrevOK("");
178         }
179         
180         // While (LAST), previous sample OK - should loop until false
181
public void testLastPrevOK() throws Exception JavaDoc
182         {
183             log.info("testLASTPrevOK");
184             runtestPrevOK("LAST");
185         }
186         
187         private static final String JavaDoc OTHER = "X"; // Dummy for testing functions
188
// While (LAST), previous sample OK - should loop until false
189
public void testOtherPrevOK() throws Exception JavaDoc
190         {
191             log.info("testOtherPrevOK");
192             runtestPrevOK(OTHER);
193         }
194         
195         public void runtestPrevOK(String JavaDoc type) throws Exception JavaDoc
196         {
197             testMode=true;
198             testModeResult=true;
199             GenericController controller = new GenericController();
200             WhileController while_cont = new WhileController();
201             while_cont.setCondition(type);
202             while_cont.addTestElement(new TestSampler("one"));
203             while_cont.addTestElement(new TestSampler("two"));
204             while_cont.addTestElement(new TestSampler("three"));
205             controller.addTestElement(while_cont);
206             controller.addTestElement(new TestSampler("four"));
207             controller.initialize();
208             assertEquals("one",nextName(controller));
209             assertEquals("two",nextName(controller));
210             assertEquals("three",nextName(controller));
211             assertEquals("one",nextName(controller));
212             assertEquals("two",nextName(controller));
213             assertEquals("three",nextName(controller));
214             assertEquals("one",nextName(controller));
215             testModeResult=false;// one and two fail
216
if (type.equals(OTHER)) while_cont.setCondition("false");
217             assertEquals("two",nextName(controller));
218             assertEquals("three",nextName(controller));
219             testModeResult=true;// but three OK, so does not exit
220
if (type.equals(OTHER)) while_cont.setCondition(OTHER);
221             assertEquals("one",nextName(controller));
222             assertEquals("two",nextName(controller));
223             assertEquals("three",nextName(controller));
224             testModeResult=false;// three fails, so exits while
225
if (type.equals(OTHER)) while_cont.setCondition("false");
226             assertEquals("four",nextName(controller));
227             assertNull(nextName(controller));
228             testModeResult=true;
229             if (type.equals(OTHER)) while_cont.setCondition(OTHER);
230             assertEquals("one",nextName(controller));
231         }
232         
233         // While (blank), previous sample failed - should run once
234
public void testBlankPrevFailed() throws Exception JavaDoc
235         {
236             log.info("testBlankPrevFailed");
237             testMode=true;
238             testModeResult=false;
239             GenericController controller = new GenericController();
240             WhileController while_cont = new WhileController();
241             while_cont.setCondition("");
242             while_cont.addTestElement(new TestSampler("one"));
243             while_cont.addTestElement(new TestSampler("two"));
244             controller.addTestElement(while_cont);
245             controller.addTestElement(new TestSampler("three"));
246             controller.initialize();
247             assertEquals("one",nextName(controller));
248             assertEquals("two",nextName(controller));
249             assertEquals("three",nextName(controller));
250             assertNull(nextName(controller));
251             assertEquals("one",nextName(controller));
252             assertEquals("two",nextName(controller));
253             assertEquals("three",nextName(controller));
254             assertNull(nextName(controller));
255         }
256
257         // While LAST, previous sample failed - should not run
258
public void testLASTPrevFailed() throws Exception JavaDoc
259         {
260             log.info("testLastPrevFailed");
261             runTestPrevFailed("LAST");
262         }
263         // While False, previous sample failed - should not run
264
public void testfalsePrevFailed() throws Exception JavaDoc
265         {
266             log.info("testFalsePrevFailed");
267             runTestPrevFailed("False");
268         }
269         public void runTestPrevFailed(String JavaDoc s) throws Exception JavaDoc
270         {
271             testMode=true;
272             testModeResult=false;
273             GenericController controller = new GenericController();
274             WhileController while_cont = new WhileController();
275             while_cont.setCondition(s);
276             while_cont.addTestElement(new TestSampler("one"));
277             while_cont.addTestElement(new TestSampler("two"));
278             controller.addTestElement(while_cont);
279             controller.addTestElement(new TestSampler("three"));
280             controller.initialize();
281             assertEquals("three",nextName(controller));
282             assertNull(nextName(controller));
283             assertEquals("three",nextName(controller));
284             assertNull(nextName(controller));
285         }
286
287         // Tests for Stack Overflow (bug 33954)
288
public void testAlwaysFailOK() throws Exception JavaDoc
289         {
290             runTestAlwaysFail(true); // Should be OK
291
}
292         
293         // TODO - re-enable when fix found
294
public void disabletestAlwaysFailBAD() throws Exception JavaDoc
295         {
296             runTestAlwaysFail(false); // Currently fails
297
}
298         
299         public void runTestAlwaysFail(boolean other)
300         {
301             testMode=true;
302             testModeResult=false;
303             LoopController controller = new LoopController();
304             controller.setContinueForever(true);
305             controller.setLoops(-1);
306             WhileController while_cont = new WhileController();
307             while_cont.setCondition("false");
308             while_cont.addTestElement(new TestSampler("one"));
309             while_cont.addTestElement(new TestSampler("two"));
310             controller.addTestElement(while_cont);
311             if (other) controller.addTestElement(new TestSampler("three"));
312             controller.initialize();
313             try {
314                 if (other){
315                 assertEquals("three",nextName(controller));
316                 } else {
317                     assertNull(nextName(controller));
318                 }
319             } catch (StackOverflowError JavaDoc e){
320                 //e.printStackTrace();
321
fail(e.toString());
322             }
323         }
324
325    }
326 }
Popular Tags