001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.assertions;
020:
021: import org.apache.jmeter.samplers.SampleResult;
022: import org.apache.jmeter.threads.JMeterContext;
023: import org.apache.jmeter.threads.JMeterContextService;
024: import org.apache.jmeter.threads.JMeterVariables;
025: import org.apache.jmeter.util.BeanShellInterpreter;
026: import org.apache.jmeter.util.BeanShellTestElement;
027: import org.apache.jorphan.logging.LoggingManager;
028: import org.apache.jorphan.util.JOrphanUtils;
029: import org.apache.log.Logger;
030:
031: /**
032: * A sampler which understands BeanShell
033: *
034: */
035: public class BeanShellAssertion extends BeanShellTestElement implements
036: Assertion {
037: private static final Logger log = LoggingManager
038: .getLoggerForClass();
039:
040: private static final long serialVersionUID = 3;
041:
042: public static final String FILENAME = "BeanShellAssertion.filename"; //$NON-NLS-1$
043:
044: public static final String SCRIPT = "BeanShellAssertion.query"; //$NON-NLS-1$
045:
046: public static final String PARAMETERS = "BeanShellAssertion.parameters"; //$NON-NLS-1$
047:
048: // can be specified in jmeter.properties
049: public static final String INIT_FILE = "beanshell.assertion.init"; //$NON-NLS-1$
050:
051: protected String getInitFileProperty() {
052: return INIT_FILE;
053: }
054:
055: public String getScript() {
056: return getPropertyAsString(SCRIPT);
057: }
058:
059: public String getFilename() {
060: return getPropertyAsString(FILENAME);
061: }
062:
063: public String getParameters() {
064: return getPropertyAsString(PARAMETERS);
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.apache.jmeter.assertions.Assertion#getResult(org.apache.jmeter.samplers.SampleResult)
071: */
072: public AssertionResult getResult(SampleResult response) {
073: AssertionResult result = new AssertionResult(getName());
074:
075: final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
076: if (bshInterpreter == null) {
077: result.setFailure(true);
078: result.setError(true);
079: result.setFailureMessage("BeanShell Interpreter not found");
080: return result;
081: }
082: try {
083: String request = getScript();
084: String fileName = getFilename();
085:
086: bshInterpreter.set("FileName", getFilename());//$NON-NLS-1$
087: // Set params as a single line
088: bshInterpreter.set("Parameters", getParameters()); // $NON-NLS-1$
089: bshInterpreter.set("bsh.args",//$NON-NLS-1$
090: JOrphanUtils.split(getParameters(), " "));//$NON-NLS-1$
091:
092: // Add SamplerData for consistency with BeanShell Sampler
093: bshInterpreter.set("SampleResult", response); //$NON-NLS-1$
094: bshInterpreter.set("Response", response); //$NON-NLS-1$
095: bshInterpreter.set(
096: "ResponseData", response.getResponseData());//$NON-NLS-1$
097: bshInterpreter.set(
098: "ResponseCode", response.getResponseCode());//$NON-NLS-1$
099: bshInterpreter.set(
100: "ResponseMessage", response.getResponseMessage());//$NON-NLS-1$
101: bshInterpreter.set(
102: "ResponseHeaders", response.getResponseHeaders());//$NON-NLS-1$
103: bshInterpreter.set(
104: "RequestHeaders", response.getRequestHeaders());//$NON-NLS-1$
105: bshInterpreter
106: .set("SampleLabel", response.getSampleLabel());//$NON-NLS-1$
107: bshInterpreter
108: .set("SamplerData", response.getSamplerData());//$NON-NLS-1$
109: bshInterpreter.set("Successful", response.isSuccessful());//$NON-NLS-1$
110:
111: // The following are used to set the Result details on return from
112: // the script:
113: bshInterpreter.set("FailureMessage", "");//$NON-NLS-1$ //$NON-NLS-2$
114: bshInterpreter.set("Failure", false);//$NON-NLS-1$
115:
116: // Add variables for access to context and variables
117: JMeterContext jmctx = JMeterContextService.getContext();
118: JMeterVariables vars = jmctx.getVariables();
119: bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
120: bshInterpreter.set("vars", vars);//$NON-NLS-1$
121:
122: // Object bshOut;
123:
124: if (fileName.length() == 0) {
125: // bshOut =
126: bshInterpreter.eval(request);
127: } else {
128: // bshOut =
129: bshInterpreter.source(fileName);
130: }
131:
132: result.setFailureMessage(bshInterpreter.get(
133: "FailureMessage").toString());//$NON-NLS-1$
134: result.setFailure(Boolean.valueOf(
135: bshInterpreter.get("Failure") //$NON-NLS-1$
136: .toString()).booleanValue());
137: result.setError(false);
138: }
139: /*
140: * To avoid class loading problems when the BSH jar is missing, we don't
141: * try to catch this error separately catch (bsh.EvalError ex) {
142: * log.debug("",ex); result.setError(true);
143: * result.setFailureMessage(ex.toString()); }
144: */
145: // but we do trap this error to make tests work better
146: catch (NoClassDefFoundError ex) {
147: log.error("BeanShell Jar missing? " + ex.toString());
148: result.setError(true);
149: result.setFailureMessage("BeanShell Jar missing? "
150: + ex.toString());
151: response.setStopThread(true); // No point continuing
152: } catch (Exception ex) // Mainly for bsh.EvalError
153: {
154: result.setError(true);
155: result.setFailureMessage(ex.toString());
156: log.warn(ex.toString());
157: }
158:
159: return result;
160: }
161: }
|