001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.control;
020:
021: import org.apache.jmeter.junit.JMeterTestCase;
022: import org.apache.jmeter.junit.stubs.TestSampler;
023: import org.apache.jmeter.samplers.Sampler;
024: import org.apache.jmeter.testelement.TestElement;
025:
026: public class TestSwitchController extends JMeterTestCase {
027: static {
028: // LoggingManager.setPriority("DEBUG","jmeter");
029: // LoggingManager.setTarget(new java.io.PrintWriter(System.out));
030: }
031:
032: public TestSwitchController(String name) {
033: super (name);
034: }
035:
036: // Get next sample and its name
037: private String nextName(GenericController c) {
038: Sampler s = c.next();
039: String n;
040: if (s == null) {
041: return null;
042: } else {
043: n = s.getPropertyAsString(TestElement.NAME);
044: return n;
045: }
046: }
047:
048: public void test() throws Exception {
049: runSimpleTests("", "zero");
050: }
051:
052: public void test0() throws Exception {
053: runSimpleTests("0", "zero");
054: }
055:
056: public void test1() throws Exception {
057: runSimpleTests("1", "one");
058: }
059:
060: public void test2() throws Exception {
061: runSimpleTests("2", "two");
062: }
063:
064: public void test3() throws Exception {
065: runSimpleTests("3", "three");
066: }
067:
068: public void test4() throws Exception {
069: runSimpleTests("4", "zero");
070: }
071:
072: public void testX() throws Exception {
073: runSimpleTests("X", "zero");
074: }
075:
076: public void runSimpleTests(String cond, String exp)
077: throws Exception {
078: runSimpleTest(cond, exp);
079: runSimpleTest2(cond, exp);
080: }
081:
082: // Simple test with single Selection controller
083: public void runSimpleTest(String cond, String exp) throws Exception {
084: GenericController controller = new GenericController();
085:
086: SwitchController switch_cont = new SwitchController();
087: switch_cont.setSelection(cond);
088:
089: controller.addTestElement(new TestSampler("before"));
090: controller.addTestElement(switch_cont);
091:
092: switch_cont.addTestElement(new TestSampler("zero"));
093: switch_cont.addTestElement(new TestSampler("one"));
094: switch_cont.addTestElement(new TestSampler("two"));
095: switch_cont.addTestElement(new TestSampler("three"));
096:
097: controller.addTestElement(new TestSampler("after"));
098:
099: controller.initialize();
100:
101: for (int i = 1; i <= 3; i++) {
102: assertEquals("Loop " + i, "before", nextName(controller));
103: assertEquals("Loop " + i, exp, nextName(controller));
104: assertEquals("Loop " + i, "after", nextName(controller));
105: assertNull(nextName(controller));
106: }
107: }
108:
109: // Selection controller with two sub-controllers, but each has only 1
110: // child
111: public void runSimpleTest2(String cond, String exp)
112: throws Exception {
113: GenericController controller = new GenericController();
114: GenericController sub_1 = new GenericController();
115: GenericController sub_2 = new GenericController();
116:
117: SwitchController switch_cont = new SwitchController();
118: switch_cont.setSelection(cond);
119:
120: switch_cont.addTestElement(new TestSampler("zero"));
121: switch_cont.addTestElement(sub_1);
122: sub_1.addTestElement(new TestSampler("one"));
123:
124: switch_cont.addTestElement(new TestSampler("two"));
125:
126: switch_cont.addTestElement(sub_2);
127: sub_2.addTestElement(new TestSampler("three"));
128:
129: controller.addTestElement(new TestSampler("before"));
130: controller.addTestElement(switch_cont);
131: controller.addTestElement(new TestSampler("after"));
132: controller.initialize();
133: for (int i = 1; i <= 3; i++) {
134: assertEquals("before", nextName(controller));
135: assertEquals(exp, nextName(controller));
136: assertEquals("after", nextName(controller));
137: assertNull(nextName(controller));
138: }
139: }
140:
141: public void testTest2() throws Exception {
142: runTest2("", new String[] { "zero" });
143: runTest2("0", new String[] { "zero" });
144: runTest2("7", new String[] { "zero" });
145: runTest2("5", new String[] { "zero" });
146: runTest2("4", new String[] { "six" });
147: runTest2("3", new String[] { "five" });
148: runTest2("1", new String[] { "one", "two" });
149: runTest2("2", new String[] { "three", "four" });
150: }
151:
152: /*
153: * Test: Before Selection Controller - zero (default) - simple
154: * controller 1 - - one - - two - simple controller 2 - - three - - four -
155: * five - six After
156: */
157: public void runTest2(String cond, String exp[]) throws Exception {
158: int loops = 3;
159: LoopController controller = new LoopController();
160: controller.setLoops(loops);
161: controller.setContinueForever(false);
162: GenericController sub_1 = new GenericController();
163: GenericController sub_2 = new GenericController();
164:
165: SwitchController switch_cont = new SwitchController();
166: switch_cont.setSelection(cond);
167:
168: switch_cont.addTestElement(new TestSampler("zero"));
169: switch_cont.addTestElement(sub_1);
170: sub_1.addTestElement(new TestSampler("one"));
171: sub_1.addTestElement(new TestSampler("two"));
172:
173: switch_cont.addTestElement(sub_2);
174: sub_2.addTestElement(new TestSampler("three"));
175: sub_2.addTestElement(new TestSampler("four"));
176:
177: switch_cont.addTestElement(new TestSampler("five"));
178: switch_cont.addTestElement(new TestSampler("six"));
179:
180: controller.addTestElement(new TestSampler("before"));
181: controller.addTestElement(switch_cont);
182: controller.addTestElement(new TestSampler("after"));
183: controller.setRunningVersion(true);
184: sub_1.setRunningVersion(true);
185: sub_2.setRunningVersion(true);
186: switch_cont.setRunningVersion(true);
187: controller.initialize();
188: for (int i = 1; i <= 3; i++) {
189: assertEquals("Loop:" + i, "before", nextName(controller));
190: for (int j = 0; j < exp.length; j++) {
191: assertEquals("Loop:" + i, exp[j], nextName(controller));
192: }
193: assertEquals("Loop:" + i, "after", nextName(controller));
194: }
195: assertNull("Loops:" + loops, nextName(controller));
196: }
197: }
|