Package org.apache.jmeter.assertions

Source Code of org.apache.jmeter.assertions.BeanShellAssertion

// $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/BeanShellAssertion.java,v 1.3.2.4 2004/10/08 23:20:10 sebb Exp $
/*
* Copyright 2003-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.jmeter.assertions;

import java.io.IOException;
import java.io.Serializable;

//import bsh.EvalError;
import bsh.Interpreter;
  
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.log.Logger;

/**
* A sampler which understands BeanShell
*
* @version    $Revision: 1.3.2.4 $ Updated on: $Date: 2004/10/08 23:20:10 $
*/
public class BeanShellAssertion extends AbstractTestElement
    implements Serializable, Assertion
{
  private static Logger log = LoggingManager.getLoggerForClass();

    public static final String FILENAME   = "BeanShellAssertion.filename"; //$NON-NLS-1$
  public static final String SCRIPT     = "BeanShellAssertion.query"; //$NON-NLS-1$
  public static final String PARAMETERS = "BeanShellAssertion.parameters"; //$NON-NLS-1$

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

    private Interpreter bshInterpreter;
 
  public BeanShellAssertion()
  {
    String init="";
    try{
      bshInterpreter = new Interpreter();
      init = JMeterUtils.getPropDefault(INIT_FILE,null);
      if (init != null)
      {
        try
        {
           bshInterpreter.source(null);
        } catch (IOException e){
          log.warn("Error processing init file "+init+" "+e);
        } catch (Exception e){
          log.warn("Error processing init file "+init+" "+e);
        }
      }
    } catch (NoClassDefFoundError e){
      bshInterpreter=null;
    }
  }

  public String getScript()
  {
    return getPropertyAsString(SCRIPT);
  }
   
  public String getFilename()
  {
    return getPropertyAsString(FILENAME);
  }

  public String getParameters()
  {
    return getPropertyAsString(PARAMETERS);
  }

    /* (non-Javadoc)
     * @see org.apache.jmeter.assertions.Assertion#getResult(org.apache.jmeter.samplers.SampleResult)
     */
  public AssertionResult getResult(SampleResult response)
  {
    AssertionResult result = new AssertionResult();
   
    try
        {
          String request=getScript();
          String fileName=getFilename();
         
          // Has to be done after construction, otherwise fails serialisation check
          bshInterpreter.set("log",log)//$NON-NLS-1$
     
          bshInterpreter.set("FileName",getFilename());//$NON-NLS-1$
      bshInterpreter.set("Parameters",getParameters());// as a single line $NON-NLS-1$
      bshInterpreter.set("bsh.args",//$NON-NLS-1$
          JOrphanUtils.split(getParameters()," "));//$NON-NLS-1$
     
      bshInterpreter.set("Response",response);// Raw access to the response //$NON-NLS-1$
      bshInterpreter.set("ResponseData",response.getResponseData());//$NON-NLS-1$
      bshInterpreter.set("ResponseCode",response.getResponseCode());//$NON-NLS-1$
      bshInterpreter.set("ResponseMessage",response.getResponseMessage());//$NON-NLS-1$
      bshInterpreter.set("ResponseHeaders",response.getResponseHeaders());//$NON-NLS-1$
      bshInterpreter.set("RequestHeaders",response.getRequestHeaders());//$NON-NLS-1$
      bshInterpreter.set("SampleLabel",response.getSampleLabel());//$NON-NLS-1$
      bshInterpreter.set("SamplerData",response.getSamplerData());//$NON-NLS-1$
      bshInterpreter.set("Successful",response.isSuccessful());//$NON-NLS-1$

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

      //Object bshOut;
     
      if (fileName.length() == 0){
        //bshOut =
        bshInterpreter.eval(request);
      } else {
        //bshOut =
        bshInterpreter.source(fileName);
      }
     
          result.setFailureMessage(bshInterpreter.get("FailureMessage").toString());//$NON-NLS-1$
          result.setFailure(Boolean.valueOf(bshInterpreter.get("Failure") //$NON-NLS-1$
              .toString()).booleanValue());
      result.setError(false);
        }
/*
* To avoid class loading problems when the BSH jar is missing,
* we don't try to catch this error separately
*     catch (bsh.EvalError ex)
    {
      log.debug("",ex);
      result.setError(true);
      result.setFailureMessage(ex.toString());
    }
*/
        // but we do trap this error to make tests work better
        catch(NoClassDefFoundError ex){
      log.error("BeanShell Jar missing? "+ex.toString());
      result.setError(true);
      result.setFailureMessage("BeanShell Jar missing? "+ex.toString());
      response.setStopThread(true); // No point continuing
        }
    catch (IOException ex)
    {
      result.setError(true);
      result.setFailureMessage(ex.toString());
      log.warn(ex.toString());
    }
    catch (Exception ex) // Mainly for bsh.EvalError
    {
      result.setError(true);
      result.setFailureMessage(ex.toString());
      log.warn(ex.toString());
    }

        return result;
    }
}
TOP

Related Classes of org.apache.jmeter.assertions.BeanShellAssertion

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.