001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps;
014:
015: import javax.swing.ImageIcon;
016:
017: import org.apache.log4j.Logger;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.config.TestStepConfig;
021: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
022: import com.eviware.soapui.model.support.DefaultTestStepProperty;
023: import com.eviware.soapui.model.testsuite.TestRunContext;
024: import com.eviware.soapui.model.testsuite.TestRunner;
025: import com.eviware.soapui.model.testsuite.TestStepResult;
026: import com.eviware.soapui.model.testsuite.TestRunner.Status;
027: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
028: import com.eviware.soapui.support.UISupport;
029: import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
030: import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
031: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
032: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
033:
034: /**
035: * TestStep that executes an arbitraty Groovy script
036: *
037: * @author ole.matzura
038: */
039:
040: public class WsdlGroovyScriptTestStep extends WsdlTestStep {
041: private final static Logger logger = Logger.getLogger("groovy.log");
042: private String scriptText = "";
043: private Object scriptResult;
044: private ImageIcon failedIcon;
045: private ImageIcon okIcon;
046: private SoapUIScriptEngine scriptEngine;
047:
048: public WsdlGroovyScriptTestStep(WsdlTestCase testCase,
049: TestStepConfig config, boolean forLoadTest) {
050: super (testCase, config, true, forLoadTest);
051:
052: if (!forLoadTest) {
053: okIcon = UISupport.createImageIcon("/groovy_script.gif");
054: setIcon(okIcon);
055: failedIcon = UISupport
056: .createImageIcon("/groovy_script_failed.gif");
057: }
058:
059: if (config.getConfig() == null) {
060: if (!forLoadTest)
061: saveScript(config);
062: } else {
063: readConfig(config);
064: }
065:
066: addProperty(new DefaultTestStepProperty("result", true,
067: new DefaultTestStepProperty.PropertyHandlerAdapter() {
068:
069: public String getValue() {
070: return scriptResult == null ? null
071: : scriptResult.toString();
072: }
073: }, this ));
074:
075: scriptEngine = SoapUIScriptEngineRegistry.create(
076: SoapUIScriptEngineRegistry.GROOVY_ID, this );
077: scriptEngine.setScript(getScript());
078: if (forLoadTest && !isDisabled())
079: try {
080: scriptEngine.compile();
081: } catch (Exception e) {
082: SoapUI.logError(e);
083: }
084: }
085:
086: public Logger getLogger() {
087: SoapUI.ensureGroovyLog();
088: return logger;
089: }
090:
091: private void readConfig(TestStepConfig config) {
092: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
093: config.getConfig());
094: scriptText = reader.readString("script", "");
095: }
096:
097: private void saveScript(TestStepConfig config) {
098: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
099: builder.add("script", scriptText);
100: config.setConfig(builder.finish());
101: }
102:
103: public void resetConfigOnMove(TestStepConfig config) {
104: super .resetConfigOnMove(config);
105: readConfig(config);
106: }
107:
108: public TestStepResult run(TestRunner testRunner,
109: TestRunContext context) {
110: SoapUI.ensureGroovyLog();
111:
112: WsdlTestStepResult result = new WsdlTestStepResult(this );
113: Logger log = (Logger) context.getProperty("log");
114: if (log == null)
115: log = logger;
116:
117: try {
118: if (scriptText.trim().length() > 0)
119: synchronized (this ) {
120: scriptEngine.setVariable("context", context);
121: scriptEngine.setVariable("testRunner", testRunner);
122: scriptEngine.setVariable("log", log);
123:
124: result.setTimeStamp(System.currentTimeMillis());
125: result.startTimer();
126: scriptResult = scriptEngine.run();
127: result.stopTimer();
128:
129: if (scriptResult != null)
130: result.addMessage("Script-result: "
131: + scriptResult.toString());
132: }
133:
134: // testRunner status may have been changed by script..
135: Status testRunnerStatus = testRunner.getStatus();
136: if (testRunnerStatus == Status.FAILED)
137: result.setStatus(TestStepStatus.FAILED);
138: else if (testRunnerStatus == Status.CANCELED)
139: result.setStatus(TestStepStatus.CANCELED);
140: else
141: result.setStatus(TestStepStatus.OK);
142: } catch (Throwable e) {
143: log.error(e);
144: result.stopTimer();
145: result.addMessage(e.getMessage());
146: result.setError(e);
147: result.setStatus(TestStepStatus.FAILED);
148: } finally {
149: if (!isForLoadTest())
150: setIcon(result.getStatus() == TestStepStatus.FAILED ? failedIcon
151: : okIcon);
152:
153: if (scriptEngine != null)
154: scriptEngine.clearVariables();
155: }
156:
157: return result;
158: }
159:
160: public String getScript() {
161: return scriptText;
162: }
163:
164: public void setScript(String scriptText) {
165: if (scriptText.equals(this .scriptText))
166: return;
167:
168: this .scriptText = scriptText;
169: scriptEngine.setScript(scriptText);
170: saveScript(getConfig());
171: }
172:
173: @Override
174: public void release() {
175: super.release();
176: scriptEngine.release();
177: }
178: }
|