001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.flow.java.test;
018:
019: import java.lang.reflect.Method;
020: import java.util.HashMap;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.cocoon.components.flow.java.Continuable;
025: import org.apache.cocoon.components.flow.java.Continuation;
026: import org.apache.cocoon.components.flow.java.ContinuationClassLoader;
027: import org.apache.cocoon.components.flow.java.ContinuationContext;
028: import org.apache.cocoon.components.flow.java.VarMap;
029: import org.apache.cocoon.components.flow.java.VarMapHandler;
030: import org.apache.cocoon.environment.mock.MockRequest;
031: import org.apache.cocoon.environment.mock.MockRedirector;
032: import org.apache.commons.jxpath.JXPathContext;
033: import org.apache.commons.jxpath.JXPathIntrospector;
034: import org.apache.cocoon.components.ContextHelper;
035: import org.apache.avalon.framework.context.DefaultContext;
036: import org.apache.cocoon.components.flow.FlowHelper;
037: import org.apache.cocoon.environment.ObjectModelHelper;
038:
039: public class FlowTest extends TestCase {
040: public FlowTest(String s) {
041: super (s);
042: }
043:
044: static {
045: JXPathIntrospector.registerDynamicClass(VarMap.class,
046: VarMapHandler.class);
047: }
048:
049: private static ClassLoader loader = new ContinuationClassLoader(
050: FlowTest.class.getClassLoader());
051: private ContinuationContext context;
052: private MockRequest request;
053: private MockRedirector redirector;
054: private HashMap objectmodel;
055:
056: public void setUp() throws Exception {
057:
058: super .setUp();
059: context = new ContinuationContext();
060:
061: DefaultContext avalonContext = new DefaultContext();
062:
063: request = new MockRequest();
064: avalonContext
065: .put(ContextHelper.CONTEXT_REQUEST_OBJECT, request);
066: objectmodel = new HashMap();
067: objectmodel.put(ObjectModelHelper.REQUEST_OBJECT, request);
068: avalonContext.put(ContextHelper.CONTEXT_OBJECT_MODEL,
069: objectmodel);
070: redirector = new MockRedirector();
071:
072: context.setAvalonContext(avalonContext);
073: context.setRedirector(redirector);
074: }
075:
076: public void testSimple() throws Exception {
077:
078: /* ClassLoader cl = getClass().getClassLoader();
079: while (cl != null) {
080: System.out.println(cl);
081: cl = cl.getParent();
082: }
083: try {
084: System.out.println(
085: getClass().
086: getProtectionDomain().
087: getCodeSource().
088: getLocation());
089: }
090: catch (Exception e) {
091: }*/
092:
093: Class clazz = loader
094: .loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
095: Continuable flow = (Continuable) clazz.newInstance();
096:
097: Method method = clazz.getMethod("run", new Class[0]);
098:
099: Continuation c = new Continuation(context);
100: assertTrue(!c.isRestoring());
101: assertTrue(!c.isCapturing());
102:
103: System.out.println("*** start flow");
104: c.registerThread();
105: method.invoke(flow, new Object[0]);
106: if (c.isCapturing())
107: c.getStack().popReference();
108: c.deregisterThread();
109: System.out.println("*** return from flow");
110:
111: assertTrue(!c.isRestoring());
112: assertTrue(c.isCapturing());
113:
114: //System.out.println("request=" + request);
115: request.addParameter("a", "2.3");
116: redirector.reset();
117: c = new Continuation(c, context);
118:
119: assertTrue(c.isRestoring());
120: assertTrue(!c.isCapturing());
121:
122: System.out.println("*** resume flow");
123: c.registerThread();
124: method.invoke(flow, new Object[0]);
125: if (c.isCapturing())
126: c.getStack().popReference();
127: c.deregisterThread();
128: System.out.println("*** return from flow");
129:
130: assertTrue(!c.isRestoring());
131: assertTrue(!c.isCapturing());
132:
133: VarMap map = (VarMap) FlowHelper.getContextObject(objectmodel);
134:
135: assertEquals(((Float) map.getMap().get("result")).floatValue(),
136: 3.3f, 0.1f);
137:
138: JXPathContext jxcontext = JXPathContext.newContext(FlowHelper
139: .getContextObject(objectmodel));
140: Float result = (Float) jxcontext.getValue("result");
141:
142: assertEquals(result.floatValue(), 3.3f, 0.1f);
143: }
144:
145: public void testCatch() throws Exception {
146:
147: Class clazz = loader
148: .loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
149: Continuable flow = (Continuable) clazz.newInstance();
150:
151: Method method = clazz.getMethod("testCatch", new Class[0]);
152:
153: Continuation c = new Continuation(context);
154: assertTrue(!c.isRestoring());
155: assertTrue(!c.isCapturing());
156:
157: System.out.println("*** start flow");
158: c.registerThread();
159: method.invoke(flow, new Object[0]);
160: if (c.isCapturing())
161: c.getStack().popReference();
162: c.deregisterThread();
163: System.out.println("*** return from flow");
164:
165: assertTrue(!c.isRestoring());
166: assertTrue(c.isCapturing());
167:
168: assertEquals(redirector.getRedirect(), "cocoon:/getNumberA");
169:
170: request.addParameter("a", "bla");
171: redirector.reset();
172: c = new Continuation(c, context);
173:
174: assertTrue(c.isRestoring());
175: assertTrue(!c.isCapturing());
176:
177: System.out.println("*** resume flow");
178: c.registerThread();
179: method.invoke(flow, new Object[0]);
180: if (c.isCapturing())
181: c.getStack().popReference();
182: c.deregisterThread();
183: System.out.println("*** return from flow");
184:
185: assertTrue(!c.isRestoring());
186: assertTrue(c.isCapturing());
187:
188: assertEquals(redirector.getRedirect(), "cocoon:/error");
189:
190: redirector.reset();
191: c = new Continuation(c, context);
192:
193: assertTrue(c.isRestoring());
194: assertTrue(!c.isCapturing());
195:
196: System.out.println("*** resume flow");
197: c.registerThread();
198: method.invoke(flow, new Object[0]);
199: if (c.isCapturing())
200: c.getStack().popReference();
201: c.deregisterThread();
202: System.out.println("*** return from flow");
203:
204: assertTrue(!c.isRestoring());
205: assertTrue(!c.isCapturing());
206:
207: assertEquals(redirector.getRedirect(), "cocoon:/result");
208: }
209:
210: /* public void testFinally() throws Exception {
211:
212: Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
213: Continuable flow = (Continuable) clazz.newInstance();
214:
215: Method method = clazz.getMethod("testFinally", new Class[0]);
216:
217: Continuation c = new Continuation(context);
218: assertTrue(!c.isRestoring());
219: assertTrue(!c.isCapturing());
220:
221: System.out.println("*** start flow");
222: c.registerThread();
223: method.invoke(flow, new Object[0]);
224: c.deregisterThread();
225: System.out.println("*** return from flow");
226:
227: assertTrue(!c.isRestoring());
228: assertTrue(c.isCapturing());
229:
230: assertEquals(context.getRedirectedURI(), "cocoon:/getNumberA");
231:
232: request.addParameter("a", "bla");
233: redirector.reset();
234: c = new Continuation(c, context);
235:
236: assertTrue(c.isRestoring());
237: assertTrue(!c.isCapturing());
238:
239: System.out.println("*** resume flow");
240: c.registerThread();
241: method.invoke(flow, new Object[0]);
242: c.deregisterThread();
243: System.out.println("*** return from flow");
244:
245: assertTrue(!c.isRestoring());
246: assertTrue(c.isCapturing());
247:
248: assertEquals(context.getRedirectedURI(), "cocoon:/result");
249:
250: redirector.reset();
251: c = new Continuation(c, context);
252:
253: assertTrue(c.isRestoring());
254: assertTrue(!c.isCapturing());
255:
256: try {
257:
258: System.out.println("*** resume flow");
259: c.registerThread();
260: method.invoke(flow, new Object[0]);
261: c.deregisterThread();
262: System.out.println("*** return from flow");
263:
264: fail("NumberFormatException should be thrown");
265: } catch (NumberFormatException nfe) {
266: // sucessful
267: }
268: }*/
269:
270: public void testFormFlow() throws Exception {
271: Class clazz = loader
272: .loadClass("org.apache.cocoon.samples.flow.java.FormFlow");
273: Continuable flow = (Continuable) clazz.newInstance();
274:
275: assertNotNull(flow);
276: }
277:
278: /* public static void testOJBFlow() throws Exception {
279: ClassLoader loader = new ContinuationClassLoader(Thread.currentThread().getContextClassLoader());
280: Class clazz = loader.loadClass("org.apache.cocoon.samples.flow.java.PersistenceFlow");
281: //Class clazz = Class.forName("org.apache.cocoon.samples.flow.java.PersistenceFlow");
282: Continuable flow = (Continuable) clazz.newInstance();
283: }*/
284:
285: public void testAbstract() throws Exception {
286:
287: Class clazz = loader
288: .loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
289: Continuable flow = (Continuable) clazz.newInstance();
290:
291: Method method = clazz.getMethod("testAbstract", new Class[0]);
292:
293: Continuation c = new Continuation(context);
294: assertTrue(!c.isRestoring());
295: assertTrue(!c.isCapturing());
296:
297: System.out.println("*** start flow");
298: c.registerThread();
299: method.invoke(flow, new Object[0]);
300: if (c.isCapturing())
301: c.getStack().popReference();
302: c.deregisterThread();
303: System.out.println("*** return from flow");
304:
305: assertTrue(!c.isRestoring());
306: assertTrue(c.isCapturing());
307:
308: assertEquals(redirector.getRedirect(), "cocoon:/parent");
309: }
310:
311: public void testDelegate() throws Exception {
312:
313: ClassLoader loader = new ContinuationClassLoader(getClass()
314: .getClassLoader());
315: Class clazz = loader
316: .loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow");
317: Continuable flow = (Continuable) clazz.newInstance();
318:
319: Method method = clazz.getMethod("testDelegate", new Class[0]);
320:
321: Continuation c = new Continuation(context);
322: assertTrue(!c.isRestoring());
323: assertTrue(!c.isCapturing());
324:
325: System.out.println("*** start flow");
326: c.registerThread();
327: method.invoke(flow, new Object[0]);
328: if (c.isCapturing())
329: c.getStack().popReference();
330: c.deregisterThread();
331: System.out.println("*** return from flow");
332: System.out.println();
333:
334: assertTrue(!c.isRestoring());
335: assertTrue(c.isCapturing());
336:
337: assertEquals(redirector.getRedirect(),
338: "cocoon:/page/getNumberA");
339:
340: request.addParameter("a", "2");
341: redirector.reset();
342: c = new Continuation(c, context);
343:
344: assertTrue(c.isRestoring());
345: assertTrue(!c.isCapturing());
346:
347: System.out.println();
348: System.out.println("*** resume flow");
349: c.registerThread();
350: method.invoke(flow, new Object[0]);
351: if (c.isCapturing())
352: c.getStack().popReference();
353: c.deregisterThread();
354: System.out.println("*** return from flow");
355: System.out.println();
356:
357: assertTrue(!c.isRestoring());
358: assertTrue(c.isCapturing());
359:
360: assertEquals(redirector.getRedirect(),
361: "cocoon:/page/getNumberB");
362:
363: request.addParameter("b", "2");
364: redirector.reset();
365: c = new Continuation(c, context);
366:
367: assertTrue(c.isRestoring());
368: assertTrue(!c.isCapturing());
369:
370: System.out.println();
371: System.out.println("*** resume flow");
372: c.registerThread();
373: method.invoke(flow, new Object[0]);
374: if (c.isCapturing())
375: c.getStack().popReference();
376: c.deregisterThread();
377: System.out.println("*** return from flow");
378: System.out.println();
379:
380: assertTrue(!c.isRestoring());
381: assertTrue(c.isCapturing());
382:
383: assertEquals(redirector.getRedirect(),
384: "cocoon:/page/getOperator");
385:
386: request.addParameter("operator", "plus");
387: redirector.reset();
388: c = new Continuation(c, context);
389:
390: assertTrue(c.isRestoring());
391: assertTrue(!c.isCapturing());
392:
393: System.out.println();
394: System.out.println("*** resume flow");
395: c.registerThread();
396: method.invoke(flow, new Object[0]);
397: if (c.isCapturing())
398: c.getStack().popReference();
399: c.deregisterThread();
400: System.out.println("*** return from flow");
401: System.out.println();
402:
403: assertTrue(!c.isRestoring());
404: assertTrue(!c.isCapturing());
405:
406: assertEquals(redirector.getRedirect(),
407: "cocoon:/page/displayResult");
408: }
409:
410: public static void main(String[] args) throws Exception {
411: new FlowTest("test").testDelegate();
412: }
413: }
|