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