1 7 package com.inversoft.verge.mvc.controller.actionflow.test; 8 9 10 import java.io.File ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import org.jdom.Document; 15 import org.jdom.JDOMException; 16 import org.jdom.input.SAXBuilder; 17 18 import com.inversoft.config.ConfigurationException; 19 import com.inversoft.junit.WebTestCase; 20 import com.inversoft.junit.internal.http.MockHttpServletRequest; 21 import com.inversoft.verge.config.VergeConfigConstants; 22 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowException; 23 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowExecutor; 24 import com.inversoft.verge.mvc.controller.actionflow.ActionFlowState; 25 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigBuilder; 26 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigFactory; 27 import com.inversoft.verge.mvc.controller.actionflow.config.ActionFlowConfigRegistry; 28 import com.inversoft.verge.mvc.controller.actionflow.config.Node; 29 import com.inversoft.verge.repository.config.RepositoryConfigRegistry; 30 31 32 33 40 public class ActionFlowExecutorTest extends WebTestCase { 41 42 45 public static final String TEST_FILE = 46 "src/com/inversoft/verge/mvc/controller/actionflow/test/good-config.xml"; 47 public static final String TEST_FILE2 = 48 "src/com/inversoft/verge/mvc/controller/actionflow/test/good-config2.xml"; 49 50 51 54 public ActionFlowExecutorTest(String name) { 55 super(name); 56 setLocal(true); 57 } 58 59 60 63 public void setUp() { 64 try { 65 SAXBuilder saxBuilder = new SAXBuilder(); 66 Document document = saxBuilder.build(new File (TEST_FILE)); 67 Document document2 = saxBuilder.build(new File (TEST_FILE2)); 68 69 ActionFlowConfigBuilder builder = new ActionFlowConfigBuilder(); 70 ActionFlowConfigFactory factory = new ActionFlowConfigFactory(); 71 ActionFlowConfigRegistry registry = 72 (ActionFlowConfigRegistry) factory.createRegistry(); 73 builder.build(document, registry); 74 builder.build(document2, registry); 75 76 Map registries = new HashMap (); 77 registries.put(VergeConfigConstants.REPOSITORY_KEY, 78 RepositoryConfigRegistry.getInstance( 79 new MockHttpServletRequest(null))); 80 builder.validate(registry, registries); 81 builder.commit(registry, registries); 82 } catch (ConfigurationException ce) { 83 fail(ce.toString()); 84 } catch (JDOMException jdome) { 85 fail(jdome.toString()); 86 } 87 } 88 89 92 public void testExecutor() { 93 try { 94 ActionFlowExecutor.execute(this.request, this.response, "namespace", 95 "/index.jsp", "login", null); 96 } catch (Exception e) { 97 fail(e.toString()); 98 } 99 100 assertTrue("ActionHandler should have been called", ActionHandler.isCalled()); 101 ActionFlowState state = new ActionFlowState(request); 102 Node node = state.getCurrentNodeForNamespace("namespace"); 103 assertTrue("Should have ended on /success.jsp", 104 node.getName().equals("/success.jsp")); 105 } 106 107 110 public void testExecutorDefault() { 111 try { 112 ActionFlowExecutor.execute(this.request, this.response, "namespace", 113 null, "login", null); 114 } catch (Exception e) { 115 fail(e.toString()); 116 } 117 118 assertTrue("ActionHandler should have been called", ActionHandler.isCalled()); 120 ActionFlowState state = new ActionFlowState(request); 121 Node node = state.getCurrentNodeForNamespace("namespace"); 122 assertTrue("Should have ended on /success.jsp", 123 node.getName().equals("/success.jsp")); 124 } 125 126 130 public void testExecutorState() { 131 try { 133 ActionFlowExecutor.execute(this.request, this.response, "namespace", 134 "/index.jsp", "login", null); 135 } catch (Exception e) { 136 fail(e.toString()); 137 } 138 139 assertTrue("ActionHandler should have been called", ActionHandler.isCalled()); 141 ActionFlowState state = new ActionFlowState(request); 142 Node node = state.getCurrentNodeForNamespace("namespace"); 143 assertTrue("Should have ended on /success.jsp", 144 node.getName().equals("/success.jsp")); 145 146 try { 148 ActionFlowExecutor.execute(this.request, this.response, "namespace", 149 null, "logout", null); 150 } catch (Exception e) { 151 fail(e.toString()); 152 } 153 154 assertTrue("ActionHandler should have been called", ActionHandler.isLogoutCalled()); 156 state = new ActionFlowState(request); 157 node = state.getCurrentNodeForNamespace("namespace"); 158 assertTrue("Should have ended on /index.jsp", 159 node.getName().equals("/index.jsp")); 160 } 161 162 165 public void testFailure() { 166 try { 167 ActionFlowExecutor.execute(this.request, this.response, "foo", 168 "/index.jsp", "login", null); 169 fail("Should have failed, foo is not a namespace"); 170 } catch (Throwable t) { 171 System.out.println(t.toString()); 172 assertTrue("Throwable should be an ActionFlowException", 173 t instanceof ActionFlowException); 174 } 175 176 try { 177 ActionFlowExecutor.execute(this.request, this.response, "namespace", 178 "foo", "login", null); 179 fail("Should have failed, foo is not a valid entry node"); 180 } catch (Throwable t) { 181 System.out.println(t.toString()); 182 assertTrue("Throwable should be an ActionFlowException", 183 t instanceof ActionFlowException); 184 } 185 186 try { 187 ActionFlowExecutor.execute(this.request, this.response, "namespace", 188 "/index.jsp", "foo", null); 189 fail("Should have failed, foo is not a valid link"); 190 } catch (Throwable t) { 191 System.out.println(t.toString()); 192 assertTrue("Throwable should be an ActionFlowException", 193 t instanceof ActionFlowException); 194 } 195 } 196 197 200 public void testException() { 201 try { 202 ActionFlowExecutor.execute(this.request, this.response, "namespace", 203 "/index.jsp", "failure", null); 204 } catch (Exception e) { 205 fail(e.toString()); 206 } 207 208 assertTrue("ActionHandler should have been called", 209 ActionHandler.isCalled()); 210 ActionFlowState state = new ActionFlowState(request); 211 Node node = state.getCurrentNodeForNamespace("namespace"); 212 assertTrue("Should have ended on /failure.jsp", 213 node.getName().equals("/failure.jsp")); 214 } 215 216 public void testException2() { 217 try { 218 ActionFlowExecutor.execute(this.request, this.response, "namespace", 219 "/index.jsp", "failure2", null); 220 } catch (ActionFlowException e) { 221 fail(e.toString()); 222 } 223 224 assertTrue("ActionHandler should have been called", 225 ActionHandler.isCalled()); 226 assertTrue("ActionHandler should have been exceptionCalled", 227 ActionHandler2.isExceptionCalled()); 228 ActionFlowState state = new ActionFlowState(request); 229 Node node = state.getCurrentNodeForNamespace("namespace"); 230 assertTrue("Should have ended on /error.jsp", 231 node.getName().equals("/error.jsp")); 232 } 233 234 237 public void testExceptionHandler() throws Exception { 238 ActionFlowExecutor.execute(this.request, this.response, "namespace2", 239 "/index.jsp", "foo", null); 240 241 assertTrue("Should have forwarded to /error.jsp", 242 getRequest().getRequestDispatcher().getURL().equals("/error.jsp")); 243 assertTrue("Should have forwarded to /error.jsp", 244 getRequest().getRequestDispatcher().isForwarded()); 245 } 246 247 250 public void testTwoStep() { 251 try { 252 ActionFlowExecutor.execute(this.request, this.response, "namespace", 253 "/index.jsp", "twoStep", null); 254 } catch (Exception e) { 255 fail(e.toString()); 256 } 257 258 assertTrue("ActionHandler step1 should have been called", ActionHandler.step1); 259 assertTrue("ActionHandler step2 should have been called", ActionHandler.step2); 260 ActionFlowState state = new ActionFlowState(request); 261 Node node = state.getCurrentNodeForNamespace("namespace"); 262 assertTrue("Should have ended on /success.jsp", 263 node.getName().equals("/success.jsp")); 264 } 265 266 269 public void testThreeStep() { 270 try { 271 ActionFlowExecutor.execute(this.request, this.response, "namespace", 272 "/index.jsp", "threeStep1", null); 273 } catch (Exception e) { 274 fail(e.toString()); 275 } 276 277 assertTrue("ActionHandler step1 should have been called", ActionHandler.step1); 278 assertTrue("ActionHandler step2 should have been called", ActionHandler.step2); 279 assertTrue("ActionHandler2 step3 should have been called", ActionHandler2.step3); 280 ActionFlowState state = new ActionFlowState(request); 281 Node node = state.getCurrentNodeForNamespace("namespace"); 282 assertTrue("Should have ended on /success.jsp", 283 node.getName().equals("/success.jsp")); 284 } 285 286 289 public void testPresToPres() { 290 try { 291 ActionFlowExecutor.execute(this.request, this.response, "namespace", 292 "/go-back.jsp", "go-back", null); 293 } catch (Exception e) { 294 fail(e.toString()); 295 } 296 297 ActionFlowState state = new ActionFlowState(request); 298 Node node = state.getCurrentNodeForNamespace("namespace"); 299 assertTrue("Should have ended on /beginning.jsp", 300 node.getName().equals("/beginning.jsp")); 301 } 302 } | Popular Tags |