1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48 public class IOExecuteTestCase extends TestCase
49 implements ContextListener {
50
51
52
53
54
55
56
57
58 protected Activity activity = null;
59
60
61
62
63
64 protected Context context = null;
65
66
67
68
69
70 protected StringBuffer trail = new StringBuffer();
71
72
73
74
75
76
77
78
79
80
81 public IOExecuteTestCase(String name) {
82
83 super(name);
84
85 }
86
87
88
89
90
91
92
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
105
106 public static Test suite() {
107
108 return (new TestSuite(IOExecuteTestCase.class));
109
110 }
111
112
113
114
115
116 public void tearDown() {
117
118 context.removeContextListener(this);
119 activity = null;
120 context = null;
121
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 public void testGetDisplay() {
136
137
138 String url = System.getProperty("ioexecute.url");
139 assertNotNull("The 'ioexecute.url' system property is set", url);
140
141
142 activity.addStep(new GetStep("01", url));
143 activity.addStep(new PeekStep("02"));
144 activity.addStep(new PopStep("03"));
145
146
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
177
178 public void testReadWrite() {
179
180
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
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
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
234
235
236
237
238
239
240
241
242
243
244 public void afterActivity(ContextEvent event) {
245
246 trail.append("afterActivity()/");
247
248 }
249
250
251
252
253
254
255
256 public void afterStep(ContextEvent event) {
257
258 trail.append("afterStep(");
259 trail.append(event.getStep().getId());
260 trail.append(")");
261
262
263
264
265
266
267
268 trail.append("/");
269
270 }
271
272
273
274
275
276
277
278
279
280 public void beforeActivity(ContextEvent event) {
281
282 trail.setLength(0);
283 trail.append("beforeActivity()/");
284
285 }
286
287
288
289
290
291
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 }