KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > assertions > BeanShellAssertion


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/BeanShellAssertion.java,v 1.3.2.6 2005/03/07 00:26:56 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.assertions;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
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 /**
34  * A sampler which understands BeanShell
35  *
36  * @version $Revision: 1.3.2.6 $ Updated on: $Date: 2005/03/07 00:26:56 $
37  */

38 public class BeanShellAssertion extends AbstractTestElement
39     implements Serializable JavaDoc, Assertion
40 {
41     private static Logger log = LoggingManager.getLoggerForClass();
42
43     public static final String JavaDoc FILENAME = "BeanShellAssertion.filename"; //$NON-NLS-1$
44
public static final String JavaDoc SCRIPT = "BeanShellAssertion.query"; //$NON-NLS-1$
45
public static final String JavaDoc PARAMETERS = "BeanShellAssertion.parameters"; //$NON-NLS-1$
46

47     // can be specified in jmeter.properties
48
public static final String JavaDoc INIT_FILE = "beanshell.assertion.init"; //$NON-NLS-1$
49

50     transient private BeanShellInterpreter bshInterpreter = null;
51     
52     public BeanShellAssertion()
53     {
54         try {
55             bshInterpreter = new BeanShellInterpreter();
56             String JavaDoc init = JMeterUtils.getProperty(INIT_FILE);
57             try {
58                 bshInterpreter.init(init,log);
59             } catch (IOException JavaDoc 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 JavaDoc e) {
65             log.error("Could not establish BeanShellInterpreter: "+e);
66         }
67     }
68
69     public String JavaDoc getScript()
70     {
71         return getPropertyAsString(SCRIPT);
72     }
73     
74     public String JavaDoc getFilename()
75     {
76         return getPropertyAsString(FILENAME);
77     }
78
79     public String JavaDoc getParameters()
80     {
81         return getPropertyAsString(PARAMETERS);
82     }
83
84         /* (non-Javadoc)
85          * @see org.apache.jmeter.assertions.Assertion#getResult(org.apache.jmeter.samplers.SampleResult)
86          */

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 JavaDoc request=getScript();
100             String JavaDoc fileName=getFilename();
101             
102             bshInterpreter.set("FileName",getFilename());//$NON-NLS-1$
103
bshInterpreter.set("Parameters",getParameters());// as a single line $NON-NLS-1$
104
bshInterpreter.set("bsh.args",//$NON-NLS-1$
105
JOrphanUtils.split(getParameters()," "));//$NON-NLS-1$
106

107             // Add SamplerData for consistency with BeanShell Sampler
108
bshInterpreter.set("SampleResult",response);// Raw access to the response //$NON-NLS-1$
109
bshInterpreter.set("Response",response);// Raw access to the response //$NON-NLS-1$
110
bshInterpreter.set("ResponseData",response.getResponseData());//$NON-NLS-1$
111
bshInterpreter.set("ResponseCode",response.getResponseCode());//$NON-NLS-1$
112
bshInterpreter.set("ResponseMessage",response.getResponseMessage());//$NON-NLS-1$
113
bshInterpreter.set("ResponseHeaders",response.getResponseHeaders());//$NON-NLS-1$
114
bshInterpreter.set("RequestHeaders",response.getRequestHeaders());//$NON-NLS-1$
115
bshInterpreter.set("SampleLabel",response.getSampleLabel());//$NON-NLS-1$
116
bshInterpreter.set("SamplerData",response.getSamplerData());//$NON-NLS-1$
117
bshInterpreter.set("Successful",response.isSuccessful());//$NON-NLS-1$
118

119             // The following are used to set the Result details on return from the script:
120
bshInterpreter.set("FailureMessage","");//$NON-NLS-1$ //$NON-NLS-2$
121
bshInterpreter.set("Failure",false);//$NON-NLS-1$
122

123             //Object bshOut;
124

125             if (fileName.length() == 0){
126                 //bshOut =
127
bshInterpreter.eval(request);
128             } else {
129                 //bshOut =
130
bshInterpreter.source(fileName);
131             }
132             
133             result.setFailureMessage(bshInterpreter.get("FailureMessage").toString());//$NON-NLS-1$
134
result.setFailure(Boolean.valueOf(bshInterpreter.get("Failure") //$NON-NLS-1$
135
.toString()).booleanValue());
136             result.setError(false);
137         }
138 /*
139  * To avoid class loading problems when the BSH jar is missing,
140  * we don't try to catch this error separately
141  * catch (bsh.EvalError ex)
142         {
143             log.debug("",ex);
144             result.setError(true);
145             result.setFailureMessage(ex.toString());
146         }
147  */

148         // but we do trap this error to make tests work better
149
catch(NoClassDefFoundError JavaDoc 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); // No point continuing
154
}
155         catch (Exception JavaDoc ex) // Mainly for bsh.EvalError
156
{
157             result.setError(true);
158             result.setFailureMessage(ex.toString());
159             log.warn(ex.toString());
160         }
161
162         return result;
163     }
164 }
Popular Tags