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  
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( SoapUIScriptEngineRegistry.GROOVY_ID, 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( TestRunner testRunner, TestRunContext 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          log.error( 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 }
218