01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.control;
03:
04: import java.util.Iterator;
05:
06: import org.apache.log4j.Logger;
07: import org.apache.tools.ant.BuildException;
08: import org.apache.tools.ant.Task;
09:
10: import com.canoo.webtest.engine.StepFailedException;
11: import com.canoo.webtest.steps.AbstractStepContainer;
12:
13: /**
14: * A NotStep accepts a single test step or a sequence of test steps
15: * and expects upon execution that <em>each</em> of these steps fails.
16: * <p/>
17: * It serves basically as container and simply forwards the execution
18: * to each of the contained steps preserving the initial order in which
19: * the steps were provided. Each of the steps <em>must</em> raise a
20: * StepFailedException. As soon as a step does not do that, the NotStep
21: * step is considered to have failed and raises itself a TestStepFailedError.
22: *
23: * @author Carsten Seibert
24: * @webtest.step category="Core"
25: * name="not"
26: * description="This step encapsulates one or more test steps that are ALL expected to fail. Any kind of step can be nested."
27: */
28: public class NotStep extends AbstractStepContainer {
29: private static final Logger LOG = Logger.getLogger(NotStep.class);
30: public static final String DEFAULT_DESCRIPTION = "not";
31:
32: /**
33: * Forward the execution to each of the wrapped steps. Each step must raise a StepFailedException.
34: * As soon as the first step passes, i.e. does not raise StepFailedException, execution is
35: * terminated.
36: *
37: * @throws com.canoo.webtest.engine.StepFailedException
38: * Raises this exception if one of the wrapped steps not fails
39: */
40: public void doExecute() throws Exception {
41:
42: boolean allStepsFailed = true;
43: Task currentStep = null;
44: for (final Iterator iter = getSteps().iterator(); iter
45: .hasNext()
46: && allStepsFailed;) {
47: try {
48: currentStep = (Task) iter.next();
49: executeContainedStep(currentStep);
50: allStepsFailed = false;
51: } catch (final BuildException e) {
52: if (StepFailedException
53: .isCausedByStepFailedException(e)) {
54: LOG.debug("Ignoring expected exception: "
55: + e.getMessage());
56: } else {
57: LOG.debug("Rethrowing exception");
58: throw e;
59: }
60: } catch (final Exception e) {
61: LOG.debug("Step failed", e);
62: throw e;
63: } finally {
64: LOG.debug("finished NOT step");
65: }
66: }
67: if (!allStepsFailed) {
68: final StringBuffer message = new StringBuffer(
69: "Wrapped step did not fail");
70: if (currentStep.getDescription() != null) {
71: message.append(": ");
72: message.append(currentStep.getDescription());
73: }
74: throw new StepFailedException(message.toString(), this);
75: }
76: }
77: }
|