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.samplers.Sampler; 25 import org.apache.jmeter.testelement.TestElement; 26 import org.apache.jmeter.testelement.property.StringProperty; 27 import org.apache.jorphan.logging.LoggingManager; 28 import org.apache.log.Logger; 29 import org.mozilla.javascript.Context; 30 import org.mozilla.javascript.Scriptable; 31 32 53 54 public class IfController extends GenericController implements Serializable 55 { 56 57 private static Logger logger = 58 LoggingManager.getLoggerForClass(); 59 private final static String CONDITION = "IfController.condition"; 60 61 64 public IfController() { 65 super(); 66 } 67 68 71 public IfController(String condition) { 72 super(); 73 this.setCondition(condition); 74 } 75 76 79 public void setCondition(String condition) { 80 setProperty(new StringProperty(CONDITION, condition)); 81 } 82 83 86 public String getCondition() { 87 return getPropertyAsString(CONDITION); 88 } 89 90 94 static boolean evaluateCondition(String cond) { 95 logger.debug(" getCondition() : [" + cond + "]"); 96 97 String resultStr = ""; 98 boolean result = false; 99 100 Context cx = Context.enter(); 102 try { 103 Scriptable scope = cx.initStandardObjects(null); 104 Object cxResultObject = 105 cx.evaluateString(scope, cond 106 107 , "<cmd>", 1, null); 108 resultStr = Context.toString(cxResultObject); 109 110 if (resultStr.equals("false")) { 111 result = false; 112 } else if (resultStr.equals("true")) { 113 result = true; 114 } else { 115 throw new Exception (" BAD CONDITION :: " + cond); 116 } 117 118 logger.debug( 119 " >> evaluate Condition - [ " 120 + cond 121 + "] results is [" 122 + result 123 + "]"); 124 } catch (Exception e) { 125 logger.error(e.getMessage(),e); 126 } finally { 127 Context.exit(); 128 } 129 130 return result; 131 } 132 133 138 public boolean isDone() { 139 return false; 149 } 150 151 162 public Sampler next() 163 { 164 getProperty(CONDITION).recoverRunningVersion(null); 166 boolean result = evaluateCondition(getCondition()); 167 if (result) 168 return super.next(); 169 else try 170 { 171 return nextIsNull(); 172 } 173 catch (NextIsNullException e1) 174 { 175 return null; 176 } 177 } 178 179 181 184 public static class Test extends JMeterTestCase { 185 public Test(String name) { 186 super(name); 187 } 188 189 public void testProcessing() throws Exception { 190 191 GenericController controller = new GenericController(); 192 193 controller.addTestElement(new IfController("false==false")); 194 controller.addTestElement(new IfController(" \"a\".equals(\"a\")")); 195 controller.addTestElement(new IfController("2<100")); 196 197 202 203 222 223 226 logger.debug(">>>>> testProcessing : Starting the iteration "); 227 TestElement sampler = null; 228 while ((sampler = controller.next()) != null) { 229 logger.debug( 230 " ->>> Gonna assertTrue :" 231 + sampler.getClass().getName() 232 + " Property is ---->>>" 233 + sampler.getPropertyAsString(TestElement.NAME)); 234 } 235 } 236 } 237 238 } | Popular Tags |