KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > java > test > FlowTest


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.components.flow.java.test;
17
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.apache.cocoon.components.flow.java.Continuable;
24 import org.apache.cocoon.components.flow.java.Continuation;
25 import org.apache.cocoon.components.flow.java.ContinuationClassLoader;
26 import org.apache.cocoon.components.flow.java.ContinuationContext;
27 import org.apache.cocoon.components.flow.java.VarMap;
28 import org.apache.cocoon.components.flow.java.VarMapHandler;
29 import org.apache.cocoon.environment.mock.MockRequest;
30 import org.apache.cocoon.environment.mock.MockRedirector;
31 import org.apache.commons.jxpath.JXPathContext;
32 import org.apache.commons.jxpath.JXPathIntrospector;
33 import org.apache.cocoon.components.ContextHelper;
34 import org.apache.avalon.framework.context.DefaultContext;
35 import org.apache.cocoon.components.flow.FlowHelper;
36 import org.apache.cocoon.environment.ObjectModelHelper;
37
38 public class FlowTest extends TestCase {
39     public FlowTest(String JavaDoc s) {
40         super(s);
41     }
42
43     static {
44         JXPathIntrospector.registerDynamicClass(VarMap.class, VarMapHandler.class);
45     }
46
47     private static ClassLoader JavaDoc loader = new ContinuationClassLoader(FlowTest.class.getClassLoader());
48     private ContinuationContext context;
49     private MockRequest request;
50     private MockRedirector redirector;
51     private HashMap JavaDoc objectmodel;
52
53     public void setUp() throws Exception JavaDoc {
54
55         super.setUp();
56         context = new ContinuationContext();
57       
58         DefaultContext avalonContext = new DefaultContext();
59
60         request = new MockRequest();
61         avalonContext.put(ContextHelper.CONTEXT_REQUEST_OBJECT, request);
62         objectmodel = new HashMap JavaDoc();
63         objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request);
64         avalonContext.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectmodel);
65         redirector = new MockRedirector();
66
67         context.setAvalonContext(avalonContext);
68         context.setRedirector(redirector);
69     }
70
71     public void testSimple() throws Exception JavaDoc {
72
73 /* ClassLoader cl = getClass().getClassLoader();
74         while (cl != null) {
75             System.out.println(cl);
76             cl = cl.getParent();
77         }
78         try {
79             System.out.println(
80                     getClass().
81                     getProtectionDomain().
82                     getCodeSource().
83                     getLocation());
84         }
85         catch (Exception e) {
86         }*/

87
88         Class JavaDoc clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
89         Continuable flow = (Continuable) clazz.newInstance();
90
91         Method JavaDoc method = clazz.getMethod("run", new Class JavaDoc[0]);
92
93         Continuation c = new Continuation(context);
94         assertTrue(!c.isRestoring());
95         assertTrue(!c.isCapturing());
96
97         System.out.println("*** start flow");
98         c.registerThread();
99         method.invoke(flow, new Object JavaDoc[0]);
100         if (c.isCapturing())
101             c.getStack().popReference();
102         c.deregisterThread();
103         System.out.println("*** return from flow");
104
105         assertTrue(!c.isRestoring());
106         assertTrue(c.isCapturing());
107
108         //System.out.println("request=" + request);
109
request.addParameter("a", "2.3");
110         redirector.reset();
111         c = new Continuation(c, context);
112
113         assertTrue(c.isRestoring());
114         assertTrue(!c.isCapturing());
115
116         System.out.println("*** resume flow");
117         c.registerThread();
118         method.invoke(flow, new Object JavaDoc[0]);
119         if (c.isCapturing())
120             c.getStack().popReference();
121         c.deregisterThread();
122         System.out.println("*** return from flow");
123
124         assertTrue(!c.isRestoring());
125         assertTrue(!c.isCapturing());
126
127         VarMap map = (VarMap)FlowHelper.getContextObject(objectmodel);
128         
129         assertEquals(((Float JavaDoc)map.getMap().get("result")).floatValue(), 3.3f, 0.1f);
130
131         JXPathContext jxcontext = JXPathContext.newContext(FlowHelper.getContextObject(objectmodel));
132         Float JavaDoc result = (Float JavaDoc)jxcontext.getValue("result");
133
134         assertEquals(result.floatValue(), 3.3f, 0.1f);
135     }
136
137     public void testCatch() throws Exception JavaDoc {
138
139         Class JavaDoc clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
140         Continuable flow = (Continuable) clazz.newInstance();
141
142         Method JavaDoc method = clazz.getMethod("testCatch", new Class JavaDoc[0]);
143
144         Continuation c = new Continuation(context);
145         assertTrue(!c.isRestoring());
146         assertTrue(!c.isCapturing());
147
148         System.out.println("*** start flow");
149         c.registerThread();
150         method.invoke(flow, new Object JavaDoc[0]);
151         if (c.isCapturing())
152             c.getStack().popReference();
153         c.deregisterThread();
154         System.out.println("*** return from flow");
155
156         assertTrue(!c.isRestoring());
157         assertTrue(c.isCapturing());
158
159         assertEquals(redirector.getRedirect(), "cocoon:/getNumberA");
160
161         request.addParameter("a", "bla");
162         redirector.reset();
163         c = new Continuation(c, context);
164
165         assertTrue(c.isRestoring());
166         assertTrue(!c.isCapturing());
167
168         System.out.println("*** resume flow");
169         c.registerThread();
170         method.invoke(flow, new Object JavaDoc[0]);
171         if (c.isCapturing())
172             c.getStack().popReference();
173         c.deregisterThread();
174         System.out.println("*** return from flow");
175
176         assertTrue(!c.isRestoring());
177         assertTrue(c.isCapturing());
178
179         assertEquals(redirector.getRedirect(), "cocoon:/error");
180
181         redirector.reset();
182         c = new Continuation(c, context);
183
184         assertTrue(c.isRestoring());
185         assertTrue(!c.isCapturing());
186
187         System.out.println("*** resume flow");
188         c.registerThread();
189         method.invoke(flow, new Object JavaDoc[0]);
190         if (c.isCapturing())
191             c.getStack().popReference();
192         c.deregisterThread();
193         System.out.println("*** return from flow");
194
195         assertTrue(!c.isRestoring());
196         assertTrue(!c.isCapturing());
197
198         assertEquals(redirector.getRedirect(), "cocoon:/result");
199     }
200
201 /* public void testFinally() throws Exception {
202
203         Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
204         Continuable flow = (Continuable) clazz.newInstance();
205
206         Method method = clazz.getMethod("testFinally", new Class[0]);
207
208         Continuation c = new Continuation(context);
209         assertTrue(!c.isRestoring());
210         assertTrue(!c.isCapturing());
211
212         System.out.println("*** start flow");
213         c.registerThread();
214         method.invoke(flow, new Object[0]);
215         c.deregisterThread();
216         System.out.println("*** return from flow");
217
218         assertTrue(!c.isRestoring());
219         assertTrue(c.isCapturing());
220
221         assertEquals(context.getRedirectedURI(), "cocoon:/getNumberA");
222
223         request.addParameter("a", "bla");
224         redirector.reset();
225         c = new Continuation(c, context);
226
227         assertTrue(c.isRestoring());
228         assertTrue(!c.isCapturing());
229
230         System.out.println("*** resume flow");
231         c.registerThread();
232         method.invoke(flow, new Object[0]);
233         c.deregisterThread();
234         System.out.println("*** return from flow");
235
236         assertTrue(!c.isRestoring());
237         assertTrue(c.isCapturing());
238
239         assertEquals(context.getRedirectedURI(), "cocoon:/result");
240
241         redirector.reset();
242         c = new Continuation(c, context);
243
244         assertTrue(c.isRestoring());
245         assertTrue(!c.isCapturing());
246
247         try {
248
249             System.out.println("*** resume flow");
250             c.registerThread();
251             method.invoke(flow, new Object[0]);
252             c.deregisterThread();
253             System.out.println("*** return from flow");
254
255             fail("NumberFormatException should be thrown");
256         } catch (NumberFormatException nfe) {
257             // sucessful
258         }
259     }*/

260
261     public void testFormFlow() throws Exception JavaDoc {
262         Class JavaDoc clazz = loader.loadClass("org.apache.cocoon.samples.flow.java.FormFlow");
263         Continuable flow = (Continuable) clazz.newInstance();
264         
265         assertNotNull(flow);
266     }
267
268 /* public static void testOJBFlow() throws Exception {
269         ClassLoader loader = new ContinuationClassLoader(Thread.currentThread().getContextClassLoader());
270         Class clazz = loader.loadClass("org.apache.cocoon.samples.flow.java.PersistenceFlow");
271         //Class clazz = Class.forName("org.apache.cocoon.samples.flow.java.PersistenceFlow");
272         Continuable flow = (Continuable) clazz.newInstance();
273     }*/

274
275
276     public void testAbstract() throws Exception JavaDoc {
277
278         Class JavaDoc clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
279         Continuable flow = (Continuable) clazz.newInstance();
280
281         Method JavaDoc method = clazz.getMethod("testAbstract", new Class JavaDoc[0]);
282
283         Continuation c = new Continuation(context);
284         assertTrue(!c.isRestoring());
285         assertTrue(!c.isCapturing());
286
287         System.out.println("*** start flow");
288         c.registerThread();
289         method.invoke(flow, new Object JavaDoc[0]);
290         if (c.isCapturing())
291             c.getStack().popReference();
292         c.deregisterThread();
293         System.out.println("*** return from flow");
294
295         assertTrue(!c.isRestoring());
296         assertTrue(c.isCapturing());
297
298         assertEquals(redirector.getRedirect(), "cocoon:/parent");
299     }
300
301     public void testDelegate() throws Exception JavaDoc {
302
303         ClassLoader JavaDoc loader = new ContinuationClassLoader(getClass().getClassLoader());
304         Class JavaDoc clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
305         Continuable flow = (Continuable) clazz.newInstance();
306
307         Method JavaDoc method = clazz.getMethod("testDelegate", new Class JavaDoc[0]);
308
309         Continuation c = new Continuation(context);
310         assertTrue(!c.isRestoring());
311         assertTrue(!c.isCapturing());
312
313         System.out.println("*** start flow");
314         c.registerThread();
315         method.invoke(flow, new Object JavaDoc[0]);
316         if (c.isCapturing())
317             c.getStack().popReference();
318         c.deregisterThread();
319         System.out.println("*** return from flow");
320         System.out.println();
321
322         assertTrue(!c.isRestoring());
323         assertTrue(c.isCapturing());
324
325         assertEquals(redirector.getRedirect(), "cocoon:/page/getNumberA");
326
327         request.addParameter("a", "2");
328         redirector.reset();
329         c = new Continuation(c, context);
330
331         assertTrue(c.isRestoring());
332         assertTrue(!c.isCapturing());
333
334         System.out.println();
335         System.out.println("*** resume flow");
336         c.registerThread();
337         method.invoke(flow, new Object JavaDoc[0]);
338         if (c.isCapturing())
339             c.getStack().popReference();
340         c.deregisterThread();
341         System.out.println("*** return from flow");
342         System.out.println();
343
344         assertTrue(!c.isRestoring());
345         assertTrue(c.isCapturing());
346
347         assertEquals(redirector.getRedirect(), "cocoon:/page/getNumberB");
348
349         request.addParameter("b", "2");
350         redirector.reset();
351         c = new Continuation(c, context);
352
353         assertTrue(c.isRestoring());
354         assertTrue(!c.isCapturing());
355
356         System.out.println();
357         System.out.println("*** resume flow");
358         c.registerThread();
359         method.invoke(flow, new Object JavaDoc[0]);
360         if (c.isCapturing())
361             c.getStack().popReference();
362         c.deregisterThread();
363         System.out.println("*** return from flow");
364         System.out.println();
365
366         assertTrue(!c.isRestoring());
367         assertTrue(c.isCapturing());
368
369         assertEquals(redirector.getRedirect(), "cocoon:/page/getOperator");
370         
371         request.addParameter("operator", "plus");
372         redirector.reset();
373         c = new Continuation(c, context);
374
375         assertTrue(c.isRestoring());
376         assertTrue(!c.isCapturing());
377
378         System.out.println();
379         System.out.println("*** resume flow");
380         c.registerThread();
381         method.invoke(flow, new Object JavaDoc[0]);
382         if (c.isCapturing())
383             c.getStack().popReference();
384         c.deregisterThread();
385         System.out.println("*** return from flow");
386         System.out.println();
387
388         assertTrue(!c.isRestoring());
389         assertTrue(!c.isCapturing());
390
391         assertEquals(redirector.getRedirect(), "cocoon:/page/displayResult");
392     }
393
394     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
395         new FlowTest("test").testDelegate();
396     }
397 }
398
Popular Tags