Source Code Cross Referenced for TestRequestUtils.java in  » Web-Framework » struts-1.3.8 » org » apache » struts » 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 » Web Framework » struts 1.3.8 » org.apache.struts.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: TestRequestUtils.java 471754 2006-11-06 14:55:09Z husted $
003:         *
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *  http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:        package org.apache.struts.util;
022:
023:        import junit.framework.Test;
024:        import junit.framework.TestSuite;
025:
026:        import org.apache.struts.Globals;
027:        import org.apache.struts.action.ActionForm;
028:        import org.apache.struts.action.ActionMapping;
029:        import org.apache.struts.action.DynaActionForm;
030:        import org.apache.struts.action.RequestProcessor;
031:        import org.apache.struts.config.ForwardConfig;
032:        import org.apache.struts.config.ModuleConfig;
033:        import org.apache.struts.mock.MockFormBean;
034:        import org.apache.struts.mock.MockPrincipal;
035:        import org.apache.struts.mock.TestMockBase;
036:
037:        import java.net.MalformedURLException;
038:
039:        /**
040:         * <p>Unit tests for <code>org.apache.struts.util.RequestUtils</code>.</p>
041:         *
042:         * @version $Rev: 471754 $ $Date: 2005-08-14 17:24:39 -0400 (Sun, 14 Aug 2005)
043:         *          $
044:         */
045:        public class TestRequestUtils extends TestMockBase {
046:            // ----------------------------------------------------------------- Basics
047:            public TestRequestUtils(String name) {
048:                super (name);
049:            }
050:
051:            public static void main(String[] args) {
052:                junit.awtui.TestRunner
053:                        .main(new String[] { TestRequestUtils.class.getName() });
054:            }
055:
056:            public static Test suite() {
057:                return (new TestSuite(TestRequestUtils.class));
058:            }
059:
060:            // ----------------------------------------------------- Instance Variables
061:            // ----------------------------------------------------- Setup and Teardown
062:            public void setUp() {
063:                super .setUp();
064:            }
065:
066:            public void tearDown() {
067:                super .tearDown();
068:            }
069:
070:            // ------------------------------------------------------- Individual Tests
071:            // ---------------------------------------------------------- absoluteURL()
072:            public void testAbsoluteURL() {
073:                request.setPathElements("/myapp", "/action.do", null, null);
074:
075:                String url = null;
076:
077:                try {
078:                    url = RequestUtils.absoluteURL(request, "/foo/bar.jsp")
079:                            .toString();
080:                    assertEquals("absoluteURL is correct",
081:                            "http://localhost:8080/myapp/foo/bar.jsp", url);
082:                } catch (MalformedURLException e) {
083:                    fail("Threw MalformedURLException: " + e);
084:                }
085:            }
086:
087:            // ------------------------------------------------------------ actionURL()
088:            // Default application -- extension mapping
089:            public void testActionURL1() {
090:                request.setAttribute(Globals.MODULE_KEY, moduleConfig);
091:                request.setPathElements("/myapp", "/foo.do", null, null);
092:
093:                String url = RequestUtils.actionURL(request, moduleConfig
094:                        .findActionConfig("/dynamic"), "*.do");
095:
096:                assertNotNull("URL was returned", url);
097:                assertEquals("URL value", "/dynamic.do", url);
098:            }
099:
100:            // Second application -- extension mapping
101:            public void testActionURL2() {
102:                request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
103:                request.setPathElements("/myapp", "/2/foo.do", null, null);
104:
105:                String url = RequestUtils.actionURL(request, moduleConfig2
106:                        .findActionConfig("/dynamic2"), "*.do");
107:
108:                assertNotNull("URL was returned", url);
109:                assertEquals("URL value", "/2/dynamic2.do", url);
110:            }
111:
112:            // Default application -- path mapping
113:            public void testActionURL3() {
114:                request.setAttribute(Globals.MODULE_KEY, moduleConfig);
115:                request.setPathElements("/myapp", "/do/foo", null, null);
116:
117:                String url = RequestUtils.actionURL(request, moduleConfig
118:                        .findActionConfig("/dynamic"), "/do/*");
119:
120:                assertNotNull("URL was returned", url);
121:                assertEquals("URL value", "/do/dynamic", url);
122:            }
123:
124:            // ----------------------------------------------------- createActionForm()
125:            // Default module -- No ActionForm should be created
126:            public void testCreateActionForm1a() {
127:                request.setPathElements("/myapp", "/noform.do", null, null);
128:
129:                ActionMapping mapping = (ActionMapping) moduleConfig
130:                        .findActionConfig("/noform");
131:
132:                assertNotNull("Found /noform mapping", mapping);
133:
134:                ActionForm form = RequestUtils.createActionForm(request,
135:                        mapping, moduleConfig, null);
136:
137:                assertNull("No ActionForm returned", form);
138:            }
139:
140:            // Second module -- No ActionForm should be created
141:            public void testCreateActionForm1b() {
142:                request.setPathElements("/myapp", "/2/noform.do", null, null);
143:
144:                ActionMapping mapping = (ActionMapping) moduleConfig2
145:                        .findActionConfig("/noform");
146:
147:                assertNotNull("Found /noform mapping", mapping);
148:
149:                ActionForm form = RequestUtils.createActionForm(request,
150:                        mapping, moduleConfig2, null);
151:
152:                assertNull("No ActionForm returned", form);
153:            }
154:
155:            // Default module -- Standard ActionForm should be created
156:            public void testCreateActionForm2a() {
157:                request.setPathElements("/myapp", "/static.do", null, null);
158:
159:                ActionMapping mapping = (ActionMapping) moduleConfig
160:                        .findActionConfig("/static");
161:
162:                assertNotNull("Found /static mapping", mapping);
163:                assertNotNull("Mapping has non-null name", mapping.getName());
164:                assertEquals("Mapping has correct name", "static", mapping
165:                        .getName());
166:                assertNotNull("AppConfig has form bean " + mapping.getName(),
167:                        moduleConfig.findFormBeanConfig(mapping.getName()));
168:
169:                ActionForm form = RequestUtils.createActionForm(request,
170:                        mapping, moduleConfig, null);
171:
172:                assertNotNull("ActionForm returned", form);
173:                assertTrue("ActionForm of correct type",
174:                        form instanceof  MockFormBean);
175:            }
176:
177:            // Second module -- Standard ActionForm should be created
178:            public void testCreateActionForm2b() {
179:                request.setPathElements("/myapp", "/2/static.do", null, null);
180:
181:                ActionMapping mapping = (ActionMapping) moduleConfig2
182:                        .findActionConfig("/static");
183:
184:                assertNotNull("Found /static mapping", mapping);
185:                assertNotNull("Mapping has non-null name", mapping.getName());
186:                assertEquals("Mapping has correct name", "static", mapping
187:                        .getName());
188:                assertNotNull("AppConfig has form bean " + mapping.getName(),
189:                        moduleConfig.findFormBeanConfig(mapping.getName()));
190:
191:                ActionForm form = RequestUtils.createActionForm(request,
192:                        mapping, moduleConfig2, null);
193:
194:                assertNotNull("ActionForm returned", form);
195:                assertTrue("ActionForm of correct type",
196:                        form instanceof  MockFormBean);
197:            }
198:
199:            // Default module -- Dynamic ActionForm should be created
200:            public void testCreateActionForm3a() {
201:                request.setPathElements("/myapp", "/dynamic.do", null, null);
202:
203:                ActionMapping mapping = (ActionMapping) moduleConfig
204:                        .findActionConfig("/dynamic");
205:
206:                assertNotNull("Found /dynamic mapping", mapping);
207:                assertNotNull("Mapping has non-null name", mapping.getName());
208:                assertEquals("Mapping has correct name", "dynamic", mapping
209:                        .getName());
210:                assertNotNull("AppConfig has form bean " + mapping.getName(),
211:                        moduleConfig.findFormBeanConfig(mapping.getName()));
212:
213:                ActionForm form = RequestUtils.createActionForm(request,
214:                        mapping, moduleConfig, null);
215:
216:                assertNotNull("ActionForm returned", form);
217:                assertTrue("ActionForm of correct type",
218:                        form instanceof  DynaActionForm);
219:            }
220:
221:            // Second module -- Dynamic ActionForm should be created
222:            public void testCreateActionForm3b() {
223:                request.setPathElements("/myapp", "/2/dynamic2.do", null, null);
224:
225:                ActionMapping mapping = (ActionMapping) moduleConfig2
226:                        .findActionConfig("/dynamic2");
227:
228:                assertNotNull("Found /dynamic2 mapping", mapping);
229:                assertNotNull("Mapping has non-null name", mapping.getName());
230:                assertEquals("Mapping has correct name", "dynamic2", mapping
231:                        .getName());
232:                assertNotNull("AppConfig has form bean " + mapping.getName(),
233:                        moduleConfig2.findFormBeanConfig(mapping.getName()));
234:
235:                ActionForm form = RequestUtils.createActionForm(request,
236:                        mapping, moduleConfig2, null);
237:
238:                assertNotNull("ActionForm returned", form);
239:                assertTrue("ActionForm of correct type",
240:                        form instanceof  DynaActionForm);
241:            }
242:
243:            // Default module -- Dynamic ActionForm with initializers
244:            public void testCreateActionForm4a() {
245:                // Retrieve an appropriately configured DynaActionForm instance
246:                request.setPathElements("/myapp", "/dynamic0.do", null, null);
247:
248:                ActionMapping mapping = (ActionMapping) moduleConfig
249:                        .findActionConfig("/dynamic0");
250:
251:                assertNotNull("Found /dynamic0 mapping", mapping);
252:                assertNotNull("Mapping has non-null name", mapping.getName());
253:                assertEquals("Mapping has correct name", "dynamic0", mapping
254:                        .getName());
255:                assertNotNull("AppConfig has form bean " + mapping.getName(),
256:                        moduleConfig.findFormBeanConfig(mapping.getName()));
257:
258:                ActionForm form = RequestUtils.createActionForm(request,
259:                        mapping, moduleConfig, null);
260:
261:                assertNotNull("ActionForm returned", form);
262:                assertTrue("ActionForm of correct type",
263:                        form instanceof  DynaActionForm);
264:
265:                // Validate the property values
266:                DynaActionForm dform = (DynaActionForm) form;
267:                Boolean booleanProperty = (Boolean) dform
268:                        .get("booleanProperty");
269:
270:                assertTrue("booleanProperty is true", booleanProperty
271:                        .booleanValue());
272:
273:                String stringProperty = (String) dform.get("stringProperty");
274:
275:                assertEquals("stringProperty is correct", "String Property",
276:                        stringProperty);
277:
278:                Object value = null;
279:
280:                value = dform.get("intArray1");
281:                assertNotNull("intArray1 exists", value);
282:                assertTrue("intArray1 is int[]", value instanceof  int[]);
283:
284:                int[] intArray1 = (int[]) value;
285:
286:                assertEquals("intArray1 length is correct", 3, intArray1.length);
287:                assertEquals("intArray1[0] value is correct", 1, intArray1[0]);
288:                assertEquals("intArray1[1] value is correct", 2, intArray1[1]);
289:                assertEquals("intArray1[2] value is correct", 3, intArray1[2]);
290:
291:                value = dform.get("intArray2");
292:                assertNotNull("intArray2 exists", value);
293:                assertTrue("intArray2 is int[]", value instanceof  int[]);
294:
295:                int[] intArray2 = (int[]) value;
296:
297:                assertEquals("intArray2 length is correct", 5, intArray2.length);
298:                assertEquals("intArray2[0] value is correct", 0, intArray2[0]);
299:                assertEquals("intArray2[1] value is correct", 0, intArray2[1]);
300:                assertEquals("intArray2[2] value is correct", 0, intArray2[2]);
301:                assertEquals("intArray2[3] value is correct", 0, intArray2[3]);
302:                assertEquals("intArray2[4] value is correct", 0, intArray2[4]);
303:
304:                value = dform.get("principal");
305:                assertNotNull("principal exists", value);
306:                assertTrue("principal is MockPrincipal",
307:                        value instanceof  MockPrincipal);
308:
309:                value = dform.get("stringArray1");
310:                assertNotNull("stringArray1 exists", value);
311:                assertTrue("stringArray1 is int[]", value instanceof  String[]);
312:
313:                String[] stringArray1 = (String[]) value;
314:
315:                assertEquals("stringArray1 length is correct", 3,
316:                        stringArray1.length);
317:                assertEquals("stringArray1[0] value is correct", "aaa",
318:                        stringArray1[0]);
319:                assertEquals("stringArray1[1] value is correct", "bbb",
320:                        stringArray1[1]);
321:                assertEquals("stringArray1[2] value is correct", "ccc",
322:                        stringArray1[2]);
323:
324:                value = dform.get("stringArray2");
325:                assertNotNull("stringArray2 exists", value);
326:                assertTrue("stringArray2 is int[]", value instanceof  String[]);
327:
328:                String[] stringArray2 = (String[]) value;
329:
330:                assertEquals("stringArray2 length is correct", 3,
331:                        stringArray2.length);
332:                assertEquals("stringArray2[0] value is correct", new String(),
333:                        stringArray2[0]);
334:                assertEquals("stringArray2[1] value is correct", new String(),
335:                        stringArray2[1]);
336:                assertEquals("stringArray2[2] value is correct", new String(),
337:                        stringArray2[2]);
338:
339:                // Different form beans should get different property value instances
340:                Object value1 = null;
341:                DynaActionForm dform1 = (DynaActionForm) RequestUtils
342:                        .createActionForm(request, mapping, moduleConfig, null);
343:
344:                value = dform.get("principal");
345:                value1 = dform1.get("principal");
346:                assertEquals("Different form beans get equal instance values",
347:                        value, value1);
348:                assertTrue("Different form beans get different instances 1",
349:                        value != value1);
350:
351:                value = dform.get("stringArray1");
352:                value1 = dform1.get("stringArray1");
353:                assertTrue("Different form beans get different instances 2",
354:                        value != value1);
355:
356:                dform1.set("stringProperty", "Different stringProperty value");
357:                value = dform.get("stringProperty");
358:                value1 = dform1.get("stringProperty");
359:                assertTrue("Different form beans get different instances 3",
360:                        value != value1);
361:            }
362:
363:            // ----------------------------------------------------------- forwardURL()
364:            // Default module (default forwardPattern)
365:            public void testForwardURL1() {
366:                request.setAttribute(Globals.MODULE_KEY, moduleConfig);
367:                request.setPathElements("/myapp", "/action.do", null, null);
368:
369:                ForwardConfig forward = null;
370:                String result = null;
371:
372:                // redirect=false, module=null
373:                forward = moduleConfig.findForwardConfig("moduleForward");
374:                assertNotNull("moduleForward found", forward);
375:                result = RequestUtils.forwardURL(request, forward, null);
376:                assertNotNull("moduleForward computed", result);
377:                assertEquals("moduleForward value", "/module/forward", result);
378:
379:                // redirect=true, module=null
380:                forward = moduleConfig.findForwardConfig("moduleRedirect");
381:                assertNotNull("moduleRedirect found", forward);
382:                result = RequestUtils.forwardURL(request, forward, null);
383:                assertNotNull("moduleRedirect computed", result);
384:                assertEquals("moduleRedirect value", "/module/redirect", result);
385:
386:                // redirect=false, module=/context
387:                forward = moduleConfig.findForwardConfig("contextForward");
388:                assertNotNull("contextForward found", forward);
389:                result = RequestUtils.forwardURL(request, forward, null);
390:                assertNotNull("contextForward computed", result);
391:                assertEquals("contextForward value", "/context/forward", result);
392:
393:                // redirect=true, module=/context
394:                forward = moduleConfig.findForwardConfig("contextRedirect");
395:                assertNotNull("contextRedirect found", forward);
396:                result = RequestUtils.forwardURL(request, forward, null);
397:                assertNotNull("contextRedirect computed", result);
398:                assertEquals("contextRedirct value", "/context/redirect",
399:                        result);
400:
401:                // noslash, module=null
402:                forward = moduleConfig.findForwardConfig("moduleNoslash");
403:                assertNotNull("moduleNoslash found", forward);
404:                result = RequestUtils.forwardURL(request, forward, null);
405:                assertNotNull("moduleNoslash computed", result);
406:                assertEquals("moduleNoslash value", "/module/noslash", result);
407:
408:                // noslash, module=/
409:                forward = moduleConfig.findForwardConfig("contextNoslash");
410:                assertNotNull("contextNoslash found", forward);
411:                result = RequestUtils.forwardURL(request, forward, null);
412:                assertNotNull("contextNoslash computed", result);
413:                assertEquals("contextNoslash value", "/context/noslash", result);
414:            }
415:
416:            // Second module (default forwardPattern)
417:            public void testForwardURL2() {
418:                request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
419:                request.setPathElements("/myapp", "/2/action.do", null, null);
420:
421:                ForwardConfig forward = null;
422:                String result = null;
423:
424:                // redirect=false, module=null
425:                forward = moduleConfig2.findForwardConfig("moduleForward");
426:                assertNotNull("moduleForward found", forward);
427:                result = RequestUtils.forwardURL(request, forward, null);
428:                assertNotNull("moduleForward computed", result);
429:                assertEquals("moduleForward value", "/2/module/forward", result);
430:
431:                // redirect=true, module=null
432:                forward = moduleConfig2.findForwardConfig("moduleRedirect");
433:                assertNotNull("moduleRedirect found", forward);
434:                result = RequestUtils.forwardURL(request, forward, null);
435:                assertNotNull("moduleRedirect computed", result);
436:                assertEquals("moduleRedirect value", "/2/module/redirect",
437:                        result);
438:
439:                // redirect=false, module=/context
440:                forward = moduleConfig2.findForwardConfig("contextForward");
441:                assertNotNull("contextForward found", forward);
442:                result = RequestUtils.forwardURL(request, forward, null);
443:                assertNotNull("contextForward computed", result);
444:                assertEquals("contextForward value", "/context/forward", result);
445:
446:                // redirect=true, module=/context
447:                forward = moduleConfig2.findForwardConfig("contextRedirect");
448:                assertNotNull("contextRedirect found", forward);
449:                result = RequestUtils.forwardURL(request, forward, null);
450:                assertNotNull("contextRedirect computed", result);
451:                assertEquals("contextRedirct value", "/context/redirect",
452:                        result);
453:
454:                // noslash, module=null
455:                forward = moduleConfig2.findForwardConfig("moduleNoslash");
456:                assertNotNull("moduleNoslash found", forward);
457:                result = RequestUtils.forwardURL(request, forward, null);
458:                assertNotNull("moduleNoslash computed", result);
459:                assertEquals("moduleNoslash value", "/2/module/noslash", result);
460:
461:                // noslash, module=/
462:                forward = moduleConfig2.findForwardConfig("contextNoslash");
463:                assertNotNull("contextNoslash found", forward);
464:                result = RequestUtils.forwardURL(request, forward, null);
465:                assertNotNull("contextNoslash computed", result);
466:                assertEquals("contextNoslash value", "/context/noslash", result);
467:            }
468:
469:            // Third module (custom forwardPattern)
470:            public void testForwardURL3() {
471:                request.setAttribute(Globals.MODULE_KEY, moduleConfig3);
472:                request.setPathElements("/myapp", "/3/action.do", null, null);
473:
474:                ForwardConfig forward = null;
475:                String result = null;
476:
477:                // redirect=false, module=null
478:                forward = moduleConfig3.findForwardConfig("moduleForward");
479:                assertNotNull("moduleForward found", forward);
480:                result = RequestUtils.forwardURL(request, forward, null);
481:                assertNotNull("moduleForward computed", result);
482:                assertEquals("moduleForward value",
483:                        "/forwarding/3/module/forward", result);
484:
485:                // redirect=true, module=null
486:                forward = moduleConfig3.findForwardConfig("moduleRedirect");
487:                assertNotNull("moduleRedirect found", forward);
488:                result = RequestUtils.forwardURL(request, forward, null);
489:                assertNotNull("moduleRedirect computed", result);
490:                assertEquals("moduleRedirect value",
491:                        "/forwarding/3/module/redirect", result);
492:
493:                // redirect=false, module=/context
494:                forward = moduleConfig3.findForwardConfig("contextForward");
495:                assertNotNull("contextForward found", forward);
496:                result = RequestUtils.forwardURL(request, forward, null);
497:                assertNotNull("contextForward computed", result);
498:                assertEquals("contextForward value",
499:                        "/forwarding/context/forward", result);
500:
501:                // redirect=true, module=/context
502:                forward = moduleConfig3.findForwardConfig("contextRedirect");
503:                assertNotNull("contextRedirect found", forward);
504:                result = RequestUtils.forwardURL(request, forward, null);
505:                assertNotNull("contextRedirect computed", result);
506:                assertEquals("contextRedirct value",
507:                        "/forwarding/context/redirect", result);
508:
509:                // noslash, module=null
510:                forward = moduleConfig3.findForwardConfig("moduleNoslash");
511:                assertNotNull("moduleNoslash found", forward);
512:                result = RequestUtils.forwardURL(request, forward, null);
513:                assertNotNull("moduleNoslash computed", result);
514:                assertEquals("moduleNoslash value",
515:                        "/forwarding/3/module/noslash", result);
516:
517:                // noslash, module=/
518:                forward = moduleConfig3.findForwardConfig("contextNoslash");
519:                assertNotNull("contextNoslash found", forward);
520:                result = RequestUtils.forwardURL(request, forward, null);
521:                assertNotNull("contextNoslash computed", result);
522:                assertEquals("contextNoslash value",
523:                        "/forwarding/context/noslash", result);
524:            }
525:
526:            // Cross module forwards
527:            public void testForwardURLa() {
528:                request.setAttribute(Globals.MODULE_KEY, moduleConfig);
529:                request.setPathElements("/myapp", "/action.do", null, null);
530:
531:                ForwardConfig forward = null;
532:                String result = null;
533:
534:                // redirect=false, contextRelative=false, link to module 3
535:                forward = moduleConfig3.findForwardConfig("moduleForward");
536:                assertNotNull("moduleForward found", forward);
537:                result = RequestUtils.forwardURL(request, forward,
538:                        moduleConfig3);
539:                assertNotNull("moduleForward computed", result);
540:                assertEquals("moduleForward value",
541:                        "/forwarding/3/module/forward", result);
542:
543:                // redirect=true, contextRelative=false, link to module 3
544:                forward = moduleConfig3.findForwardConfig("moduleRedirect");
545:                assertNotNull("moduleRedirect found", forward);
546:                result = RequestUtils.forwardURL(request, forward,
547:                        moduleConfig3);
548:                assertNotNull("moduleRedirect computed", result);
549:                assertEquals("moduleRedirect value",
550:                        "/forwarding/3/module/redirect", result);
551:
552:                // redirect=false, module=/context
553:                forward = moduleConfig3.findForwardConfig("contextForward");
554:                assertNotNull("contextForward found", forward);
555:                result = RequestUtils.forwardURL(request, forward,
556:                        moduleConfig3);
557:                assertNotNull("contextForward computed", result);
558:                assertEquals("contextForward value",
559:                        "/forwarding/context/forward", result);
560:
561:                // redirect=true, module=/context
562:                forward = moduleConfig3.findForwardConfig("contextRedirect");
563:                assertNotNull("contextRedirect found", forward);
564:                result = RequestUtils.forwardURL(request, forward,
565:                        moduleConfig3);
566:                assertNotNull("contextRedirect computed", result);
567:                assertEquals("contextRedirct value",
568:                        "/forwarding/context/redirect", result);
569:
570:                // noslash, contextRelative=false, link to module 3
571:                forward = moduleConfig3.findForwardConfig("moduleNoslash");
572:                assertNotNull("moduleNoslash found", forward);
573:                result = RequestUtils.forwardURL(request, forward,
574:                        moduleConfig3);
575:                assertNotNull("moduleNoslash computed", result);
576:                assertEquals("moduleNoslash value",
577:                        "/forwarding/3/module/noslash", result);
578:
579:                // noslash, module=/
580:                forward = moduleConfig3.findForwardConfig("contextNoslash");
581:                assertNotNull("contextNoslash found", forward);
582:                result = RequestUtils.forwardURL(request, forward,
583:                        moduleConfig3);
584:                assertNotNull("contextNoslash computed", result);
585:                assertEquals("contextNoslash value",
586:                        "/forwarding/context/noslash", result);
587:            }
588:
589:            // ----------------------------------------------------------- requestURL()
590:            public void testRequestURL() {
591:                request.setPathElements("/myapp", "/foo.do", null, null);
592:
593:                String url = null;
594:
595:                try {
596:                    url = RequestUtils.requestURL(request).toString();
597:                } catch (MalformedURLException e) {
598:                    fail("MalformedURLException: " + e);
599:                }
600:
601:                assertNotNull("URL was returned", url);
602:                assertEquals("URL value", "http://localhost:8080/myapp/foo.do",
603:                        url);
604:            }
605:
606:            // ---------------------------------------------------- selectApplication()
607:            // Map to the default module -- direct
608:            public void testSelectApplication1a() {
609:                request.setPathElements("/myapp", "/noform.do", null, null);
610:                ModuleUtils.getInstance().selectModule(request, context);
611:
612:                ModuleConfig moduleConfig = (ModuleConfig) request
613:                        .getAttribute(Globals.MODULE_KEY);
614:
615:                assertNotNull("Selected a module", moduleConfig);
616:                assertEquals("Selected correct module", "", moduleConfig
617:                        .getPrefix());
618:
619:                // FIXME - check module resources?
620:            }
621:
622:            // Map to the second module -- direct
623:            public void testSelectApplication1b() {
624:                String[] prefixes = { "/1", "/2" };
625:
626:                context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
627:                request.setPathElements("/myapp", "/2/noform.do", null, null);
628:
629:                ModuleUtils.getInstance().selectModule(request, context);
630:
631:                ModuleConfig moduleConfig = (ModuleConfig) request
632:                        .getAttribute(Globals.MODULE_KEY);
633:
634:                assertNotNull("Selected a module", moduleConfig);
635:                assertEquals("Selected correct module", "/2", moduleConfig
636:                        .getPrefix());
637:
638:                // FIXME - check module resources?
639:            }
640:
641:            // Map to the default module -- include
642:            public void testSelectApplication2a() {
643:                request.setPathElements("/myapp", "/2/noform.do", null, null);
644:                request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH,
645:                        "/noform.do");
646:                ModuleUtils.getInstance().selectModule(request, context);
647:
648:                ModuleConfig moduleConfig = (ModuleConfig) request
649:                        .getAttribute(Globals.MODULE_KEY);
650:
651:                assertNotNull("Selected an application", moduleConfig);
652:                assertEquals("Selected correct application", "", moduleConfig
653:                        .getPrefix());
654:
655:                // FIXME - check application resources?
656:            }
657:
658:            // Map to the second module -- include
659:            public void testSelectApplication2b() {
660:                String[] prefixes = { "/1", "/2" };
661:
662:                context.setAttribute(Globals.MODULE_PREFIXES_KEY, prefixes);
663:                request.setPathElements("/myapp", "/noform.do", null, null);
664:                request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH,
665:                        "/2/noform.do");
666:                ModuleUtils.getInstance().selectModule(request, context);
667:
668:                ModuleConfig moduleConfig = (ModuleConfig) request
669:                        .getAttribute(Globals.MODULE_KEY);
670:
671:                assertNotNull("Selected a module", moduleConfig);
672:                assertEquals("Selected correct module", "/2", moduleConfig
673:                        .getPrefix());
674:
675:                // FIXME - check application resources?
676:            }
677:
678:            // ------------------------------------------------------------ serverURL()
679:            // Basic test on values in mock objects
680:            public void testServerURL() {
681:                String url = null;
682:
683:                try {
684:                    url = RequestUtils.serverURL(request).toString();
685:                } catch (MalformedURLException e) {
686:                    fail("Threw MalformedURLException: " + e);
687:                }
688:
689:                assertNotNull("serverURL is present", url);
690:                assertEquals("serverURL value", "http://localhost:8080", url);
691:            }
692:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.