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.StringProperty; 28 import org.apache.jmeter.threads.JMeterContextService; 29 import org.apache.jmeter.threads.JMeterThread; 30 import org.apache.jmeter.threads.JMeterVariables; 31 import org.apache.jorphan.logging.LoggingManager; 32 import org.apache.log.Logger; 33 34 37 public class WhileController extends GenericController implements Serializable 38 { 39 private static Logger log = LoggingManager.getLoggerForClass(); 40 private final static String CONDITION = "WhileController.condition"; 42 private static boolean testMode=false; private static boolean testModeResult=false; 45 public WhileController() 46 { 47 } 48 49 50 53 public boolean isDone() 54 { 55 if (getSubControllers().size() == 0) { 57 return true; 58 } 59 return false; } 61 62 73 private boolean endOfLoop(boolean loopEnd) 74 { 75 getProperty(CONDITION).recoverRunningVersion(null); 77 String cnd = getCondition(); 78 log.debug("Condition string:"+cnd); 79 boolean res; 80 if ((loopEnd && cnd.length() == 0) 82 || "LAST".equalsIgnoreCase(cnd)) { if (testMode) { 84 res=!testModeResult; 85 } else { 86 JMeterVariables threadVars = 87 JMeterContextService.getContext().getVariables(); 88 res = "false".equalsIgnoreCase(threadVars.get(JMeterThread.LAST_SAMPLE_OK)); } 91 } else { 92 res = "false".equalsIgnoreCase(cnd); } 95 log.debug("Condition value: "+res); 96 return res; 97 } 98 99 103 protected Sampler nextIsNull() throws NextIsNullException 104 { 105 reInitialize(); 106 if (!endOfLoop(true)) 107 { 108 return super.next(); 109 } 110 else 111 { 112 setDone(true); 113 return null; 114 } 115 } 116 117 public Sampler next() 118 { 119 if (current!=0){ return super.next(); 121 } 122 if(!endOfLoop(false)) 124 { 125 return super.next(); } 127 else 128 { 129 reInitialize(); return null; 131 } 132 } 133 134 137 public void setCondition(String string) { 138 log.debug("setCondition("+ string+")"); 139 setProperty(new StringProperty(CONDITION, string)); 140 } 141 142 145 public String getCondition() { 146 String cnd; 147 cnd=getPropertyAsString(CONDITION); 148 log.debug("getCondition() => "+cnd); 149 return cnd; 150 } 151 public static class Test extends JMeterTestCase 152 { 153 static{ 154 } 157 158 public Test(String name) 159 { 160 super(name); 161 } 162 163 private String nextName(GenericController c){ 165 Sampler s = c.next(); 166 if (s==null){ 167 return null; 168 } else { 169 return s.getPropertyAsString(TestElement.NAME); 170 } 171 } 172 173 public void testBlankPrevOK() throws Exception 175 { 176 log.info("testBlankPrevOK"); 177 runtestPrevOK(""); 178 } 179 180 public void testLastPrevOK() throws Exception 182 { 183 log.info("testLASTPrevOK"); 184 runtestPrevOK("LAST"); 185 } 186 187 private static final String OTHER = "X"; public void testOtherPrevOK() throws Exception 190 { 191 log.info("testOtherPrevOK"); 192 runtestPrevOK(OTHER); 193 } 194 195 public void runtestPrevOK(String type) throws Exception 196 { 197 testMode=true; 198 testModeResult=true; 199 GenericController controller = new GenericController(); 200 WhileController while_cont = new WhileController(); 201 while_cont.setCondition(type); 202 while_cont.addTestElement(new TestSampler("one")); 203 while_cont.addTestElement(new TestSampler("two")); 204 while_cont.addTestElement(new TestSampler("three")); 205 controller.addTestElement(while_cont); 206 controller.addTestElement(new TestSampler("four")); 207 controller.initialize(); 208 assertEquals("one",nextName(controller)); 209 assertEquals("two",nextName(controller)); 210 assertEquals("three",nextName(controller)); 211 assertEquals("one",nextName(controller)); 212 assertEquals("two",nextName(controller)); 213 assertEquals("three",nextName(controller)); 214 assertEquals("one",nextName(controller)); 215 testModeResult=false; if (type.equals(OTHER)) while_cont.setCondition("false"); 217 assertEquals("two",nextName(controller)); 218 assertEquals("three",nextName(controller)); 219 testModeResult=true; if (type.equals(OTHER)) while_cont.setCondition(OTHER); 221 assertEquals("one",nextName(controller)); 222 assertEquals("two",nextName(controller)); 223 assertEquals("three",nextName(controller)); 224 testModeResult=false; if (type.equals(OTHER)) while_cont.setCondition("false"); 226 assertEquals("four",nextName(controller)); 227 assertNull(nextName(controller)); 228 testModeResult=true; 229 if (type.equals(OTHER)) while_cont.setCondition(OTHER); 230 assertEquals("one",nextName(controller)); 231 } 232 233 public void testBlankPrevFailed() throws Exception 235 { 236 log.info("testBlankPrevFailed"); 237 testMode=true; 238 testModeResult=false; 239 GenericController controller = new GenericController(); 240 WhileController while_cont = new WhileController(); 241 while_cont.setCondition(""); 242 while_cont.addTestElement(new TestSampler("one")); 243 while_cont.addTestElement(new TestSampler("two")); 244 controller.addTestElement(while_cont); 245 controller.addTestElement(new TestSampler("three")); 246 controller.initialize(); 247 assertEquals("one",nextName(controller)); 248 assertEquals("two",nextName(controller)); 249 assertEquals("three",nextName(controller)); 250 assertNull(nextName(controller)); 251 assertEquals("one",nextName(controller)); 252 assertEquals("two",nextName(controller)); 253 assertEquals("three",nextName(controller)); 254 assertNull(nextName(controller)); 255 } 256 257 public void testLASTPrevFailed() throws Exception 259 { 260 log.info("testLastPrevFailed"); 261 runTestPrevFailed("LAST"); 262 } 263 public void testfalsePrevFailed() throws Exception 265 { 266 log.info("testFalsePrevFailed"); 267 runTestPrevFailed("False"); 268 } 269 public void runTestPrevFailed(String s) throws Exception 270 { 271 testMode=true; 272 testModeResult=false; 273 GenericController controller = new GenericController(); 274 WhileController while_cont = new WhileController(); 275 while_cont.setCondition(s); 276 while_cont.addTestElement(new TestSampler("one")); 277 while_cont.addTestElement(new TestSampler("two")); 278 controller.addTestElement(while_cont); 279 controller.addTestElement(new TestSampler("three")); 280 controller.initialize(); 281 assertEquals("three",nextName(controller)); 282 assertNull(nextName(controller)); 283 assertEquals("three",nextName(controller)); 284 assertNull(nextName(controller)); 285 } 286 287 public void testAlwaysFailOK() throws Exception 289 { 290 runTestAlwaysFail(true); } 292 293 public void disabletestAlwaysFailBAD() throws Exception 295 { 296 runTestAlwaysFail(false); } 298 299 public void runTestAlwaysFail(boolean other) 300 { 301 testMode=true; 302 testModeResult=false; 303 LoopController controller = new LoopController(); 304 controller.setContinueForever(true); 305 controller.setLoops(-1); 306 WhileController while_cont = new WhileController(); 307 while_cont.setCondition("false"); 308 while_cont.addTestElement(new TestSampler("one")); 309 while_cont.addTestElement(new TestSampler("two")); 310 controller.addTestElement(while_cont); 311 if (other) controller.addTestElement(new TestSampler("three")); 312 controller.initialize(); 313 try { 314 if (other){ 315 assertEquals("three",nextName(controller)); 316 } else { 317 assertNull(nextName(controller)); 318 } 319 } catch (StackOverflowError e){ 320 fail(e.toString()); 322 } 323 } 324 325 } 326 } | Popular Tags |