1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.struts.taglib;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.struts.Globals;
24 import org.apache.struts.action.ActionMessage;
25 import org.apache.struts.action.ActionMessages;
26 import org.apache.struts.config.ActionConfig;
27 import org.apache.struts.config.ModuleConfig;
28 import org.apache.struts.config.impl.ModuleConfigImpl;
29 import org.apache.struts.mock.MockFormBean;
30 import org.apache.struts.mock.MockHttpServletRequest;
31 import org.apache.struts.mock.MockHttpServletResponse;
32 import org.apache.struts.mock.MockPageContext;
33 import org.apache.struts.mock.MockServletConfig;
34 import org.apache.struts.mock.MockServletContext;
35 import org.apache.struts.taglib.html.Constants;
36
37 import javax.servlet.jsp.JspException;
38 import javax.servlet.jsp.PageContext;
39
40 import java.net.MalformedURLException;
41
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Locale;
45 import java.util.Map;
46
47 /***
48 * Unit tests for the TagUtils.
49 */
50 public class TestTagUtils extends TagTestBase {
51 private static final Log log = LogFactory.getLog(TestTagUtils.class);
52
53 /***
54 * Defines the testcase name for JUnit.
55 *
56 * @param theName the testcase's name.
57 */
58 public TestTagUtils(String theName) {
59 super(theName);
60 }
61
62 /***
63 * Start the tests.
64 *
65 * @param theArgs the arguments. Not used
66 */
67 public static void main(String[] theArgs) {
68 junit.awtui.TestRunner.main(new String[] { TestTagUtils.class.getName() });
69 }
70
71 /***
72 * @return a test suite (<code>TestSuite</code>) that includes all methods
73 * starting with "test"
74 */
75 public static Test suite() {
76
77 return new TestSuite(TestTagUtils.class);
78 }
79
80 /***
81 * Test Operators.
82 */
83 public void testFilter() {
84 assertNull("Null", null);
85
86
87 assertNull("Filter Test 1", tagutils.filter(null));
88
89
90 assertEquals("Filter Test 2", "", tagutils.filter(""));
91
92
93 assertEquals("Filter Test 3", "a", tagutils.filter("a"));
94
95
96 assertEquals("Filter Test 4", "adhdfhdfhadhf",
97 tagutils.filter("adhdfhdfhadhf"));
98
99
100 assertEquals("Filter Test 5", "<", tagutils.filter("<"));
101 assertEquals("Filter Test 6", ">", tagutils.filter(">"));
102 assertEquals("Filter Test 7", "&", tagutils.filter("&"));
103 assertEquals("Filter Test 8", """, tagutils.filter("\""));
104 assertEquals("Filter Test 9", "'", tagutils.filter("'"));
105
106
107 assertEquals("Filter Test 10", "a<", tagutils.filter("a<"));
108 assertEquals("Filter Test 11", "<a", tagutils.filter("<a"));
109 assertEquals("Filter Test 12", "a<a", tagutils.filter("a<a"));
110
111
112 assertEquals("Filter Test 13", "abc<", tagutils.filter("abc<"));
113 assertEquals("Filter Test 14", "<abc", tagutils.filter("<abc"));
114 assertEquals("Filter Test 15", "abc<abc", tagutils.filter("abc<abc"));
115
116
117 assertEquals("Filter Test 16",
118 "<input type="text" value='Me & You'>",
119 tagutils.filter("<input type=\"text\" value='Me & You'>"));
120 }
121
122
123
124 public void testComputeParameters0a() {
125 Map map = null;
126
127 try {
128 map = tagutils.computeParameters(pageContext, null, null, null,
129 null, null, null, null, false);
130 } catch (JspException e) {
131 fail("JspException: " + e);
132 }
133
134 assertNull("Map is null", map);
135 }
136
137
138 public void testComputeParameters0b() {
139 request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
140
141 Map map = null;
142
143 try {
144 map = tagutils.computeParameters(pageContext, null, null, null,
145 null, null, null, null, true);
146 } catch (JspException e) {
147 fail("JspException: " + e);
148 }
149
150 assertNotNull("Map is not null", map);
151 assertEquals("One parameter in the returned map", 1, map.size());
152 assertTrue("Transaction token parameter present",
153 map.containsKey(Constants.TOKEN_KEY));
154 assertEquals("Transaction token parameter value", "token",
155 (String) map.get(Constants.TOKEN_KEY));
156 }
157
158
159 public void testComputeParametersInvalidScope() {
160 Map map = null;
161
162 try {
163 map = tagutils.computeParameters(pageContext, null, null, null,
164 "session", "i-do-not-exist", null, null, false);
165
166 fail("JspException not thrown");
167 } catch (JspException e) {
168 assertNull("map is null", map);
169 }
170 }
171
172
173 public void testComputeParametersBeanNotFound() {
174 Map map = null;
175
176 try {
177 map = tagutils.computeParameters(pageContext, null, null, null,
178 null, "i-do-not-exist", null, null, false);
179
180 fail("JspException not thrown");
181 } catch (JspException e) {
182 assertNull("map is null", map);
183 }
184 }
185
186
187 public void testComputeParametersPropertyThrowsException() {
188 request.getSession().setAttribute("SomeBean", new MockFormBean(true));
189
190 Map map = null;
191
192 try {
193 map = tagutils.computeParameters(pageContext, null, null, null,
194 null, "SomeBean", "justThrowAnException", null, false);
195 fail("JspException not thrown");
196 } catch (JspException e) {
197 assertNull("map is null", map);
198 }
199 }
200
201 public void testComputeParametersParamIdParamPropThrowException() {
202 request.getSession().setAttribute("SomeBean", new MockFormBean(true));
203
204 Map map = null;
205
206 try {
207 map = tagutils.computeParameters(pageContext, "paramId",
208 "SomeBean", "justThrowAnException", null, null, null, null,
209 false);
210
211 fail("JspException not thrown");
212 } catch (JspException e) {
213 assertNull("map is null", map);
214 }
215 }
216
217 public void testComputeParametersParamValueToString() {
218 request.getSession().setAttribute("SomeBean",
219 new MockFormBean(false, false, new Double(1)));
220
221 Map map = null;
222
223 try {
224 map = tagutils.computeParameters(pageContext, "paramId",
225 "SomeBean", "doubleValue", null, null, null, null, false);
226
227 assertNotNull("map is null", map);
228
229 String val = (String) map.get("paramId");
230
231 assertTrue("paramId should be 1.0", "1.0".equals(val));
232 } catch (JspException e) {
233 fail("JspException not thrown");
234 }
235 }
236
237 public void skiptestComputeParametersParamIdAsStringArray() {
238 Map params = new HashMap();
239
240
241 params.put("fooParamId", "fooParamValue");
242
243 request.getSession().setAttribute("SomeBean", params);
244
245 Map map = null;
246
247 try {
248 map = tagutils.computeParameters(pageContext, "fooParamId",
249 "SomeBean", null, null, "SomeBean", null, null, false);
250
251
252
253
254 assertNotNull("map is null", map);
255
256 String val = (String) map.get("key0");
257
258 assertTrue("paramId should be \"test\"", "1.0".equals(val));
259 } catch (JspException e) {
260 fail("JspException not thrown");
261 }
262 }
263
264
265 public void testComputeParameters1a() {
266 request.getSession().setAttribute("attr", "bar");
267
268 Map map = null;
269
270 try {
271 map = tagutils.computeParameters(pageContext, "foo", "attr", null,
272 null, null, null, null, false);
273 } catch (JspException e) {
274 fail("JspException: " + e);
275 }
276
277 assertNotNull("Map is not null", map);
278 assertEquals("One parameter in the returned map", 1, map.size());
279 assertTrue("Parameter present", map.containsKey("foo"));
280 assertEquals("Parameter value", "bar", (String) map.get("foo"));
281 }
282
283
284 public void testComputeParameters1b() {
285 request.setAttribute("attr", "bar");
286
287 Map map = null;
288
289 try {
290 map = tagutils.computeParameters(pageContext, "foo", "attr", null,
291 "request", null, null, null, false);
292 } catch (JspException e) {
293 fail("JspException: " + e);
294 }
295
296 assertNotNull("Map is not null", map);
297 assertEquals("One parameter in the returned map", 1, map.size());
298 assertTrue("Parameter present", map.containsKey("foo"));
299 assertEquals("Parameter value", "bar", (String) map.get("foo"));
300 }
301
302
303 public void testComputeParameters1c() {
304 request.setAttribute("attr", new MockFormBean("bar"));
305
306 Map map = null;
307
308 try {
309 map = tagutils.computeParameters(pageContext, "foo", "attr",
310 "stringProperty", "request", null, null, null, false);
311 } catch (JspException e) {
312 fail("JspException: " + e);
313 }
314
315 assertNotNull("Map is not null", map);
316 assertEquals("One parameter in the returned map", 1, map.size());
317 assertTrue("Parameter present", map.containsKey("foo"));
318 assertEquals("Parameter value", "bar", (String) map.get("foo"));
319 }
320
321
322 public void testComputeParameters2a() {
323 Map map = new HashMap();
324
325 map.put("foo1", "bar1");
326 map.put("foo2", "bar2");
327 request.getSession().setAttribute("attr", map);
328
329 try {
330 map = tagutils.computeParameters(pageContext, null, null, null,
331 null, "attr", null, null, false);
332 } catch (JspException e) {
333 fail("JspException: " + e);
334 }
335
336 assertNotNull("Map is not null", map);
337 assertEquals("Two parameter in the returned map", 2, map.size());
338 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
339 assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1"));
340 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
341 assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
342 }
343
344
345 public void testComputeParameters2b() {
346 Map map = new HashMap();
347
348 map.put("foo1", "bar1");
349 map.put("foo2", "bar2");
350 request.setAttribute("attr", map);
351
352 try {
353 map = tagutils.computeParameters(pageContext, null, null, null,
354 null, "attr", null, "request", false);
355 } catch (JspException e) {
356 fail("JspException: " + e);
357 }
358
359 assertNotNull("Map is not null", map);
360 assertEquals("Two parameter in the returned map", 2, map.size());
361 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
362 assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1"));
363 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
364 assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
365 }
366
367
368 public void testComputeParameters2c() {
369 request.setAttribute("attr", new MockFormBean());
370
371 Map map = null;
372
373 try {
374 map = tagutils.computeParameters(pageContext, null, null, null,
375 null, "attr", "mapProperty", "request", false);
376 } catch (JspException e) {
377 fail("JspException: " + e);
378 }
379
380 assertNotNull("Map is not null", map);
381 assertEquals("Two parameter in the returned map", 2, map.size());
382 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
383 assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1"));
384 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
385 assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
386 }
387
388
389 public void testComputeParameters2d() {
390 Map map = new HashMap();
391
392 map.put("foo", new String[] { "bar1", "bar2" });
393 request.getSession().setAttribute("attr", map);
394
395 try {
396 map = tagutils.computeParameters(pageContext, null, null, null,
397 null, "attr", null, null, false);
398 } catch (JspException e) {
399 fail("JspException: " + e);
400 }
401
402 assertNotNull("Map is not null", map);
403 assertEquals("One parameter in the returned map", 1, map.size());
404 assertTrue("Parameter foo present", map.containsKey("foo"));
405 assertTrue("Parameter foo value type",
406 map.get("foo") instanceof String[]);
407
408 String[] values = (String[]) map.get("foo");
409
410 assertEquals("Values count", 2, values.length);
411 }
412
413
414 public void testComputeParameters3a() {
415 request.setAttribute("attr", new MockFormBean("bar3"));
416 request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
417
418 Map map = null;
419
420 try {
421 map = tagutils.computeParameters(pageContext, "foo1", "attr",
422 "stringProperty", "request", "attr", "mapProperty",
423 "request", true);
424 } catch (JspException e) {
425 fail("JspException: " + e);
426 }
427
428 assertNotNull("Map is not null", map);
429 assertEquals("Three parameter in the returned map", 3, map.size());
430
431 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
432 assertTrue("Parameter foo1 value type",
433 map.get("foo1") instanceof String[]);
434
435 String[] values = (String[]) map.get("foo1");
436
437 assertEquals("Values count", 2, values.length);
438
439 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
440 assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
441
442 assertTrue("Transaction token parameter present",
443 map.containsKey(Constants.TOKEN_KEY));
444 assertEquals("Transaction token parameter value", "token",
445 (String) map.get(Constants.TOKEN_KEY));
446 }
447
448
449
450 public void testComputeParameters3aa() {
451 request.setAttribute("attr", new MockFormBean("bar3"));
452 request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
453
454 Map map = null;
455
456 try {
457 map = tagutils.computeParameters(pageContext, "foo1", "attr",
458 "stringProperty", "request", "attr",
459 "mapPropertyArrayValues", "request", true);
460 } catch (JspException e) {
461 fail("JspException: " + e);
462 }
463
464 assertNotNull("Map is not null", map);
465 assertEquals("Three parameter in the returned map", 3, map.size());
466
467 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
468 assertTrue("Parameter foo1 value type",
469 map.get("foo1") instanceof String[]);
470
471 String[] values = (String[]) map.get("foo1");
472
473 assertEquals("Values count", 3, values.length);
474
475 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
476
477 String[] arrayValues = (String[]) map.get("foo2");
478 String val = arrayValues[0];
479
480 assertEquals("Parameter foo2 value", "bar2", val);
481
482 assertTrue("Transaction token parameter present",
483 map.containsKey(Constants.TOKEN_KEY));
484 assertEquals("Transaction token parameter value", "token",
485 (String) map.get(Constants.TOKEN_KEY));
486 }
487
488
489 public void testComputeParameters3b() {
490 request.setAttribute("attr", new MockFormBean("bar3"));
491 request.getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
492
493 Map map = null;
494
495 try {
496 map = tagutils.computeParameters(pageContext, "foo1", "attr",
497 "stringProperty", "request", "attr", "mapProperty",
498 "request", true);
499 } catch (JspException e) {
500 fail("JspException: " + e);
501 }
502
503 assertNotNull("Map is not null", map);
504 assertEquals("Three parameter in the returned map", 3, map.size());
505
506 assertTrue("Parameter foo1 present", map.containsKey("foo1"));
507 assertTrue("Parameter foo1 value type",
508 map.get("foo1") instanceof String[]);
509
510 String[] values = (String[]) map.get("foo1");
511
512 assertEquals("Values count", 2, values.length);
513
514 assertTrue("Parameter foo2 present", map.containsKey("foo2"));
515 assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2"));
516
517 assertTrue("Transaction token parameter present",
518 map.containsKey(Constants.TOKEN_KEY));
519 assertEquals("Transaction token parameter value", "token",
520 (String) map.get(Constants.TOKEN_KEY));
521 }
522
523
524
525 public void testComputeURL1a() {
526 request.setPathElements("/myapp", "/action.do", null, null);
527
528 String url = null;
529
530 try {
531 url = tagutils.computeURL(pageContext, "foo", null, null, null,
532 null, null, null, false);
533 } catch (MalformedURLException e) {
534 fail("MalformedURLException: " + e);
535 }
536
537 assertNotNull("url present", url);
538 assertEquals("url value", "/myapp/bar.jsp", url);
539 }
540
541
542 public void testComputeURL1b() {
543 request.setPathElements("/myapp", "/action.do", null, null);
544
545 String url = null;
546
547 try {
548 url = tagutils.computeURL(pageContext, null, "http://foo.com/bar",
549 null, null, null, null, null, false);
550 } catch (MalformedURLException e) {
551 fail("MalformedURLException: " + e);
552 }
553
554 assertNotNull("url present", url);
555 assertEquals("url value", "http://foo.com/bar", url);
556 }
557
558
559 public void testComputeURL1c() {
560 request.setPathElements("/myapp", "/action.do", null, null);
561
562 String url = null;
563
564 try {
565 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
566 null, null, null, false);
567 } catch (MalformedURLException e) {
568 fail("MalformedURLException: " + e);
569 }
570
571 assertNotNull("url present", url);
572 assertEquals("url value", "/myapp/bar", url);
573 }
574
575
576 public void testComputeURL1d() {
577 moduleConfig.getControllerConfig().setForwardPattern("$C/WEB-INF/pages$M$P");
578 request.setPathElements("/myapp", "/action.do", null, null);
579
580 String url = null;
581
582 try {
583 url = tagutils.computeURL(pageContext, "foo", null, null, null,
584 null, null, null, false);
585 } catch (MalformedURLException e) {
586 fail("MalformedURLException: " + e);
587 }
588
589 assertNotNull("url present", url);
590 assertEquals("url value", "/myapp/WEB-INF/pages/bar.jsp", url);
591 }
592
593
594 public void testComputeURL1e() {
595 moduleConfig.getControllerConfig().setPagePattern("$C/WEB-INF/pages$M$P");
596 request.setPathElements("/myapp", "/action.do", null, null);
597
598 String url = null;
599
600 try {
601 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
602 null, null, null, false);
603 } catch (MalformedURLException e) {
604 fail("MalformedURLException: " + e);
605 }
606
607 assertNotNull("url present", url);
608 assertEquals("url value", "/myapp/WEB-INF/pages/bar", url);
609 }
610
611
612 public void testComputeURL1f() {
613 request.setPathElements("/myapp", "/action.do", null, null);
614
615 String url = null;
616
617 try {
618 url = tagutils.computeURL(pageContext, "relative1", null, null,
619 null, null, null, null, false);
620 } catch (MalformedURLException e) {
621 fail("MalformedURLException: " + e);
622 }
623
624 assertNotNull("url present", url);
625 assertEquals("url value",
626
627 "relative.jsp", url);
628 }
629
630
631 public void testComputeURL1g() {
632 request.setPathElements("/myapp", "/action.do", null, null);
633
634 String url = null;
635
636 try {
637 url = tagutils.computeURL(pageContext, "relative2", null, null,
638 null, null, null, null, false);
639 } catch (MalformedURLException e) {
640 fail("MalformedURLException: " + e);
641 }
642
643 assertNotNull("url present", url);
644 assertEquals("url value",
645
646 "relative.jsp", url);
647 }
648
649
650 public void testComputeURL1h() {
651 request.setPathElements("/myapp", "/action.do", null, null);
652
653 String url = null;
654
655 try {
656 url = tagutils.computeURL(pageContext, "external", null, null,
657 null, null, null, null, false);
658 } catch (MalformedURLException e) {
659 fail("MalformedURLException: " + e);
660 }
661
662 assertNotNull("url present", url);
663 assertEquals("url value", "http://struts.apache.org/", url);
664 }
665
666
667 public void testComputeURL2a() {
668 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
669 request.setPathElements("/myapp", "/2/action.do", null, null);
670
671 String url = null;
672
673 try {
674 url = tagutils.computeURL(pageContext, "foo", null, null, null,
675 null, null, null, false);
676 } catch (MalformedURLException e) {
677 fail("MalformedURLException: " + e);
678 }
679
680 assertNotNull("url present", url);
681 assertEquals("url value", "/myapp/2/baz.jsp", url);
682 }
683
684
685 public void testComputeURL2b() {
686 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
687 request.setPathElements("/myapp", "/2/action.do", null, null);
688
689 String url = null;
690
691 try {
692 url = tagutils.computeURL(pageContext, null, "http://foo.com/bar",
693 null, null, null, null, null, false);
694 } catch (MalformedURLException e) {
695 fail("MalformedURLException: " + e);
696 }
697
698 assertNotNull("url present", url);
699 assertEquals("url value", "http://foo.com/bar", url);
700 }
701
702
703 public void testComputeURL2c() {
704 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
705 request.setPathElements("/myapp", "/2/action.do", null, null);
706
707 String url = null;
708
709 try {
710 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
711 null, null, null, false);
712 } catch (MalformedURLException e) {
713 fail("MalformedURLException: " + e);
714 }
715
716 assertNotNull("url present", url);
717 assertEquals("url value", "/myapp/2/bar", url);
718 }
719
720
721 public void testComputeURL2d() {
722 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
723 moduleConfig2.getControllerConfig().setForwardPattern("$C/WEB-INF/pages$M$P");
724 request.setPathElements("/myapp", "/2/action.do", null, null);
725
726 String url = null;
727
728 try {
729 url = tagutils.computeURL(pageContext, "foo", null, null, null,
730 null, null, null, false);
731 } catch (MalformedURLException e) {
732 fail("MalformedURLException: " + e);
733 }
734
735 assertNotNull("url present", url);
736 assertEquals("url value", "/myapp/WEB-INF/pages/2/baz.jsp", url);
737 }
738
739
740 public void testComputeURL2e() {
741 moduleConfig2.getControllerConfig().setPagePattern("$C/WEB-INF/pages$M$P");
742 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
743 request.setPathElements("/myapp", "/2/action.do", null, null);
744
745 String url = null;
746
747 try {
748 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
749 null, null, null, false);
750 } catch (MalformedURLException e) {
751 fail("MalformedURLException: " + e);
752 }
753
754 assertNotNull("url present", url);
755 assertEquals("url value", "/myapp/WEB-INF/pages/2/bar", url);
756 }
757
758
759 public void testComputeURL2f() {
760 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
761 request.setPathElements("/myapp", "/2/action.do", null, null);
762
763 String url = null;
764
765 try {
766 url = tagutils.computeURL(pageContext, "relative1", null, null,
767 null, null, null, null, false);
768 } catch (MalformedURLException e) {
769 fail("MalformedURLException: " + e);
770 }
771
772 assertNotNull("url present", url);
773 assertEquals("url value",
774
775 "relative.jsp", url);
776 }
777
778
779 public void testComputeURL2g() {
780 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
781 request.setPathElements("/myapp", "/2/action.do", null, null);
782
783 String url = null;
784
785 try {
786 url = tagutils.computeURL(pageContext, "relative2", null, null,
787 null, null, null, null, false);
788 } catch (MalformedURLException e) {
789 fail("MalformedURLException: " + e);
790 }
791
792 assertNotNull("url present", url);
793 assertEquals("url value",
794
795 "relative.jsp", url);
796 }
797
798
799 public void testComputeURL2h() {
800 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
801 request.setPathElements("/myapp", "/2/action.do", null, null);
802
803 String url = null;
804
805 try {
806 url = tagutils.computeURL(pageContext, "external", null, null,
807 null, null, null, null, false);
808 } catch (MalformedURLException e) {
809 fail("MalformedURLException: " + e);
810 }
811
812 assertNotNull("url present", url);
813 assertEquals("url value", "http://struts.apache.org/", url);
814 }
815
816
817 public void testComputeURL3a() {
818 request.setPathElements("/myapp", "/action.do", null, null);
819
820 Map map = new HashMap();
821
822 map.put("foo1", "bar1");
823 map.put("foo2", "bar2");
824
825 String url = null;
826
827 try {
828 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
829 null, map, null, false);
830 } catch (MalformedURLException e) {
831 fail("MalformedURLException: " + e);
832 }
833
834 assertNotNull("url present", url);
835 assertTrue("url value",
836 url.equals("/myapp/bar?foo1=bar1&foo2=bar2")
837 || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
838 }
839
840
841 public void testComputeURL3b() {
842 request.setPathElements("/myapp", "/action.do", null, null);
843
844 String url = null;
845
846 try {
847 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
848 null, null, "anchor", false);
849 } catch (MalformedURLException e) {
850 fail("MalformedURLException: " + e);
851 }
852
853 assertNotNull("url present", url);
854 assertEquals("url value", "/myapp/bar#anchor", url);
855 }
856
857
858 public void testComputeURL3c() {
859 request.setPathElements("/myapp", "/action.do", null, null);
860
861 Map map = new HashMap();
862
863 map.put("foo1", "bar1");
864 map.put("foo2", "bar2");
865
866 String url = null;
867
868 try {
869 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
870 null, map, "anchor", false);
871 } catch (MalformedURLException e) {
872 fail("MalformedURLException: " + e);
873 }
874
875 assertNotNull("url present", url);
876 assertTrue("url value",
877 url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor")
878 || url.equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor"));
879 }
880
881
882 public void testComputeURL3d() {
883 request.setPathElements("/myapp", "/action.do", null, null);
884
885 Map map = new HashMap();
886
887 map.put("foo1", "bar1");
888 map.put("foo2", "bar2");
889
890 String url = null;
891
892 try {
893 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
894 null, map, null, true);
895 } catch (MalformedURLException e) {
896 fail("MalformedURLException: " + e);
897 }
898
899 assertNotNull("url present", url);
900 assertTrue("url value",
901 url.equals("/myapp/bar?foo1=bar1&foo2=bar2")
902 || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
903 }
904
905
906 public void testComputeURL3e() {
907 request.setPathElements("/myapp", "/action.do", null, null);
908
909 String url = null;
910
911 try {
912 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
913 null, null, "anchor", true);
914 } catch (MalformedURLException e) {
915 fail("MalformedURLException: " + e);
916 }
917
918 assertNotNull("url present", url);
919 assertEquals("url value", "/myapp/bar#anchor", url);
920 }
921
922
923 public void testComputeURL3f() {
924 request.setPathElements("/myapp", "/action.do", null, null);
925
926 Map map = new HashMap();
927
928 map.put("foo1", "bar1");
929 map.put("foo2", "bar2");
930
931 String url = null;
932
933 try {
934 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
935 null, map, "anchor", false);
936 } catch (MalformedURLException e) {
937 fail("MalformedURLException: " + e);
938 }
939
940 assertNotNull("url present", url);
941 assertTrue("url value",
942 url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor")
943 || url.equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor"));
944 }
945
946
947 public void testComputeURL3g() {
948 request.setPathElements("/myapp", "/action.do", null, null);
949
950 Map map = new HashMap();
951
952 map.put("foo1", "bar1");
953 map.put("foo2", "bar2");
954
955 String url = null;
956
957 try {
958 url = tagutils.computeURLWithCharEncoding(pageContext, null, null,
959 "/bar", null, null, map, null, false, false, false);
960 } catch (MalformedURLException e) {
961 fail("MalformedURLException: " + e);
962 }
963
964 assertNotNull("url present", url);
965 assertTrue("url value",
966 url.equals("/myapp/bar?foo1=bar1&foo2=bar2")
967 || url.equals("/myapp/bar?foo2=bar2&foo1=bar1"));
968 }
969
970
971
972
973
974 public void testComputeURL3h() {
975 request.setPathElements("/myapp", "/action.do", null, null);
976
977 Map map = new HashMap();
978
979 map.put("foo1", null);
980
981 String url = null;
982
983 try {
984 url = tagutils.computeURLWithCharEncoding(pageContext, null, null,
985 "/bar", null, null, map, null, false, false, false);
986 } catch (MalformedURLException e) {
987 fail("MalformedURLException: " + e);
988 }
989
990 assertNotNull("url present", url);
991 assertTrue("url value", url.equals("/myapp/bar?foo1="));
992 }
993
994
995
996
997
998
999 public void testComputeURL3i() {
1000 request.setPathElements("/myapp", "/action.do", null, null);
1001
1002 Map map = new HashMap();
1003
1004 map.put("foo1", null);
1005
1006 String url = null;
1007
1008 try {
1009 url = tagutils.computeURLWithCharEncoding(pageContext, null, null,
1010 "/bar?", null, null, map, null, false, false, false);
1011 } catch (MalformedURLException e) {
1012 fail("MalformedURLException: " + e);
1013 }
1014
1015 assertNotNull("url present", url);
1016 assertTrue("url value", url.equals("/myapp/bar?&foo1="));
1017 }
1018
1019
1020
1021
1022
1023
1024 public void testComputeURL3j() {
1025 request.setPathElements("/myapp", "/action.do", null, null);
1026
1027 Map map = new HashMap();
1028
1029 map.put("foo1", null);
1030 map.put("foo2", "bar2");
1031
1032 String url = null;
1033
1034 try {
1035 url = tagutils.computeURLWithCharEncoding(pageContext, null, null,
1036 "/bar?a=b", null, null, map, null, false, false, false);
1037 } catch (MalformedURLException e) {
1038 fail("MalformedURLException: " + e);
1039 }
1040
1041 assertNotNull("url present", url);
1042 assertTrue("url value",
1043 url.equals("/myapp/bar?a=b&foo1=&foo2=bar2")
1044 || url.equals("/myapp/bar?a=b&foo2=bar2&foo1="));
1045 }
1046
1047
1048
1049 public void testComputeURL3k() {
1050 request.setPathElements("/myapp", "/action.do", null, null);
1051
1052 Map map = new HashMap();
1053
1054 map.put("foo1", new String[] { "bar1", "baz1" });
1055
1056 String url = null;
1057
1058 try {
1059 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
1060 null, map, null, false);
1061 } catch (MalformedURLException e) {
1062 fail("MalformedURLException: " + e);
1063 }
1064
1065 assertNotNull("url present", url);
1066 assertTrue("url value",
1067 url.equals("/myapp/bar?foo1=bar1&foo1=baz1")
1068 || url.equals("/myapp/bar?foo1=baz1&foo1=bar1"));
1069 }
1070
1071
1072
1073 public void testComputeURL3l() {
1074 request.setPathElements("/myapp", "/action.do", null, null);
1075
1076 Map map = new HashMap();
1077
1078 map.put("foo1", new Double(0));
1079
1080 String url = null;
1081
1082 try {
1083 url = tagutils.computeURL(pageContext, null, null, "/bar", null,
1084 null, map, null, false);
1085 } catch (MalformedURLException e) {
1086 fail("MalformedURLException: " + e);
1087 }
1088
1089 assertNotNull("url present", url);
1090 assertTrue("url value", url.equals("/myapp/bar?foo1=0.0"));
1091 }
1092
1093
1094
1095
1096 public void testComputeURL3m() {
1097 request.setPathElements("/myapp", "/action.do", null, null);
1098
1099 Map map = new HashMap();
1100
1101 map.put("foo1", new Double(0));
1102
1103 String url = null;
1104
1105 try {
1106 url = tagutils.computeURL(pageContext, null, null, "/bar?", null,
1107 null, map, null, false);
1108 } catch (MalformedURLException e) {
1109 fail("MalformedURLException: " + e);
1110 }
1111
1112 assertNotNull("url present", url);
1113 assertTrue("url value", url.equals("/myapp/bar?&foo1=0.0"));
1114 }
1115
1116 public void testComputeURLCharacterEncoding() {
1117 request.setPathElements("/myapp", "/action.do", null, null);
1118
1119 String url = null;
1120
1121 try {
1122 url = tagutils.computeURLWithCharEncoding(pageContext, "foo", null,
1123 null, null, null, null, null, false, true, true);
1124 fail("Exception not thrown");
1125 } catch (MalformedURLException e) {
1126 fail("MalformedURLException: " + e);
1127 } catch (UnsupportedOperationException e) {
1128 assertNull("url should be null", url);
1129 }
1130 }
1131
1132 public void testComputeURLCharacterEncodingMultipleSpecifier() {
1133 verifyBadSetOfSpecifiers("foo", "foo", null, null);
1134 verifyBadSetOfSpecifiers("foo", null, "foo", null);
1135 verifyBadSetOfSpecifiers("foo", null, null, "foo");
1136
1137 verifyBadSetOfSpecifiers(null, "foo", "foo", null);
1138 verifyBadSetOfSpecifiers(null, "foo", null, "foo");
1139
1140 verifyBadSetOfSpecifiers(null, null, "foo", "foo");
1141 }
1142
1143 public void testComputeURLCharacterEncodingAction() {
1144 ActionConfig actionConfig = new ActionConfig();
1145
1146 actionConfig.setName("baz");
1147 actionConfig.setPath("/baz");
1148
1149 moduleConfig.addActionConfig(actionConfig);
1150
1151 request.setPathElements("/myapp", "/foo.do", null, null);
1152
1153 Map map = new HashMap();
1154
1155 map.put("foo1", "bar1");
1156 map.put("foo2", "bar2");
1157
1158 String url = null;
1159
1160 try {
1161 url = tagutils.computeURL(pageContext, null, null, null, "baz",
1162 null, map, "anchor", false);
1163 } catch (MalformedURLException e) {
1164 fail("MalformedURLException: " + e);
1165 }
1166
1167 assertNotNull("url present", url);
1168 assertTrue("url value",
1169 url.equals("/myapp/baz?foo1=bar1&foo2=bar2#anchor")
1170 || url.equals("/myapp/baz?foo2=bar2&foo1=bar1#anchor"));
1171 }
1172
1173
1174
1175 public void testPageURL1() {
1176 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1177 request.setPathElements("/myapp", "/action.do", null, null);
1178
1179 String page = null;
1180 String result = null;
1181
1182
1183 page = "/mypages/index.jsp";
1184 result = tagutils.pageURL(request, page, moduleConfig);
1185 assertNotNull("straight sub found", result);
1186 assertEquals("straight sub value", "/mypages/index.jsp", result);
1187 }
1188
1189
1190 public void testPageURL2() {
1191 request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
1192 request.setPathElements("/myapp", "/2/action.do", null, null);
1193
1194 String page = null;
1195 String result = null;
1196
1197
1198 page = "/mypages/index.jsp";
1199 result = tagutils.pageURL(request, page, moduleConfig2);
1200 assertNotNull("straight sub found", result);
1201 assertEquals("straight sub value", "/2/mypages/index.jsp", result);
1202 }
1203
1204
1205
1206 public void testPageURL3a() {
1207 request.setAttribute(Globals.MODULE_KEY, moduleConfig3);
1208 request.setPathElements("/myapp", "/3/action.do", null, null);
1209
1210
1211
1212 }
1213
1214
1215 public void testPageURL3b() {
1216 request.setAttribute(Globals.MODULE_KEY, moduleConfig3);
1217 request.setPathElements("/myapp", "/3/action.do", null, null);
1218
1219 String page = null;
1220 String result = null;
1221
1222
1223 page = "/mypages/index.jsp";
1224 result = tagutils.pageURL(request, page, moduleConfig3);
1225 assertNotNull("straight sub found", result);
1226 assertEquals("straight sub value", "/3/mypages/index.jsp", result);
1227 }
1228
1229 /***
1230 * Helper method that verifies the supplied specifiers.
1231 *
1232 * @param forward The forward specified
1233 * @param href The href specified
1234 * @param pageString The pageString specified
1235 * @param action The action specified
1236 */
1237 private void verifyBadSetOfSpecifiers(String forward, String href,
1238 String pageString, String action) {
1239 String url = null;
1240
1241 try {
1242 url = tagutils.computeURLWithCharEncoding(pageContext, forward,
1243 href, pageString, action, null, null, null, false, true,
1244 false);
1245 } catch (MalformedURLException e) {
1246 assertNull("url should be null", url);
1247 } catch (UnsupportedOperationException e) {
1248 fail("MalformedURLException not thrown");
1249 }
1250 }
1251
1252
1253
1254 public void testencodeURL1() {
1255 String encodedURL = null;
1256
1257 encodedURL = tagutils.encodeURL("foo-bar.baz");
1258 assertEquals("encode url", "foo-bar.baz", encodedURL);
1259 }
1260
1261
1262
1263 public void testGetActionErrors1a() {
1264 ActionMessages actionErrors = new ActionMessages();
1265
1266 actionErrors.add("prop", new ActionMessage("key.key"));
1267 request.setAttribute("errors", actionErrors);
1268
1269 try {
1270 ActionMessages errors =
1271 tagutils.getActionMessages(pageContext, "errors");
1272
1273 assertNotNull("errors should not be null", errors);
1274 assertNotNull("errors prop should not be null", errors.get("prop"));
1275
1276 String val = null;
1277 int i = 0;
1278
1279 for (Iterator iter = errors.get("prop"); iter.hasNext();) {
1280 ActionMessage error = (ActionMessage) iter.next();
1281
1282 val = error.getKey();
1283 i++;
1284 }
1285
1286 assertEquals("only 1 error", i, 1);
1287 assertEquals("errors prop should match", val, "key.key");
1288 } catch (JspException e) {
1289 fail(e.getMessage());
1290 }
1291 }
1292
1293
1294 public void testGetActionErrors1b() {
1295 request.setAttribute("foo", "bar");
1296
1297 try {
1298 ActionMessages errors =
1299 tagutils.getActionMessages(pageContext, "foo");
1300
1301 assertNotNull("errors should not be null", errors);
1302 assertNotNull("errors prop should not be null", errors.get("prop"));
1303
1304 String key = null;
1305 int i = 0;
1306
1307 for (Iterator iter = errors.get(ActionMessages.GLOBAL_MESSAGE);
1308 iter.hasNext();) {
1309 ActionMessage error = (ActionMessage) iter.next();
1310
1311 key = error.getKey();
1312
1313 Object[] values = error.getValues();
1314
1315 assertNull(values);
1316 i++;
1317 }
1318
1319 assertEquals("only 1 error", i, 1);
1320 assertEquals("key should match", key, "bar");
1321 } catch (JspException e) {
1322 fail(e.getMessage());
1323 }
1324 }
1325
1326
1327 public void testGetActionErrors1c() {
1328 String[] vals = new String[] { "bar", "baz" };
1329
1330 request.setAttribute("foo", vals);
1331
1332 try {
1333 ActionMessages errors =
1334 tagutils.getActionMessages(pageContext, "foo");
1335
1336 assertNotNull("errors should not be null", errors);
1337 assertNotNull("errors prop should not be null", errors.get("prop"));
1338
1339 String key = null;
1340 int i = 0;
1341
1342 for (Iterator iter = errors.get(ActionMessages.GLOBAL_MESSAGE);
1343 iter.hasNext();) {
1344 ActionMessage error = (ActionMessage) iter.next();
1345
1346 key = error.getKey();
1347
1348 Object[] values = error.getValues();
1349
1350 assertNull((values));
1351 assertEquals("1st key should match", key, vals[i]);
1352 i++;
1353 }
1354
1355 assertEquals("only 1 error", i, 2);
1356 } catch (JspException e) {
1357 fail(e.getMessage());
1358 }
1359 }
1360
1361
1362 public void testGetActionErrors1d() {
1363 request.setAttribute("foo", new MockFormBean());
1364
1365 ActionMessages errors = null;
1366
1367 try {
1368 errors = tagutils.getActionMessages(pageContext, "foo");
1369 fail("should have thrown JspException");
1370 } catch (JspException e) {
1371 assertNull("errors should be null", errors);
1372 }
1373 }
1374
1375
1376
1377 public void testGetActionErrors1e() {
1378 ActionMessages actionErrors = new ActionMessages();
1379
1380 actionErrors.add("prop", new ActionMessage("key.key"));
1381 request.setAttribute("errors", actionErrors);
1382
1383 try {
1384 ActionMessages errors =
1385 tagutils.getActionMessages(pageContext, "does-not-exist");
1386
1387 assertNotNull("errors should not be null", errors);
1388 assertNotNull("errors prop should not be null", errors.get("prop"));
1389
1390 for (Iterator iter = errors.get("prop"); iter.hasNext();) {
1391 fail("Should not have any errors for does-not-exist");
1392 }
1393 } catch (JspException e) {
1394 fail(e.getMessage());
1395 }
1396 }
1397
1398
1399 public void testGetActionMappingName1() {
1400 String[] paths =
1401 {
1402 "foo", "foo.do", "foo?foo=bar", "foo?foo=bar&bar=baz",
1403 "foo?foo=bar&bar=baz"
1404 };
1405
1406 String[][] prepends =
1407 {
1408 { "", "/foo" },
1409 { "/", "/foo" },
1410 { "bar/", "/bar/foo" },
1411 { "/bar/", "/bar/foo" }
1412 };
1413
1414 String[] appends =
1415 {
1416 "", "#anchor", "?", "?#", "?foo=bar", "?foo1=bar1&foo2=bar2",
1417 "?foo1=bar1&foo2=bar2"
1418 };
1419
1420 String finalResult = null;
1421
1422 String path = null;
1423 String results = null;
1424 boolean equality = false;
1425 int ct = 0;
1426
1427 for (int i = 0; i < appends.length; i++) {
1428 for (int j = 0; j < prepends.length; j++) {
1429 finalResult = prepends[j][1];
1430
1431 for (int k = 0; k < paths.length; k++) {
1432 path = prepends[j][0] + paths[k] + appends[i];
1433 results = tagutils.getActionMappingName(path);
1434 equality = finalResult.equals(results);
1435
1436 if (!equality) {
1437 fail("Path does not return correct result\n"
1438 + "\nexpected: " + results + "\nfound: " + path);
1439 }
1440
1441 assertTrue("Path should translate to result", equality);
1442 ct++;
1443 }
1444 }
1445 }
1446
1447 log.debug(ct + " assertions run in this test");
1448 }
1449
1450 public void testString_getActionMappingURL_String_PageContext() {
1451 ActionConfig actionConfig = new ActionConfig();
1452
1453 actionConfig.setParameter("/foo");
1454 moduleConfig.addActionConfig(actionConfig);
1455
1456 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1457 request.setPathElements("/myapp", "/foo.do", null, null);
1458
1459 assertEquals("Check path /foo",
1460 tagutils.getActionMappingURL("/foo", pageContext), "/myapp/foo");
1461 }
1462
1463
1464 public void testString_getActionMappingURL_String_String_PageContext_boolean1() {
1465 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY, "*.do");
1466
1467 ActionConfig actionConfig = new ActionConfig();
1468
1469 actionConfig.setParameter("/foo");
1470 moduleConfig.addActionConfig(actionConfig);
1471
1472 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1473 request.setPathElements("/myapp", "/baz.do", null, null);
1474
1475 assertEquals("Check path /foo",
1476 tagutils.getActionMappingURL("/foo", pageContext), "/myapp/foo.do");
1477 }
1478
1479
1480
1481 public void testString_getActionMappingURL_String_String_PageContext_boolean2() {
1482 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY, "*.do");
1483
1484 ActionConfig actionConfig = new ActionConfig();
1485
1486 actionConfig.setParameter("/foo");
1487 moduleConfig.addActionConfig(actionConfig);
1488
1489 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1490 request.setPathElements("/myapp", "/baz.do?foo=bar", null, null);
1491
1492 assertEquals("Check path /foo",
1493 tagutils.getActionMappingURL("/foo?foo=bar", pageContext),
1494 "/myapp/foo.do?foo=bar");
1495 }
1496
1497
1498
1499
1500 public void testString_getActionMappingURL_String_String_PageContext_boolean3() {
1501 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY, "*.do");
1502
1503 ActionConfig actionConfig = new ActionConfig();
1504
1505 actionConfig.setParameter("/foo");
1506 moduleConfig.addActionConfig(actionConfig);
1507
1508 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1509 request.setPathElements("/mycontext", "/baz", null, null);
1510
1511 assertEquals("Check path /foo",
1512 tagutils.getActionMappingURL("/", pageContext), "/mycontext/.do");
1513 }
1514
1515
1516 public void testString_getActionMappingURL_String_String_PageContext_boolean4() {
1517 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY,
1518 "/myapp/*");
1519
1520 ActionConfig actionConfig = new ActionConfig();
1521
1522 actionConfig.setParameter("/foo");
1523 moduleConfig.addActionConfig(actionConfig);
1524
1525 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1526 request.setPathElements("/mycontext", "/baz", null, null);
1527
1528 assertEquals("Check path /foo",
1529 tagutils.getActionMappingURL("/foo", pageContext),
1530 "/mycontext/myapp/foo");
1531 }
1532
1533
1534
1535 public void testString_getActionMappingURL_String_String_PageContext_boolean5() {
1536 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY,
1537 "/myapp/*");
1538
1539 ActionConfig actionConfig = new ActionConfig();
1540
1541 actionConfig.setParameter("/foo");
1542 moduleConfig.addActionConfig(actionConfig);
1543
1544 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1545 request.setPathElements("/mycontext", "/baz?foo=bar", null, null);
1546
1547 assertEquals("Check path /foo",
1548 tagutils.getActionMappingURL("/foo?foo=bar", pageContext),
1549 "/mycontext/myapp/foo?foo=bar");
1550 }
1551
1552
1553
1554 public void testString_getActionMappingURL_String_String_PageContext_boolean6() {
1555 pageContext.getServletContext().setAttribute(Globals.SERVLET_KEY, "/");
1556
1557 ActionConfig actionConfig = new ActionConfig();
1558
1559 actionConfig.setParameter("/foo");
1560 moduleConfig.addActionConfig(actionConfig);
1561
1562 request.setAttribute(Globals.MODULE_KEY, moduleConfig);
1563 request.setPathElements("/mycontext", "/baz", null, null);
1564
1565 assertEquals("Check path /foo",
1566 tagutils.getActionMappingURL("/", pageContext), "/mycontext/");
1567 }
1568
1569
1570
1571 public void testActionMessages_getActionMessages_PageContext_String1() {
1572 ActionMessages actionMessages = new ActionMessages();
1573
1574 actionMessages.add("prop", new ActionMessage("key.key"));
1575 request.setAttribute("messages", actionMessages);
1576
1577 try {
1578 ActionMessages messages =
1579 tagutils.getActionMessages(pageContext, "messages");
1580
1581 assertNotNull("messages should not be null", messages);
1582 assertNotNull("messages prop should not be null",
1583 messages.get("prop"));
1584
1585 String val = null;
1586 int i = 0;
1587
1588 for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1589 ActionMessage message = (ActionMessage) iter.next();
1590
1591 val = message.getKey();
1592 i++;
1593 }
1594
1595 assertEquals("only 1 message", i, 1);
1596 assertEquals("messages prop should match", val, "key.key");
1597 } catch (JspException e) {
1598 fail(e.getMessage());
1599 }
1600 }
1601
1602
1603 public void testActionMessages_getActionMessages_PageContext_String2() {
1604 ActionMessages actionMessages = new ActionMessages();
1605
1606 actionMessages.add("prop", new ActionMessage("key.key"));
1607 request.setAttribute("messages", actionMessages);
1608
1609 try {
1610 ActionMessages messages =
1611 tagutils.getActionMessages(pageContext, "messages");
1612
1613 assertNotNull("messages should not be null", messages);
1614 assertNotNull("messages prop should not be null",
1615 messages.get("prop"));
1616
1617 String val = null;
1618 int i = 0;
1619
1620 for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1621 ActionMessage message = (ActionMessage) iter.next();
1622
1623 val = message.getKey();
1624 i++;
1625 }
1626
1627 assertEquals("only 1 message", i, 1);
1628 assertEquals("messages prop should match", val, "key.key");
1629 } catch (JspException e) {
1630 fail(e.getMessage());
1631 }
1632 }
1633
1634
1635 public void testActionMessages_getActionMessages_PageContext_String3() {
1636 request.setAttribute("foo", "bar");
1637
1638 try {
1639 ActionMessages messages =
1640 tagutils.getActionMessages(pageContext, "foo");
1641
1642 assertNotNull("messages should not be null", messages);
1643 assertNotNull("messages prop should not be null",
1644 messages.get("prop"));
1645
1646 String key = null;
1647 int i = 0;
1648
1649 for (Iterator iter = messages.get(ActionMessages.GLOBAL_MESSAGE);
1650 iter.hasNext();) {
1651 ActionMessage message = (ActionMessage) iter.next();
1652
1653 key = message.getKey();
1654
1655 Object[] values = message.getValues();
1656
1657 assertNull(values);
1658 i++;
1659 }
1660
1661 assertEquals("only 1 message", i, 1);
1662 assertEquals("key should match", key, "bar");
1663 } catch (JspException e) {
1664 fail(e.getMessage());
1665 }
1666 }
1667
1668
1669 public void testActionMessages_getActionMessages_PageContext_String4() {
1670 String[] vals = new String[] { "bar", "baz" };
1671
1672 request.setAttribute("foo", vals);
1673
1674 try {
1675 ActionMessages messages =
1676 tagutils.getActionMessages(pageContext, "foo");
1677
1678 assertNotNull("messages should not be null", messages);
1679 assertNotNull("messages prop should not be null",
1680 messages.get("prop"));
1681
1682 String key = null;
1683 int i = 0;
1684
1685 for (Iterator iter = messages.get(ActionMessages.GLOBAL_MESSAGE);
1686 iter.hasNext();) {
1687 ActionMessage message = (ActionMessage) iter.next();
1688
1689 key = message.getKey();
1690
1691 Object[] values = message.getValues();
1692
1693 assertNull((values));
1694 assertEquals("1st key should match", key, vals[i]);
1695 i++;
1696 }
1697
1698 assertEquals("only 1 message", i, 2);
1699 } catch (JspException e) {
1700 fail(e.getMessage());
1701 }
1702 }
1703
1704
1705 public void testActionMessages_getActionMessages_PageContext_String5() {
1706 request.setAttribute("foo", new MockFormBean());
1707
1708 ActionMessages messages = null;
1709
1710 try {
1711 messages = tagutils.getActionMessages(pageContext, "foo");
1712 fail("should have thrown JspException");
1713 } catch (JspException e) {
1714 assertNull("messages should be null", messages);
1715 }
1716 }
1717
1718
1719
1720 public void testActionMessages_getActionMessages_PageContext_String6() {
1721 ActionMessages actionMessages = new ActionMessages();
1722
1723 actionMessages.add("prop", new ActionMessage("key.key"));
1724 request.setAttribute("messages", actionMessages);
1725
1726 try {
1727 ActionMessages messages =
1728 tagutils.getActionMessages(pageContext, "does-not-exist");
1729
1730 assertNotNull("messages should not be null", messages);
1731 assertNotNull("messages prop should not be null",
1732 messages.get("prop"));
1733
1734 for (Iterator iter = messages.get("prop"); iter.hasNext();) {
1735 fail("Should not have any messages for does-not-exist");
1736 }
1737 } catch (JspException e) {
1738 fail(e.getMessage());
1739 }
1740 }
1741
1742
1743 public void testModuleConfig_getModuleConfig_PageContext() {
1744 MockServletConfig mockServletConfig = new MockServletConfig();
1745 ModuleConfig moduleConfig = new ModuleConfigImpl("");
1746 MockServletContext mockServletContext = new MockServletContext();
1747 MockHttpServletRequest mockHttpServletRequest =
1748 new MockHttpServletRequest();
1749 MockHttpServletResponse mockHttpServletResponse =
1750 new MockHttpServletResponse();
1751
1752 mockServletConfig.setServletContext(mockServletContext);
1753
1754 MockPageContext mockPageContext =
1755 new MockPageContext(mockServletConfig, mockHttpServletRequest,
1756 mockHttpServletResponse);
1757
1758 ModuleConfig foundModuleConfig = null;
1759
1760 try {
1761 foundModuleConfig = tagutils.getModuleConfig(mockPageContext);
1762 fail("Expected ModuleConfig to not be found");
1763 } catch (NullPointerException ignore) {
1764
1765 }
1766
1767 mockHttpServletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
1768
1769 mockPageContext.getServletContext().setAttribute(Globals.MODULE_KEY,
1770 mockPageContext);
1771
1772 foundModuleConfig = tagutils.getModuleConfig(mockPageContext);
1773 assertNotNull(foundModuleConfig);
1774 }
1775
1776
1777 public void testLocale_getUserLocale_PageContext_String() {
1778 request.setLocale(Locale.ENGLISH);
1779 assertEquals(tagutils.getUserLocale(pageContext, ""), Locale.ENGLISH);
1780
1781 request.setLocale(Locale.CANADA);
1782 assertEquals(tagutils.getUserLocale(pageContext, ""), Locale.CANADA);
1783 }
1784
1785
1786 public void test_boolean_isXhtml_PageContext() {
1787 assertFalse(tagutils.isXhtml(pageContext));
1788 pageContext.setAttribute(Globals.XHTML_KEY, "true");
1789
1790 assertTrue(tagutils.isXhtml(pageContext));
1791 }
1792
1793
1794
1795 public void test_Object_lookup_PageContext_String__String1() {
1796 pageContext.setAttribute("bean", new MockFormBean());
1797
1798 try {
1799 Object val = tagutils.lookup(pageContext, "bean", null);
1800
1801 assertNotNull((val));
1802 } catch (JspException e) {
1803 fail("bean not found:" + e.getMessage());
1804 }
1805 }
1806
1807
1808 public void test_Object_lookup_PageContext_String__String2() {
1809 pageContext.setAttribute("bean", new MockFormBean());
1810
1811 try {
1812 Object val = tagutils.lookup(pageContext, "bean", "page");
1813
1814 assertNotNull((val));
1815 } catch (JspException e) {
1816 fail("bean not found:" + e.getMessage());
1817 }
1818 }
1819
1820
1821
1822 public void test_Object_lookup_PageContext_String__String3() {
1823 pageContext.setAttribute("bean", new MockFormBean());
1824
1825 Object val = null;
1826
1827 try {
1828 val = tagutils.lookup(pageContext, "bean", "invalid");
1829 fail("invalid scope :");
1830 } catch (JspException e) {
1831 assertNull((val));
1832 }
1833 }
1834
1835
1836 public void test_Object_lookup_PageContext_String_String_String1() {
1837
1838
1839
1840
1841
1842
1843
1844
1845 }
1846
1847
1848 public void test_Object_lookup_PageContext_String_String_String2() {
1849 pageContext.setAttribute("bean", new MockFormBean());
1850
1851 Object val = null;
1852
1853 try {
1854 val = tagutils.lookup(pageContext, "bean", "doesNotExistMethod",
1855 "page");
1856 fail("should have thrown exception");
1857 } catch (JspException e) {
1858 assertNull(val);
1859 }
1860 }
1861
1862
1863 public void test_Object_lookup_PageContext_String_String_String3() {
1864 pageContext.setAttribute("bean", new MockFormBean());
1865
1866 Object val = null;
1867
1868 try {
1869 val = tagutils.lookup(pageContext, "bean", "doesNotExistMethod");
1870 fail("should have thrown exception");
1871 } catch (JspException e) {
1872 assertNull(val);
1873 }
1874 }
1875
1876 /***
1877 * Testing message()
1878 *
1879 * public String message( PageContext pageContext, String bundle, String
1880 * locale, String key) throws JspException
1881 */
1882 public void testMessageBadParams() {
1883 String val = null;
1884
1885 try {
1886 val = tagutils.message(pageContext, "bundle", "locale", "key");
1887 fail("val should be null");
1888 } catch (JspException e) {
1889 assertNull(val);
1890 }
1891 }
1892
1893
1894
1895
1896 public void donttestMessagePageBadKey() {
1897 putBundleInScope(PageContext.PAGE_SCOPE, true);
1898
1899 String val = null;
1900
1901 try {
1902 val = tagutils.message(pageContext, null, null,
1903 "foo.bar.does.not.exist");
1904 fail("val should be null");
1905 } catch (JspException e) {
1906 assertNull(val);
1907 }
1908 }
1909
1910
1911
1912 public void testMessageRequestBadKey() {
1913 putBundleInScope(PageContext.REQUEST_SCOPE, true);
1914
1915 String val = null;
1916
1917 try {
1918 val = tagutils.message(pageContext, null, null,
1919 "foo.bar.does.not.exist");
1920 assertNull(val);
1921 } catch (JspException e) {
1922 fail("val should be null, no exception");
1923 }
1924 }
1925
1926
1927
1928
1929
1930
1931
1932
1933 public void testMessageSessionBadKey() {
1934 putBundleInScope(PageContext.SESSION_SCOPE, true);
1935
1936 String val = null;
1937
1938 try {
1939 val = tagutils.message(pageContext, null, null,
1940 "foo.bar.does.not.exist");
1941 fail("MessageResources should never be put in session scope.");
1942 } catch (JspException e) {
1943 assertNull(val);
1944 }
1945 }
1946
1947
1948
1949 public void testMessageApplicationBadKey() {
1950 putBundleInScope(PageContext.APPLICATION_SCOPE, true);
1951
1952 String val = null;
1953
1954 try {
1955 val = tagutils.message(pageContext, null, null,
1956 "foo.bar.does.not.exist");
1957 assertNull(val);
1958 } catch (JspException e) {
1959 fail("val should be null, no exception");
1960 }
1961 }
1962
1963
1964
1965 public void testMessageRequestGoodKey() {
1966 putBundleInScope(PageContext.REQUEST_SCOPE, true);
1967
1968 String val = null;
1969
1970 try {
1971 val = tagutils.message(pageContext, null, null, "foo");
1972 assertTrue("Validate message value", "bar".equals(val));
1973 } catch (JspException e) {
1974 fail("val should be \"bar\"");
1975 }
1976 }
1977
1978
1979
1980 public void testMessageApplicationGoodKey() {
1981 putBundleInScope(PageContext.APPLICATION_SCOPE, true);
1982
1983 String val = null;
1984
1985 try {
1986 val = tagutils.message(pageContext, null, null, "foo");
1987 assertTrue("Validate message value", "bar".equals(val));
1988 } catch (JspException e) {
1989 fail("val should be \"bar\"");
1990 }
1991 }
1992
1993 /***
1994 * Tests for:
1995 *
1996 * public String message( PageContext pageContext, String bundle, String
1997 * locale, String key, Object args[]) throws JspException
1998 */
1999 public void testMessageRequestGoodKeyWithNullParams() {
2000 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2001
2002 String[] args = null;
2003
2004 String val = null;
2005
2006 try {
2007 val = tagutils.message(pageContext, null, null, "foo", args);
2008 assertTrue("Validate message value", "bar".equals(val));
2009 } catch (JspException e) {
2010 fail("val should be \"bar\"");
2011 }
2012 }
2013
2014 public void testMessageApplicationGoodKeyWithNullParams() {
2015 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2016
2017 String[] args = null;
2018
2019 String val = null;
2020
2021 try {
2022 val = tagutils.message(pageContext, null, null, "foo", args);
2023 assertTrue("Validate message value", "bar".equals(val));
2024 } catch (JspException e) {
2025 fail("val should be \"bar\"");
2026 }
2027 }
2028
2029 public void testMessageRequestGoodKeyWithParams() {
2030 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2031
2032 String[] args = { "I love this" };
2033
2034 String val = null;
2035
2036 try {
2037 val = tagutils.message(pageContext, null, null, "foo.bar", args);
2038 assertTrue("Validate message value", "I love this bar".equals(val));
2039 } catch (JspException e) {
2040 fail("val should be \"bar\"");
2041 }
2042 }
2043
2044 public void testMessageApplicationGoodKeyWithParams() {
2045 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2046
2047 String[] args = { "I love this" };
2048
2049 String val = null;
2050
2051 try {
2052 val = tagutils.message(pageContext, null, null, "foo.bar", args);
2053 assertTrue("Validate message value", "I love this bar".equals(val));
2054 } catch (JspException e) {
2055 fail("val should be \"bar\"");
2056 }
2057 }
2058
2059 /***
2060 * Tests for: public boolean present( PageContext pageContext, String
2061 * bundle, String locale, String key) throws JspException {
2062 */
2063 public void testPresentNulls() {
2064 boolean result = false;
2065
2066 try {
2067 result = tagutils.present(null, null, null, null);
2068 fail("An exception should have been thrown");
2069 } catch (JspException e) {
2070 fail("An npe should have been thrown");
2071 } catch (NullPointerException e) {
2072 assertFalse("Correct behaviour", result);
2073 }
2074 }
2075
2076 public void testPresentBadKey() {
2077 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2078
2079 boolean result = false;
2080
2081 try {
2082 result =
2083 tagutils.present(pageContext, null, null, "foo.bar.not.exist");
2084 assertFalse("Value should be null", result);
2085 } catch (JspException e) {
2086 fail("An npe should have been thrown");
2087 } catch (NullPointerException e) {
2088 assertFalse("Correct behaviour", result);
2089 }
2090 }
2091
2092 public void testPresentGoodKey() {
2093 putBundleInScope(PageContext.REQUEST_SCOPE, true);
2094
2095 boolean result = false;
2096
2097 try {
2098 result = tagutils.present(pageContext, null, null, "foo");
2099 assertTrue("Key should have been found", result);
2100 } catch (JspException e) {
2101 fail("An exception should have been thrown");
2102 }
2103 }
2104
2105 /***
2106 * public void write(PageContext pageContext, String text) throws
2107 * JspException {
2108 */
2109 public void testWriteNullParams() {
2110 try {
2111 tagutils.write(null, null);
2112 fail("NullPointerException should have been thrown");
2113 } catch (JspException e) {
2114 fail("NullPointerException should have been thrown");
2115 } catch (NullPointerException e) {
2116
2117 }
2118 }
2119
2120 public void testWrite() {
2121 MockPageContext pg = new MockPageContext(false, false);
2122
2123 try {
2124 tagutils.write(pg, null);
2125 } catch (JspException e) {
2126 fail("JspException should not have been thrown");
2127 }
2128 }
2129
2130 public void testWriteThrowException() {
2131 MockPageContext pg = new MockPageContext(true, false);
2132
2133 try {
2134 tagutils.write(pg, null);
2135 fail("JspException should have been thrown");
2136 } catch (JspException e) {
2137
2138 }
2139 }
2140
2141 public void testWritePrevious() {
2142 MockPageContext pg = new MockPageContext(false, false);
2143
2144 try {
2145 tagutils.writePrevious(pg, null);
2146 } catch (JspException e) {
2147 fail("JspException should not have been thrown");
2148 }
2149 }
2150
2151 public void testWritePreviousThrowException() {
2152 MockPageContext pg = new MockPageContext(true, false);
2153
2154 try {
2155 tagutils.writePrevious(pg, null);
2156 fail("JspException should have been thrown");
2157 } catch (JspException e) {
2158
2159 }
2160 }
2161
2162 public void testWritePreviousBody() {
2163 MockPageContext pg = new MockPageContext(false, true);
2164
2165 try {
2166 tagutils.writePrevious(pg, null);
2167 } catch (JspException e) {
2168 fail("JspException should not have been thrown");
2169 }
2170 }
2171
2172 public void testOverrideInstance(){
2173
2174 class CustomTagUtils extends TagUtils{
2175 public String filter(String value) {
2176 return "I HAVE BEEN OVERRIDDEN!";
2177 }
2178 }
2179
2180 assertNull("Filter Test", TagUtils.getInstance().filter(null));
2181
2182
2183 TagUtils.setInstance(new CustomTagUtils());
2184 assertEquals("Custom Instance Test", TagUtils.getInstance().filter(null), "I HAVE BEEN OVERRIDDEN!");
2185
2186
2187 TagUtils.setInstance(tagutils);
2188 assertNull("Filter Test", TagUtils.getInstance().filter(null));
2189
2190 }
2191 }