1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nl.openedge.baritus.test;
33
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Map;
40
41 import javax.servlet.ServletException;
42
43 import nl.openedge.baritus.ExecutionParams;
44 import nl.openedge.baritus.FormBeanContext;
45 import nl.openedge.baritus.FormBeanCtrlBase;
46 import nl.openedge.baritus.test.mock.MockHttpServletRequest;
47 import nl.openedge.baritus.test.mock.MockHttpServletResponse;
48
49 import org.infohazard.maverick.flow.MaverickContext;
50
51 import com.mockobjects.servlet.MockHttpSession;
52 import com.mockobjects.servlet.MockRequestDispatcher;
53 import com.mockobjects.servlet.MockServletConfig;
54 import com.mockobjects.servlet.MockServletContext;
55
56 import junit.framework.TestCase;
57
58 /***
59 * Testcase for population of form beans and interceptors.
60 *
61 * @author Eelco Hillenius
62 * @author Sander Hofstee
63 */
64 public class PopulationTest extends TestCase
65 {
66
67 private Locale dutch = new Locale("nl", "NL");
68
69 private MockRequestDispatcher requestDispatcher = null;
70 private MockServletContext servletContext = null;
71 private MockServletConfig servletConfig = null;
72 private MockHttpSession session = null;
73 private MockHttpServletResponse response = null;
74 private MockHttpServletRequest request = null;
75
76 /***
77 * construct
78 * @param name
79 */
80 public PopulationTest(String name)
81 {
82 super(name);
83 }
84
85 /***
86 * @see junit.framework.TestCase#setUp()
87 */
88 protected void setUp() throws Exception
89 {
90 this.requestDispatcher = new MockRequestDispatcher();
91 this.servletContext = new MockServletContext();
92 this.servletContext.setupGetRequestDispatcher(requestDispatcher);
93 this.servletConfig = new MockServletConfig();
94 this.servletConfig.setServletContext(servletContext);
95 this.session = new MockHttpSession();
96 this.session.setupGetAttribute(
97 FormBeanCtrlBase.SESSION_KEY_CURRENT_LOCALE, dutch);
98
99 this.session.setupServletContext(servletContext);
100 this.response = new MockHttpServletResponse();
101 this.request = new MockHttpServletRequest();
102 this.request.setupGetAttribute("__formBeanContext");
103 this.request.setSession(session);
104 this.request.setupGetRequestDispatcher(requestDispatcher);
105 }
106
107
108 public void testIntegerPopulationAndBeforePerformInterceptor()
109 {
110 TestCtrl ctrl = new TestCtrl();
111 Map requestParams = new HashMap();
112 requestParams.put("testInteger1", "1");
113 requestParams.put("testInteger2", new String[]{"2"});
114 request.setupGetParameterMap(requestParams);
115 MaverickContext mockMavCtx = new MaverickContext(
116 null, request, response);
117
118 try
119 {
120 ctrl.init(null);
121 ctrl.go(mockMavCtx);
122 TestBean bean = ctrl.getTestBean();
123 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
124 assertNotNull(bean.getTestInteger1());
125 assertEquals(new Integer(1), bean.getTestInteger1());
126 assertNotNull(bean.getTestInteger2());
127 assertEquals(new Integer(2), bean.getTestInteger2());
128 }
129 catch (ServletException e)
130 {
131 e.printStackTrace();
132 fail(e.getMessage());
133 }
134 }
135
136
137 public void testLongPopulation()
138 {
139 TestCtrl ctrl = new TestCtrl();
140 Map requestParams = new HashMap();
141 requestParams.put("testLong1", "1");
142 requestParams.put("testLong2", new String[]{"2"});
143 request.setupGetParameterMap(requestParams);
144 MaverickContext mockMavCtx = new MaverickContext(
145 null, request, response);
146 try
147 {
148 ctrl.init(null);
149 ctrl.go(mockMavCtx);
150 TestBean bean = ctrl.getTestBean();
151 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
152 assertNotNull(bean.getTestLong1());
153 assertEquals(new Long(1), bean.getTestLong1());
154 assertNotNull(bean.getTestLong2());
155 assertEquals(new Long(2), bean.getTestLong2());
156 }
157 catch (ServletException e)
158 {
159 e.printStackTrace();
160 fail(e.getMessage());
161 }
162 }
163
164
165 public void testDoublePopulationAndLocalizedDisplayProperty()
166 {
167 TestCtrl ctrl = new TestCtrl();
168 Map requestParams = new HashMap();
169 requestParams.put("testDouble1", "1,1");
170 requestParams.put("testDouble2", new String[]{"1,2"});
171 request.setupGetParameterMap(requestParams);
172 MaverickContext mockMavCtx = new MaverickContext(
173 null, request, response);
174 try
175 {
176 ctrl.init(null);
177 ctrl.go(mockMavCtx);
178 TestBean bean = ctrl.getTestBean();
179 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
180 assertNotNull(bean.getTestDouble1());
181 assertEquals(new Double(1.1), bean.getTestDouble1());
182 assertNotNull(bean.getTestDouble2());
183 assertEquals(new Double(1.2), bean.getTestDouble2());
184 FormBeanContext formBeanContext = ctrl.getFormBeanContext();
185 assertEquals("dutch locale should be used for formatting a double property",
186 "1,1", formBeanContext.displayProperty("testDouble1"));
187 }
188 catch (ServletException e)
189 {
190 e.printStackTrace();
191 fail(e.getMessage());
192 }
193 }
194
195 public void testDatePopulation()
196 {
197 TestCtrl ctrl = new TestCtrl();
198 Map requestParams = new HashMap();
199 requestParams.put("testDate1", "20-02-2004");
200 requestParams.put("testDate2", new String[]{"21-03-2005"});
201 request.setupGetParameterMap(requestParams);
202 MaverickContext mockMavCtx = new MaverickContext(
203 null, request, response);
204 try
205 {
206 ctrl.init(null);
207 ctrl.go(mockMavCtx);
208 TestBean bean = ctrl.getTestBean();
209 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
210 assertNotNull(bean.getTestDate1());
211 assertNotNull(bean.getTestDate2());
212
213 Calendar cal = Calendar.getInstance();
214 Date date = bean.getTestDate1();
215 cal.setTime(date);
216 assertEquals(cal.get(Calendar.YEAR), 2004);
217 assertEquals(cal.get(Calendar.MONTH), 1);
218 assertEquals(cal.get(Calendar.DAY_OF_MONTH), 20);
219
220 date = bean.getTestDate2();
221 cal.setTime(date);
222 assertEquals(cal.get(Calendar.YEAR), 2005);
223 assertEquals(cal.get(Calendar.MONTH), 2);
224 assertEquals(cal.get(Calendar.DAY_OF_MONTH), 21);
225 }
226 catch (ServletException e)
227 {
228 e.printStackTrace();
229 fail(e.getMessage());
230 }
231 }
232
233 /***
234 * Test the population of arrays when the parameters are in the request as
235 * array = {value1, value2}
236 */
237 public void testRequestStringArrayPopulation()
238 {
239 TestCtrl ctrl = new TestCtrl();
240 Map requestParams = new HashMap();
241 requestParams.put("testStringArray1", new String[] {"arrayelem0", "arrayelem1"});
242 request.setupGetParameterMap(requestParams);
243 MaverickContext mockMavCtx = new MaverickContext(
244 null, request, response);
245 try
246 {
247 ctrl.init(null);
248 ctrl.go(mockMavCtx);
249 TestBean bean = ctrl.getTestBean();
250 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
251 String[] testStringArray1 = bean.getTestStringArray1();
252 assertNotNull(testStringArray1);
253 assertEquals(2, testStringArray1.length);
254 assertEquals("arrayelem0", testStringArray1[0]);
255 assertEquals("arrayelem1", testStringArray1[1]);
256 }
257 catch (ServletException e)
258 {
259 e.printStackTrace();
260 fail(e.getMessage());
261 }
262 }
263
264 public void testStringMapPopulation()
265 {
266 TestCtrl ctrl = new TestCtrl();
267 Map requestParams = new HashMap();
268 requestParams.put("testMap['key1']", "val1");
269 requestParams.put("testMap['key2']", "val2");
270 request.setupGetParameterMap(requestParams);
271 MaverickContext mockMavCtx = new MaverickContext(
272 null, request, response);
273 try
274 {
275 ctrl.init(null);
276 ctrl.go(mockMavCtx);
277 TestBean bean = ctrl.getTestBean();
278 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
279 Map map = bean.getTestMap();
280 assertNotNull(map);
281 assertEquals(2, map.size());
282 assertEquals("val1", map.get("key1"));
283 assertEquals("val2", map.get("key2"));
284 }
285 catch (ServletException e)
286 {
287 e.printStackTrace();
288 fail(e.getMessage());
289 }
290 }
291
292 public void testPopulationWithCustomPopulators()
293 {
294 TestCtrl ctrl = new TestCtrl();
295 Map requestParams = new HashMap();
296 requestParams.put("uppercaseTest", "this once was lower case");
297 requestParams.put("ignore", "this should never come through");
298 request.setupGetParameterMap(requestParams);
299 MaverickContext mockMavCtx = new MaverickContext(
300 null, request, response);
301 try
302 {
303 ctrl.init(null);
304 ctrl.go(mockMavCtx);
305 TestBean bean = ctrl.getTestBean();
306 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
307 assertEquals("THIS ONCE WAS LOWER CASE", bean.getUppercaseTest());
308 assertEquals("unchanged", bean.getIgnore());
309 }
310 catch (ServletException e)
311 {
312 e.printStackTrace();
313 fail(e.getMessage());
314 }
315 }
316
317 public void testPopulationWithCustomPopulatorByRegexMatch()
318 {
319 TestCtrl ctrl = new TestCtrl();
320 Map requestParams = new HashMap();
321 requestParams.put("ignoreByRegex", "this should never come through");
322 request.setupGetParameterMap(requestParams);
323 MaverickContext mockMavCtx = new MaverickContext(
324 null, request, response);
325 try
326 {
327 ctrl.init(null);
328 ctrl.go(mockMavCtx);
329 TestBean bean = ctrl.getTestBean();
330 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
331 assertEquals("unchanged (regex)", bean.getIgnoreByRegex());
332 }
333 catch (ServletException e)
334 {
335 e.printStackTrace();
336 fail(e.getMessage());
337 }
338 }
339
340 /***
341 * Test the population of indexed properties.
342 *
343 */
344 public void testPopulationWithListProperties()
345 {
346 TestCtrl ctrl = new TestCtrl();
347 Map requestParams = new HashMap();
348 requestParams.put("listProperty[0]", "newval0");
349 requestParams.put("listProperty[1]", "newval1");
350 request.setupGetParameterMap(requestParams);
351 MaverickContext mockMavCtx = new MaverickContext(
352 null, request, response);
353 try
354 {
355 ctrl.init(null);
356 ctrl.go(mockMavCtx);
357 TestBean bean = ctrl.getTestBean();
358 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
359
360 List listProperty = bean.getListProperty();
361 assertNotNull(listProperty);
362 assertEquals(2, listProperty.size());
363 assertEquals("newval0", listProperty.get(0));
364 assertEquals("newval1", listProperty.get(1));
365 }
366 catch (ServletException e)
367 {
368 e.printStackTrace();
369 fail(e.getMessage());
370 }
371 }
372
373 /***
374 * Test the population of multidimensional mapped properties.
375 *
376 */
377 public void testPopulationWithMultiDimensionalMappedProperties()
378 {
379 TestCtrl ctrl = new TestCtrl();
380 Map requestParams = new HashMap();
381 requestParams.put("multiDimensionalMap['one']['one']", "newval0");
382 requestParams.put("multiDimensionalMap['one']['two']", "newval1");
383 requestParams.put("multiDimensionalMap['one']['three']", "newval2");
384 requestParams.put("multiDimensionalMap['two']['one']", "newval3");
385 request.setupGetParameterMap(requestParams);
386 MaverickContext mockMavCtx = new MaverickContext(
387 null, request, response);
388 try
389 {
390 ctrl.init(null);
391 ctrl.go(mockMavCtx);
392 TestBean bean = ctrl.getTestBean();
393 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
394
395 Map listProperty = bean.getMultiDimensionalMap();
396 assertNotNull(listProperty);
397 assertEquals(2, listProperty.size());
398
399 Map one = (Map)listProperty.get("one");
400 assertEquals(3, one.size());
401
402 assertTrue(one.containsKey("one"));
403 assertTrue(one.containsKey("two"));
404 assertTrue(one.containsKey("three"));
405
406 assertEquals("newval0", one.get("one"));
407 assertEquals("newval1", one.get("two"));
408 assertEquals("newval2", one.get("three"));
409
410 Map two = (Map)listProperty.get("two");
411 assertEquals(1, two.size());
412 assertTrue(two.containsKey("one"));
413 assertEquals("newval3", two.get("one"));
414 }
415 catch (ServletException e)
416 {
417 e.printStackTrace();
418 fail(e.getMessage());
419 }
420 }
421
422 /***
423 * Test the population of multidimensional List properties.
424 *
425 */
426 public void testPopulationWithMultiDimensionalListProperties()
427 {
428 TestCtrl ctrl = new TestCtrl();
429 Map requestParams = new HashMap();
430 requestParams.put("multiDimensionalList[0][0]", "newval0");
431 requestParams.put("multiDimensionalList[0][1]", "newval1");
432 requestParams.put("multiDimensionalList[0][2]", "newval2");
433 requestParams.put("multiDimensionalList[1][0]", "newval0");
434 request.setupGetParameterMap(requestParams);
435 MaverickContext mockMavCtx = new MaverickContext(
436 null, request, response);
437 try
438 {
439 ctrl.init(null);
440 ctrl.go(mockMavCtx);
441 TestBean bean = ctrl.getTestBean();
442 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
443
444 List listProperty = bean.getMultiDimensionalList();
445 assertNotNull(listProperty);
446 assertEquals(2, listProperty.size());
447
448 List one = (List)listProperty.get(0);
449 assertEquals(3, one.size());
450
451 List two = (List)listProperty.get(1);
452 assertEquals(1, two.size());
453
454 assertTrue(listProperty.get(0).getClass().getName() +
455 " with value " + listProperty.get(0)
456 , listProperty.get(0) instanceof List);
457 assertTrue(listProperty.get(1).getClass().getName() +
458 " with value " + listProperty.get(0)
459 , listProperty.get(1) instanceof List);
460 }
461 catch (ServletException e)
462 {
463 e.printStackTrace();
464 fail(e.getMessage());
465 }
466 }
467
468 public void testError1()
469 {
470 TestCtrl ctrl = new TestCtrl();
471 Map requestParams = new HashMap();
472 requestParams.put("testDouble1", "wrong");
473 request.setupGetParameterMap(requestParams);
474 MaverickContext mockMavCtx = new MaverickContext(
475 null, request, response);
476 try
477 {
478 ctrl.init(null);
479 ctrl.go(mockMavCtx);
480 TestBean bean = ctrl.getTestBean();
481 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
482 assertNull(bean.getTestDouble1());
483
484 }
485 catch (ServletException e)
486 {
487 e.printStackTrace();
488 fail(e.getMessage());
489 }
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 public void testObjectFromRequestAttributesPopulation()
519 {
520 TestCtrl ctrl = new TestCtrl();
521
522 Map requestParams = new HashMap();
523 TestObject testObject = new TestObject();
524 testObject.setTestString("a test");
525 request.setAttribute("testObject", testObject);
526 request.setupGetParameterMap(requestParams);
527
528 MaverickContext mockMavCtx = new MaverickContext(
529 null, request, response);
530 try
531 {
532 ctrl.init(null);
533 ctrl.go(mockMavCtx);
534 TestBean bean = ctrl.getTestBean();
535 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
536 assertNotNull(bean.getTestObject());
537 assertEquals("a test", bean.getTestObject().getTestString());
538 }
539 catch (ServletException e)
540 {
541 e.printStackTrace();
542 fail(e.getMessage());
543 }
544 }
545
546
547 public void testStrictParsing()
548 {
549 TestCtrl ctrl = new TestCtrl();
550 Map requestParams = new HashMap();
551 requestParams.put("not-a-valid-name", "foo");
552 request.setupGetParameterMap(requestParams);
553 MaverickContext mockMavCtx = new MaverickContext(
554 null, request, response);
555 try
556 {
557 ctrl.init(null);
558 ctrl.go(mockMavCtx);
559 TestBean bean = ctrl.getTestBean();
560 assertEquals(FormBeanCtrlBase.ERROR, ctrl.getView());
561 }
562 catch (ServletException e)
563 {
564 e.printStackTrace();
565 fail(e.getMessage());
566 }
567 }
568
569
570 public void testNonStrictParsing()
571 {
572 TestCtrl ctrl = new TestCtrl();
573 ExecutionParams params = ctrl.getExecutionParams(null);
574 params.setStrictPopulationMode(false);
575 ctrl.fixExecutionParams(params);
576 Map requestParams = new HashMap();
577 requestParams.put("not-a-valid-name", "foo");
578 request.setupGetParameterMap(requestParams);
579 MaverickContext mockMavCtx = new MaverickContext(
580 null, request, response);
581 try
582 {
583 ctrl.init(null);
584 ctrl.go(mockMavCtx);
585 TestBean bean = ctrl.getTestBean();
586 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
587 }
588 catch (ServletException e)
589 {
590 e.printStackTrace();
591 fail(e.getMessage());
592 }
593 }
594
595 public void testTrimString1()
596 {
597 TestCtrl ctrl = new TestCtrl();
598 Map requestParams = new HashMap();
599 requestParams.put("testTrimString", " tobetrimmed ");
600 request.setupGetParameterMap(requestParams);
601 MaverickContext mockMavCtx = new MaverickContext(
602 null, request, response);
603 try
604 {
605 ctrl.init(null);
606 ctrl.go(mockMavCtx);
607 TestBean bean = ctrl.getTestBean();
608 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
609 assertNotNull(bean.getTestTrimString());
610 assertEquals("tobetrimmed", bean.getTestTrimString());
611 }
612 catch (ServletException e)
613 {
614 e.printStackTrace();
615 fail(e.getMessage());
616 }
617 }
618
619 public void testTrimString2()
620 {
621 TestCtrl ctrl = new TestCtrl();
622 Map requestParams = new HashMap();
623 requestParams.put("testTrimStringArray[0]", " tobetrimmed ");
624 request.setupGetParameterMap(requestParams);
625 MaverickContext mockMavCtx = new MaverickContext(
626 null, request, response);
627 try
628 {
629 ctrl.init(null);
630 ctrl.go(mockMavCtx);
631 TestBean bean = ctrl.getTestBean();
632 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
633 assertNotNull(bean.getTestTrimStringArray());
634 assertNotNull(bean.getTestTrimStringArray()[0]);
635 assertEquals("tobetrimmed", bean.getTestTrimStringArray()[0]);
636 }
637 catch (ServletException e)
638 {
639 e.printStackTrace();
640 fail(e.getMessage());
641 }
642 }
643
644 public void testTrimString3()
645 {
646 TestCtrl ctrl = new TestCtrl();
647 ExecutionParams params = ctrl.getExecutionParams(null);
648 params.setTrimStringInputValues(false);
649 ctrl.fixExecutionParams(params);
650 Map requestParams = new HashMap();
651 requestParams.put("testTrimString", " notbetrimmed ");
652 request.setupGetParameterMap(requestParams);
653 MaverickContext mockMavCtx = new MaverickContext(
654 null, request, response);
655 try
656 {
657 ctrl.init(null);
658 ctrl.go(mockMavCtx);
659 TestBean bean = ctrl.getTestBean();
660 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
661 assertNotNull(bean.getTestTrimString());
662 assertEquals(" notbetrimmed ", bean.getTestTrimString());
663 }
664 catch (ServletException e)
665 {
666 e.printStackTrace();
667 fail(e.getMessage());
668 }
669 }
670
671 public void testTrimString4()
672 {
673 TestCtrl ctrl = new TestCtrl();
674 ExecutionParams params = ctrl.getExecutionParams(null);
675 params.setTrimStringInputValues(false);
676 ctrl.fixExecutionParams(params);
677 Map requestParams = new HashMap();
678 requestParams.put("testTrimStringArray[0]", " notbetrimmed ");
679 request.setupGetParameterMap(requestParams);
680 MaverickContext mockMavCtx = new MaverickContext(
681 null, request, response);
682 try
683 {
684 ctrl.init(null);
685 ctrl.go(mockMavCtx);
686 TestBean bean = ctrl.getTestBean();
687 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
688 assertNotNull(bean.getTestTrimStringArray());
689 assertNotNull(bean.getTestTrimStringArray()[0]);
690 assertEquals(" notbetrimmed ", bean.getTestTrimStringArray()[0]);
691 }
692 catch (ServletException e)
693 {
694 e.printStackTrace();
695 fail(e.getMessage());
696 }
697 }
698
699 /***
700 * Tests that a request attribute overrides a request parameter,
701 * and that the population with the request parameter is never tried
702 * when there is an overruling parameter.
703 */
704 public void testRequestParamAndRequestAttribPopulation()
705 {
706 TestCtrl ctrl = new TestCtrl();
707 Map requestParams = new HashMap();
708 requestParams.put("testInteger1", "not a number at all");
709 request.setupGetParameterMap(requestParams);
710 request.setAttribute("testInteger1", "1");
711
712
713 MaverickContext mockMavCtx = new MaverickContext(
714 null, request, response);
715 try
716 {
717 ctrl.init(null);
718 ctrl.go(mockMavCtx);
719 TestBean bean = ctrl.getTestBean();
720 assertEquals(FormBeanCtrlBase.SUCCESS, ctrl.getView());
721 assertNotNull(bean.getTestInteger1());
722 assertEquals(new Integer(1), bean.getTestInteger1());
723 }
724 catch (ServletException e)
725 {
726 e.printStackTrace();
727 fail(e.getMessage());
728 }
729 }
730
731
732 /***
733 // * Test the population of arrays when the parameters are in the request as
734 // * array[0] = 1
735 // * array[1] = 2
736 // */
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765 }