KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > util > UrlHelperTest


1 /*
2  * Copyright (c) 2002-2005 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.util;
6
7 import com.mockobjects.dynamic.Mock;
8 import com.opensymphony.webwork.config.Configuration;
9 import com.opensymphony.webwork.WebWorkTestCase;
10
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
13 import java.util.TreeMap JavaDoc;
14
15
16 /**
17  * @author Matt Ho <a HREF="mailto:matt@enginegreen.com">&lt;matt@enginegreen.com&gt;</a>
18  * @version $Id: UrlHelperTest.java,v 1.11 2005/08/07 16:17:27 plightbo Exp $
19  */

20 public class UrlHelperTest extends WebWorkTestCase {
21     /**
22      * just one &, not &amp;
23      */

24     public void testBuildUrlCorrectlyAddsAmp() {
25         String JavaDoc expectedString = "my.actionName?foo=bar&amp;hello=world";
26         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
27         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
28         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
29
30         String JavaDoc actionName = "my.actionName";
31         TreeMap JavaDoc params = new TreeMap JavaDoc();
32         params.put("hello", "world");
33         params.put("foo", "bar");
34
35         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params);
36         assertEquals(expectedString, urlString);
37     }
38
39     public void testBuildUrlWithStringArray() {
40         String JavaDoc expectedString = "my.actionName?foo=bar&amp;hello=earth&amp;hello=mars";
41         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
42         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
43         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
44
45         String JavaDoc actionName = "my.actionName";
46         TreeMap JavaDoc params = new TreeMap JavaDoc();
47         params.put("hello", new String JavaDoc[]{"earth", "mars"});
48         params.put("foo", "bar");
49
50         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params);
51         assertEquals(expectedString, urlString);
52     }
53
54     /**
55      * The UrlHelper should build a URL that starts with "https" followed by the server name
56      * when the scheme of the current request is "http" and the port for the "https" scheme is 443.
57      */

58     public void testSwitchToHttpsScheme() {
59         String JavaDoc expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
60
61         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
62         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
63         mockHttpServletRequest.expectAndReturn("getScheme", "http");
64         mockHttpServletRequest.expectAndReturn("getServerPort", 80);
65         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
66
67         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
68         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
69
70         String JavaDoc actionName = "/MyAction.action";
71         TreeMap JavaDoc params = new TreeMap JavaDoc();
72         params.put("hello", new String JavaDoc[]{"earth", "mars"});
73         params.put("foo", "bar");
74
75         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params, "https", true, true);
76         assertEquals(expectedString, urlString);
77     }
78
79     /**
80      * The UrlHelper should build a URL that starts with "http" followed by the server name when
81      * the scheme of the current request is "https" and the port for the "http" scheme is 80.
82      */

83     public void testSwitchToHttpScheme() {
84         String JavaDoc expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
85
86         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
87         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
88         mockHttpServletRequest.expectAndReturn("getScheme", "https");
89         mockHttpServletRequest.expectAndReturn("getServerPort", 443);
90         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
91
92         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
93         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
94
95         String JavaDoc actionName = "/MyAction.action";
96         TreeMap JavaDoc params = new TreeMap JavaDoc();
97         params.put("hello", new String JavaDoc[]{"earth", "mars"});
98         params.put("foo", "bar");
99
100         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params, "http", true, true);
101         assertEquals(expectedString, urlString);
102     }
103
104     /**
105      * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001
106      * and the HTTPS port equal to 7002.
107      */

108     public void testSwitchToHttpsNonDefaultPort() {
109
110         String JavaDoc expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
111
112         Configuration.set("webwork.url.http.port", "7001");
113         Configuration.set("webwork.url.https.port", "7002");
114
115         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
116         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
117         mockHttpServletRequest.expectAndReturn("getScheme", "http");
118         mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
119         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
120
121         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
122         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
123
124         String JavaDoc actionName = "/MyAction.action";
125         TreeMap JavaDoc params = new TreeMap JavaDoc();
126         params.put("hello", new String JavaDoc[]{"earth", "mars"});
127         params.put("foo", "bar");
128
129         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params, "https", true, true);
130         assertEquals(expectedString, urlString);
131     }
132
133     /**
134      * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001
135      * and the HTTPS port equal to port 7002.
136      */

137     public void testSwitchToHttpNonDefaultPort() {
138
139         String JavaDoc expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
140
141         Configuration.set("webwork.url.http.port", "7001");
142         Configuration.set("webwork.url.https.port", "7002");
143
144         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
145         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
146         mockHttpServletRequest.expectAndReturn("getScheme", "https");
147         mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
148         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
149
150         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
151         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
152
153         String JavaDoc actionName = "/MyAction.action";
154         TreeMap JavaDoc params = new TreeMap JavaDoc();
155         params.put("hello", new String JavaDoc[]{"earth", "mars"});
156         params.put("foo", "bar");
157
158         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params, "http", true, true);
159         assertEquals(expectedString, urlString);
160     }
161
162     /**
163      * A check to verify that the scheme, server, and port number are omitted when the
164      * scheme of the current request matches the scheme supplied when building the URL.
165      */

166     public void testBuildWithSameScheme() {
167         String JavaDoc expectedString = "/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
168
169         Mock mockHttpServletRequest = new Mock(HttpServletRequest JavaDoc.class);
170         mockHttpServletRequest.expectAndReturn("getServerName", "www.mydomain.com");
171         mockHttpServletRequest.expectAndReturn("getScheme", "https");
172         mockHttpServletRequest.expectAndReturn("getServerPort", 443);
173         mockHttpServletRequest.expectAndReturn("getContextPath", "/mywebapp");
174
175         Mock mockHttpServletResponse = new Mock(HttpServletResponse JavaDoc.class);
176         mockHttpServletResponse.expectAndReturn("encodeURL", expectedString, expectedString);
177
178         String JavaDoc actionName = "/MyAction.action";
179         TreeMap JavaDoc params = new TreeMap JavaDoc();
180         params.put("hello", new String JavaDoc[]{"earth", "mars"});
181         params.put("foo", "bar");
182
183         String JavaDoc urlString = UrlHelper.buildUrl(actionName, (HttpServletRequest JavaDoc) mockHttpServletRequest.proxy(), (HttpServletResponse JavaDoc) mockHttpServletResponse.proxy(), params, "https", true, true);
184         assertEquals(expectedString, urlString);
185     }
186 }
Popular Tags