View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.impl.wsdl.teststeps;
    14
    15  import javax.swing.ImageIcon;
    16
    17  import org.apache.log4j.Logger;
    18
    19  import com.eviware.soapui.SoapUI;
    20  import com.eviware.soapui.config.TestStepConfig;
    21  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
    22  import com.eviware.soapui.model.support.DefaultTestStepProperty;
    23  import com.eviware.soapui.model.testsuite.TestRunContext;
    24  import com.eviware.soapui.model.testsuite.TestRunner;
    25  import com.eviware.soapui.model.testsuite.TestStepResult;
    26  import com.eviware.soapui.model.testsuite.TestRunner.Status;
    27  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
    28  import com.eviware.soapui.support.UISupport;
    29  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
    30  import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
    31  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
    32  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
    33
    34  /***
    35   * TestStep that executes an arbitraty Groovy script
    36   *
    37   * @author ole.matzura
    38   */
    39
    40  public class WsdlGroovyScriptTestStep extends WsdlTestStep
    41  {
    42  	private final static Logger logger = Logger.getLogger( "groovy.log" );
    43  	private String scriptText = "";
    44  	private Object scriptResult;
    45  	private ImageIcon failedIcon;
    46  	private ImageIcon okIcon;
    47  	private SoapUIScriptEngine	scriptEngine;
    48
    49  	public WsdlGroovyScriptTestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
    50  	{
    51  		super(testCase, config, true, forLoadTest);
    52
    53  		if( !forLoadTest )
    54  		{
    55  			okIcon = UISupport.createImageIcon("/groovy_script.gif");
    56  			setIcon( okIcon );
    57  			failedIcon = UISupport.createImageIcon( "/groovy_script_failed.gif" );
    58  		}
    59
    60  		if( config.getConfig() == null )
    61  		{
    62  			if( !forLoadTest )
    63  				saveScript( config );
    64  		}
    65  		else
    66  		{
    67  			readConfig( config );
    68  		}
    69
    70  		addProperty( new DefaultTestStepProperty( "result", true,
    71  				new DefaultTestStepProperty.PropertyHandlerAdapter() {
    72
    73  			public String getValue()
    74  			{
    75  				return scriptResult == null ? null : scriptResult.toString();
    76  			}
    77  		}, this ));
    78
    79  		scriptEngine = SoapUIScriptEngineRegistry.create( SoapUIScriptEngineRegistry.GROOVY_ID, this );
    80  		scriptEngine.setScript( getScript() );
    81  		if( forLoadTest && !isDisabled() )
    82  			try
    83  			{
    84  				scriptEngine.compile();
    85  			}
    86  			catch( Exception e )
    87  			{
    88  				SoapUI.logError( e );
    89  			}
    90  	}
    91
    92  	public Logger getLogger()
    93  	{
    94  		SoapUI.ensureGroovyLog();
    95  		return logger;
    96  	}
    97
    98  	private void readConfig(TestStepConfig config)
    99  	{
    100 		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
    101 		scriptText = reader.readString( "script", "" );
    102 	}
    103
    104 	private void saveScript(TestStepConfig config)
    105 	{
    106 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    107 		builder.add( "script", scriptText );
    108 		config.setConfig( builder.finish() );
    109 	}
    110
    111 	public void resetConfigOnMove(TestStepConfig config)
    112 	{
    113 		super.resetConfigOnMove( config );
    114 		readConfig( config );
    115 	}
    116
    117 	public TestStepResult run(TestRunner testRunner, TestRunContext context)
    118 	{
    119 		SoapUI.ensureGroovyLog();
    120
    121 		WsdlTestStepResult result = new WsdlTestStepResult( this );
    122 		Logger log = (Logger) context.getProperty( "log" );
    123 		if( log == null )
    124 			log = logger;
    125
    126 		try
    127 		{
    128 			if( scriptText.trim().length() > 0 )
    129 			synchronized( this )
    130 			{
    131 				scriptEngine.setVariable("context", context);
    132 				scriptEngine.setVariable("testRunner", testRunner);
    133 				scriptEngine.setVariable( "log", log);
    134
    135 				result.setTimeStamp( System.currentTimeMillis() );
    136 				result.startTimer();
    137 				scriptResult = scriptEngine.run();
    138 				result.stopTimer();
    139
    140 				if( scriptResult != null )
    141 					result.addMessage( "Script-result: " + scriptResult.toString() );
    142 			}
    143
    144 			// testRunner status may have been changed by script..
    145 			Status testRunnerStatus = testRunner.getStatus();
    146 			if( testRunnerStatus == Status.FAILED )
    147 				result.setStatus( TestStepStatus.FAILED );
    148 			else if( testRunnerStatus == Status.CANCELED )
    149 				result.setStatus( TestStepStatus.CANCELED );
    150 			else
    151 				result.setStatus( TestStepStatus.OK );
    152 		}
    153 		catch (Throwable e)
    154 		{
    155 			log.error( e );
    156 			result.stopTimer();
    157 			result.addMessage( e.getMessage() );
    158 			result.setError( e );
    159 			result.setStatus( TestStepStatus.FAILED );
    160 		}
    161 		finally
    162 		{
    163 			if( !isForLoadTest() )
    164 				setIcon( result.getStatus() == TestStepStatus.FAILED ? failedIcon : okIcon );
    165
    166 			if( scriptEngine != null )
    167 				scriptEngine.clearVariables();
    168 		}
    169
    170 		return result;
    171 	}
    172
    173 	public String getScript()
    174 	{
    175 		return scriptText;
    176 	}
    177
    178 	public void setScript( String scriptText )
    179 	{
    180 		if( scriptText.equals( this.scriptText ))
    181 			return;
    182
    183 		this.scriptText = scriptText;
    184 		scriptEngine.setScript( scriptText );
    185 		saveScript( getConfig() );
    186 	}
    187
    188 	@Override
    189 	public void release()
    190 	{
    191 		super.release();
    192 		scriptEngine.release();
    193 	}
    194 }
    195