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