KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > test > FormControllerHandlerTest


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.form.test;
8
9
10 import java.io.IOException JavaDoc;
11 import java.net.URISyntaxException JavaDoc;
12
13 import javax.servlet.ServletException JavaDoc;
14
15 import org.jdom.Document;
16 import org.jdom.Element;
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.config.servlet.ConfigServlet;
23 import com.inversoft.verge.mvc.MVCException;
24 import com.inversoft.verge.mvc.MVCRegistry;
25 import com.inversoft.verge.mvc.MVCRequest;
26 import com.inversoft.verge.mvc.controller.ControllerMVCInfo;
27 import com.inversoft.verge.mvc.controller.Result;
28 import com.inversoft.verge.mvc.controller.form.FormControllerHandler;
29 import com.inversoft.verge.mvc.controller.form.FormMVCConstants;
30 import com.inversoft.verge.mvc.controller.form.FormURLTools;
31 import com.inversoft.verge.mvc.controller.form.config.Constants;
32 import com.inversoft.verge.mvc.controller.form.config.FormMVCConfigBuilder;
33 import com.inversoft.verge.mvc.controller.form.config.FormMVCConfigRegistry;
34 import com.inversoft.verge.util.url.test.URLGeneratorTest;
35
36
37 /**
38  * <p>
39  * This class is a test for the form controller handler
40  * </p>
41  *
42  * @author Brian Pontarelli
43  * @since 2.0
44  * @version 2.0
45  */

46 public class FormControllerHandlerTest extends WebTestCase {
47
48     static boolean setup = false;
49
50
51     /**
52      * Constructor for FormControllerHandlerTest.
53      * @param name
54      */

55     public FormControllerHandlerTest(String JavaDoc name) {
56         super(name);
57         setLocal(true);
58     }
59
60
61     /**
62      * Initialize the form MVC config
63      */

64     public void setUp() {
65         if (!setup) {
66             build();
67             setup = true;
68         }
69     }
70
71     /**
72      * Tests everything
73      */

74     public void testAll() {
75
76         // Call the handler
77
FormControllerHandler handler = new FormControllerHandler();
78         MVCRegistry.register("handler", handler);
79
80         try {
81             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.testAction");
82             MVCRequest mvcRequest = new MVCRequest(request, response);
83             mvcRequest.setControllerInfo(info);
84
85             handler.preExecute(mvcRequest);
86             handler.execute(mvcRequest);
87         } catch (MVCException mvce) {
88             fail(mvce.toString());
89         }
90
91         // Test that the handler is in the request
92
TestActionHandler ah =
93             (TestActionHandler) request.getAttribute(
94                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
95         assertTrue("Should be in request", ah != null);
96         assertTrue("Should have called handleTestAction", ah.action);
97     }
98
99     /**
100      * Tests failure
101      */

102     public void testFailure() {
103
104         // Call the handler
105
FormControllerHandler handler = new FormControllerHandler();
106         MVCRegistry.register("handler", handler);
107
108         try {
109             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.doesNotExist");
110             MVCRequest mvcRequest = new MVCRequest(request, response);
111             mvcRequest.setControllerInfo(info);
112
113             handler.preExecute(mvcRequest);
114             handler.execute(mvcRequest);
115             fail("Should have failed, because action handler does not exist");
116         } catch (MVCException mvce) {
117             System.out.println(mvce.toString());
118         }
119     }
120
121     /**
122      * Tests sending a String result and forwarding the request to another page
123      */

124     public void testMappingReturn() {
125
126         // Call the handler
127
FormControllerHandler handler = new FormControllerHandler();
128         Result result = null;
129         MVCRegistry.register("handler", handler);
130
131         try {
132             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.testMapping");
133             MVCRequest mvcRequest = new MVCRequest(request, response);
134             mvcRequest.setControllerInfo(info);
135
136             handler.preExecute(mvcRequest);
137             result = handler.execute(mvcRequest);
138         } catch (MVCException mvce) {
139             fail(mvce.toString());
140         }
141
142         // Test that the handler is in the request
143
TestActionHandler ah =
144             (TestActionHandler) request.getAttribute(
145                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
146         assertTrue("Should be in request", ah != null);
147         assertTrue("Should have called handleTestMapping", ah.mapping);
148         assertEquals("Should be forwarded to testMapping1.jsp", result.getURL(),
149             "testMapping1.jsp");
150     }
151
152     /**
153      * Tests sending a String result and redirecting the request to another page
154      */

155     public void testMappingRedirect() {
156
157         // Setup some categories
158
new URLGeneratorTest("");
159
160         // Call the handler
161
FormControllerHandler handler = new FormControllerHandler();
162         Result result = null;
163         MVCRegistry.register("handler", handler);
164
165         try {
166             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1");
167             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
168                 FormURLTools.generateSubmitParameter("submitName", "testMapping6"));
169             getRequest().setParameter("submitName", "value.x");
170             MVCRequest mvcRequest = new MVCRequest(request, response);
171             mvcRequest.setControllerInfo(info);
172
173             handler.preExecute(mvcRequest);
174             result = handler.execute(mvcRequest);
175         } catch (MVCException mvce) {
176             fail(mvce.toString());
177         }
178
179         // Test that the handler is in the request
180
TestActionHandler ah =
181             (TestActionHandler) request.getAttribute(
182                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
183         assertTrue("Should be in request", ah != null);
184         assertTrue("Should have called handleTestMapping", ah.mapping);
185
186         try {
187             String JavaDoc expected = "https://www.inversoft.com:8000/MyWebApp/testMapping5.jsp";
188             assertEquals(expected, result.getGeneratedURL(request));
189         } catch (URISyntaxException JavaDoc use) {
190             fail(use.toString());
191         }
192     }
193
194     /**
195      * Tests the pre and post methods
196      */

197     public void testPreAndPost() {
198
199         // Call the handler
200
FormControllerHandler handler = new FormControllerHandler();
201         MVCRegistry.register("handler", handler);
202
203         TestActionHandler2.pre = false;
204         TestActionHandler2.post = false;
205         try {
206             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.testPreAndPost");
207             MVCRequest mvcRequest = new MVCRequest(request, response);
208             mvcRequest.setControllerInfo(info);
209
210             handler.preExecute(mvcRequest);
211             handler.execute(mvcRequest);
212         } catch (MVCException mvce) {
213             fail(mvce.toString());
214         }
215
216         // Test that the handler is in the request
217
TestActionHandler2 ah =
218             (TestActionHandler2) request.getAttribute(
219                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
220         assertNotNull("Should be in request", ah);
221         assertTrue("Should have called preHandle", TestActionHandler2.pre);
222         assertTrue("Should have called postHandle", TestActionHandler2.post);
223     }
224
225     /**
226      * Tests the long txn handling
227      */

228     public void testLongTxn() {
229
230         // Call the handler
231
FormControllerHandler handler = new FormControllerHandler();
232         MVCRegistry.register("handler", handler);
233
234         TestActionHandler2.longTxn = true;
235         try {
236             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.testLongTxn");
237             MVCRequest mvcRequest = new MVCRequest(request, response);
238             mvcRequest.setControllerInfo(info);
239
240             handler.preExecute(mvcRequest);
241             handler.execute(mvcRequest);
242         } catch (MVCException mvce) {
243             fail(mvce.toString());
244         }
245
246         // Test that the handler is in the request
247
TestActionHandler2 ah =
248             (TestActionHandler2) request.getAttribute(
249                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
250         assertNotNull("Should be in request", ah);
251         String JavaDoc expected = "<head><meta http-equiv='REFRESH' content='0;url=/doneLongTxn.jsp'/></head></html>";
252         String JavaDoc actual = getResponse().getText();
253         System.out.println("Expected: " + expected);
254         System.out.println("Actual: " + actual);
255         assertEquals("Should have written out", expected, actual);
256     }
257
258     /**
259      * Tests disabling the meta data
260      */

261     public void testMetaDataDisable() {
262
263         // Call the handler
264
FormControllerHandler handler = new FormControllerHandler();
265         MVCRequest mvcRequest = new MVCRequest(request, response);
266         MVCRegistry.register("handler", handler);
267
268         try {
269             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.form1/a.testDisabled");
270             mvcRequest.setControllerInfo(info);
271
272             handler.preExecute(mvcRequest);
273             handler.execute(mvcRequest);
274         } catch (MVCException mvce) {
275             fail(mvce.toString());
276         }
277
278         // Test that the handler is in the request
279
TestActionHandler ah =
280             (TestActionHandler) request.getAttribute(
281                 FormMVCConstants.ACTION_HANDLER_REQUEST_KEY);
282         assertTrue("Should be in request", ah != null);
283         assertTrue("Should have model disabled", !mvcRequest.isModelEnabled());
284         assertTrue("Should have validation disabled", !mvcRequest.isValidationEnabled());
285     }
286
287     /**
288      * Tests the global actions and mappings
289      */

290     public void testGlobalActionAndMappingWithForm() {
291
292         // Setup the configuration using the ConfigServlet
293
setupConfig("src/com/inversoft/verge/mvc/controller/form/test/test-form-mvc.xml");
294
295         // Call the handler
296
FormControllerHandler handler = new FormControllerHandler();
297         MVCRegistry.register("handler", handler);
298
299         Result result = null;
300         try {
301             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.testForm/a.globalAction1");
302             MVCRequest mvcRequest = new MVCRequest(request, response);
303             mvcRequest.setControllerInfo(info);
304
305             handler.preExecute(mvcRequest);
306             result = handler.execute(mvcRequest);
307         } catch (MVCException mvce) {
308             fail(mvce.toString());
309         }
310
311         assertEquals("/global/testMapping1.jsp", result.getURL());
312     }
313
314     /**
315      * Tests the global actions and mappings
316      */

317     public void testGlobalActionAndMappingWithoutForm() {
318
319         // Setup the configuration using the ConfigServlet
320
setupConfig("src/com/inversoft/verge/mvc/controller/form/test/test-form-mvc.xml");
321
322         // Call the handler
323
FormControllerHandler handler = new FormControllerHandler();
324         MVCRegistry.register("handler", handler);
325
326         Result result = null;
327         try {
328             ControllerMVCInfo info = new ControllerMVCInfo("/form/a.globalAction2");
329             MVCRequest mvcRequest = new MVCRequest(request, response);
330             mvcRequest.setControllerInfo(info);
331
332             handler.preExecute(mvcRequest);
333             result = handler.execute(mvcRequest);
334         } catch (MVCException mvce) {
335             fail(mvce.toString());
336         }
337
338         assertEquals("/global/testMapping2.jsp", result.getURL());
339     }
340
341     /**
342      * Tests that multiple submit parameters still works if one exists for the
343      * submit button hit.
344      */

345     public void testMultipleSubmits() {
346
347         // Setup the configuration using the ConfigServlet
348
setupConfig("src/com/inversoft/verge/mvc/controller/form/test/test-form-mvc.xml");
349
350         // Test with a multiple parameter, one value
351
try {
352             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
353                 FormURLTools.generateSubmitParameter("sub1", "action1"));
354             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
355                 FormURLTools.generateSubmitParameter("sub2", "action2"));
356             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
357                 FormURLTools.generateSubmitParameter("sub3", "action3"));
358             getRequest().setParameter("sub1", "value.x");
359
360             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.testForm");
361             MVCRequest mvcRequest = new MVCRequest(request, response);
362             mvcRequest.setControllerInfo(info);
363
364             FormControllerHandler handler = new FormControllerHandler();
365             TestActionHandler.called = false;
366
367             handler.preExecute(mvcRequest);
368             handler.execute(mvcRequest);
369             assertTrue(TestActionHandler.called);
370         } catch (MVCException e) {
371             fail(e.toString());
372         }
373     }
374
375     /**
376      * Tests that multiple submit parameters fails if one doesn't exists for the
377      * submit button hit.
378      */

379     public void testMultipleSubmitsFailure() {
380
381         // Setup the configuration using the ConfigServlet
382
setupConfig("src/com/inversoft/verge/mvc/controller/form/test/test-form-mvc.xml");
383
384         // Test with a multiple parameter, one value
385
try {
386             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
387                 FormURLTools.generateSubmitParameter("sub1", "action1"));
388             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
389                 FormURLTools.generateSubmitParameter("sub2", "action2"));
390             getRequest().setParameter(FormURLTools.SUBMIT_PARAMETER,
391                 FormURLTools.generateSubmitParameter("sub3", "action3"));
392             getRequest().setParameter("sub4", "value.x");
393
394             ControllerMVCInfo info = new ControllerMVCInfo("/form/f.testForm");
395             MVCRequest mvcRequest = new MVCRequest(request, response);
396             mvcRequest.setControllerInfo(info);
397
398             FormControllerHandler handler = new FormControllerHandler();
399             handler.preExecute(mvcRequest);
400             handler.execute(mvcRequest);
401             fail("Should have failed");
402         } catch (MVCException e) {
403             // Expected
404
}
405     }
406
407
408     /**
409      * Sets up the configuration using the file name given.
410      */

411     public void setupConfig(String JavaDoc fileName) {
412         getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM, fileName);
413
414         try {
415             ConfigServlet servlet = new ConfigServlet();
416             servlet.init(createConfig("ConfigServlet"));
417             servlet.doPost(request, response);
418         } catch (ServletException JavaDoc se) {
419             fail(se.toString());
420         } catch (IOException JavaDoc ioe) {
421             fail(ioe.toString());
422         }
423     }
424
425     /**
426      * Tests the build method correctly builds an action config object
427      */

428     public void build() {
429         Document document = makeGoodDocument();
430
431         try {
432             FormMVCConfigBuilder builder = new FormMVCConfigBuilder();
433             builder.build(document,
434                 FormMVCConfigRegistry.getInstance(new MockHttpServletRequest(null)));
435         } catch (ConfigurationException e) {
436             fail(e.toString());
437         }
438     }
439
440     /**
441      * Makes a good document
442      */

443     protected Document makeGoodDocument() {
444         Document document = new Document();
445         Element root = new Element("forms");
446         document.setRootElement(root);
447
448         Element form1 = new Element("form");
449         form1.setAttribute(Constants.NAME_ATTRIBUTE, "form1");
450
451         Element formBean1 = new Element("formBean");
452         formBean1.setAttribute(Constants.NAME_ATTRIBUTE, "testFormBean");
453         formBean1.setAttribute(Constants.CLASS_ATTRIBUTE,
454             "com.inversoft.verge.mvc.controller.form.test.TestFormBean");
455         form1.addContent(formBean1);
456
457         Element form1action1 = new Element("action");
458         form1action1.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
459         form1action1.setAttribute(Constants.HANDLER_ATTRIBUTE,
460             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
461         Element form1action2 = new Element("action");
462         form1action2.setAttribute(Constants.NAME_ATTRIBUTE, "testMapping");
463         form1action2.setAttribute(Constants.HANDLER_ATTRIBUTE,
464             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
465         Element form1action3 = new Element("action");
466         form1action3.setAttribute(Constants.NAME_ATTRIBUTE, "testDisabled");
467         form1action3.setAttribute(Constants.HANDLER_ATTRIBUTE,
468             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
469         form1action3.setAttribute(Constants.MODEL_ENABLED_ATTRIBUTE, "false");
470         form1action3.setAttribute(Constants.VALIDATION_ENABLED_ATTRIBUTE, "false");
471         Element form1action4 = new Element("action");
472         form1action4.setAttribute(Constants.NAME_ATTRIBUTE, "testPreAndPost");
473         form1action4.setAttribute(Constants.HANDLER_ATTRIBUTE,
474             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler2");
475         Element form1action5 = new Element("action");
476         form1action5.setAttribute(Constants.NAME_ATTRIBUTE, "testLongTxn");
477         form1action5.setAttribute(Constants.HANDLER_ATTRIBUTE,
478             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler2");
479         form1action5.setAttribute(Constants.LONG_TXN_START_URL_ATTRIBUTE,
480             "/longTxnWait.jsp");
481         form1action5.setAttribute(Constants.LONG_TXN_ENABLED_ATTRIBUTE, "true");
482         Element form1action6 = new Element("action");
483         form1action6.setAttribute(Constants.NAME_ATTRIBUTE, "testMapping6");
484         form1action6.setAttribute(Constants.HANDLER_ATTRIBUTE,
485             "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
486         form1.addContent(form1action1);
487         form1.addContent(form1action2);
488         form1.addContent(form1action3);
489         form1.addContent(form1action4);
490         form1.addContent(form1action5);
491         form1.addContent(form1action6);
492
493         Element form1mapping1 = new Element("mapping");
494         form1mapping1.setAttribute(Constants.NAME_ATTRIBUTE, "form1mapping1");
495         form1mapping1.setAttribute(Constants.URL_ATTRIBUTE, "testMapping1.jsp");
496         form1mapping1.setAttribute(Constants.FORWARD_ATTRIBUTE, "true");
497         Element form1mapping2 = new Element("mapping");
498         form1mapping2.setAttribute(Constants.NAME_ATTRIBUTE, "form1mapping2");
499         form1mapping2.setAttribute(Constants.URL_ATTRIBUTE, "testMapping2.jsp");
500         form1mapping2.setAttribute(Constants.FORWARD_ATTRIBUTE, "true");
501         Element form1mapping3 = new Element("mapping");
502         form1mapping3.setAttribute(Constants.NAME_ATTRIBUTE, "foo");
503         form1mapping3.setAttribute(Constants.URL_ATTRIBUTE, "testMapping1.jsp");
504         form1mapping3.setAttribute(Constants.FORWARD_ATTRIBUTE, "true");
505         Element form1mapping4 = new Element("mapping");
506         form1mapping4.setAttribute(Constants.NAME_ATTRIBUTE, "doneLongTxn");
507         form1mapping4.setAttribute(Constants.URL_ATTRIBUTE, "/doneLongTxn.jsp");
508         form1mapping4.setAttribute(Constants.FORWARD_ATTRIBUTE, "true");
509         Element form1mapping5 = new Element("mapping");
510         form1mapping5.setAttribute(Constants.NAME_ATTRIBUTE, "form1mapping5");
511         form1mapping5.setAttribute(Constants.URL_ATTRIBUTE, "testMapping5.jsp");
512         form1mapping5.setAttribute(Constants.FORWARD_ATTRIBUTE, "false");
513         form1mapping5.setAttribute(Constants.CATEGORY_ATTRIBUTE, "testCat1");
514         form1.addContent(form1mapping1);
515         form1.addContent(form1mapping2);
516         form1.addContent(form1mapping3);
517         form1.addContent(form1mapping4);
518         form1.addContent(form1mapping5);
519         root.addContent(form1);
520
521         return document;
522     }
523 }
Popular Tags