1 18 19 package org.apache.jmeter.assertions; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 24 import org.apache.jmeter.samplers.SampleResult; 25 import org.apache.jmeter.testelement.AbstractTestElement; 26 import org.apache.jmeter.util.BeanShellInterpreter; 27 import org.apache.jmeter.util.JMeterUtils; 28 import org.apache.jorphan.logging.LoggingManager; 29 import org.apache.jorphan.util.JMeterException; 30 import org.apache.jorphan.util.JOrphanUtils; 31 import org.apache.log.Logger; 32 33 38 public class BeanShellAssertion extends AbstractTestElement 39 implements Serializable , Assertion 40 { 41 private static Logger log = LoggingManager.getLoggerForClass(); 42 43 public static final String FILENAME = "BeanShellAssertion.filename"; public static final String SCRIPT = "BeanShellAssertion.query"; public static final String PARAMETERS = "BeanShellAssertion.parameters"; 47 public static final String INIT_FILE = "beanshell.assertion.init"; 50 transient private BeanShellInterpreter bshInterpreter = null; 51 52 public BeanShellAssertion() 53 { 54 try { 55 bshInterpreter = new BeanShellInterpreter(); 56 String init = JMeterUtils.getProperty(INIT_FILE); 57 try { 58 bshInterpreter.init(init,log); 59 } catch (IOException e) { 60 log.warn("Could not initialise interpreter",e); 61 } catch (JMeterException e) { 62 log.warn("Could not initialise interpreter",e); 63 } 64 } catch (ClassNotFoundException e) { 65 log.error("Could not establish BeanShellInterpreter: "+e); 66 } 67 } 68 69 public String getScript() 70 { 71 return getPropertyAsString(SCRIPT); 72 } 73 74 public String getFilename() 75 { 76 return getPropertyAsString(FILENAME); 77 } 78 79 public String getParameters() 80 { 81 return getPropertyAsString(PARAMETERS); 82 } 83 84 87 public AssertionResult getResult(SampleResult response) 88 { 89 AssertionResult result = new AssertionResult(); 90 91 if (bshInterpreter == null){ 92 result.setFailure(true); 93 result.setError(true); 94 result.setFailureMessage("BeanShell Interpreter not found"); 95 return result; 96 } 97 try 98 { 99 String request=getScript(); 100 String fileName=getFilename(); 101 102 bshInterpreter.set("FileName",getFilename()); bshInterpreter.set("Parameters",getParameters()); bshInterpreter.set("bsh.args", JOrphanUtils.split(getParameters()," ")); 107 bshInterpreter.set("SampleResult",response); bshInterpreter.set("Response",response); bshInterpreter.set("ResponseData",response.getResponseData()); bshInterpreter.set("ResponseCode",response.getResponseCode()); bshInterpreter.set("ResponseMessage",response.getResponseMessage()); bshInterpreter.set("ResponseHeaders",response.getResponseHeaders()); bshInterpreter.set("RequestHeaders",response.getRequestHeaders()); bshInterpreter.set("SampleLabel",response.getSampleLabel()); bshInterpreter.set("SamplerData",response.getSamplerData()); bshInterpreter.set("Successful",response.isSuccessful()); 119 bshInterpreter.set("FailureMessage",""); bshInterpreter.set("Failure",false); 123 125 if (fileName.length() == 0){ 126 bshInterpreter.eval(request); 128 } else { 129 bshInterpreter.source(fileName); 131 } 132 133 result.setFailureMessage(bshInterpreter.get("FailureMessage").toString()); result.setFailure(Boolean.valueOf(bshInterpreter.get("Failure") .toString()).booleanValue()); 136 result.setError(false); 137 } 138 148 catch(NoClassDefFoundError ex){ 150 log.error("BeanShell Jar missing? "+ex.toString()); 151 result.setError(true); 152 result.setFailureMessage("BeanShell Jar missing? "+ex.toString()); 153 response.setStopThread(true); } 155 catch (Exception ex) { 157 result.setError(true); 158 result.setFailureMessage(ex.toString()); 159 log.warn(ex.toString()); 160 } 161 162 return result; 163 } 164 } | Popular Tags |