001: // Copyright (c) 2002-2005 Canoo Engineering AG, Switzerland. All Rights
002: // Reserved.
003: package com.canoo.webtest.extension.groovy;
004:
005: import java.io.File;
006:
007: import org.apache.log4j.Logger;
008:
009: import com.canoo.webtest.steps.Step;
010: import com.canoo.webtest.util.FileUtil;
011:
012: /**
013: * Wrapper class for groovy scripting.
014: * <p>
015: * @author Dierk Koenig
016: * @webtest.step category="Extension" name="groovy"
017: * description="Executes the provided Groovy script (the binding is the same for all
018: * groovy steps within a webtest)."
019: */
020: public class GroovyStep extends Step {
021: private static final Logger LOG = Logger
022: .getLogger(GroovyStep.class);
023:
024: private File fFile;
025: private String fText;
026: private boolean fReplaceProperties = false;
027:
028: public void doExecute() {
029: String script = getScript();
030: if (isReplaceProperties()) {
031: script = getProject().replaceProperties(script);
032: }
033:
034: final GroovyInvoker invoker = new GroovyInvoker();
035: invoker.doExecute(this , script);
036: }
037:
038: public boolean isReplaceProperties() {
039: return fReplaceProperties;
040: }
041:
042: /**
043: * Indicates if properties should be replaced in the script (default false)
044: * @webtest.parameter required="no"
045: * default="false"
046: * description="Indicates if properties (${...} and #{...}) present in the script
047: * should be replaced by their value. Use carefully as ${...} is although the syntax for GString."
048: */
049: public void setReplaceProperties(final boolean replaceProperties) {
050: fReplaceProperties = replaceProperties;
051: }
052:
053: /**
054: * Gets the script code from the file or the nested content
055: * @return the script code
056: */
057: private String getScript() {
058: final String script;
059: if (fFile != null) {
060: LOG.debug("Reading script from file: " + fFile);
061: script = FileUtil.readFileToString(fFile, this );
062: } else {
063: LOG.debug("Reading script from nested text");
064: script = fText;
065: }
066: return script;
067: }
068:
069: protected void verifyParameters() {
070: super .verifyParameters();
071: paramCheck(fFile == null && fText == null,
072: "Either \"file\" attribute or nested groovy text must be given.");
073: paramCheck(fFile != null && fText != null,
074: "Only one of \"file\" attribute or nested groovy text may be given.");
075: }
076:
077: /**
078: * Defines the file containing scripting code (optional).
079: * @param fileName Sets the name of the file containing script code.
080: * @webtest.parameter required="yes/no" description="The name of the file
081: * containing the script code. You may omit this
082: * parameter if you have embedded script code."
083: */
084: public void setFile(final File fileName) {
085: fFile = fileName;
086: }
087:
088: public File getFile() {
089: return fFile;
090: }
091:
092: /**
093: * The script text.
094: * @param text Sets the value for the script variable.
095: * @webtest.nested.parameter required="yes/no" description="The script to
096: * execute. You may omit this if you use the
097: * attribute file."
098: */
099: public void addText(final String text) {
100: fText = text;
101: }
102:
103: }
|