1 16 package org.apache.cocoon.components.flow.java.test; 17 18 import java.lang.reflect.Method ; 19 import java.util.HashMap ; 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 s) { 40 super(s); 41 } 42 43 static { 44 JXPathIntrospector.registerDynamicClass(VarMap.class, VarMapHandler.class); 45 } 46 47 private static ClassLoader loader = new ContinuationClassLoader(FlowTest.class.getClassLoader()); 48 private ContinuationContext context; 49 private MockRequest request; 50 private MockRedirector redirector; 51 private HashMap objectmodel; 52 53 public void setUp() throws Exception { 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 (); 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 { 72 73 87 88 Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow"); 89 Continuable flow = (Continuable) clazz.newInstance(); 90 91 Method method = clazz.getMethod("run", new Class [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 [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 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 [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 )map.getMap().get("result")).floatValue(), 3.3f, 0.1f); 130 131 JXPathContext jxcontext = JXPathContext.newContext(FlowHelper.getContextObject(objectmodel)); 132 Float result = (Float )jxcontext.getValue("result"); 133 134 assertEquals(result.floatValue(), 3.3f, 0.1f); 135 } 136 137 public void testCatch() throws Exception { 138 139 Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow"); 140 Continuable flow = (Continuable) clazz.newInstance(); 141 142 Method method = clazz.getMethod("testCatch", new Class [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 [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 [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 [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 260 261 public void testFormFlow() throws Exception { 262 Class clazz = loader.loadClass("org.apache.cocoon.samples.flow.java.FormFlow"); 263 Continuable flow = (Continuable) clazz.newInstance(); 264 265 assertNotNull(flow); 266 } 267 268 274 275 276 public void testAbstract() throws Exception { 277 278 Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow"); 279 Continuable flow = (Continuable) clazz.newInstance(); 280 281 Method method = clazz.getMethod("testAbstract", new Class [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 [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 { 302 303 ClassLoader loader = new ContinuationClassLoader(getClass().getClassLoader()); 304 Class clazz = loader.loadClass("org.apache.cocoon.components.flow.java.test.SimpleFlow"); 305 Continuable flow = (Continuable) clazz.newInstance(); 306 307 Method method = clazz.getMethod("testDelegate", new Class [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 [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 [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 [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 [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 [] args) throws Exception { 395 new FlowTest("test").testDelegate(); 396 } 397 } 398 | Popular Tags |