[Webtest] If statement patch

Jeannie Graham webtest@lists.canoo.com
Wed, 5 May 2004 10:01:34 -0700


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C432C2.9ECBD820
Content-Type: text/plain;
	charset="iso-8859-1"

It looks like my stupid email blocker won't let me send zip files, so I am
only sending the actual files (cuz they're small) but I can't send the html
syntax file.   The iF statment has to look like this in your scripts, "iF" -
small i, capital F.


-----Original Message-----
From: Jeannie Graham 
Sent: Wednesday, May 05, 2004 9:55 AM
To: 'webtest@lists.canoo.com'
Subject: [Webtest] If statement patch


Hello everyone,

	Well, here it is!   I finally got the IF statement patch that Aatish
sent to work (endless thanks to Aatish for making it possible!).   First, I
had to make some tweaks to the files that Aatish sent me, IfStep.java and
IfWrapper.java.   I also had to tweak TestStepSequence.java.

Now, to add the patch and compile Canoo Webtest:
1).  Put both IfStep.java and IfWrapper.java into the WEBTESTHOME, extension
directory.
2).  Update your TestStepSequence.java file, which is in the WEBTESTHOME,
ant directory.   
3).  Update your webtestTaskdefs.props file, which is in the WEBTESTHOME
directory, with "iF=com.canoo.webtest.extension.IfStep".
4).  Build the build file to compile the changes, C:\WEBTESTHOME>ant.
5).  Copy the new webtest.jar file in the deployment/lib directory to the
main lib directory.

And now, you should be able to do an iF statement.   I am also including the
html file that Aatish sent me, showing the syntax of the iF statement.

Hope everyone enjoys,
Jeannie
 <<ATTACHMENT_OR_CONTENT_BLOCKING_ifpatch.zip.TXT>> 



------_=_NextPart_000_01C432C2.9ECBD820
Content-Type: application/octet-stream;
	name="IfStep.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="IfStep.java"

// Copyright =A9 2002 Canoo Engineering AG, Switzerland.=0A=
package com.canoo.webtest.extension;=0A=
=0A=
import com.canoo.webtest.steps.*;=0A=
import com.canoo.webtest.ant.*;=0A=
import com.canoo.webtest.interfaces.IRepeatStep;=0A=
=0A=
/**=0A=
 * This is a helper to support the attributes on=0A=
 * the repeat step.=0A=
 *=0A=
 * Used by ResultWrapper for accessing the attributes.=0A=
 *=0A=
 * @author Carsten Seibert=0A=
 */=0A=
public class IfStep extends TestStepSequence=0A=
{=0A=
	private String test =3D "";=0A=
	private String unless =3D "";=0A=
	=0A=
	/**=0A=
	 * @return=0A=
	 */=0A=
	public String getTest() {=0A=
		return test;=0A=
	}=0A=
=0A=
	/**=0A=
	 * @return=0A=
	 */=0A=
	public String getUnless() {=0A=
		return unless;=0A=
	}=0A=
=0A=
	/**=0A=
	 * @param string=0A=
	 */=0A=
	public void setTest(String string) {=0A=
		test =3D string;=0A=
	}=0A=
=0A=
	/**=0A=
	 * @param string=0A=
	 */=0A=
	public void setUnless(String string) {=0A=
		unless =3D string;=0A=
	}=0A=
    /**=0A=
     * Just forward to superclass=0A=
     */=0A=
    public IfStep()=0A=
    {=0A=
        super();=0A=
    }=0A=
}=0A=

------_=_NextPart_000_01C432C2.9ECBD820
Content-Type: application/octet-stream;
	name="IfWrapper.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="IfWrapper.java"

// Copyright =A9 2002 Canoo Engineering AG, Switzerland.=0A=
package com.canoo.webtest.extension;=0A=
=0A=
import com.canoo.webtest.engine.Context;=0A=
import com.canoo.webtest.engine.StepExecutionException;=0A=
import com.canoo.webtest.engine.StepFailedException;=0A=
import com.canoo.webtest.interfaces.IStepSequence;=0A=
import com.canoo.webtest.steps.*;=0A=
import com.canoo.webtest.ant.*;=0A=
import java.util.*;=0A=
=0A=
/**=0A=
 * A FailWrapper accepts a single test step or a sequence of test =
steps=0A=
 * and expects upon execution that <em>each</em> of these steps =
fails.=0A=
 *=0A=
 * It serves basically as container and simply forwards the =
execution=0A=
 * to each of the contained steps preserving the initial order in =
which=0A=
 * the steps were provided. Each of the steps <em>must</em> raise a=0A=
 * StepFailedException. As soon as a step does not do that, the =
FailErapper=0A=
 * step is considered to have failed and raises itself a =
TestStepFaildError.=0A=
 *=0A=
 * @author Carsten Seibert=0A=
 */=0A=
public class IfWrapper extends StepContainer=0A=
{=0A=
    public static String DEFAULT_STEPID =3D "If";=0A=
=0A=
    /**=0A=
     * Accepts an IStepSequence where all steps must fail=0A=
     * upon execution in order to successfully complete this step.=0A=
     * Use the step id of the provided test step sequence if=0A=
     * it exists or create a default id and forward to the=0A=
     * c'tor (String, IStepSequence).=0A=
     *=0A=
     * @param stepsToFail A sequence of steps that must all fail=0A=
     */=0A=
	 //this first line was public IfWrapper(IfStep stepsToRun)=0A=
    public IfWrapper(IStepSequence stepsToRun)=0A=
    {=0A=
        this(null, stepsToRun);=0A=
    }=0A=
=0A=
    /**=0A=
     * Accepts a step id and a IStepSequence. All steps=0A=
     * in this sequence must fail upon execution in order=0A=
     * to successfully complete this step.=0A=
     *=0A=
     * @param stepId The step id of this step=0A=
     * @param stepsToFail A sequence of steps that must all fail=0A=
     */=0A=
    public IfWrapper(String stepId, IStepSequence stepsToRun)=0A=
    {=0A=
        super(stepId, stepsToRun);=0A=
    }=0A=
=0A=
	protected IfStep getIfStep(){=0A=
		return (IfStep)this.getStepSequence();=0A=
	}=0A=
	=0A=
    /**=0A=
     * Execute all of the nested step, but only if the =0A=
     * "if" property evaluates to true, and unless evaluates to =
false.=0A=
     * specifically:=0A=
     * =0A=
     * if			unless    execute?=0A=
     * _________________________________=0A=
     * true			 missing	yes=0A=
     * false		 missing	no=0A=
     * missing		 true		no=0A=
     * missing		 false		yes=0A=
     * true			 true		error=0A=
     * false		 false		error=0A=
     * true			 false		error=0A=
     * false		 true		error=0A=
     * =0A=
     *=0A=
     * @param context The current test context=0A=
     * @exception StepFailedException Raises this exception if one of =
the wrapped steps not fails=0A=
     */=0A=
    public void doExecute(Context context) =0A=
		throws CloneNotSupportedException=0A=
    {=0A=
        super.doExecute(context);=0A=
=0A=
        if ( ! runNestedTests() ){=0A=
        	return;	=0A=
        }=0A=
                =0A=
        Step currentStep =3D null;=0A=
=0A=
        for (Iterator iter =3D getIfStep().getSteps().iterator(); =
iter.hasNext();)=0A=
        {=0A=
            currentStep =3D (Step) iter.next();=0A=
            currentStep.execute(context);=0A=
        }=0A=
    }=0A=
=0A=
	protected String getTestString(){=0A=
		return getIfStep().getTest();=0A=
	}=0A=
	=0A=
	protected String getUnlessString(){=0A=
		return getIfStep().getUnless();=0A=
	}=0A=
=0A=
	protected boolean runNestedTests(){=0A=
		if ( getTestString() !=3D null){=0A=
			return getTest();=0A=
		}=0A=
		if ( getUnlessString() !=3D null){=0A=
			return getUnless();=0A=
		}=0A=
		return false;=0A=
	}=0A=
	protected boolean getTest(){=0A=
		//returns the value of the test attribute as a boolean=0A=
		String test =3D getTestString();=0A=
		if ( "true".equalsIgnoreCase(test) ||=0A=
		     "yes".equalsIgnoreCase(test) ||=0A=
		     "on".equalsIgnoreCase(test)){=0A=
		     	return true;=0A=
		     }=0A=
		return false;     =0A=
	}=0A=
	=0A=
	protected boolean getUnless(){=0A=
		//returns the value of the test attribute as a boolean=0A=
		String test =3D getUnlessString();=0A=
		if ( "true".equalsIgnoreCase(test) ||=0A=
			 "yes".equalsIgnoreCase(test) ||=0A=
			 "on".equalsIgnoreCase(test)){=0A=
				return false;=0A=
			 }=0A=
		return true;		=0A=
	}=0A=
	=0A=
    /**=0A=
     * Show the contained fail steps=0A=
     */=0A=
    public String toString()=0A=
    {=0A=
        StringBuffer sb =3D new StringBuffer();=0A=
        sb.append(super.toString());=0A=
        sb.append(", wrappedSteps=3D");=0A=
        sb.append(getStepSequence().toString());=0A=
        sb.append(")");=0A=
        return sb.toString();=0A=
    }=0A=
    =0A=
    protected String getDefaultStepId()=0A=
    {=0A=
        return DEFAULT_STEPID;=0A=
    }=0A=
    =0A=
	protected void verifyParameters()=0A=
	{=0A=
		//only one of test and unless allowed=0A=
		=0A=
		if (getTestString() !=3D null && getUnlessString() !=3D null){=0A=
			throw new StepExecutionException("Only one of test and unless =
attributes allowed",this);=0A=
		}=0A=
		=0A=
		if ( getTestString() =3D=3D null && getUnlessString() =3D=3D =
null){=0A=
			throw new StepExecutionException("Either test or unless attributes =
are required.",this);=0A=
		}=0A=
	}=0A=
=0A=
	public Map getParameterDictionary()=0A=
	{=0A=
		Map map =3D super.getParameterDictionary();=0A=
		if ( getTestString() !=3D null ){=0A=
			map.put("test", getTestString());			=0A=
		}=0A=
		if ( getUnlessString() !=3D null ){=0A=
			map.put("unless", getUnlessString());			=0A=
		}=0A=
		return map;=0A=
	}=0A=
=0A=
=0A=
	/* (non-Javadoc)=0A=
	 * @see com.canoo.webtest.steps.Step#expandProperties()=0A=
	 */=0A=
	public void expandProperties() {=0A=
		super.expandProperties();=0A=
		=0A=
		IfStep step =3D getIfStep();=0A=
		step.setTest(expandDynamicProperties(step.getTest() ));=0A=
		step.setUnless(expandDynamicProperties(step.getUnless() ));=0A=
	}=0A=
=0A=
}
------_=_NextPart_000_01C432C2.9ECBD820
Content-Type: application/octet-stream;
	name="TestStepSequence.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="TestStepSequence.java"

=0A=
// Copyright =A9 2002 Canoo Engineering AG, Switzerland.=0A=
package com.canoo.webtest.ant;=0A=
=0A=
import com.canoo.webtest.extension.GroupStep;=0A=
import com.canoo.webtest.extension.GroupWrapper;=0A=
import com.canoo.webtest.interfaces.IRepeatStep;=0A=
import com.canoo.webtest.interfaces.IStepSequence;=0A=
import com.canoo.webtest.steps.FailWrapper;=0A=
import com.canoo.webtest.steps.RepeatWrapper;=0A=
import com.canoo.webtest.steps.Step;=0A=
import org.apache.tools.ant.BuildException;=0A=
import org.apache.tools.ant.Task;=0A=
import org.apache.tools.ant.TaskContainer;=0A=
import org.apache.tools.ant.UnknownElement;=0A=
import com.canoo.webtest.extension.*;=0A=
=0A=
import java.io.*;=0A=
import java.util.ArrayList;=0A=
import java.util.Iterator;=0A=
import java.util.List;=0A=
=0A=
/**=0A=
 * This is a container class for a list of =
AbstractTestSpecificationSteps. It is used for=0A=
 * the <code>&lt;steps&gt;</code> element in ant build scripts as well =
as for the=0A=
 * <code>&lt;not&gt;</code> test step elements.=0A=
 * <p/>=0A=
 * <p>An instance of this class is usually created by ant if it =
encounters the nested=0A=
 * <code>&lt;steps&gt;</code> element within a =
<code>&lt;testSpec&gt;</code> element.=0A=
 * For each nested element within the <code>&lt;steps&gt;</code> =
element is the=0A=
 * appropriate <code>addXxx</code> method called. Ant matches the =
nested element name to the=0A=
 * to the method name (e.g. <code>&lt;clickbutton&gt;</code> is mapped =
to=0A=
 * <code>addClickbutton</code> and instantiates an object according to =
the signature of=0A=
 * this method, e.g. a new instance of ClickButton.</p>=0A=
 * <p/>=0A=
 * <p>Since there is not 1:1 association between test step element =
names and test step=0A=
 * implementation classes it is necessary to set the step type in order =
to allow=0A=
 * later a mapping back to the original test step for meaningful =
reporting.</p>=0A=
 * <p/>=0A=
 * <p><em>Note: The nested element object <code>ClickButton</code> is =
instantiated and "added" to the=0A=
 * <code>TestStepSequence</code> before the nested element is =
completely parsed! No attributes=0A=
 * or nested elements of the <code>&lt;clickbutton&gt;</code> elements =
are available at time=0A=
 * when it is added to <code>TestStepSequence</code>.</em></p>=0A=
 *=0A=
 * @author Carsten Seibert=0A=
 * @see com.canoo.webtest.ant.TestSpecificationTask=0A=
 */=0A=
=0A=
// todo: dk - smell "inappropriate intimacy", superclass knows to much =
about subclasses=0A=
=0A=
public class TestStepSequence extends Task implements TaskContainer, =
IStepSequence, Serializable, Cloneable {=0A=
=0A=
    private List fSteps;=0A=
    private String fStepId;=0A=
=0A=
    public TestStepSequence() {=0A=
        setSteps(new ArrayList());=0A=
    }=0A=
=0A=
 /**   public void addIf( IfStep ifstep){=0A=
		IfWrapper step =3D new IfWrapper(ifstep);=0A=
		step.setStepType("if");=0A=
		addStep(step);=0A=
	} */=0A=
=0A=
	public void addTask(Task task) {=0A=
        if (task instanceof UnknownElement) {=0A=
            ((UnknownElement) task).maybeConfigure();=0A=
            task =3D ((UnknownElement) task).getTask();=0A=
        }=0A=
//		System.out.println ("task is " + task.getClass().getName());=0A=
        Step step =3D null;=0A=
        if (task instanceof NotStep) {=0A=
            step =3D new FailWrapper((IStepSequence) task);=0A=
		} else if (task instanceof IfStep) {=0A=
			step =3D new IfWrapper((IStepSequence) task);=0A=
        } else if (task instanceof RepeatStep) {=0A=
            step =3D new RepeatWrapper((IRepeatStep) task);=0A=
        } else if (task instanceof GroupStep) {=0A=
            step =3D new GroupWrapper((IStepSequence) task);=0A=
        } else if (task instanceof Step) {=0A=
            step =3D (Step) task;=0A=
        }=0A=
=0A=
        if (step !=3D null) {=0A=
            step.setStepType(step.getTaskName());=0A=
            addStep(step);=0A=
        } else {=0A=
            throw new BuildException("Task must be of type \"Step\": =
"=0A=
                    + task.getTaskName() + " at "=0A=
                    + task.getLocation().toString() + " is of type "=0A=
                    + task.getClass().getName());=0A=
        }=0A=
    }=0A=
=0A=
    public void addStep(Step step) {=0A=
        fSteps.add(step);=0A=
    }=0A=
=0A=
    public String getStepId() {=0A=
        return fStepId;=0A=
    }=0A=
=0A=
    public List getSteps() {=0A=
        return fSteps;=0A=
    }=0A=
=0A=
    public void setStepId(String stepId) {=0A=
        fStepId =3D stepId;=0A=
    }=0A=
=0A=
    public void setSteps(List newSteps) {=0A=
        fSteps =3D newSteps;=0A=
    }=0A=
=0A=
	=0A=
    public String toString() {=0A=
        StringBuffer sb =3D new StringBuffer();=0A=
        sb.append(getClass().getName());=0A=
        sb.append("(stepId=3D\"");=0A=
        sb.append(getStepId());=0A=
        sb.append("\", wrappedSteps=3D[");=0A=
        for (Iterator iterator =3D fSteps.iterator(); =
iterator.hasNext();) {=0A=
            sb.append("<");=0A=
            sb.append(iterator.next().toString());=0A=
            sb.append("> ");=0A=
        }=0A=
        sb.append("])");=0A=
        return sb.toString();=0A=
    }=0A=
=0A=
    /**=0A=
     * Clone the step list by deepcopying it.=0A=
     */=0A=
    public Object clone() {=0A=
        TestStepSequence newClone;=0A=
=0A=
        try {=0A=
            newClone =3D (TestStepSequence) super.clone();=0A=
            newClone.fSteps =3D (List) deepCopyObject(fSteps);=0A=
        } catch (Exception e) {=0A=
            throw new IllegalStateException(e.toString());=0A=
        }=0A=
=0A=
        return newClone;=0A=
    }=0A=
=0A=
    /**=0A=
     * Use serialization to create a deepcopy of a given object, =
thus=0A=
     * it must implement the serializable interface.=0A=
     *=0A=
     * @param object A serializable object.=0A=
     */=0A=
    protected Object deepCopyObject(Object object) throws Exception =
{=0A=
        Object result;=0A=
=0A=
        ByteArrayOutputStream baos =3D new ByteArrayOutputStream();=0A=
        ObjectOutputStream oos =3D new ObjectOutputStream(baos);=0A=
        oos.writeObject(object);=0A=
        ByteArrayInputStream bais =3D new =
ByteArrayInputStream(baos.toByteArray());=0A=
        ObjectInputStream ois =3D new ObjectInputStream(bais);=0A=
        result =3D ois.readObject();=0A=
=0A=
        return result;=0A=
    }=0A=
}=0A=

------_=_NextPart_000_01C432C2.9ECBD820--