KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > util > TestRequestUtils


1 /*
2  * $Id: TestRequestUtils.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.util;
21
22
23 import java.net.MalformedURLException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.jsp.JspException JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.DynaActionForm;
36 import org.apache.struts.action.RequestProcessor;
37 import org.apache.struts.config.ForwardConfig;
38 import org.apache.struts.config.ModuleConfig;
39 import org.apache.struts.mock.MockFormBean;
40 import org.apache.struts.mock.MockPrincipal;
41 import org.apache.struts.mock.TestMockBase;
42 import org.apache.struts.taglib.html.Constants;
43
44
45 /**
46  * <p>Unit tests for <code>org.apache.struts.util.RequestUtils</code>.</p>
47  *
48  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
49  */

50
51 public class TestRequestUtils extends TestMockBase {
52
53
54     // ----------------------------------------------------------------- Basics
55

56
57     public TestRequestUtils(String JavaDoc name) {
58         super(name);
59     }
60
61
62     public static void main(String JavaDoc args[]) {
63         junit.awtui.TestRunner.main
64             (new String JavaDoc[] { TestRequestUtils.class.getName() } );
65     }
66
67
68     public static Test suite() {
69         return (new TestSuite(TestRequestUtils.class));
70     }
71
72
73     // ----------------------------------------------------- Instance Variables
74

75
76
77     // ----------------------------------------------------- Setup and Teardown
78

79
80     public void setUp() {
81
82         super.setUp();
83
84     }
85
86
87     public void tearDown() {
88
89         super.tearDown();
90
91     }
92
93
94     // ------------------------------------------------------- Individual Tests
95

96
97     // ---------------------------------------------------------- absoluteURL()
98

99
100     public void testAbsoluteURL() {
101
102         request.setPathElements("/myapp", "/action.do", null, null);
103         String JavaDoc url = null;
104         try {
105             url = RequestUtils.absoluteURL(request, "/foo/bar.jsp").toString();
106             assertEquals("absoluteURL is correct",
107                          "http://localhost:8080/myapp/foo/bar.jsp",
108                          url);
109         } catch (MalformedURLException JavaDoc e) {
110             fail("Threw MalformedURLException: " + e);
111         }
112
113     }
114
115
116     // ------------------------------------------------------------ actionURL()
117

118
119     // Default application -- extension mapping
120
public void testActionURL1() {
121
122         request.setAttribute(Globals.MODULE_KEY, moduleConfig);
123         request.setPathElements("/myapp", "/foo.do", null, null);
124         String JavaDoc url = RequestUtils.actionURL
125             (request, moduleConfig.findActionConfig("/dynamic"), "*.do");
126         assertNotNull("URL was returned", url);
127         assertEquals("URL value",
128                      "/dynamic.do",
129                      url);
130
131     }
132
133
134     // Second application -- extension mapping
135
public void testActionURL2() {
136
137         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
138         request.setPathElements("/myapp", "/2/foo.do", null, null);
139         String JavaDoc url = RequestUtils.actionURL
140             (request, moduleConfig2.findActionConfig("/dynamic2"), "*.do");
141         assertNotNull("URL was returned", url);
142         assertEquals("URL value",
143                      "/2/dynamic2.do",
144                      url);
145
146     }
147
148
149     // Default application -- path mapping
150
public void testActionURL3() {
151
152         request.setAttribute(Globals.MODULE_KEY, moduleConfig);
153         request.setPathElements("/myapp", "/do/foo", null, null);
154         String JavaDoc url = RequestUtils.actionURL
155             (request, moduleConfig.findActionConfig("/dynamic"), "/do/*");
156         assertNotNull("URL was returned", url);
157         assertEquals("URL value",
158                      "/do/dynamic",
159                      url);
160
161     }
162
163
164     // ---------------------------------------------------- computeParameters()
165

166
167     // No parameters and no transaction token
168
public void testComputeParameters0a() {
169
170         Map JavaDoc map = null;
171         try {
172             map = RequestUtils.computeParameters(page,
173                                                  null, null, null, null,
174                                                  null, null, null, false);
175         } catch (JspException JavaDoc e) {
176             fail("JspException: " + e);
177         }
178         assertNull("Map is null", map);
179
180     }
181
182
183     // No parameters but add transaction token
184
public void testComputeParameters0b() {
185
186         session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
187         Map JavaDoc map = null;
188         try {
189             map = RequestUtils.computeParameters
190                 (page, null, null, null, null,
191                  null, null, null, true);
192         } catch (JspException JavaDoc e) {
193             fail("JspException: " + e);
194         }
195         assertNotNull("Map is not null", map);
196         assertEquals("One parameter in the returned map",
197                      1, map.size());
198         assertTrue("Transaction token parameter present",
199                    map.containsKey(Constants.TOKEN_KEY));
200         assertEquals("Transaction token parameter value",
201                      "token",
202                      (String JavaDoc) map.get(Constants.TOKEN_KEY));
203
204     }
205
206
207     // Single parameter -- name
208
public void testComputeParameters1a() {
209
210         session.setAttribute("attr", "bar");
211         Map JavaDoc map = null;
212         try {
213             map = RequestUtils.computeParameters
214                 (page, "foo", "attr", null, null,
215                  null, null, null, false);
216         } catch (JspException JavaDoc e) {
217             fail("JspException: " + e);
218         }
219         assertNotNull("Map is not null", map);
220         assertEquals("One parameter in the returned map",
221                      1, map.size());
222         assertTrue("Parameter present",
223                    map.containsKey("foo"));
224         assertEquals("Parameter value",
225                      "bar",
226                      (String JavaDoc) map.get("foo"));
227
228     }
229
230
231     // Single parameter -- scope + name
232
public void testComputeParameters1b() {
233
234         request.setAttribute("attr", "bar");
235         Map JavaDoc map = null;
236         try {
237             map = RequestUtils.computeParameters
238                 (page, "foo", "attr", null, "request",
239                  null, null, null, false);
240         } catch (JspException JavaDoc e) {
241             fail("JspException: " + e);
242         }
243         assertNotNull("Map is not null", map);
244         assertEquals("One parameter in the returned map",
245                      1, map.size());
246         assertTrue("Parameter present",
247                    map.containsKey("foo"));
248         assertEquals("Parameter value",
249                      "bar",
250                      (String JavaDoc) map.get("foo"));
251
252     }
253
254
255     // Single parameter -- scope + name + property
256
public void testComputeParameters1c() {
257
258         request.setAttribute("attr", new MockFormBean("bar"));
259
260         Map JavaDoc map = null;
261         try {
262             map = RequestUtils.computeParameters
263                 (page, "foo", "attr", "stringProperty", "request",
264                  null, null, null, false);
265         } catch (JspException JavaDoc e) {
266             fail("JspException: " + e);
267         }
268         assertNotNull("Map is not null", map);
269         assertEquals("One parameter in the returned map",
270                      1, map.size());
271         assertTrue("Parameter present",
272                    map.containsKey("foo"));
273         assertEquals("Parameter value",
274                      "bar",
275                      (String JavaDoc) map.get("foo"));
276
277     }
278
279
280     // Provided map -- name
281
public void testComputeParameters2a() {
282
283         Map JavaDoc map = new HashMap JavaDoc();
284         map.put("foo1", "bar1");
285         map.put("foo2", "bar2");
286         session.setAttribute("attr", map);
287
288         try {
289             map = RequestUtils.computeParameters
290                 (page, null, null, null, null,
291                  "attr", null, null, false);
292         } catch (JspException JavaDoc e) {
293             fail("JspException: " + e);
294         }
295         assertNotNull("Map is not null", map);
296         assertEquals("Two parameter in the returned map",
297                      2, map.size());
298         assertTrue("Parameter foo1 present",
299                    map.containsKey("foo1"));
300         assertEquals("Parameter foo1 value",
301                      "bar1",
302                      (String JavaDoc) map.get("foo1"));
303         assertTrue("Parameter foo2 present",
304                    map.containsKey("foo2"));
305         assertEquals("Parameter foo2 value",
306                      "bar2",
307                      (String JavaDoc) map.get("foo2"));
308
309     }
310
311
312     // Provided map -- scope + name
313
public void testComputeParameters2b() {
314
315         Map JavaDoc map = new HashMap JavaDoc();
316         map.put("foo1", "bar1");
317         map.put("foo2", "bar2");
318         request.setAttribute("attr", map);
319
320         try {
321             map = RequestUtils.computeParameters
322                 (page, null, null, null, null,
323                  "attr", null, "request", false);
324         } catch (JspException JavaDoc e) {
325             fail("JspException: " + e);
326         }
327         assertNotNull("Map is not null", map);
328         assertEquals("Two parameter in the returned map",
329                      2, map.size());
330         assertTrue("Parameter foo1 present",
331                    map.containsKey("foo1"));
332         assertEquals("Parameter foo1 value",
333                      "bar1",
334                      (String JavaDoc) map.get("foo1"));
335         assertTrue("Parameter foo2 present",
336                    map.containsKey("foo2"));
337         assertEquals("Parameter foo2 value",
338                      "bar2",
339                      (String JavaDoc) map.get("foo2"));
340
341     }
342
343
344     // Provided map -- scope + name + property
345
public void testComputeParameters2c() {
346
347         request.setAttribute("attr", new MockFormBean());
348
349         Map JavaDoc map = null;
350         try {
351             map = RequestUtils.computeParameters
352                 (page, null, null, null, null,
353                  "attr", "mapProperty", "request", false);
354         } catch (JspException JavaDoc e) {
355             fail("JspException: " + e);
356         }
357         assertNotNull("Map is not null", map);
358         assertEquals("Two parameter in the returned map",
359                      2, map.size());
360         assertTrue("Parameter foo1 present",
361                    map.containsKey("foo1"));
362         assertEquals("Parameter foo1 value",
363                      "bar1",
364                      (String JavaDoc) map.get("foo1"));
365         assertTrue("Parameter foo2 present",
366                    map.containsKey("foo2"));
367         assertEquals("Parameter foo2 value",
368                      "bar2",
369                      (String JavaDoc) map.get("foo2"));
370
371     }
372
373
374     // Provided map -- name with one key and two values
375
public void testComputeParameters2d() {
376
377         Map JavaDoc map = new HashMap JavaDoc();
378         map.put("foo", new String JavaDoc[] { "bar1", "bar2" });
379         session.setAttribute("attr", map);
380
381         try {
382             map = RequestUtils.computeParameters
383                 (page, null, null, null, null,
384                  "attr", null, null, false);
385         } catch (JspException JavaDoc e) {
386             fail("JspException: " + e);
387         }
388         assertNotNull("Map is not null", map);
389         assertEquals("One parameter in the returned map",
390                      1, map.size());
391         assertTrue("Parameter foo present",
392                    map.containsKey("foo"));
393         assertTrue("Parameter foo value type",
394                    map.get("foo") instanceof String JavaDoc[]);
395         String JavaDoc values[] = (String JavaDoc[]) map.get("foo");
396         assertEquals("Values count",
397                      2, values.length);
398
399     }
400
401
402     // Kitchen sink combination of parameters with a merge
403
public void testComputeParameters3a() {
404
405         request.setAttribute("attr", new MockFormBean("bar3"));
406         session.setAttribute(Globals.TRANSACTION_TOKEN_KEY, "token");
407
408         Map JavaDoc map = null;
409         try {
410             map = RequestUtils.computeParameters
411                 (page, "foo1", "attr", "stringProperty", "request",
412                  "attr", "mapProperty", "request", true);
413         } catch (JspException JavaDoc e) {
414             fail("JspException: " + e);
415         }
416         assertNotNull("Map is not null", map);
417         assertEquals("Three parameter in the returned map",
418                      3, map.size());
419
420         assertTrue("Parameter foo1 present",
421                    map.containsKey("foo1"));
422         assertTrue("Parameter foo1 value type",
423                    map.get("foo1") instanceof String JavaDoc[]);
424         String JavaDoc values[] = (String JavaDoc[]) map.get("foo1");
425         assertEquals("Values count",
426                      2, values.length);
427
428         assertTrue("Parameter foo2 present",
429                    map.containsKey("foo2"));
430         assertEquals("Parameter foo2 value",
431                      "bar2",
432                      (String JavaDoc) map.get("foo2"));
433
434         assertTrue("Transaction token parameter present",
435                    map.containsKey(Constants.TOKEN_KEY));
436         assertEquals("Transaction token parameter value",
437                      "token",
438                      (String JavaDoc) map.get(Constants.TOKEN_KEY));
439     }
440
441
442     // ----------------------------------------------------------- computeURL()
443

444
445     // Default module -- Forward only
446
public void testComputeURL1a() {
447
448         request.setPathElements("/myapp", "/action.do", null, null);
449         String JavaDoc url = null;
450         try {
451             url = RequestUtils.computeURL
452                 (page, "foo",
453                  null, null,
454                  null, null, false);
455         } catch (MalformedURLException JavaDoc e) {
456             fail("MalformedURLException: " + e);
457         }
458         assertNotNull("url present", url);
459         assertEquals("url value",
460                      "/myapp/bar.jsp",
461                      url);
462
463     }
464
465
466     // Default module -- Href only
467
public void testComputeURL1b() {
468
469         request.setPathElements("/myapp", "/action.do", null, null);
470         String JavaDoc url = null;
471         try {
472             url = RequestUtils.computeURL
473                 (page, null,
474                  "http://foo.com/bar", null,
475                  null, null, false);
476         } catch (MalformedURLException JavaDoc e) {
477             fail("MalformedURLException: " + e);
478         }
479         assertNotNull("url present", url);
480         assertEquals("url value",
481                      "http://foo.com/bar",
482                      url);
483
484     }
485
486
487     // Default module -- Page only
488
public void testComputeURL1c() {
489
490         request.setPathElements("/myapp", "/action.do", null, null);
491         String JavaDoc url = null;
492         try {
493             url = RequestUtils.computeURL
494                 (page, null,
495                  null, "/bar",
496                  null, null, false);
497         } catch (MalformedURLException JavaDoc e) {
498             fail("MalformedURLException: " + e);
499         }
500         assertNotNull("url present", url);
501         assertEquals("url value",
502                      "/myapp/bar",
503                      url);
504
505     }
506
507
508     // Default module -- Forward with pattern
509
public void testComputeURL1d() {
510
511         moduleConfig.getControllerConfig().setForwardPattern
512             ("$C/WEB-INF/pages$M$P");
513         request.setPathElements("/myapp", "/action.do", null, null);
514         String JavaDoc url = null;
515         try {
516             url = RequestUtils.computeURL
517                 (page, "foo",
518                  null, null,
519                  null, null, false);
520         } catch (MalformedURLException JavaDoc e) {
521             fail("MalformedURLException: " + e);
522         }
523         assertNotNull("url present", url);
524         assertEquals("url value",
525                      "/myapp/WEB-INF/pages/bar.jsp",
526                      url);
527
528     }
529
530
531     // Default module -- Page with pattern
532
public void testComputeURL1e() {
533
534         moduleConfig.getControllerConfig().setPagePattern
535             ("$C/WEB-INF/pages$M$P");
536         request.setPathElements("/myapp", "/action.do", null, null);
537         String JavaDoc url = null;
538         try {
539             url = RequestUtils.computeURL
540                 (page, null,
541                  null, "/bar",
542                  null, null, false);
543         } catch (MalformedURLException JavaDoc e) {
544             fail("MalformedURLException: " + e);
545         }
546         assertNotNull("url present", url);
547         assertEquals("url value",
548                      "/myapp/WEB-INF/pages/bar",
549                      url);
550
551     }
552
553
554     // Default module -- Forward with relative path (non-context-relative)
555
public void testComputeURL1f() {
556
557         request.setPathElements("/myapp", "/action.do", null, null);
558         String JavaDoc url = null;
559         try {
560             url = RequestUtils.computeURL
561                 (page, "relative1",
562                  null, null,
563                  null, null, false);
564         } catch (MalformedURLException JavaDoc e) {
565             fail("MalformedURLException: " + e);
566         }
567         assertNotNull("url present", url);
568         assertEquals("url value",
569                      // "/myapp/relative.jsp",
570
"relative.jsp",
571                      url);
572     }
573
574
575     // Default module -- Forward with relative path (context-relative)
576
public void testComputeURL1g() {
577
578         request.setPathElements("/myapp", "/action.do", null, null);
579         String JavaDoc url = null;
580         try {
581             url = RequestUtils.computeURL
582                 (page, "relative2",
583                  null, null,
584                  null, null, false);
585         } catch (MalformedURLException JavaDoc e) {
586             fail("MalformedURLException: " + e);
587         }
588         assertNotNull("url present", url);
589         assertEquals("url value",
590                      // "/myapp/relative.jsp",
591
"relative.jsp",
592                      url);
593     }
594
595
596     // Default module -- Forward with external path
597
public void testComputeURL1h() {
598
599         request.setPathElements("/myapp", "/action.do", null, null);
600         String JavaDoc url = null;
601         try {
602             url = RequestUtils.computeURL
603                 (page, "external",
604                  null, null,
605                  null, null, false);
606         } catch (MalformedURLException JavaDoc e) {
607             fail("MalformedURLException: " + e);
608         }
609         assertNotNull("url present", url);
610         assertEquals("url value",
611                      "http://jakarta.apache.org/",
612                      url);
613     }
614
615
616     // Second module -- Forward only
617
public void testComputeURL2a() {
618
619         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
620         request.setPathElements("/myapp", "/2/action.do", null, null);
621         String JavaDoc url = null;
622         try {
623             url = RequestUtils.computeURL
624                 (page, "foo",
625                  null, null,
626                  null, null, false);
627         } catch (MalformedURLException JavaDoc e) {
628             fail("MalformedURLException: " + e);
629         }
630         assertNotNull("url present", url);
631         assertEquals("url value",
632                      "/myapp/2/baz.jsp",
633                      url);
634
635     }
636
637
638     // Second module -- Href only
639
public void testComputeURL2b() {
640
641         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
642         request.setPathElements("/myapp", "/2/action.do", null, null);
643         String JavaDoc url = null;
644         try {
645             url = RequestUtils.computeURL
646                 (page, null,
647                  "http://foo.com/bar", null,
648                  null, null, false);
649         } catch (MalformedURLException JavaDoc e) {
650             fail("MalformedURLException: " + e);
651         }
652         assertNotNull("url present", url);
653         assertEquals("url value",
654                      "http://foo.com/bar",
655                      url);
656
657     }
658
659
660     // Second module -- Page only
661
public void testComputeURL2c() {
662
663         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
664         request.setPathElements("/myapp", "/2/action.do", null, null);
665         String JavaDoc url = null;
666         try {
667             url = RequestUtils.computeURL
668                 (page, null,
669                  null, "/bar",
670                  null, null, false);
671         } catch (MalformedURLException JavaDoc e) {
672             fail("MalformedURLException: " + e);
673         }
674         assertNotNull("url present", url);
675         assertEquals("url value",
676                      "/myapp/2/bar",
677                      url);
678
679     }
680
681
682     // Default module -- Forward with pattern
683
public void testComputeURL2d() {
684
685         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
686         moduleConfig2.getControllerConfig().setForwardPattern
687             ("$C/WEB-INF/pages$M$P");
688         request.setPathElements("/myapp", "/2/action.do", null, null);
689         String JavaDoc url = null;
690         try {
691             url = RequestUtils.computeURL
692                 (page, "foo",
693                  null, null,
694                  null, null, false);
695         } catch (MalformedURLException JavaDoc e) {
696             fail("MalformedURLException: " + e);
697         }
698         assertNotNull("url present", url);
699         assertEquals("url value",
700                      "/myapp/WEB-INF/pages/2/baz.jsp",
701                      url);
702
703     }
704
705
706     // Second module -- Page with pattern
707
public void testComputeURL2e() {
708
709         moduleConfig2.getControllerConfig().setPagePattern
710             ("$C/WEB-INF/pages$M$P");
711         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
712         request.setPathElements("/myapp", "/2/action.do", null, null);
713         String JavaDoc url = null;
714         try {
715             url = RequestUtils.computeURL
716                 (page, null,
717                  null, "/bar",
718                  null, null, false);
719         } catch (MalformedURLException JavaDoc e) {
720             fail("MalformedURLException: " + e);
721         }
722         assertNotNull("url present", url);
723         assertEquals("url value",
724                      "/myapp/WEB-INF/pages/2/bar",
725                      url);
726
727     }
728
729
730     // Second module -- Forward with relative path (non-context-relative)
731
public void testComputeURL2f() {
732
733         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
734         request.setPathElements("/myapp", "/2/action.do", null, null);
735         String JavaDoc url = null;
736         try {
737             url = RequestUtils.computeURL
738                 (page, "relative1",
739                  null, null,
740                  null, null, false);
741         } catch (MalformedURLException JavaDoc e) {
742             fail("MalformedURLException: " + e);
743         }
744         assertNotNull("url present", url);
745         assertEquals("url value",
746                      // "/myapp/2/relative.jsp",
747
"relative.jsp",
748                      url);
749     }
750
751
752     // Second module -- Forward with relative path (context-relative)
753
public void testComputeURL2g() {
754
755         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
756         request.setPathElements("/myapp", "/2/action.do", null, null);
757         String JavaDoc url = null;
758         try {
759             url = RequestUtils.computeURL
760                 (page, "relative2",
761                  null, null,
762                  null, null, false);
763         } catch (MalformedURLException JavaDoc e) {
764             fail("MalformedURLException: " + e);
765         }
766         assertNotNull("url present", url);
767         assertEquals("url value",
768                      // "/myapp/relative.jsp",
769
"relative.jsp",
770                      url);
771     }
772
773
774     // Second module -- Forward with external path
775
public void testComputeURL2h() {
776
777         request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
778         request.setPathElements("/myapp", "/2/action.do", null, null);
779         String JavaDoc url = null;
780         try {
781             url = RequestUtils.computeURL
782                 (page, "external",
783                  null, null,
784                  null, null, false);
785         } catch (MalformedURLException JavaDoc e) {
786             fail("MalformedURLException: " + e);
787         }
788         assertNotNull("url present", url);
789         assertEquals("url value",
790                      "http://jakarta.apache.org/",
791                      url);
792     }
793
794
795     // Add parameters only -- forward URL
796
public void testComputeURL3a() {
797