KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/Attic/SwitchController.java,v 1.1.2.2 2005/03/16 23:26:42 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
29 public class SwitchController extends InterleaveControl implements Serializable JavaDoc
30 {
31     private final static String JavaDoc SWITCH_VALUE = "SwitchController.value";
32
33
34     public SwitchController()
35     {
36         super();
37         this.setStyle(USE_SUB_CONTROLLERS);
38     }
39     public void reInitialize()
40     {
41         int was = current;
42         super.reInitialize();
43         current=getSelectionAsInt();
44     }
45
46     /**
47      * @see org.apache.jmeter.control.GenericController#resetCurrent()
48      */

49     protected void resetCurrent()
50     {
51         int c = getSubControllers().size();
52         if (c > 0)
53         {
54            current = getSelectionAsInt();
55         }
56         else
57         {
58             current = 0;
59         }
60     }
61
62     /**
63      * @see org.apache.jmeter.control.GenericController#incrementCurrent()
64      */

65     protected void incrementCurrent()
66     {
67         int was = current;
68         super.incrementCurrent();
69         current = getSelectionAsInt();
70     }
71
72     public void setSelection(String JavaDoc inputValue)
73     {
74         setProperty(new StringProperty(SWITCH_VALUE, inputValue));
75     }
76
77     private int getSelectionAsInt()
78     {
79         int ret;
80         getProperty(SWITCH_VALUE).recoverRunningVersion(null);
81         String JavaDoc sel =getSelection();
82         try{
83             ret = Integer.parseInt(sel);
84         } catch (NumberFormatException JavaDoc e){
85             ret=0;
86         }
87         if (ret < 0 || ret >= getSubControllers().size()){
88             ret = 0;
89         }
90         return ret;
91     }
92     public String JavaDoc getSelection()
93     {
94         return getPropertyAsString(SWITCH_VALUE);
95     }
96
97     public static class Test extends JMeterTestCase
98     {
99         static{
100             //LoggingManager.setPriority("DEBUG","jmeter");
101
//LoggingManager.setTarget(new java.io.PrintWriter(System.out));
102
}
103
104         public Test(String JavaDoc name)
105         {
106             super(name);
107         }
108
109         // Get next sample and its name
110
private String JavaDoc nextName(GenericController c){
111             Sampler s = c.next();
112             String JavaDoc n;
113             if (s==null){
114                 return null;
115             } else {
116                 n=s.getPropertyAsString(TestElement.NAME);
117                 return n;
118             }
119         }
120         public void test() throws Exception JavaDoc
121         {
122             runSimpleTests("","zero");
123         }
124         public void test0() throws Exception JavaDoc
125         {
126             runSimpleTests("0","zero");
127         }
128         public void test1() throws Exception JavaDoc
129         {
130             runSimpleTests("1","one");
131         }
132         public void test2() throws Exception JavaDoc
133         {
134             runSimpleTests("2","two");
135         }
136         public void test3() throws Exception JavaDoc
137         {
138             runSimpleTests("3","three");
139         }
140         public void test4() throws Exception JavaDoc
141         {
142             runSimpleTests("4","zero");
143         }
144         public void testX() throws Exception JavaDoc
145         {
146             runSimpleTests("X","zero");
147         }
148         
149         public void runSimpleTests(String JavaDoc cond, String JavaDoc exp) throws Exception JavaDoc
150         {
151             runSimpleTest(cond,exp);
152             runSimpleTest2(cond,exp);
153         }
154         
155         // Simple test with single Selection controller
156
public void runSimpleTest(String JavaDoc cond, String JavaDoc exp) throws Exception JavaDoc
157         {
158             GenericController controller = new GenericController();
159
160             SwitchController switch_cont = new SwitchController();
161             switch_cont.setSelection(cond);
162
163             controller.addTestElement(new TestSampler("before"));
164             controller.addTestElement(switch_cont);
165
166             switch_cont.addTestElement(new TestSampler("zero"));
167             switch_cont.addTestElement(new TestSampler("one"));
168             switch_cont.addTestElement(new TestSampler("two"));
169             switch_cont.addTestElement(new TestSampler("three"));
170             
171             controller.addTestElement(new TestSampler("after"));
172             
173             controller.initialize();
174             
175             for (int i=1;i<=3;i++){
176             assertEquals("Loop "+i,"before",nextName(controller));
177             assertEquals("Loop "+i,exp,nextName(controller));
178             assertEquals("Loop "+i,"after",nextName(controller));
179             assertNull(nextName(controller));
180             }
181         }
182
183         // Selection controller with two sub-controllers, but each has only 1 child
184
public void runSimpleTest2(String JavaDoc cond, String JavaDoc exp) throws Exception JavaDoc
185         {
186             GenericController controller = new GenericController();
187             GenericController sub_1 = new GenericController();
188             GenericController sub_2 = new GenericController();
189
190             SwitchController switch_cont = new SwitchController();
191             switch_cont.setSelection(cond);
192
193             switch_cont.addTestElement(new TestSampler("zero"));
194             switch_cont.addTestElement(sub_1);
195             sub_1.addTestElement(new TestSampler("one"));
196             
197             switch_cont.addTestElement(new TestSampler("two"));
198             
199             switch_cont.addTestElement(sub_2);
200             sub_2.addTestElement(new TestSampler("three"));
201             
202             controller.addTestElement(new TestSampler("before"));
203             controller.addTestElement(switch_cont);
204             controller.addTestElement(new TestSampler("after"));
205             controller.initialize();
206             for (int i=1;i<=3;i++){
207             assertEquals("before",nextName(controller));
208             assertEquals(exp,nextName(controller));
209             assertEquals("after",nextName(controller));
210             assertNull(nextName(controller));
211             }
212         }
213
214         public void testTest2() throws Exception JavaDoc
215         {
216             runTest2("",new String JavaDoc[]{"zero"});
217             runTest2("0",new String JavaDoc[]{"zero"});
218             runTest2("7",new String JavaDoc[]{"zero"});
219             runTest2("5",new String JavaDoc[]{"zero"});
220             runTest2("4",new String JavaDoc[]{"six"});
221             runTest2("3",new String JavaDoc[]{"five"});
222             runTest2("1",new String JavaDoc[]{"one","two"});
223             runTest2("2",new String JavaDoc[]{"three","four"});
224         }
225         /*
226          * Test:
227          * Before
228          * Selection Controller
229          * - zero (default)
230          * - simple controller 1
231          * - - one
232          * - - two
233          * - simple controller 2
234          * - - three
235          * - - four
236          * - five
237          * - six
238          * After
239          */

240         public void runTest2(String JavaDoc cond, String JavaDoc exp[]) throws Exception JavaDoc
241         {
242             int loops = 3;
243             LoopController controller = new LoopController();
244             controller.setLoops(loops);
245             controller.setContinueForever(false);
246             GenericController sub_1 = new GenericController();
247             GenericController sub_2 = new GenericController();
248
249             SwitchController switch_cont = new SwitchController();
250             switch_cont.setSelection(cond);
251
252             switch_cont.addTestElement(new TestSampler("zero"));
253             switch_cont.addTestElement(sub_1);
254             sub_1.addTestElement(new TestSampler("one"));
255             sub_1.addTestElement(new TestSampler("two"));
256             
257             switch_cont.addTestElement(sub_2);
258             sub_2.addTestElement(new TestSampler("three"));
259             sub_2.addTestElement(new TestSampler("four"));
260             
261             switch_cont.addTestElement(new TestSampler("five"));
262             switch_cont.addTestElement(new TestSampler("six"));
263
264             controller.addTestElement(new TestSampler("before"));
265             controller.addTestElement(switch_cont);
266             controller.addTestElement(new TestSampler("after"));
267             controller.initialize();
268             for (int i=1;i<=3;i++){
269                 assertEquals("Loop:"+i,"before",nextName(controller));
270                 for (int j=0;j<exp.length;j++) {
271                     assertEquals("Loop:"+i,exp[j],nextName(controller));
272                 }
273                 assertEquals("Loop:"+i,"after",nextName(controller));
274             }
275             assertNull("Loops:"+loops,nextName(controller));
276         }
277
278     }
279 }
280
Popular Tags