KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/IfController.java,v 1.3.2.3 2005/03/12 02:26:19 sebb Exp $
2
/*
3  * Copyright 2003-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.samplers.Sampler;
25 import org.apache.jmeter.testelement.TestElement;
26 import org.apache.jmeter.testelement.property.StringProperty;
27 import org.apache.jorphan.logging.LoggingManager;
28 import org.apache.log.Logger;
29 import org.mozilla.javascript.Context;
30 import org.mozilla.javascript.Scriptable;
31
32 /**
33  *
34  *@author Cyrus Montakab
35  * created 2003/06/30
36  *@version $Date: 2005/03/12 02:26:19 $ $Revision: 1.3.2.3 $
37  * This is a Conditional Controller; it will execute the set of statements
38  * (samplers/controllers, etc) while the 'condition' is true.
39  * In a programming world - this is equivalant of :
40  * if (condition) {
41  * statements ....
42  * }
43  * In JMeter you may have :
44  * Thread-Group (set to loop a number of times or indefinitely,
45  * ... Samplers ... (e.g. Counter )
46  * ... Other Controllers ....
47  * ... IfController ( condition set to something like - ${counter}<10)
48  * ... statements to perform if condition is true ...
49  * ... Other Controllers /Samplers
50  * }
51  *
52  ***************************************/

53
54 public class IfController extends GenericController implements Serializable JavaDoc
55 {
56
57       private static Logger logger =
58             LoggingManager.getLoggerForClass();
59       private final static String JavaDoc CONDITION = "IfController.condition";
60
61       /**
62        * constructor
63        */

64       public IfController() {
65             super();
66       }
67
68       /**
69        * constructor
70        */

71       public IfController(String JavaDoc condition) {
72             super();
73             this.setCondition(condition);
74       }
75
76       /**
77        * Condition Accessor - this is gonna be like ${count}<10
78        */

79       public void setCondition(String JavaDoc condition) {
80             setProperty(new StringProperty(CONDITION, condition));
81       }
82
83       /**
84        * Condition Accessor - this is gonna be like ${count}<10
85        */

86       public String JavaDoc getCondition() {
87             return getPropertyAsString(CONDITION);
88       }
89
90       /**
91        * evaluate the condition clause
92        * log error if bad condition
93        */

94       static boolean evaluateCondition(String JavaDoc cond) {
95             logger.debug(" getCondition() : [" + cond + "]");
96
97             String JavaDoc resultStr = "";
98             boolean result = false;
99
100             // now evaluate the condition using JavaScript
101
Context cx = Context.enter();
102             try {
103                   Scriptable scope = cx.initStandardObjects(null);
104                   Object JavaDoc cxResultObject =
105                         cx.evaluateString(scope, cond
106                   /*** conditionString ***/
107                   , "<cmd>", 1, null);
108                   resultStr = Context.toString(cxResultObject);
109
110                   if (resultStr.equals("false")) {
111                         result = false;
112                   } else if (resultStr.equals("true")) {
113                         result = true;
114                   } else {
115                         throw new Exception JavaDoc(" BAD CONDITION :: " + cond);
116                   }
117
118                   logger.debug(
119                         " >> evaluate Condition - [ "
120                               + cond
121                               + "] results is ["
122                               + result
123                               + "]");
124             } catch (Exception JavaDoc e) {
125                     logger.error(e.getMessage(),e);
126             } finally {
127                   Context.exit();
128             }
129
130             return result;
131       }
132
133       /**
134        * This is overriding the parent method.
135        * IsDone indicates whether the termination condition is reached.
136        * I.e. if the condition evaluates to False - then isDone() returns TRUE
137        */

138     public boolean isDone() {
139 // boolean result = true;
140
// try {
141
// result = !evaluateCondition();
142
// } catch (Exception e) {
143
// logger.error(e.getMessage(), e);
144
// }
145
// setDone(true);
146
// return result;
147
// setDone(false);
148
return false;
149     }
150
151       /**
152        * @see org.apache.jmeter.control.Controller#next()
153        * 'JMeterThread' iterates thru the Controller by calling this method.
154        * IF a valid 'Sampler' is returned, then it executes the sampler
155        * (calls sampler.sampler(xxx) method) .
156        * So here we make sure that the samplers belonging to this
157        * Controller do not get called
158        * - if isDone is true
159        * - if its the first time this is run. The first time is special
160        * cause it is called prior the iteration even starts !
161        */

162     public Sampler next()
163     {
164 // clear cached condition
165
getProperty(CONDITION).recoverRunningVersion(null);
166         boolean result = evaluateCondition(getCondition());
167         if (result)
168            return super.next();
169         else try
170         {
171            return nextIsNull();
172         }
173         catch (NextIsNullException e1)
174         {
175            return null;
176         }
177     }
178
179 ////////////////////////////// Start of Test Code ///////////////////////////
180

181       /**
182        * JUnit test
183        */

184       public static class Test extends JMeterTestCase {
185             public Test(String JavaDoc name) {
186                   super(name);
187             }
188
189             public void testProcessing() throws Exception JavaDoc {
190
191                   GenericController controller = new GenericController();
192
193                   controller.addTestElement(new IfController("false==false"));
194                   controller.addTestElement(new IfController(" \"a\".equals(\"a\")"));
195                   controller.addTestElement(new IfController("2<100"));
196
197                   /* GenericController sub_1 = new GenericController();
198                               sub_1.addTestElement(new IfController("3==3"));
199                               controller.addTestElement(sub_1);
200                               controller.addTestElement(new IfController("false==true"));
201                   */

202
203                   /*
204                   GenericController controller = new GenericController();
205                   GenericController sub_1 = new GenericController();
206                   sub_1.addTestElement(new IfController("10<100"));
207                   sub_1.addTestElement(new IfController("true==false"));
208                   controller.addTestElement(sub_1);
209                   controller.addTestElement(new IfController("false==false"));
210
211                   IfController sub_2 = new IfController();
212                   sub_2.setCondition( "10<10000");
213                   GenericController sub_3 = new GenericController();
214
215                   sub_2.addTestElement(new IfController( " \"a\".equals(\"a\")" ) );
216                   sub_3.addTestElement(new IfController("2>100"));
217                   sub_3.addTestElement(new IfController("false==true"));
218                   sub_2.addTestElement(sub_3);
219                   sub_2.addTestElement(new IfController("2==3"));
220                   controller.addTestElement(sub_2);
221                   */

222
223                   /* IfController controller = new IfController("12==12");
224                           controller.initialize();
225                   */

226                   logger.debug(">>>>> testProcessing : Starting the iteration ");
227                   TestElement sampler = null;
228                   while ((sampler = controller.next()) != null) {
229                         logger.debug(
230                               " ->>> Gonna assertTrue :"
231                                     + sampler.getClass().getName()
232                                     + " Property is ---->>>"
233                                     + sampler.getPropertyAsString(TestElement.NAME));
234                   }
235             }
236       }
237
238 }
Popular Tags