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: /**
027: * @version $Revision: 595966 $
028: */
029: public class TestWhileController extends JMeterTestCase {
030: static {
031: // LoggingManager.setPriority("DEBUG","jmeter");
032: // LoggingManager.setTarget(new java.io.PrintWriter(System.out));
033: }
034:
035: public TestWhileController(String name) {
036: super (name);
037: }
038:
039: // Get next sample and its name
040: private String nextName(GenericController c) {
041: Sampler s = c.next();
042: if (s == null) {
043: return null;
044: } else {
045: return s.getPropertyAsString(TestElement.NAME);
046: }
047: }
048:
049: // While (blank), previous sample OK - should loop until false
050: public void testBlankPrevOK() throws Exception {
051: // log.info("testBlankPrevOK");
052: runtestPrevOK("");
053: }
054:
055: // While (LAST), previous sample OK - should loop until false
056: public void testLastPrevOK() throws Exception {
057: // log.info("testLASTPrevOK");
058: runtestPrevOK("LAST");
059: }
060:
061: private static final String OTHER = "X"; // Dummy for testing
062:
063: // functions
064:
065: // While (LAST), previous sample OK - should loop until false
066: public void testOtherPrevOK() throws Exception {
067: // log.info("testOtherPrevOK");
068: runtestPrevOK(OTHER);
069: }
070:
071: public void runtestPrevOK(String type) throws Exception {
072: GenericController controller = new GenericController();
073: WhileController while_cont = new WhileController();
074: while_cont.testMode = true;
075: while_cont.testModeResult = true;
076: while_cont.setCondition(type);
077: while_cont.addTestElement(new TestSampler("one"));
078: while_cont.addTestElement(new TestSampler("two"));
079: while_cont.addTestElement(new TestSampler("three"));
080: controller.addTestElement(while_cont);
081: controller.addTestElement(new TestSampler("four"));
082: controller.initialize();
083: assertEquals("one", nextName(controller));
084: assertEquals("two", nextName(controller));
085: assertEquals("three", nextName(controller));
086: assertEquals("one", nextName(controller));
087: assertEquals("two", nextName(controller));
088: assertEquals("three", nextName(controller));
089: assertEquals("one", nextName(controller));
090: while_cont.testModeResult = false;// one and two fail
091: if (type.equals(OTHER))
092: while_cont.setCondition("false");
093: assertEquals("two", nextName(controller));
094: assertEquals("three", nextName(controller));
095: while_cont.testModeResult = true;// but three OK, so does not exit
096: if (type.equals(OTHER))
097: while_cont.setCondition(OTHER);
098: assertEquals("one", nextName(controller));
099: assertEquals("two", nextName(controller));
100: assertEquals("three", nextName(controller));
101: while_cont.testModeResult = false;// three fails, so exits while
102: if (type.equals(OTHER))
103: while_cont.setCondition("false");
104: assertEquals("four", nextName(controller));
105: assertNull(nextName(controller));
106: while_cont.testModeResult = true;
107: if (type.equals(OTHER))
108: while_cont.setCondition(OTHER);
109: assertEquals("one", nextName(controller));
110: }
111:
112: // While (blank), previous sample failed - should run once
113: public void testBlankPrevFailed() throws Exception {
114: // log.info("testBlankPrevFailed");
115: GenericController controller = new GenericController();
116: WhileController while_cont = new WhileController();
117: while_cont.testMode = true;
118: while_cont.testModeResult = false;
119: while_cont.setCondition("");
120: while_cont.addTestElement(new TestSampler("one"));
121: while_cont.addTestElement(new TestSampler("two"));
122: controller.addTestElement(while_cont);
123: controller.addTestElement(new TestSampler("three"));
124: controller.initialize();
125: assertEquals("one", nextName(controller));
126: assertEquals("two", nextName(controller));
127: assertEquals("three", nextName(controller));
128: assertNull(nextName(controller));
129: assertEquals("one", nextName(controller));
130: assertEquals("two", nextName(controller));
131: assertEquals("three", nextName(controller));
132: assertNull(nextName(controller));
133: }
134:
135: // While LAST, previous sample failed - should not run
136: public void testLASTPrevFailed() throws Exception {
137: // log.info("testLastPrevFailed");
138: runTestPrevFailed("LAST");
139: }
140:
141: // While False, previous sample failed - should not run
142: public void testfalsePrevFailed() throws Exception {
143: // log.info("testFalsePrevFailed");
144: runTestPrevFailed("False");
145: }
146:
147: public void runTestPrevFailed(String s) throws Exception {
148: GenericController controller = new GenericController();
149: WhileController while_cont = new WhileController();
150: while_cont.testMode = true;
151: while_cont.testModeResult = false;
152: while_cont.setCondition(s);
153: while_cont.addTestElement(new TestSampler("one"));
154: while_cont.addTestElement(new TestSampler("two"));
155: controller.addTestElement(while_cont);
156: controller.addTestElement(new TestSampler("three"));
157: controller.initialize();
158: assertEquals("three", nextName(controller));
159: assertNull(nextName(controller));
160: assertEquals("three", nextName(controller));
161: assertNull(nextName(controller));
162: }
163:
164: // Tests for Stack Overflow (bug 33954)
165: public void testAlwaysFailOK() throws Exception {
166: runTestAlwaysFail(true); // Should be OK
167: }
168:
169: public void testAlwaysFailBAD() throws Exception {
170: runTestAlwaysFail(false); // Currently fails
171: }
172:
173: public void runTestAlwaysFail(boolean other) {
174: LoopController controller = new LoopController();
175: controller.setContinueForever(true);
176: controller.setLoops(-1);
177: WhileController while_cont = new WhileController();
178: while_cont.testMode = true;
179: while_cont.testModeResult = false;
180: while_cont.setCondition("false");
181: while_cont.addTestElement(new TestSampler("one"));
182: while_cont.addTestElement(new TestSampler("two"));
183: controller.addTestElement(while_cont);
184: if (other)
185: controller.addTestElement(new TestSampler("three"));
186: controller.initialize();
187: try {
188: if (other) {
189: assertEquals("three", nextName(controller));
190: } else {
191: assertNull(nextName(controller));
192: }
193: } catch (StackOverflowError e) {
194: // e.printStackTrace();
195: fail(e.toString());
196: }
197: }
198: }
|