View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.propertyexpansion.PropertyExpansion;
23  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContainer;
24  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionsResult;
25  import com.eviware.soapui.model.support.DefaultTestStepProperty;
26  import com.eviware.soapui.model.support.TestStepBeanProperty;
27  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
28  import com.eviware.soapui.model.testsuite.TestCaseRunner;
29  import com.eviware.soapui.model.testsuite.TestStepResult;
30  import com.eviware.soapui.model.testsuite.TestRunner.Status;
31  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
34  import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
35  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
36  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
37  
38  /***
39   * TestStep that executes an arbitraty Groovy script
40   * 
41   * @author ole.matzura
42   */
43  
44  public class WsdlGroovyScriptTestStep extends WsdlTestStepWithProperties implements PropertyExpansionContainer
45  {
46  	private final static Logger logger = Logger.getLogger( "groovy.log" );
47  	private String scriptText = "";
48  	private Object scriptResult;
49  	private ImageIcon failedIcon;
50  	private ImageIcon okIcon;
51  	private SoapUIScriptEngine scriptEngine;
52  
53  	public WsdlGroovyScriptTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
54  	{
55  		super( testCase, config, true, forLoadTest );
56  
57  		if( !forLoadTest )
58  		{
59  			okIcon = UISupport.createImageIcon( "/groovy_script.gif" );
60  			setIcon( okIcon );
61  			failedIcon = UISupport.createImageIcon( "/groovy_script_failed.gif" );
62  		}
63  
64  		if( config.getConfig() == null )
65  		{
66  			if( !forLoadTest )
67  				saveScript( config );
68  		}
69  		else
70  		{
71  			readConfig( config );
72  		}
73  
74  		addProperty( new DefaultTestStepProperty( "result", true, new DefaultTestStepProperty.PropertyHandlerAdapter()
75  		{
76  
77  			public String getValue( DefaultTestStepProperty property )
78  			{
79  				return scriptResult == null ? null : scriptResult.toString();
80  			}
81  		}, this ) );
82  
83  		addProperty( new TestStepBeanProperty( "script", false, this, "script", this ) );
84  
85  		scriptEngine = SoapUIScriptEngineRegistry.create( this );
86  		scriptEngine.setScript( getScript() );
87  		if( forLoadTest && !isDisabled() )
88  			try
89  			{
90  				scriptEngine.compile();
91  			}
92  			catch( Exception e )
93  			{
94  				SoapUI.logError( e );
95  			}
96  	}
97  
98  	public Logger getLogger()
99  	{
100 		SoapUI.ensureGroovyLog();
101 		return logger;
102 	}
103 
104 	private void readConfig( TestStepConfig config )
105 	{
106 		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config.getConfig() );
107 		scriptText = reader.readString( "script", "" );
108 	}
109 
110 	private void saveScript( TestStepConfig config )
111 	{
112 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
113 		builder.add( "script", scriptText );
114 		config.setConfig( builder.finish() );
115 	}
116 
117 	public void resetConfigOnMove( TestStepConfig config )
118 	{
119 		super.resetConfigOnMove( config );
120 		readConfig( config );
121 	}
122 
123 	public String getDefaultSourcePropertyName()
124 	{
125 		return "result";
126 	}
127 
128 	public TestStepResult run( TestCaseRunner testRunner, TestCaseRunContext context )
129 	{
130 		SoapUI.ensureGroovyLog();
131 
132 		WsdlTestStepResult result = new WsdlTestStepResult( this );
133 		Logger log = ( Logger )context.getProperty( "log" );
134 		if( log == null )
135 			log = logger;
136 
137 		try
138 		{
139 			if( scriptText.trim().length() > 0 )
140 				synchronized( this )
141 				{
142 					scriptEngine.setVariable( "context", context );
143 					scriptEngine.setVariable( "testRunner", testRunner );
144 					scriptEngine.setVariable( "log", log );
145 
146 					result.setTimeStamp( System.currentTimeMillis() );
147 					result.startTimer();
148 					scriptResult = scriptEngine.run();
149 					result.stopTimer();
150 
151 					if( scriptResult != null )
152 						result.addMessage( "Script-result: " + scriptResult.toString() );
153 				}
154 
155 			// testRunner status may have been changed by script..
156 			Status testRunnerStatus = testRunner.getStatus();
157 			if( testRunnerStatus == Status.FAILED )
158 				result.setStatus( TestStepStatus.FAILED );
159 			else if( testRunnerStatus == Status.CANCELED )
160 				result.setStatus( TestStepStatus.CANCELED );
161 			else
162 				result.setStatus( TestStepStatus.OK );
163 		}
164 		catch( Throwable e )
165 		{
166 			SoapUI.logError( e );
167 			result.stopTimer();
168 			result.addMessage( e.getMessage() );
169 			result.setError( e );
170 			result.setStatus( TestStepStatus.FAILED );
171 		}
172 		finally
173 		{
174 			if( !isForLoadTest() )
175 				setIcon( result.getStatus() == TestStepStatus.FAILED ? failedIcon : okIcon );
176 
177 			if( scriptEngine != null )
178 				scriptEngine.clearVariables();
179 		}
180 
181 		return result;
182 	}
183 
184 	public String getScript()
185 	{
186 		return scriptText;
187 	}
188 
189 	public void setScript( String scriptText )
190 	{
191 		if( scriptText.equals( this.scriptText ) )
192 			return;
193 
194 		String oldScript = this.scriptText;
195 		this.scriptText = scriptText;
196 		scriptEngine.setScript( scriptText );
197 		saveScript( getConfig() );
198 
199 		notifyPropertyChanged( "script", oldScript, scriptText );
200 	}
201 
202 	@Override
203 	public void release()
204 	{
205 		super.release();
206 		scriptEngine.release();
207 	}
208 
209 	public PropertyExpansion[] getPropertyExpansions()
210 	{
211 		PropertyExpansionsResult result = new PropertyExpansionsResult( this );
212 
213 		result.extractAndAddAll( "script" );
214 
215 		return result.toArray();
216 	}
217 }