1 18 19 package org.apache.jmeter.control; 20 21 import java.io.Serializable ; 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.BooleanProperty; 28 import org.apache.jmeter.testelement.property.IntegerProperty; 29 import org.apache.jmeter.testelement.property.StringProperty; 30 33 38 public class LoopController extends GenericController implements Serializable 39 { 40 42 private final static String LOOPS = "LoopController.loops"; 43 private final static String CONTINUE_FOREVER = 44 "LoopController.continue_forever"; 45 private int loopCount = 0; 46 47 public LoopController() 48 { 49 setContinueForever(true); 50 } 51 52 public void setLoops(int loops) 53 { 54 setProperty(new IntegerProperty(LOOPS, loops)); 55 } 56 57 public void setLoops(String loopValue) 58 { 59 setProperty(new StringProperty(LOOPS, loopValue)); 60 } 61 62 public int getLoops() 63 { 64 try 65 { 66 return Integer.parseInt(getPropertyAsString(LOOPS)); 67 } 68 catch (NumberFormatException e) 69 { 70 return 0; 71 } 72 } 73 74 public String getLoopString() 75 { 76 return getPropertyAsString(LOOPS); 77 } 78 79 84 public void setContinueForever(boolean forever) 85 { 86 setProperty(new BooleanProperty(CONTINUE_FOREVER, forever)); 87 } 88 89 public boolean getContinueForever() 90 { 91 return getPropertyAsBoolean(CONTINUE_FOREVER); 92 } 93 94 97 public boolean isDone() 98 { 99 if (getLoops() != 0) 100 { 101 return super.isDone(); 102 } 103 else 104 { 105 return true; 106 } 107 } 108 109 private boolean endOfLoop() 110 { 111 return (getLoops() > -1) && loopCount >= getLoops(); 112 } 113 114 117 protected Sampler nextIsNull() throws NextIsNullException 118 { 119 reInitialize(); 120 if (endOfLoop()) 121 { 122 if (!getContinueForever()) 123 { 124 setDone(true); 125 } 126 else 127 { 128 resetLoopCount(); 129 } 130 return null; 131 } 132 else 133 { 134 return next(); 135 } 136 } 137 138 protected void incrementLoopCount() 139 { 140 loopCount++; 141 } 142 143 protected void resetLoopCount() 144 { 145 loopCount = 0; 146 } 147 148 151 protected int getIterCount() 152 { 153 return loopCount + 1; 154 } 155 156 159 protected void reInitialize() 160 { 161 setFirst(true); 162 resetCurrent(); 163 incrementLoopCount(); 164 } 165 166 168 public static class Test extends JMeterTestCase 169 { 170 public Test(String name) 171 { 172 super(name); 173 } 174 175 public void testProcessing() throws Exception 176 { 177 GenericController controller = new GenericController(); 178 GenericController sub_1 = new GenericController(); 179 sub_1.addTestElement(new TestSampler("one")); 180 sub_1.addTestElement(new TestSampler("two")); 181 controller.addTestElement(sub_1); 182 controller.addTestElement(new TestSampler("three")); 183 LoopController sub_2 = new LoopController(); 184 sub_2.setLoops(3); 185 GenericController sub_3 = new GenericController(); 186 sub_2.addTestElement(new TestSampler("four")); 187 sub_3.addTestElement(new TestSampler("five")); 188 sub_3.addTestElement(new TestSampler("six")); 189 sub_2.addTestElement(sub_3); 190 sub_2.addTestElement(new TestSampler("seven")); 191 controller.addTestElement(sub_2); 192 String [] order = 193 new String [] { 194 "one", 195 "two", 196 "three", 197 "four", 198 "five", 199 "six", 200 "seven", 201 "four", 202 "five", 203 "six", 204 "seven", 205 "four", 206 "five", 207 "six", 208 "seven" }; 209 int counter = 15; 210 controller.initialize(); 211 for (int i = 0; i < 2; i++) 212 { 213 assertEquals(15, counter); 214 counter = 0; 215 TestElement sampler = null; 216 while ((sampler = controller.next()) != null) 217 { 218 assertEquals( 219 order[counter++], 220 sampler.getPropertyAsString(TestElement.NAME)); 221 } 222 } 223 } 224 225 public void testLoopZeroTimes() throws Exception 226 { 227 LoopController loop = new LoopController(); 228 loop.setLoops(0); 229 loop.addTestElement(new TestSampler("never run")); 230 loop.initialize(); 231 assertNull(loop.next()); 232 } 233 234 public void testInfiniteLoop() throws Exception 235 { 236 LoopController loop = new LoopController(); 237 loop.setLoops(-1); 238 loop.addTestElement(new TestSampler("never run")); 239 loop.initialize(); 240 for (int i=0; i<42; i++) 241 { 242 assertNotNull(loop.next()); 243 } 244 } 245 246 } 247 } | Popular Tags |