1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.commons.workflow.io;
19  
20  
21  import java.io.File;
22  import java.util.EmptyStackException;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  import org.apache.commons.workflow.Activity;
27  import org.apache.commons.workflow.Context;
28  import org.apache.commons.workflow.ContextEvent;
29  import org.apache.commons.workflow.ContextListener;
30  import org.apache.commons.workflow.Scope;
31  import org.apache.commons.workflow.Step;
32  import org.apache.commons.workflow.StepException;
33  import org.apache.commons.workflow.base.BaseActivity;
34  import org.apache.commons.workflow.base.BaseContext;
35  import org.apache.commons.workflow.base.BaseScope;
36  import org.apache.commons.workflow.core.PopStep;
37  import org.apache.commons.workflow.core.StringStep;
38  
39  
40  /**
41   * <p>Test Case for the <code>io</code> library of <code>Step</code>
42   * implementations.</p>
43   *
44   * @author Craig R. McClanahan
45   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
46   */
47  
48  public class IOExecuteTestCase extends TestCase
49      implements ContextListener {
50  
51  
52      // ----------------------------------------------------- Instance Variables
53  
54  
55      /**
56       * The Activity we will use to contain the Steps that we will execute.
57       */
58      protected Activity activity = null;
59  
60  
61      /**
62       * The Context we will use to execute the Activity under test.
63       */
64      protected Context context = null;
65  
66  
67      /**
68       * The trail of execution, as recorded by our ContextListener methods.
69       */
70      protected StringBuffer trail = new StringBuffer();
71  
72  
73      // ----------------------------------------------------------- Constructors
74  
75  
76      /**
77       * Construct a new instance of this test case.
78       *
79       * @param name Name of the test case
80       */
81      public IOExecuteTestCase(String name) {
82  
83          super(name);
84  
85      }
86  
87  
88      // --------------------------------------------------- Overall Test Methods
89  
90  
91      /**
92       * Set up instance variables required by this test case.
93       */
94      public void setUp() {
95  
96          context = new BaseContext();
97          activity = new BaseActivity();
98          context.setActivity(activity);
99          context.addContextListener(this);
100     }
101 
102 
103     /**
104      * Return the tests included in this test suite.
105      */
106     public static Test suite() {
107 
108         return (new TestSuite(IOExecuteTestCase.class));
109 
110     }
111 
112 
113     /**
114      * Tear down instance variables required by this test case.
115      */
116     public void tearDown() {
117 
118         context.removeContextListener(this);
119         activity = null;
120         context = null;
121 
122     }
123 
124 
125     // ------------------------------------------------ Individual Test Methods
126 
127 
128     /**
129      * Get and display steps.  This test depends on a URL being specified
130      * by the <code>ioexecute.url</code> system property, and that this
131      * URL can be successfully retrieved via a <code>URLConnection</code>.
132      * No validation of the retrieved content is performed - it is merely
133      * displayed to system output with the <code>peek</code> command.
134      */
135     public void testGetDisplay() {
136 
137         // Identify the URL path we will be accessing
138         String url = System.getProperty("ioexecute.url");
139         assertNotNull("The 'ioexecute.url' system property is set", url);
140 
141         // Configure the steps in this activity
142         activity.addStep(new GetStep("01", url));
143         activity.addStep(new PeekStep("02"));
144         activity.addStep(new PopStep("03"));
145 
146         // Execute the activity and validate results
147         try {
148             context.execute();
149             assertEquals("Trail contents",
150                          "beforeActivity()/" +
151                          "beforeStep(01)/afterStep(01)/" +
152                          "beforeStep(02)/afterStep(02)/" +
153                          "beforeStep(03)/afterStep(03)/" +
154                          "afterActivity()/",
155                          trail.toString());
156             assertTrue("Stack is empty",
157                        context.isEmpty());
158             assertTrue("Context was not suspended",
159                        !context.getSuspend());
160         } catch (StepException e) {
161             e.printStackTrace(System.out);
162             if (e.getCause() != null) {
163                 System.out.println("ROOT CAUSE");
164                 e.getCause().printStackTrace(System.out);
165             }
166             fail("Threw StepException " + e);
167         } catch (Throwable e) {
168             e.printStackTrace();
169             fail("Threw exception " + e);
170         }
171 
172     }
173 
174 
175     /**
176      * Read and write steps.
177      */
178     public void testReadWrite() {
179 
180         // Identify a temporary file that we can manipulate
181         File file = null;
182         String path = null;
183         try {
184             file = File.createTempFile("IOExecuteTestCase", ".txt");
185             path = file.getAbsolutePath();
186             file.delete();
187         } catch (Throwable t) {
188             fail("File create threw " + t);
189         } 
190 
191         // Configure the steps in this activity
192         activity.addStep
193             (new StringStep("01", "This is a test.  It is only a test."));
194         activity.addStep(new WriteStep("02", null, path));
195         activity.addStep(new ReadStep("03", null, path));
196 
197         // Execute the activity and validate results
198         try {
199             context.execute();
200             assertEquals("Trail contents",
201                          "beforeActivity()/" +
202                          "beforeStep(01)/afterStep(01)/" +
203                          "beforeStep(02)/afterStep(02)/" +
204                          "beforeStep(03)/afterStep(03)/" +
205                          "afterActivity()/",
206                          trail.toString());
207             assertTrue("Stack is not empty",
208                        !context.isEmpty());
209             assertEquals("Retrieved written content",
210                          "This is a test.  It is only a test.",
211                          (String) context.pop());
212             assertTrue("Stack is empty",
213                        context.isEmpty());
214             assertTrue("Context was not suspended",
215                        !context.getSuspend());
216         } catch (StepException e) {
217             e.printStackTrace(System.out);
218             if (e.getCause() != null) {
219                 System.out.println("ROOT CAUSE");
220                 e.getCause().printStackTrace(System.out);
221             }
222             fail("Threw StepException " + e);
223         } catch (Throwable e) {
224             e.printStackTrace();
225             fail("Threw exception " + e);
226         } finally {
227             file.delete();
228         }
229 
230     }
231 
232 
233     // ------------------------------------------------ ContextListener Methods
234 
235 
236     /**
237      * Invoked immediately after execution of the related Activity has
238      * been completed normally, been suspended, or been aborted by
239      * the throwing of a StepException.  The Step included in this event
240      * will be the last one to be executed.
241      *
242      * @param event The <code>ContextEvent</code> that has occurred
243      */
244     public void afterActivity(ContextEvent event) {
245 
246         trail.append("afterActivity()/");
247 
248     }
249 
250 
251     /**
252      * Invoked immediately after the specified Step was executed.
253      *
254      * @param event The <code>ContextEvent</code> that has occurred
255      */
256     public void afterStep(ContextEvent event) {
257 
258         trail.append("afterStep(");
259         trail.append(event.getStep().getId());
260         trail.append(")");
261         /*
262         StepException exception = event.getException();
263         if (exception != null) {
264             trail.append("-");
265             trail.append(exception.toString());
266         }
267         */
268         trail.append("/");
269 
270     }
271 
272 
273     /**
274      * Invoked immediately before execution of the related Activity has
275      * started.  The Step included in this event will be the first one
276      * to be executed.
277      *
278      * @param event The <code>ContextEvent</code> that has occurred
279      */
280     public void beforeActivity(ContextEvent event) {
281 
282         trail.setLength(0);
283         trail.append("beforeActivity()/");
284 
285     }
286 
287 
288     /**
289      * Invoked immediately before the specified Step is executed.
290      *
291      * @param event The <code>ContextEvent</code> that has occurred
292      */
293     public void beforeStep(ContextEvent event) {
294 
295         trail.append("beforeStep(");
296         trail.append(event.getStep().getId());
297         trail.append(")/");
298 
299     }
300 
301 
302 }