craigmcc    01/08/21 18:38:12

  Modified:    workflow/src/java/org/apache/commons/workflow
                        StepException.java
               workflow/src/java/org/apache/commons/workflow/base
                        BaseContext.java
               workflow/src/test/org/apache/commons/workflow/base
                        BaseContextTestCase.java
               workflow/src/test/org/apache/commons/workflow/core
                        CoreExecuteTestCase.java
  Log:
  Add unit tests for CORE step implementations:  duplicate, exit, goto, pop,
  string, suspend, and swap.
  
  TODO:  tests for get, put, and remove (via name/scope and xpath, in
  various scopes).
  
  Revision  Changes    Path
  1.2       +29 -4     
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/StepException.java
  
  Index: StepException.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/StepException.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StepException.java        2001/08/13 21:15:03     1.1
  +++ StepException.java        2001/08/22 01:38:11     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/StepException.java,v
 1.1 2001/08/13 21:15:03 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/08/13 21:15:03 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/StepException.java,v
 1.2 2001/08/22 01:38:11 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/08/22 01:38:11 $
    *
    * ====================================================================
    * 
  @@ -66,7 +66,7 @@
    * <p>A <strong>StepException</strong> is used to report problems encountered
    * during the dynamic execution of a specific <code>Step</code>.
    *
  - * @version $Revision: 1.1 $ $Date: 2001/08/13 21:15:03 $
  + * @version $Revision: 1.2 $ $Date: 2001/08/22 01:38:11 $
    * @author Craig R. McClanahan
    */
   
  @@ -199,6 +199,31 @@
   
       public Step getStep() {
           return (this.step);
  +    }
  +
  +
  +    // --------------------------------------------------------- Public Methods
  +
  +
  +    /**
  +     * Render a printable version of this exception.
  +     */
  +    public String toString() {
  +
  +        StringBuffer sb = new StringBuffer("StepException[");
  +        sb.append("message=");
  +        sb.append(getMessage());
  +        if (step != null) {
  +            sb.append(", step=");
  +            sb.append(step);
  +        }
  +        if (cause != null) {
  +            sb.append(", cause=");
  +            sb.append(cause);
  +        }
  +        sb.append("]");
  +        return (sb.toString());
  +
       }
   
   
  
  
  
  1.8       +6 -5      
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java
  
  Index: BaseContext.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BaseContext.java  2001/08/22 01:04:37     1.7
  +++ BaseContext.java  2001/08/22 01:38:11     1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v
 1.7 2001/08/22 01:04:37 craigmcc Exp $
  - * $Revision: 1.7 $
  - * $Date: 2001/08/22 01:04:37 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/base/BaseContext.java,v
 1.8 2001/08/22 01:38:11 craigmcc Exp $
  + * $Revision: 1.8 $
  + * $Date: 2001/08/22 01:38:11 $
    *
    * ====================================================================
    * 
  @@ -84,7 +84,7 @@
    * class.  If it is used in a multiple thread environment, callers must
    * take suitable precations.</p>
    *
  - * @version $Revision: 1.7 $ $Date: 2001/08/22 01:04:37 $
  + * @version $Revision: 1.8 $ $Date: 2001/08/22 01:38:11 $
    * @author Craig R. McClanahan
    */
   
  @@ -636,7 +636,8 @@
        */
       public void setNextStep(Step nextStep) {
   
  -        if (this.activity != nextStep.getActivity())
  +        if ((nextStep != null) &&
  +            (this.activity != nextStep.getActivity()))
               throw new IllegalArgumentException
                   ("Step is not part of the current Activity");
           this.nextStep = nextStep;
  
  
  
  1.6       +9 -6      
jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java
  
  Index: BaseContextTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BaseContextTestCase.java  2001/08/20 04:14:19     1.5
  +++ BaseContextTestCase.java  2001/08/22 01:38:11     1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java,v
 1.5 2001/08/20 04:14:19 craigmcc Exp $
  - * $Revision: 1.5 $
  - * $Date: 2001/08/20 04:14:19 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/base/BaseContextTestCase.java,v
 1.6 2001/08/22 01:38:11 craigmcc Exp $
  + * $Revision: 1.6 $
  + * $Date: 2001/08/22 01:38:11 $
    *
    * ====================================================================
    *
  @@ -77,7 +77,7 @@
    * functions that are not dependent upon an associated Activity.</p>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.5 $ $Date: 2001/08/20 04:14:19 $
  + * @version $Revision: 1.6 $ $Date: 2001/08/22 01:38:11 $
    */
   
   public class BaseContextTestCase extends TestCase {
  @@ -255,8 +255,11 @@
   
   
       /**
  -     * Test the returned JPathContext for accessing items in the unified
  -     * namespace.
  +     * <p>Test the returned JPathContext for accessing items in the unified
  +     * namespace.</p>
  +     *
  +     * <p><strong>WARNING:</strong> - Dependent upon how the Scopes associated
  +     * with our Context are mapped to JPathContext objects.</p>
        */
       public void testJPathContext() {
   
  
  
  
  1.2       +249 -8    
jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/core/CoreExecuteTestCase.java
  
  Index: CoreExecuteTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/core/CoreExecuteTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CoreExecuteTestCase.java  2001/08/19 23:53:10     1.1
  +++ CoreExecuteTestCase.java  2001/08/22 01:38:12     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/core/CoreExecuteTestCase.java,v
 1.1 2001/08/19 23:53:10 craigmcc Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/08/19 23:53:10 $
  + * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/test/org/apache/commons/workflow/core/CoreExecuteTestCase.java,v
 1.2 2001/08/22 01:38:12 craigmcc Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/08/22 01:38:12 $
    *
    * ====================================================================
    *
  @@ -69,10 +69,13 @@
   import junit.framework.TestSuite;
   import org.apache.commons.workflow.Activity;
   import org.apache.commons.workflow.Context;
  +import org.apache.commons.workflow.ContextEvent;
  +import org.apache.commons.workflow.ContextListener;
   import org.apache.commons.workflow.Scope;
   import org.apache.commons.workflow.Step;
   import org.apache.commons.workflow.StepException;
  -import org.apache.commons.workflow.base.BaseExecuteTestCase;
  +import org.apache.commons.workflow.base.BaseActivity;
  +import org.apache.commons.workflow.base.BaseContext;
   
   
   /**
  @@ -80,15 +83,34 @@
    * implementations.</p>
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.1 $ $Date: 2001/08/19 23:53:10 $
  + * @version $Revision: 1.2 $ $Date: 2001/08/22 01:38:12 $
    */
   
  -public class CoreExecuteTestCase extends BaseExecuteTestCase {
  +public class CoreExecuteTestCase extends TestCase
  +    implements ContextListener {
   
   
       // ----------------------------------------------------- Instance Variables
   
   
  +    /**
  +     * The Activity we will use to contain the Steps that we will execute.
  +     */
  +    protected Activity activity = null;
  +
  +
  +    /**
  +     * The Context we will use to execute the Activity under test.
  +     */
  +    protected Context context = null;
  +
  +
  +    /**
  +     * The trail of execution, as recorded by our ContextListener methods.
  +     */
  +    protected StringBuffer trail = new StringBuffer();
  +
  +
       // ----------------------------------------------------------- Constructors
   
   
  @@ -112,8 +134,10 @@
        */
       public void setUp() {
   
  -        super.setUp();
  -
  +        context = new BaseContext();
  +        activity = new BaseActivity();
  +        context.setActivity(activity);
  +        context.addContextListener(this);
       }
   
   
  @@ -132,12 +156,229 @@
        */
       public void tearDown() {
   
  -        super.tearDown();
  +        context.removeContextListener(this);
  +        activity = null;
  +        context = null;
   
       }
   
   
       // ------------------------------------------------ Individual Test Methods
  +
  +
  +    /**
  +     * Control flow options.
  +     */
  +    public void testControlFlow() {
  +
  +        // Configure the steps in this activity
  +        activity.addStep(new GotoStep("01", "03"));
  +        activity.addStep(new StringStep("02", "Should not be executed"));
  +        activity.addStep(new SuspendStep("03"));
  +        activity.addStep(new StringStep("04", "Only after resume"));
  +
  +        // Execute the activity and validate results
  +        try {
  +            context.execute();
  +            assertEquals("Trail contents",
  +                         "beforeActivity()/" +
  +                         "beforeStep(01)/afterStep(01)/" +
  +                         "beforeStep(03)/afterStep(03)/" +
  +                         "afterActivity()/",
  +                         trail.toString());
  +            assertTrue("Stack is empty",
  +                       context.isEmpty());
  +            assertTrue("Context was suspended",
  +                       context.getSuspend());
  +        } catch (StepException e) {
  +            e.printStackTrace(System.out);
  +            if (e.getCause() != null) {
  +                System.out.println("ROOT CAUSE");
  +                e.getCause().printStackTrace(System.out);
  +            }
  +            fail("Threw StepException " + e);
  +        } catch (Throwable e) {
  +            e.printStackTrace();
  +            fail("Threw exception " + e);
  +        }
  +
  +        // Resume execution after suspension
  +        try {
  +            context.execute();
  +            assertEquals("Trail contents",
  +                         "beforeActivity()/" +
  +                         "beforeStep(04)/afterStep(04)/" +
  +                         "afterActivity()/",
  +                         trail.toString());
  +            assertTrue("Stack is not empty",
  +                       !context.isEmpty());
  +            assertTrue("Context was not suspended",
  +                       !context.getSuspend());
  +            assertEquals("Popped value is 'Only after resume'",
  +                         "Only after resume",
  +                         (String) context.pop());
  +        } catch (StepException e) {
  +            e.printStackTrace(System.out);
  +            if (e.getCause() != null) {
  +                System.out.println("ROOT CAUSE");
  +                e.getCause().printStackTrace(System.out);
  +            }
  +            fail("Threw StepException " + e);
  +        } catch (Throwable e) {
  +            e.printStackTrace();
  +            fail("Threw exception " + e);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Simplest possible activity execution.
  +     */
  +    public void testSimplestPossible() {
  +
  +        // Configure the steps in this activity
  +        activity.addStep(new ExitStep("01"));
  +
  +        // Execute the activity and validate results
  +        try {
  +            context.execute();
  +            assertEquals("Trail contents",
  +                         "beforeActivity()/" +
  +                         "beforeStep(01)/afterStep(01)/" +
  +                         "afterActivity()/",
  +                         trail.toString());
  +        } catch (StepException e) {
  +            e.printStackTrace(System.out);
  +            if (e.getCause() != null) {
  +                System.out.println("ROOT CAUSE");
  +                e.getCause().printStackTrace(System.out);
  +            }
  +            fail("Threw StepException " + e);
  +        } catch (Throwable e) {
  +            e.printStackTrace();
  +            fail("Threw exception " + e);
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Stack manipulation steps.
  +     */
  +    public void testStackManipulation() {
  +
  +        // Configure the steps in this activity
  +        activity.addStep(new StringStep("01", "First Value"));
  +        activity.addStep(new DuplicateStep("02"));
  +        activity.addStep(new StringStep("03", "Second Value"));
  +        activity.addStep(new SwapStep("04"));
  +        activity.addStep(new PopStep("05"));
  +
  +        // Execute the activity and validate results
  +        try {
  +            context.execute();
  +            assertEquals("Trail contents",
  +                         "beforeActivity()/" +
  +                         "beforeStep(01)/afterStep(01)/" +
  +                         "beforeStep(02)/afterStep(02)/" +
  +                         "beforeStep(03)/afterStep(03)/" +
  +                         "beforeStep(04)/afterStep(04)/" +
  +                         "beforeStep(05)/afterStep(05)/" +
  +                         "afterActivity()/",
  +                         trail.toString());
  +            assertTrue("Stack not empty",
  +                       !context.isEmpty());
  +            assertEquals("Popped value is 'Second Value'",
  +                         "Second Value",
  +                         (String) context.pop());
  +            assertTrue("Stack still not empty",
  +                       !context.isEmpty());
  +            assertEquals("Popped value is 'First Value'",
  +                         "First Value",
  +                         (String) context.pop());
  +            assertTrue("Stack is now empty",
  +                       context.isEmpty());
  +        } catch (StepException e) {
  +            e.printStackTrace(System.out);
  +            if (e.getCause() != null) {
  +                System.out.println("ROOT CAUSE");
  +                e.getCause().printStackTrace(System.out);
  +            }
  +            fail("Threw StepException " + e);
  +        } catch (Throwable e) {
  +            e.printStackTrace();
  +            fail("Threw exception " + e);
  +        }
  +
  +    }
  +
  +
  +    // ------------------------------------------------ ContextListener Methods
  +
  +
  +    /**
  +     * Invoked immediately after execution of the related Activity has
  +     * been completed normally, been suspended, or been aborted by
  +     * the throwing of a StepException.  The Step included in this event
  +     * will be the last one to be executed.
  +     *
  +     * @param event The <code>ContextEvent</code> that has occurred
  +     */
  +    public void afterActivity(ContextEvent event) {
  +
  +        trail.append("afterActivity()/");
  +
  +    }
  +
  +
  +    /**
  +     * Invoked immediately after the specified Step was executed.
  +     *
  +     * @param event The <code>ContextEvent</code> that has occurred
  +     */
  +    public void afterStep(ContextEvent event) {
  +
  +        trail.append("afterStep(");
  +        trail.append(event.getStep().getId());
  +        trail.append(")");
  +        StepException exception = event.getException();
  +        if (exception != null) {
  +            trail.append("-");
  +            trail.append(exception.toString());
  +        }
  +        trail.append("/");
  +
  +    }
  +
  +
  +    /**
  +     * Invoked immediately before execution of the related Activity has
  +     * started.  The Step included in this event will be the first one
  +     * to be executed.
  +     *
  +     * @param event The <code>ContextEvent</code> that has occurred
  +     */
  +    public void beforeActivity(ContextEvent event) {
  +
  +        trail.setLength(0);
  +        trail.append("beforeActivity()/");
  +
  +    }
  +
  +
  +    /**
  +     * Invoked immediately before the specified Step is executed.
  +     *
  +     * @param event The <code>ContextEvent</code> that has occurred
  +     */
  +    public void beforeStep(ContextEvent event) {
  +
  +        trail.append("beforeStep(");
  +        trail.append(event.getStep().getId());
  +        trail.append(")/");
  +
  +    }
   
   
   }
  
  
  

Reply via email to