Source Code Cross Referenced for ServletWebContextTestCase.java in  » Library » Apache-commons-chain-1.1-src » org » apache » commons » chain » web » servlet » 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 » Library » Apache commons chain 1.1 src » org.apache.commons.chain.web.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2004 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.chain.web.servlet;
017:
018:        import junit.framework.Test;
019:        import junit.framework.TestSuite;
020:        import org.apache.commons.chain.Context;
021:        import org.apache.commons.chain.impl.ContextBaseTestCase;
022:        import org.apache.commons.chain.web.WebContext;
023:
024:        import javax.servlet.ServletContext;
025:        import javax.servlet.http.HttpServletRequest;
026:        import javax.servlet.http.HttpServletResponse;
027:        import javax.servlet.http.HttpSession;
028:        import javax.servlet.http.Cookie;
029:        import java.util.HashMap;
030:        import java.util.Iterator;
031:        import java.util.Map;
032:        import java.util.Set;
033:        import java.util.Collection;
034:
035:        /**
036:         * Extension of <code>ContextBaseTestCase</code> to validate the
037:         * extra functionality of this implementation.
038:         */
039:
040:        public class ServletWebContextTestCase extends ContextBaseTestCase {
041:
042:            // ---------------------------------------------------------- Constructors
043:
044:            /**
045:             * Construct a new instance of this test case.
046:             *
047:             * @param name Name of the test case
048:             */
049:            public ServletWebContextTestCase(String name) {
050:                super (name);
051:            }
052:
053:            // ----------------------------------------------------- Instance Variables
054:
055:            // Servlet API Objects
056:            protected ServletContext scontext = null;
057:            protected HttpServletRequest request = null;
058:            protected HttpServletResponse response = null;
059:            protected HttpSession session = null;
060:
061:            // -------------------------------------------------- Overall Test Methods
062:
063:            /**
064:             * Set up instance variables required by this test case.
065:             */
066:            public void setUp() {
067:                scontext = new MockServletContext();
068:                scontext.setAttribute("akey1", "avalue1");
069:                scontext.setAttribute("akey2", "avalue2");
070:                scontext.setAttribute("akey3", "avalue3");
071:                scontext.setAttribute("akey4", "avalue4");
072:                ((MockServletContext) scontext).addInitParameter("ikey1",
073:                        "ivalue1");
074:                ((MockServletContext) scontext).addInitParameter("ikey2",
075:                        "ivalue2");
076:                ((MockServletContext) scontext).addInitParameter("ikey3",
077:                        "ivalue3");
078:                session = new MockHttpSession(scontext);
079:                session.setAttribute("skey1", "svalue1");
080:                session.setAttribute("skey2", "svalue2");
081:                session.setAttribute("skey3", "svalue3");
082:                request = new MockHttpServletRequest("/context", "/servlet",
083:                        "/path/info", "a=b&c=d", session);
084:                request.setAttribute("rkey1", "rvalue1");
085:                request.setAttribute("rkey2", "rvalue2");
086:                ((MockHttpServletRequest) request)
087:                        .addHeader("hkey1", "hvalue1");
088:                ((MockHttpServletRequest) request).addHeader("hkey2",
089:                        "hvalue2a");
090:                ((MockHttpServletRequest) request).addHeader("hkey2",
091:                        "hvalue2b");
092:                ((MockHttpServletRequest) request).addParameter("pkey1",
093:                        "pvalue1");
094:                ((MockHttpServletRequest) request).addParameter("pkey2",
095:                        "pvalue2a");
096:                ((MockHttpServletRequest) request).addParameter("pkey2",
097:                        "pvalue2b");
098:                ((MockHttpServletRequest) request)
099:                        .addCookie("ckey1", "cvalue1");
100:                ((MockHttpServletRequest) request)
101:                        .addCookie("ckey2", "cvalue2");
102:                response = new MockHttpServletResponse();
103:                context = createContext();
104:            }
105:
106:            /**
107:             * Return the tests included in this test suite.
108:             */
109:            public static Test suite() {
110:                return (new TestSuite(ServletWebContextTestCase.class));
111:            }
112:
113:            /**
114:             * Tear down instance variables required by this test case.
115:             */
116:            public void tearDown() {
117:                scontext = null;
118:                session = null;
119:                request = null;
120:                response = null;
121:                context = null;
122:            }
123:
124:            // ------------------------------------------------ Individual Test Methods
125:
126:            // Test getApplicationScope()
127:            public void testApplicationScope() {
128:
129:                Map map = ((WebContext) context).getApplicationScope();
130:                assertNotNull(map);
131:
132:                // Initial contents
133:                checkMapSize(map, 4);
134:                assertEquals("avalue1", (String) map.get("akey1"));
135:                assertEquals("avalue2", (String) map.get("akey2"));
136:                assertEquals("avalue3", (String) map.get("akey3"));
137:                assertEquals("avalue4", (String) map.get("akey4"));
138:
139:                // Transparency - entrySet()
140:                checkEntrySet(map, true);
141:
142:                // Transparency - removal via web object
143:                scontext.removeAttribute("akey1");
144:                checkMapSize(map, 3);
145:                assertNull(map.get("akey1"));
146:
147:                // Transparency - removal via map
148:                map.remove("akey2");
149:                checkMapSize(map, 2);
150:                assertNull(scontext.getAttribute("akey2"));
151:
152:                // Transparency - addition via web object
153:                scontext.setAttribute("akeyA", "avalueA");
154:                checkMapSize(map, 3);
155:                assertEquals("avalueA", (String) map.get("akeyA"));
156:
157:                // Transparency - addition via map
158:                map.put("akeyB", "avalueB");
159:                checkMapSize(map, 4);
160:                assertEquals("avalueB", (String) scontext.getAttribute("akeyB"));
161:
162:                // Transparency - replacement via web object
163:                scontext.setAttribute("akeyA", "newvalueA");
164:                checkMapSize(map, 4);
165:                assertEquals("newvalueA", (String) map.get("akeyA"));
166:
167:                // Transparency - replacement via map
168:                map.put("akeyB", "newvalueB");
169:                assertEquals(4, map.size());
170:                assertEquals("newvalueB", (String) scontext
171:                        .getAttribute("akeyB"));
172:
173:                // Clearing the map
174:                map.clear();
175:                checkMapSize(map, 0);
176:
177:            }
178:
179:            // Test equals() and hashCode()
180:            // Copied from ContextBaseTestCase with customized creation of "other"
181:            public void testEquals() {
182:
183:                // FIXME - ServletWebContext needs a better equals()
184:
185:                // Compare to self
186:                assertTrue(context.equals(context));
187:                assertTrue(context.hashCode() == context.hashCode());
188:
189:                // Compare to equivalent instance
190:                Context other = new ServletWebContext(scontext, request,
191:                        response);
192:                // assertTrue(context.equals(other));
193:                assertTrue(context.hashCode() == other.hashCode());
194:
195:                // Compare to non-equivalent instance - other modified
196:                other.put("bop", "bop value");
197:                // assertTrue(!context.equals(other));
198:                assertTrue(context.hashCode() != other.hashCode());
199:
200:                // Compare to non-equivalent instance - self modified
201:                other = new ServletWebContext(scontext, request, response);
202:                context.put("bop", "bop value");
203:                // assertTrue(!context.equals(other));
204:                assertTrue(context.hashCode() != other.hashCode());
205:
206:            }
207:
208:            // Test getHeader()
209:            public void testHeader() {
210:
211:                Map map = ((WebContext) context).getHeader();
212:                assertNotNull(map);
213:
214:                // Initial contents
215:                checkMapSize(map, 2);
216:                assertEquals("hvalue1", (String) map.get("hkey1"));
217:                assertEquals("hvalue2a", (String) map.get("hkey2"));
218:                assertTrue(map.containsKey("hkey1"));
219:                assertTrue(map.containsKey("hkey2"));
220:                assertTrue(map.containsValue("hvalue1"));
221:                assertTrue(map.containsValue("hvalue2a"));
222:
223:                // Transparency - entrySet()
224:                checkEntrySet(map, false);
225:
226:                // Unsupported operations on read-only map
227:                try {
228:                    map.clear();
229:                    fail("Should have thrown UnsupportedOperationException");
230:                } catch (UnsupportedOperationException e) {
231:                    ; // expected result
232:                }
233:                try {
234:                    map.put("hkey3", "hvalue3");
235:                    fail("Should have thrown UnsupportedOperationException");
236:                } catch (UnsupportedOperationException e) {
237:                    ; // expected result
238:                }
239:                try {
240:                    map.putAll(new HashMap());
241:                    fail("Should have thrown UnsupportedOperationException");
242:                } catch (UnsupportedOperationException e) {
243:                    ; // expected result
244:                }
245:                try {
246:                    map.remove("hkey1");
247:                    fail("Should have thrown UnsupportedOperationException");
248:                } catch (UnsupportedOperationException e) {
249:                    ; // expected result
250:                }
251:
252:            }
253:
254:            // Test getHeaderValues()
255:            public void testHeaderValues() {
256:
257:                Map map = ((WebContext) context).getHeaderValues();
258:                assertNotNull(map);
259:
260:                // Initial contents
261:                checkMapSize(map, 2);
262:                Object value1 = map.get("hkey1");
263:                assertNotNull(value1);
264:                assertTrue(value1 instanceof  String[]);
265:                String values1[] = (String[]) value1;
266:                assertEquals(1, values1.length);
267:                assertEquals("hvalue1", values1[0]);
268:                Object value2 = map.get("hkey2");
269:                assertNotNull(value2);
270:                assertTrue(value2 instanceof  String[]);
271:                String values2[] = (String[]) value2;
272:                assertEquals(2, values2.length);
273:                assertEquals("hvalue2a", values2[0]);
274:                assertEquals("hvalue2b", values2[1]);
275:                assertTrue(map.containsKey("hkey1"));
276:                assertTrue(map.containsKey("hkey2"));
277:                assertTrue(map.containsValue(values1));
278:                assertTrue(map.containsValue(values2));
279:
280:                // Transparency - entrySet()
281:                checkEntrySet(map, false);
282:
283:                // Unsupported operations on read-only map
284:                try {
285:                    map.clear();
286:                    fail("Should have thrown UnsupportedOperationException");
287:                } catch (UnsupportedOperationException e) {
288:                    ; // expected result
289:                }
290:                try {
291:                    map.put("hkey3", values2);
292:                    fail("Should have thrown UnsupportedOperationException");
293:                } catch (UnsupportedOperationException e) {
294:                    ; // expected result
295:                }
296:                try {
297:                    map.putAll(new HashMap());
298:                    fail("Should have thrown UnsupportedOperationException");
299:                } catch (UnsupportedOperationException e) {
300:                    ; // expected result
301:                }
302:                try {
303:                    map.remove("hkey1");
304:                    fail("Should have thrown UnsupportedOperationException");
305:                } catch (UnsupportedOperationException e) {
306:                    ; // expected result
307:                }
308:
309:            }
310:
311:            // Test getInitParam()
312:            public void testInitParam() {
313:
314:                Map map = ((WebContext) context).getInitParam();
315:                assertNotNull(map);
316:
317:                // Initial contents
318:                checkMapSize(map, 3);
319:                assertEquals("ivalue1", (String) map.get("ikey1"));
320:                assertEquals("ivalue2", (String) map.get("ikey2"));
321:                assertEquals("ivalue3", (String) map.get("ikey3"));
322:                assertTrue(map.containsKey("ikey1"));
323:                assertTrue(map.containsKey("ikey2"));
324:                assertTrue(map.containsKey("ikey3"));
325:                assertTrue(map.containsValue("ivalue1"));
326:                assertTrue(map.containsValue("ivalue2"));
327:                assertTrue(map.containsValue("ivalue3"));
328:
329:                // Transparency - entrySet()
330:                checkEntrySet(map, false);
331:
332:                // Unsupported operations on read-only map
333:                try {
334:                    map.clear();
335:                    fail("Should have thrown UnsupportedOperationException");
336:                } catch (UnsupportedOperationException e) {
337:                    ; // expected result
338:                }
339:                try {
340:                    map.put("ikey4", "ivalue4");
341:                    fail("Should have thrown UnsupportedOperationException");
342:                } catch (UnsupportedOperationException e) {
343:                    ; // expected result
344:                }
345:                try {
346:                    map.putAll(new HashMap());
347:                    fail("Should have thrown UnsupportedOperationException");
348:                } catch (UnsupportedOperationException e) {
349:                    ; // expected result
350:                }
351:                try {
352:                    map.remove("ikey1");
353:                    fail("Should have thrown UnsupportedOperationException");
354:                } catch (UnsupportedOperationException e) {
355:                    ; // expected result
356:                }
357:
358:            }
359:
360:            // Test getParam()
361:            public void testParam() {
362:
363:                Map map = ((WebContext) context).getParam();
364:                assertNotNull(map);
365:
366:                // Initial contents
367:                checkMapSize(map, 2);
368:                assertEquals("pvalue1", (String) map.get("pkey1"));
369:                assertEquals("pvalue2a", (String) map.get("pkey2"));
370:                assertTrue(map.containsKey("pkey1"));
371:                assertTrue(map.containsKey("pkey2"));
372:                assertTrue(map.containsValue("pvalue1"));
373:                assertTrue(map.containsValue("pvalue2a"));
374:
375:                checkEntrySet(map, false);
376:
377:                // Unsupported operations on read-only map
378:                try {
379:                    map.clear();
380:                    fail("Should have thrown UnsupportedOperationException");
381:                } catch (UnsupportedOperationException e) {
382:                    ; // expected result
383:                }
384:                try {
385:                    map.put("pkey3", "pvalue3");
386:                    fail("Should have thrown UnsupportedOperationException");
387:                } catch (UnsupportedOperationException e) {
388:                    ; // expected result
389:                }
390:                try {
391:                    map.putAll(new HashMap());
392:                    fail("Should have thrown UnsupportedOperationException");
393:                } catch (UnsupportedOperationException e) {
394:                    ; // expected result
395:                }
396:                try {
397:                    map.remove("pkey1");
398:                    fail("Should have thrown UnsupportedOperationException");
399:                } catch (UnsupportedOperationException e) {
400:                    ; // expected result
401:                }
402:
403:            }
404:
405:            // Test getParamValues()
406:            public void testParamValues() {
407:
408:                Map map = ((WebContext) context).getParamValues();
409:                assertNotNull(map);
410:
411:                // Initial contents
412:                checkMapSize(map, 2);
413:                Object value1 = map.get("pkey1");
414:                assertNotNull(value1);
415:                assertTrue(value1 instanceof  String[]);
416:                String values1[] = (String[]) value1;
417:                assertEquals(1, values1.length);
418:                assertEquals("pvalue1", values1[0]);
419:                Object value2 = map.get("pkey2");
420:                assertNotNull(value2);
421:                assertTrue(value2 instanceof  String[]);
422:                String values2[] = (String[]) value2;
423:                assertEquals(2, values2.length);
424:                assertEquals("pvalue2a", values2[0]);
425:                assertEquals("pvalue2b", values2[1]);
426:                assertTrue(map.containsKey("pkey1"));
427:                assertTrue(map.containsKey("pkey2"));
428:                assertTrue(map.containsValue(values1));
429:                assertTrue(map.containsValue(values2));
430:
431:                // Unsupported operations on read-only map
432:                try {
433:                    map.clear();
434:                    fail("Should have thrown UnsupportedOperationException");
435:                } catch (UnsupportedOperationException e) {
436:                    ; // expected result
437:                }
438:                try {
439:                    map.put("pkey3", values2);
440:                    fail("Should have thrown UnsupportedOperationException");
441:                } catch (UnsupportedOperationException e) {
442:                    ; // expected result
443:                }
444:                try {
445:                    map.putAll(new HashMap());
446:                    fail("Should have thrown UnsupportedOperationException");
447:                } catch (UnsupportedOperationException e) {
448:                    ; // expected result
449:                }
450:                try {
451:                    map.remove("pkey1");
452:                    fail("Should have thrown UnsupportedOperationException");
453:                } catch (UnsupportedOperationException e) {
454:                    ; // expected result
455:                }
456:
457:            }
458:
459:            // Test getCookies()
460:            public void testCookies() {
461:
462:                Map map = ((WebContext) context).getCookies();
463:                assertNotNull(map);
464:
465:                // Initial contents
466:                checkMapSize(map, 2);
467:                Cookie cookie1 = (Cookie) map.get("ckey1");
468:                assertNotNull(cookie1);
469:                assertEquals("cvalue1", cookie1.getValue());
470:                Cookie cookie2 = (Cookie) map.get("ckey2");
471:                assertNotNull(cookie2);
472:                assertEquals("cvalue2", cookie2.getValue());
473:                assertTrue(map.containsKey("ckey1"));
474:                assertTrue(map.containsKey("ckey2"));
475:                assertTrue(map.containsValue(cookie1));
476:                assertTrue(map.containsValue(cookie2));
477:
478:                // Unsupported operations on read-only map
479:                try {
480:                    map.clear();
481:                    fail("Should have thrown UnsupportedOperationException");
482:                } catch (UnsupportedOperationException e) {
483:                    ; // expected result
484:                }
485:                try {
486:                    map.put("ckey3", "XXX");
487:                    fail("Should have thrown UnsupportedOperationException");
488:                } catch (UnsupportedOperationException e) {
489:                    ; // expected result
490:                }
491:                try {
492:                    map.putAll(new HashMap());
493:                    fail("Should have thrown UnsupportedOperationException");
494:                } catch (UnsupportedOperationException e) {
495:                    ; // expected result
496:                }
497:                try {
498:                    map.remove("ckey1");
499:                    fail("Should have thrown UnsupportedOperationException");
500:                } catch (UnsupportedOperationException e) {
501:                    ; // expected result
502:                }
503:
504:            }
505:
506:            // Test state of newly created instance
507:            public void testPristine() {
508:
509:                super .testPristine();
510:                ServletWebContext swcontext = (ServletWebContext) context;
511:
512:                // Properties should all be non-null
513:                assertNotNull(swcontext.getApplicationScope());
514:                assertNotNull(swcontext.getHeader());
515:                assertNotNull(swcontext.getHeaderValues());
516:                assertNotNull(swcontext.getInitParam());
517:                assertNotNull(swcontext.getParam());
518:                assertNotNull(swcontext.getParamValues());
519:                assertNotNull(swcontext.getCookies());
520:                assertNotNull(swcontext.getRequestScope());
521:                assertNotNull(swcontext.getSessionScope());
522:
523:                // Attribute-property transparency
524:                assertTrue(swcontext.getApplicationScope() == swcontext
525:                        .get("applicationScope"));
526:                assertTrue(swcontext.getHeader() == swcontext.get("header"));
527:                assertTrue(swcontext.getHeaderValues() == swcontext
528:                        .get("headerValues"));
529:                assertTrue(swcontext.getInitParam() == swcontext
530:                        .get("initParam"));
531:                assertTrue(swcontext.getParam() == swcontext.get("param"));
532:                assertTrue(swcontext.getParamValues() == swcontext
533:                        .get("paramValues"));
534:                assertTrue(swcontext.getCookies() == swcontext.get("cookies"));
535:                assertTrue(swcontext.getRequestScope() == swcontext
536:                        .get("requestScope"));
537:                assertTrue(swcontext.getSessionScope() == swcontext
538:                        .get("sessionScope"));
539:
540:            }
541:
542:            // Test release()
543:            public void testRelease() {
544:
545:                ServletWebContext swcontext = (ServletWebContext) context;
546:                swcontext.release();
547:
548:                // Properties should all be null
549:                assertNull(swcontext.getApplicationScope());
550:                assertNull(swcontext.getHeader());
551:                assertNull(swcontext.getHeaderValues());
552:                assertNull(swcontext.getInitParam());
553:                assertNull(swcontext.getParam());
554:                assertNull(swcontext.getParamValues());
555:                assertNull(swcontext.getCookies());
556:                assertNull(swcontext.getRequestScope());
557:                assertNull(swcontext.getSessionScope());
558:
559:                // Attributes should all be null
560:                assertNull(swcontext.get("applicationScope"));
561:                assertNull(swcontext.get("header"));
562:                assertNull(swcontext.get("headerValues"));
563:                assertNull(swcontext.get("initParam"));
564:                assertNull(swcontext.get("param"));
565:                assertNull(swcontext.get("paramValues"));
566:                assertNull(swcontext.get("cookies"));
567:                assertNull(swcontext.get("requestScope"));
568:                assertNull(swcontext.get("sessionScope"));
569:
570:            }
571:
572:            // Test getRequestScope()
573:            public void testRequestScope() {
574:
575:                Map map = ((WebContext) context).getRequestScope();
576:                assertNotNull(map);
577:
578:                // Initial contents
579:                checkMapSize(map, 2);
580:                assertEquals("rvalue1", (String) map.get("rkey1"));
581:                assertEquals("rvalue2", (String) map.get("rkey2"));
582:
583:                // Transparency - entrySet()
584:                checkEntrySet(map, true);
585:
586:                // Transparency - removal via web object
587:                request.removeAttribute("rkey1");
588:                checkMapSize(map, 1);
589:                assertNull(map.get("rkey1"));
590:
591:                // Transparency - removal via map
592:                map.remove("rkey2");
593:                checkMapSize(map, 0);
594:                assertNull(request.getAttribute("rkey2"));
595:
596:                // Transparency - addition via web object
597:                request.setAttribute("rkeyA", "rvalueA");
598:                checkMapSize(map, 1);
599:                assertEquals("rvalueA", (String) map.get("rkeyA"));
600:
601:                // Transparency - addition via map
602:                map.put("rkeyB", "rvalueB");
603:                checkMapSize(map, 2);
604:                assertEquals("rvalueB", (String) request.getAttribute("rkeyB"));
605:
606:                // Transparency - replacement via web object
607:                request.setAttribute("rkeyA", "newvalueA");
608:                checkMapSize(map, 2);
609:                assertEquals("newvalueA", (String) map.get("rkeyA"));
610:
611:                // Transparency - replacement via map
612:                map.put("rkeyB", "newvalueB");
613:                checkMapSize(map, 2);
614:                assertEquals("newvalueB", (String) request
615:                        .getAttribute("rkeyB"));
616:
617:                // Clearing the map
618:                map.clear();
619:                checkMapSize(map, 0);
620:
621:            }
622:
623:            // Test getSessionScope()
624:            public void testSessionScope() {
625:
626:                Map map = ((WebContext) context).getSessionScope();
627:                assertNotNull(map);
628:
629:                // Initial contents
630:                checkMapSize(map, 3);
631:                assertEquals("svalue1", (String) map.get("skey1"));
632:                assertEquals("svalue2", (String) map.get("skey2"));
633:                assertEquals("svalue3", (String) map.get("skey3"));
634:
635:                // Transparency - entrySet()
636:                checkEntrySet(map, true);
637:
638:                // Transparency - removal via web object
639:                session.removeAttribute("skey1");
640:                checkMapSize(map, 2);
641:                assertNull(map.get("skey1"));
642:
643:                // Transparency - removal via map
644:                map.remove("skey2");
645:                checkMapSize(map, 1);
646:                assertNull(session.getAttribute("skey2"));
647:
648:                // Transparency - addition via web object
649:                session.setAttribute("skeyA", "svalueA");
650:                checkMapSize(map, 2);
651:                assertEquals("svalueA", (String) map.get("skeyA"));
652:
653:                // Transparency - addition via map
654:                map.put("skeyB", "svalueB");
655:                checkMapSize(map, 3);
656:                assertEquals("svalueB", (String) session.getAttribute("skeyB"));
657:
658:                // Transparency - replacement via web object
659:                session.setAttribute("skeyA", "newvalueA");
660:                checkMapSize(map, 3);
661:                assertEquals("newvalueA", (String) map.get("skeyA"));
662:
663:                // Transparency - replacement via map
664:                map.put("skeyB", "newvalueB");
665:                checkMapSize(map, 3);
666:                assertEquals("newvalueB", (String) session
667:                        .getAttribute("skeyB"));
668:
669:                // Clearing the map
670:                map.clear();
671:                checkMapSize(map, 0);
672:
673:            }
674:
675:            // Test getSessionScope() without Session
676:            public void testSessionScopeWithoutSession() {
677:
678:                // Create a Context without a session
679:                ServletWebContext ctx = new ServletWebContext(scontext,
680:                        new MockHttpServletRequest(), response);
681:                assertNull("Session(A)", ctx.getRequest().getSession(false));
682:
683:                // Get the session Map & check session doesn't exist
684:                Map sessionMap = ctx.getSessionScope();
685:                assertNull("Session(B)", ctx.getRequest().getSession(false));
686:                assertNotNull("Session Map(A)", sessionMap);
687:
688:                // test clear()
689:                sessionMap.clear();
690:                assertNull("Session(C)", ctx.getRequest().getSession(false));
691:
692:                // test containsKey()
693:                assertFalse("containsKey()", sessionMap.containsKey("ABC"));
694:                assertNull("Session(D)", ctx.getRequest().getSession(false));
695:
696:                // test containsValue()
697:                assertFalse("containsValue()", sessionMap.containsValue("ABC"));
698:                assertNull("Session(E)", ctx.getRequest().getSession(false));
699:
700:                // test entrySet()
701:                Set entrySet = sessionMap.entrySet();
702:                assertNotNull("entrySet", entrySet);
703:                assertEquals("entrySet Size", 0, entrySet.size());
704:                assertNull("Session(F)", ctx.getRequest().getSession(false));
705:
706:                // test equals()
707:                assertFalse("equals()", sessionMap.equals("ABC"));
708:                assertNull("Session(G)", ctx.getRequest().getSession(false));
709:
710:                // test get()
711:                assertNull("get()", sessionMap.get("ABC"));
712:                assertNull("Session(H)", ctx.getRequest().getSession(false));
713:
714:                // test hashCode()
715:                sessionMap.hashCode();
716:                assertNull("Session(I)", ctx.getRequest().getSession(false));
717:
718:                // test isEmpty()
719:                assertTrue("isEmpty()", sessionMap.isEmpty());
720:                assertNull("Session(J)", ctx.getRequest().getSession(false));
721:
722:                // test keySet()
723:                Set keySet = sessionMap.keySet();
724:                assertNotNull("keySet", keySet);
725:                assertEquals("keySet Size", 0, keySet.size());
726:                assertNull("Session(K)", ctx.getRequest().getSession(false));
727:
728:                // test putAll() with an empty Map
729:                sessionMap.putAll(new HashMap());
730:                assertNull("Session(L)", ctx.getRequest().getSession(false));
731:
732:                // test remove()
733:                assertNull("remove()", sessionMap.remove("ABC"));
734:                assertNull("Session(M)", ctx.getRequest().getSession(false));
735:
736:                // test size()
737:                assertEquals("size() Size", 0, sessionMap.size());
738:                assertNull("Session(N)", ctx.getRequest().getSession(false));
739:
740:                // test values()
741:                Collection values = sessionMap.values();
742:                assertNotNull("values", values);
743:                assertEquals("values Size", 0, values.size());
744:                assertNull("Session(O)", ctx.getRequest().getSession(false));
745:
746:                // test put()
747:                try {
748:                    assertNull("put()", sessionMap.put("ABC", "XYZ"));
749:                    assertNotNull("Session(P)", ctx.getRequest().getSession(
750:                            false));
751:                } catch (UnsupportedOperationException ex) {
752:                    // expected: currently MockHttpServletRequest throws this
753:                    //           when trying to create a HttpSession
754:                }
755:
756:            }
757:
758:            // ------------------------------------------------------- Protected Methods
759:
760:            protected void checkMapSize(Map map, int size) {
761:                // Check reported size of the map
762:                assertEquals(size, map.size());
763:                // Iterate over key set
764:                int nk = 0;
765:                Iterator keys = map.keySet().iterator();
766:                while (keys.hasNext()) {
767:                    keys.next();
768:                    nk++;
769:                }
770:                assertEquals(size, nk);
771:                // Iterate over entry set
772:                int nv = 0;
773:                Iterator values = map.entrySet().iterator();
774:                while (values.hasNext()) {
775:                    values.next();
776:                    nv++;
777:                }
778:                assertEquals(size, nv);
779:                // Count the values
780:                assertEquals(size, map.values().size());
781:            }
782:
783:            // Test to ensure proper entrySet() and are modifiable optionally
784:            protected void checkEntrySet(Map map, boolean modifiable) {
785:                assertTrue(map.size() > 1);
786:                Set entries = map.entrySet();
787:                assertTrue(map.size() == entries.size());
788:                Object o = entries.iterator().next();
789:
790:                assertTrue(o instanceof  Map.Entry);
791:
792:                if (!modifiable) {
793:                    try {
794:                        ((Map.Entry) o).setValue(new Object());
795:                        fail("Should have thrown UnsupportedOperationException");
796:                    } catch (UnsupportedOperationException e) {
797:                        ; // expected result
798:                    }
799:                } else {
800:                    // Should pass and not throw UnsupportedOperationException
801:                    Map.Entry e = (Map.Entry) o;
802:                    e.setValue(e.setValue(new Object()));
803:                }
804:            }
805:
806:            // Create a new instance of the appropriate Context type for this test case
807:            protected Context createContext() {
808:                return (new ServletWebContext(scontext, request, response));
809:            }
810:
811:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.