Source Code Cross Referenced for UrlHelperTest.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » views » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » webwork 2.2.6 » com.opensymphony.webwork.views.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2005 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.webwork.views.util;
006:
007:        import java.util.Arrays;
008:        import java.util.HashMap;
009:        import java.util.LinkedHashMap;
010:        import java.util.Map;
011:        import java.util.TreeMap;
012:
013:        import javax.servlet.http.HttpServletRequest;
014:        import javax.servlet.http.HttpServletResponse;
015:
016:        import com.mockobjects.dynamic.Mock;
017:        import com.opensymphony.webwork.WebWorkConstants;
018:        import com.opensymphony.webwork.WebWorkTestCase;
019:        import com.opensymphony.webwork.config.Configuration;
020:
021:        /**
022:         * Test case for UrlHelper.
023:         * 
024:         * @author Matt Ho <a href="mailto:matt@enginegreen.com">&lt;matt@enginegreen.com&gt;</a>
025:         * @author tm_jee
026:         * @version $Date: 2007-02-03 07:53:28 +0100 (Sat, 03 Feb 2007) $ $Id: UrlHelperTest.java 2827 2007-02-03 06:53:28Z tschneider22 $
027:         */
028:        public class UrlHelperTest extends WebWorkTestCase {
029:
030:            public void testForceAddSchemeHostAndPort() throws Exception {
031:                String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
032:
033:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
034:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
035:                mockHttpServletRequest.expectAndReturn("getServerName",
036:                        "localhost");
037:                mockHttpServletRequest.expectAndReturn("getContextPath",
038:                        "/contextPath");
039:
040:                Mock mockHttpServletResponse = new Mock(
041:                        HttpServletResponse.class);
042:                mockHttpServletResponse.expectAndReturn("encodeURL",
043:                        expectedUrl, expectedUrl);
044:
045:                String result = UrlHelper.buildUrl(
046:                        "/path1/path2/myAction.action",
047:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
048:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
049:                        null, "http", true, true, true);
050:                assertEquals(expectedUrl, result);
051:                mockHttpServletRequest.verify();
052:            }
053:
054:            public void testForceAddNullSchemeHostAndPort() throws Exception {
055:                String expectedUrl = "http://localhost/contextPath/path1/path2/myAction.action";
056:
057:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
058:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
059:                mockHttpServletRequest.expectAndReturn("getServerName",
060:                        "localhost");
061:                mockHttpServletRequest.expectAndReturn("getContextPath",
062:                        "/contextPath");
063:
064:                Mock mockHttpServletResponse = new Mock(
065:                        HttpServletResponse.class);
066:                mockHttpServletResponse.expectAndReturn("encodeURL",
067:                        expectedUrl, expectedUrl);
068:
069:                String result = UrlHelper.buildUrl(
070:                        "/path1/path2/myAction.action",
071:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
072:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
073:                        null, null, true, true, true);
074:                assertEquals(expectedUrl, result);
075:                mockHttpServletRequest.verify();
076:            }
077:
078:            public void testDoNotForceAddSchemeHostAndPort() throws Exception {
079:                String expectedUrl = "/contextPath/path1/path2/myAction.action";
080:
081:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
082:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
083:                mockHttpServletRequest.expectAndReturn("getServerName",
084:                        "localhost");
085:                mockHttpServletRequest.expectAndReturn("getContextPath",
086:                        "/contextPath");
087:
088:                Mock mockHttpServletResponse = new Mock(
089:                        HttpServletResponse.class);
090:                mockHttpServletResponse.expectAndReturn("encodeURL",
091:                        expectedUrl, expectedUrl);
092:
093:                String result = UrlHelper.buildUrl(
094:                        "/path1/path2/myAction.action",
095:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
096:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
097:                        null, "http", true, true, false);
098:
099:                assertEquals(expectedUrl, result);
100:            }
101:
102:            public void testBuildParametersStringWithUrlHavingSomeExistingParameters()
103:                    throws Exception {
104:                String expectedUrl = "http://localhost:8080/myContext/myPage.jsp?initParam=initValue&amp;param1=value1&amp;param2=value2";
105:
106:                Map parameters = new LinkedHashMap();
107:                parameters.put("param1", "value1");
108:                parameters.put("param2", "value2");
109:
110:                StringBuffer url = new StringBuffer(
111:                        "http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
112:
113:                UrlHelper.buildParametersString(parameters, url);
114:
115:                assertEquals(expectedUrl, url.toString());
116:            }
117:
118:            public void testBuildWithRootContext() {
119:                String expectedUrl = "/MyAction.action";
120:
121:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
122:                mockHttpServletRequest.expectAndReturn("getContextPath", "/");
123:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
124:
125:                Mock mockHttpServletResponse = new Mock(
126:                        HttpServletResponse.class);
127:                mockHttpServletResponse.expectAndReturn("encodeURL",
128:                        expectedUrl, expectedUrl);
129:
130:                String actualUrl = UrlHelper.buildUrl(expectedUrl,
131:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
132:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
133:                        new HashMap());
134:                assertEquals(expectedUrl, actualUrl);
135:            }
136:
137:            /**
138:             * just one &, not &amp;
139:             */
140:            public void testBuildUrlCorrectlyAddsAmp() {
141:                String expectedString = "my.actionName?foo=bar&amp;hello=world";
142:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
143:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
144:                Mock mockHttpServletResponse = new Mock(
145:                        HttpServletResponse.class);
146:                mockHttpServletResponse.expectAndReturn("encodeURL",
147:                        expectedString, expectedString);
148:
149:                String actionName = "my.actionName";
150:                TreeMap params = new TreeMap();
151:                params.put("hello", "world");
152:                params.put("foo", "bar");
153:
154:                String urlString = UrlHelper.buildUrl(actionName,
155:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
156:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
157:                        params);
158:                assertEquals(expectedString, urlString);
159:            }
160:
161:            public void testBuildUrlWithStringArray() {
162:                String expectedString = "my.actionName?foo=bar&amp;hello=earth&amp;hello=mars";
163:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
164:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
165:                Mock mockHttpServletResponse = new Mock(
166:                        HttpServletResponse.class);
167:                mockHttpServletResponse.expectAndReturn("encodeURL",
168:                        expectedString, expectedString);
169:
170:                String actionName = "my.actionName";
171:                TreeMap params = new TreeMap();
172:                params.put("hello", new String[] { "earth", "mars" });
173:                params.put("foo", "bar");
174:
175:                String urlString = UrlHelper.buildUrl(actionName,
176:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
177:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
178:                        params);
179:                assertEquals(expectedString, urlString);
180:            }
181:
182:            /**
183:             * The UrlHelper should build a URL that starts with "https" followed by the server name when the scheme of the
184:             * current request is "http" and the port for the "https" scheme is 443.
185:             */
186:            public void testSwitchToHttpsScheme() {
187:                String expectedString = "https://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
188:
189:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
190:                mockHttpServletRequest.expectAndReturn("getServerName",
191:                        "www.mydomain.com");
192:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
193:                mockHttpServletRequest.expectAndReturn("getServerPort", 80);
194:                mockHttpServletRequest.expectAndReturn("getContextPath",
195:                        "/mywebapp");
196:
197:                Mock mockHttpServletResponse = new Mock(
198:                        HttpServletResponse.class);
199:                mockHttpServletResponse.expectAndReturn("encodeURL",
200:                        expectedString, expectedString);
201:
202:                String actionName = "/MyAction.action";
203:                TreeMap params = new TreeMap();
204:                params.put("hello", new String[] { "earth", "mars" });
205:                params.put("foo", "bar");
206:
207:                String urlString = UrlHelper.buildUrl(actionName,
208:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
209:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
210:                        params, "https", true, true);
211:                assertEquals(expectedString, urlString);
212:            }
213:
214:            /**
215:             * The UrlHelper should build a URL that starts with "http" followed by the server name when the scheme of the
216:             * current request is "https" and the port for the "http" scheme is 80.
217:             */
218:            public void testSwitchToHttpScheme() {
219:                String expectedString = "http://www.mydomain.com/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
220:
221:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
222:                mockHttpServletRequest.expectAndReturn("getServerName",
223:                        "www.mydomain.com");
224:                mockHttpServletRequest.expectAndReturn("getScheme", "https");
225:                mockHttpServletRequest.expectAndReturn("getServerPort", 443);
226:                mockHttpServletRequest.expectAndReturn("getContextPath",
227:                        "/mywebapp");
228:
229:                Mock mockHttpServletResponse = new Mock(
230:                        HttpServletResponse.class);
231:                mockHttpServletResponse.expectAndReturn("encodeURL",
232:                        expectedString, expectedString);
233:
234:                String actionName = "/MyAction.action";
235:                TreeMap params = new TreeMap();
236:                params.put("hello", new String[] { "earth", "mars" });
237:                params.put("foo", "bar");
238:
239:                String urlString = UrlHelper.buildUrl(actionName,
240:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
241:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
242:                        params, "http", true, true);
243:                assertEquals(expectedString, urlString);
244:            }
245:
246:            /**
247:             * This test is similar to {@link #testSwitchToHttpsScheme()} with the HTTP port equal to 7001 and the HTTPS port
248:             * equal to 7002.
249:             */
250:            public void testSwitchToHttpsNonDefaultPort() {
251:
252:                String expectedString = "https://www.mydomain.com:7002/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
253:
254:                Configuration.set(WebWorkConstants.WEBWORK_URL_HTTP_PORT,
255:                        "7001");
256:                Configuration.set(WebWorkConstants.WEBWORK_URL_HTTPS_PORT,
257:                        "7002");
258:
259:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
260:                mockHttpServletRequest.expectAndReturn("getServerName",
261:                        "www.mydomain.com");
262:                mockHttpServletRequest.expectAndReturn("getScheme", "http");
263:                mockHttpServletRequest.expectAndReturn("getServerPort", 7001);
264:                mockHttpServletRequest.expectAndReturn("getContextPath",
265:                        "/mywebapp");
266:
267:                Mock mockHttpServletResponse = new Mock(
268:                        HttpServletResponse.class);
269:                mockHttpServletResponse.expectAndReturn("encodeURL",
270:                        expectedString, expectedString);
271:
272:                String actionName = "/MyAction.action";
273:                TreeMap params = new TreeMap();
274:                params.put("hello", new String[] { "earth", "mars" });
275:                params.put("foo", "bar");
276:
277:                String urlString = UrlHelper.buildUrl(actionName,
278:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
279:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
280:                        params, "https", true, true);
281:                assertEquals(expectedString, urlString);
282:            }
283:
284:            /**
285:             * This test is similar to {@link #testSwitchToHttpScheme()} with the HTTP port equal to 7001 and the HTTPS port
286:             * equal to port 7002.
287:             */
288:            public void testSwitchToHttpNonDefaultPort() {
289:
290:                String expectedString = "http://www.mydomain.com:7001/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
291:
292:                Configuration.set(WebWorkConstants.WEBWORK_URL_HTTP_PORT,
293:                        "7001");
294:                Configuration.set(WebWorkConstants.WEBWORK_URL_HTTPS_PORT,
295:                        "7002");
296:
297:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
298:                mockHttpServletRequest.expectAndReturn("getServerName",
299:                        "www.mydomain.com");
300:                mockHttpServletRequest.expectAndReturn("getScheme", "https");
301:                mockHttpServletRequest.expectAndReturn("getServerPort", 7002);
302:                mockHttpServletRequest.expectAndReturn("getContextPath",
303:                        "/mywebapp");
304:
305:                Mock mockHttpServletResponse = new Mock(
306:                        HttpServletResponse.class);
307:                mockHttpServletResponse.expectAndReturn("encodeURL",
308:                        expectedString, expectedString);
309:
310:                String actionName = "/MyAction.action";
311:                TreeMap params = new TreeMap();
312:                params.put("hello", new String[] { "earth", "mars" });
313:                params.put("foo", "bar");
314:
315:                String urlString = UrlHelper.buildUrl(actionName,
316:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
317:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
318:                        params, "http", true, true);
319:                assertEquals(expectedString, urlString);
320:            }
321:
322:            /**
323:             * A check to verify that the scheme, server, and port number are omitted when the scheme of the current request
324:             * matches the scheme supplied when building the URL.
325:             */
326:            public void testBuildWithSameScheme() {
327:                String expectedString = "/mywebapp/MyAction.action?foo=bar&amp;hello=earth&amp;hello=mars";
328:
329:                Mock mockHttpServletRequest = new Mock(HttpServletRequest.class);
330:                mockHttpServletRequest.expectAndReturn("getServerName",
331:                        "www.mydomain.com");
332:                mockHttpServletRequest.expectAndReturn("getScheme", "https");
333:                mockHttpServletRequest.expectAndReturn("getServerPort", 443);
334:                mockHttpServletRequest.expectAndReturn("getContextPath",
335:                        "/mywebapp");
336:
337:                Mock mockHttpServletResponse = new Mock(
338:                        HttpServletResponse.class);
339:                mockHttpServletResponse.expectAndReturn("encodeURL",
340:                        expectedString, expectedString);
341:
342:                String actionName = "/MyAction.action";
343:                TreeMap params = new TreeMap();
344:                params.put("hello", new String[] { "earth", "mars" });
345:                params.put("foo", "bar");
346:
347:                String urlString = UrlHelper.buildUrl(actionName,
348:                        (HttpServletRequest) mockHttpServletRequest.proxy(),
349:                        (HttpServletResponse) mockHttpServletResponse.proxy(),
350:                        params, "https", true, true);
351:                assertEquals(expectedString, urlString);
352:            }
353:
354:            public void testParseQuery() throws Exception {
355:                Map result = UrlHelper
356:                        .parseQueryString("aaa=aaaval&bbb=bbbval&ccc=");
357:
358:                assertEquals(result.get("aaa"), "aaaval");
359:                assertEquals(result.get("bbb"), "bbbval");
360:                assertEquals(result.get("ccc"), "");
361:            }
362:
363:            public void testParseEmptyQuery() throws Exception {
364:                Map result = UrlHelper.parseQueryString("");
365:
366:                assertNotNull(result);
367:                assertEquals(result.size(), 0);
368:            }
369:
370:            public void testParseNullQuery() throws Exception {
371:                Map result = UrlHelper.parseQueryString(null);
372:
373:                assertNotNull(result);
374:                assertEquals(result.size(), 0);
375:            }
376:
377:            public void testParseMultiQuery() throws Exception {
378:                Map result = UrlHelper
379:                        .parseQueryString("param=1&param=1&param=1");
380:
381:                assertNotNull(result);
382:                assertEquals(result.size(), 1);
383:                String values[] = (String[]) result.get("param");
384:                Arrays.sort(values);
385:                assertEquals(values.length, 3);
386:                assertEquals(values[0], "1");
387:                assertEquals(values[1], "1");
388:                assertEquals(values[2], "1");
389:            }
390:
391:            public void testParseDuplicateQuery() throws Exception {
392:                Map result = UrlHelper
393:                        .parseQueryString("param=1&param=2&param=3");
394:
395:                assertNotNull(result);
396:                assertEquals(result.size(), 1);
397:                String values[] = (String[]) result.get("param");
398:                Arrays.sort(values);
399:                assertEquals(values.length, 3);
400:                assertEquals(values[0], "1");
401:                assertEquals(values[1], "2");
402:                assertEquals(values[2], "3");
403:            }
404:
405:            public void testTranslateAndEncode() throws Exception {
406:                Object defaultI18nEncoding = Configuration
407:                        .get(WebWorkConstants.WEBWORK_I18N_ENCODING);
408:                try {
409:                    Configuration.set(WebWorkConstants.WEBWORK_I18N_ENCODING,
410:                            "UTF-8");
411:                    String result = UrlHelper
412:                            .translateAndEncode("\u65b0\u805e");
413:                    String expectedResult = "%E6%96%B0%E8%81%9E";
414:
415:                    assertEquals(result, expectedResult);
416:                } finally {
417:                    Configuration.set(WebWorkConstants.WEBWORK_I18N_ENCODING,
418:                            defaultI18nEncoding);
419:                }
420:            }
421:
422:            public void testTranslateAndDecode() throws Exception {
423:                Object defaultI18nEncoding = Configuration
424:                        .get(WebWorkConstants.WEBWORK_I18N_ENCODING);
425:                try {
426:                    Configuration.set(WebWorkConstants.WEBWORK_I18N_ENCODING,
427:                            "UTF-8");
428:                    String result = UrlHelper
429:                            .translateAndDecode("%E6%96%B0%E8%81%9E");
430:                    String expectedResult = "\u65b0\u805e";
431:
432:                    assertEquals(result, expectedResult);
433:                } finally {
434:                    Configuration.set(WebWorkConstants.WEBWORK_I18N_ENCODING,
435:                            defaultI18nEncoding);
436:                }
437:            }
438:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.