View Javadoc

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