KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > test > ActionFlowExecutorTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.actionflow.test;
8
9
10 import java.io.File JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
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 /**
34  * This class tests the ActionFlowExecutor class
35  *
36  * @author Brian Pontarelli
37  * @since 2.0
38  * @version 2.0
39  */

40 public class ActionFlowExecutorTest extends WebTestCase {
41
42     /**
43      * The full path to the test XML file
44      */

45     public static final String JavaDoc TEST_FILE =
46         "src/com/inversoft/verge/mvc/controller/actionflow/test/good-config.xml";
47     public static final String JavaDoc TEST_FILE2 =
48         "src/com/inversoft/verge/mvc/controller/actionflow/test/good-config2.xml";
49
50
51     /**
52      * Constructs a new <code>ActionFlowExecutorTest</code>.
53      */

54     public ActionFlowExecutorTest(String JavaDoc name) {
55         super(name);
56         setLocal(true);
57     }
58
59
60     /**
61      * Sets up the defaults and the config store
62      */

63     public void setUp() {
64         try {
65             SAXBuilder saxBuilder = new SAXBuilder();
66             Document document = saxBuilder.build(new File JavaDoc(TEST_FILE));
67             Document document2 = saxBuilder.build(new File JavaDoc(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 JavaDoc registries = new HashMap JavaDoc();
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     /**
90      * This method tests the ActionFlowExecutors execute method
91      */

92     public void testExecutor() {
93         try {
94             ActionFlowExecutor.execute(this.request, this.response, "namespace",
95                 "/index.jsp", "login", null);
96         } catch (Exception JavaDoc 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     /**
108      * This method tests that the ActionFlowExecutors uses it the default node
109      */

110     public void testExecutorDefault() {
111         try {
112             ActionFlowExecutor.execute(this.request, this.response, "namespace",
113                 null, "login", null);
114         } catch (Exception JavaDoc e) {
115             fail(e.toString());
116         }
117
118         // Call first to establish a node
119
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     /**
127      * This method tests the ActionFlowState preserves the node for a namespace
128      * and that the ActionFlowExecutors uses it
129      */

130     public void testExecutorState() {
131         // Call first to establish a node
132
try {
133             ActionFlowExecutor.execute(this.request, this.response, "namespace",
134                 "/index.jsp", "login", null);
135         } catch (Exception JavaDoc e) {
136             fail(e.toString());
137         }
138
139         // Call first to establish a node
140
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         // Call to use state
147
try {
148             ActionFlowExecutor.execute(this.request, this.response, "namespace",
149                 null, "logout", null);
150         } catch (Exception JavaDoc e) {
151             fail(e.toString());
152         }
153
154         // Call first to establish a node
155
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     /**
163      * This method tests the ActionFlowExecutors execute method failures
164      */

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 JavaDoc 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 JavaDoc 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 JavaDoc t) {
191             System.out.println(t.toString());
192             assertTrue("Throwable should be an ActionFlowException",
193                 t instanceof ActionFlowException);
194         }
195     }
196
197     /**
198      * This method tests the ActionFlowExecutors exception handling
199      */

200     public void testException() {
201         try {
202             ActionFlowExecutor.execute(this.request, this.response, "namespace",
203                 "/index.jsp", "failure", null);
204         } catch (Exception JavaDoc 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     /**
235      * This method tests that the ActionFlowExecutor calls the ExceptionHandler
236      */

237     public void testExceptionHandler() throws Exception JavaDoc {
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     /**
248      * This method tests that the ActionFlowExecutor calls multiple items
249      */

250     public void testTwoStep() {
251         try {
252             ActionFlowExecutor.execute(this.request, this.response, "namespace",
253                 "/index.jsp", "twoStep", null);
254         } catch (Exception JavaDoc 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     /**
267      * This method tests that the ActionFlowExecutor calls the ExceptionHandler
268      */

269     public void testThreeStep() {
270         try {
271             ActionFlowExecutor.execute(this.request, this.response, "namespace",
272                 "/index.jsp", "threeStep1", null);
273         } catch (Exception JavaDoc 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     /**
287      * This method tests that the ActionFlowExecutor calls the ExceptionHandler
288      */

289     public void testPresToPres() {
290         try {
291             ActionFlowExecutor.execute(this.request, this.response, "namespace",
292                 "/go-back.jsp", "go-back", null);
293         } catch (Exception JavaDoc 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