1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.web.servlet;
17
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import org.apache.commons.chain.Context;
22 import org.apache.commons.chain.impl.ContextBaseTestCase;
23 import org.apache.commons.chain.web.WebContext;
24
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import javax.servlet.http.Cookie;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.Collection;
35
36
37 /***
38 * Extension of <code>ContextBaseTestCase</code> to validate the
39 * extra functionality of this implementation.
40 */
41
42 public class ServletWebContextTestCase extends ContextBaseTestCase {
43
44
45
46
47 /***
48 * Construct a new instance of this test case.
49 *
50 * @param name Name of the test case
51 */
52 public ServletWebContextTestCase(String name) {
53 super(name);
54 }
55
56
57
58
59
60
61 protected ServletContext scontext = null;
62 protected HttpServletRequest request = null;
63 protected HttpServletResponse response = null;
64 protected HttpSession session = null;
65
66
67
68
69
70 /***
71 * Set up instance variables required by this test case.
72 */
73 public void setUp() {
74 scontext = new MockServletContext();
75 scontext.setAttribute("akey1", "avalue1");
76 scontext.setAttribute("akey2", "avalue2");
77 scontext.setAttribute("akey3", "avalue3");
78 scontext.setAttribute("akey4", "avalue4");
79 ((MockServletContext) scontext).addInitParameter("ikey1", "ivalue1");
80 ((MockServletContext) scontext).addInitParameter("ikey2", "ivalue2");
81 ((MockServletContext) scontext).addInitParameter("ikey3", "ivalue3");
82 session = new MockHttpSession(scontext);
83 session.setAttribute("skey1", "svalue1");
84 session.setAttribute("skey2", "svalue2");
85 session.setAttribute("skey3", "svalue3");
86 request = new MockHttpServletRequest("/context", "/servlet",
87 "/path/info", "a=b&c=d",
88 session);
89 request.setAttribute("rkey1", "rvalue1");
90 request.setAttribute("rkey2", "rvalue2");
91 ((MockHttpServletRequest) request).addHeader("hkey1", "hvalue1");
92 ((MockHttpServletRequest) request).addHeader("hkey2", "hvalue2a");
93 ((MockHttpServletRequest) request).addHeader("hkey2", "hvalue2b");
94 ((MockHttpServletRequest) request).addParameter("pkey1", "pvalue1");
95 ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2a");
96 ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2b");
97 ((MockHttpServletRequest) request).addCookie("ckey1", "cvalue1");
98 ((MockHttpServletRequest) request).addCookie("ckey2", "cvalue2");
99 response = new MockHttpServletResponse();
100 context = createContext();
101 }
102
103
104 /***
105 * Return the tests included in this test suite.
106 */
107 public static Test suite() {
108 return (new TestSuite(ServletWebContextTestCase.class));
109 }
110
111
112 /***
113 * Tear down instance variables required by this test case.
114 */
115 public void tearDown() {
116 scontext = null;
117 session = null;
118 request = null;
119 response = null;
120 context = null;
121 }
122
123
124
125
126
127
128 public void testApplicationScope() {
129
130 Map map = ((WebContext) context).getApplicationScope();
131 assertNotNull(map);
132
133
134 checkMapSize(map, 4);
135 assertEquals("avalue1", (String) map.get("akey1"));
136 assertEquals("avalue2", (String) map.get("akey2"));
137 assertEquals("avalue3", (String) map.get("akey3"));
138 assertEquals("avalue4", (String) map.get("akey4"));
139
140
141 checkEntrySet(map, true);
142
143
144 scontext.removeAttribute("akey1");
145 checkMapSize(map, 3);
146 assertNull(map.get("akey1"));
147
148
149 map.remove("akey2");
150 checkMapSize(map, 2);
151 assertNull(scontext.getAttribute("akey2"));
152
153
154 scontext.setAttribute("akeyA", "avalueA");
155 checkMapSize(map, 3);
156 assertEquals("avalueA", (String) map.get("akeyA"));
157
158
159 map.put("akeyB", "avalueB");
160 checkMapSize(map, 4);
161 assertEquals("avalueB", (String) scontext.getAttribute("akeyB"));
162
163
164 scontext.setAttribute("akeyA", "newvalueA");
165 checkMapSize(map, 4);
166 assertEquals("newvalueA", (String) map.get("akeyA"));
167
168
169 map.put("akeyB", "newvalueB");
170 assertEquals(4, map.size());
171 assertEquals("newvalueB", (String) scontext.getAttribute("akeyB"));
172
173
174 map.clear();
175 checkMapSize(map, 0);
176
177 }
178
179
180
181
182 public void testEquals() {
183
184
185
186
187 assertTrue(context.equals(context));
188 assertTrue(context.hashCode() == context.hashCode());
189
190
191 Context other = new ServletWebContext(scontext, request, response);
192
193 assertTrue(context.hashCode() == other.hashCode());
194
195
196 other.put("bop", "bop value");
197
198 assertTrue(context.hashCode() != other.hashCode());
199
200
201 other = new ServletWebContext(scontext, request, response);
202 context.put("bop", "bop value");
203
204 assertTrue(context.hashCode() != other.hashCode());
205
206 }
207
208
209
210 public void testHeader() {
211
212 Map map = ((WebContext) context).getHeader();
213 assertNotNull(map);
214
215
216 checkMapSize(map, 2);
217 assertEquals("hvalue1", (String) map.get("hkey1"));
218 assertEquals("hvalue2a", (String) map.get("hkey2"));
219 assertTrue(map.containsKey("hkey1"));
220 assertTrue(map.containsKey("hkey2"));
221 assertTrue(map.containsValue("hvalue1"));
222 assertTrue(map.containsValue("hvalue2a"));
223
224
225 checkEntrySet(map, false);
226
227
228 try {
229 map.clear();
230 fail("Should have thrown UnsupportedOperationException");
231 } catch (UnsupportedOperationException e) {
232 ;
233 }
234 try {
235 map.put("hkey3", "hvalue3");
236 fail("Should have thrown UnsupportedOperationException");
237 } catch (UnsupportedOperationException e) {
238 ;
239 }
240 try {
241 map.putAll(new HashMap());
242 fail("Should have thrown UnsupportedOperationException");
243 } catch (UnsupportedOperationException e) {
244 ;
245 }
246 try {
247 map.remove("hkey1");
248 fail("Should have thrown UnsupportedOperationException");
249 } catch (UnsupportedOperationException e) {
250 ;
251 }
252
253 }
254
255
256
257 public void testHeaderValues() {
258
259 Map map = ((WebContext) context).getHeaderValues();
260 assertNotNull(map);
261
262
263 checkMapSize(map, 2);
264 Object value1 = map.get("hkey1");
265 assertNotNull(value1);
266 assertTrue(value1 instanceof String[]);
267 String values1[] = (String[]) value1;
268 assertEquals(1, values1.length);
269 assertEquals("hvalue1", values1[0]);
270 Object value2 = map.get("hkey2");
271 assertNotNull(value2);
272 assertTrue(value2 instanceof String[]);
273 String values2[] = (String[]) value2;
274 assertEquals(2, values2.length);
275 assertEquals("hvalue2a", values2[0]);
276 assertEquals("hvalue2b", values2[1]);
277 assertTrue(map.containsKey("hkey1"));
278 assertTrue(map.containsKey("hkey2"));
279 assertTrue(map.containsValue(values1));
280 assertTrue(map.containsValue(values2));
281
282
283 checkEntrySet(map, false);
284
285
286 try {
287 map.clear();
288 fail("Should have thrown UnsupportedOperationException");
289 } catch (UnsupportedOperationException e) {
290 ;
291 }
292 try {
293 map.put("hkey3", values2);
294 fail("Should have thrown UnsupportedOperationException");
295 } catch (UnsupportedOperationException e) {
296 ;
297 }
298 try {
299 map.putAll(new HashMap());
300 fail("Should have thrown UnsupportedOperationException");
301 } catch (UnsupportedOperationException e) {
302 ;
303 }
304 try {
305 map.remove("hkey1");
306 fail("Should have thrown UnsupportedOperationException");
307 } catch (UnsupportedOperationException e) {
308 ;
309 }
310
311 }
312
313
314
315 public void testInitParam() {
316
317 Map map = ((WebContext) context).getInitParam();
318 assertNotNull(map);
319
320
321 checkMapSize(map, 3);
322 assertEquals("ivalue1", (String) map.get("ikey1"));
323 assertEquals("ivalue2", (String) map.get("ikey2"));
324 assertEquals("ivalue3", (String) map.get("ikey3"));
325 assertTrue(map.containsKey("ikey1"));
326 assertTrue(map.containsKey("ikey2"));
327 assertTrue(map.containsKey("ikey3"));
328 assertTrue(map.containsValue("ivalue1"));
329 assertTrue(map.containsValue("ivalue2"));
330 assertTrue(map.containsValue("ivalue3"));
331
332
333 checkEntrySet(map, false);
334
335
336 try {
337 map.clear();
338 fail("Should have thrown UnsupportedOperationException");
339 } catch (UnsupportedOperationException e) {
340 ;
341 }
342 try {
343 map.put("ikey4", "ivalue4");
344 fail("Should have thrown UnsupportedOperationException");
345 } catch (UnsupportedOperationException e) {
346 ;
347 }
348 try {
349 map.putAll(new HashMap());
350 fail("Should have thrown UnsupportedOperationException");
351 } catch (UnsupportedOperationException e) {
352 ;
353 }
354 try {
355 map.remove("ikey1");
356 fail("Should have thrown UnsupportedOperationException");
357 } catch (UnsupportedOperationException e) {
358 ;
359 }
360
361 }
362
363
364
365 public void testParam() {
366
367 Map map = ((WebContext) context).getParam();
368 assertNotNull(map);
369
370
371 checkMapSize(map, 2);
372 assertEquals("pvalue1", (String) map.get("pkey1"));
373 assertEquals("pvalue2a", (String) map.get("pkey2"));
374 assertTrue(map.containsKey("pkey1"));
375 assertTrue(map.containsKey("pkey2"));
376 assertTrue(map.containsValue("pvalue1"));
377 assertTrue(map.containsValue("pvalue2a"));
378
379 checkEntrySet(map, false);
380
381
382 try {
383 map.clear();
384 fail("Should have thrown UnsupportedOperationException");
385 } catch (UnsupportedOperationException e) {
386 ;
387 }
388 try {
389 map.put("pkey3", "pvalue3");
390 fail("Should have thrown UnsupportedOperationException");
391 } catch (UnsupportedOperationException e) {
392 ;
393 }
394 try {
395 map.putAll(new HashMap());
396 fail("Should have thrown UnsupportedOperationException");
397 } catch (UnsupportedOperationException e) {
398 ;
399 }
400 try {
401 map.remove("pkey1");
402 fail("Should have thrown UnsupportedOperationException");
403 } catch (UnsupportedOperationException e) {
404 ;
405 }
406
407 }
408
409
410
411 public void testParamValues() {
412
413 Map map = ((WebContext) context).getParamValues();
414 assertNotNull(map);
415
416
417 checkMapSize(map, 2);
418 Object value1 = map.get("pkey1");
419 assertNotNull(value1);
420 assertTrue(value1 instanceof String[]);
421 String values1[] = (String[]) value1;
422 assertEquals(1, values1.length);
423 assertEquals("pvalue1", values1[0]);
424 Object value2 = map.get("pkey2");
425 assertNotNull(value2);
426 assertTrue(value2 instanceof String[]);
427 String values2[] = (String[]) value2;
428 assertEquals(2, values2.length);
429 assertEquals("pvalue2a", values2[0]);
430 assertEquals("pvalue2b", values2[1]);
431 assertTrue(map.containsKey("pkey1"));
432 assertTrue(map.containsKey("pkey2"));
433 assertTrue(map.containsValue(values1));
434 assertTrue(map.containsValue(values2));
435
436
437 try {
438 map.clear();
439 fail("Should have thrown UnsupportedOperationException");
440 } catch (UnsupportedOperationException e) {
441 ;
442 }
443 try {
444 map.put("pkey3", values2);
445 fail("Should have thrown UnsupportedOperationException");
446 } catch (UnsupportedOperationException e) {
447 ;
448 }
449 try {
450 map.putAll(new HashMap());
451 fail("Should have thrown UnsupportedOperationException");
452 } catch (UnsupportedOperationException e) {
453 ;
454 }
455 try {
456 map.remove("pkey1");
457 fail("Should have thrown UnsupportedOperationException");
458 } catch (UnsupportedOperationException e) {
459 ;
460 }
461
462 }
463
464
465
466 public void testCookies() {
467
468 Map map = ((WebContext) context).getCookies();
469 assertNotNull(map);
470
471
472 checkMapSize(map, 2);
473 Cookie cookie1 = (Cookie)map.get("ckey1");
474 assertNotNull(cookie1);
475 assertEquals("cvalue1", cookie1.getValue());
476 Cookie cookie2 = (Cookie)map.get("ckey2");
477 assertNotNull(cookie2);
478 assertEquals("cvalue2", cookie2.getValue());
479 assertTrue(map.containsKey("ckey1"));
480 assertTrue(map.containsKey("ckey2"));
481 assertTrue(map.containsValue(cookie1));
482 assertTrue(map.containsValue(cookie2));
483
484
485 try {
486 map.clear();
487 fail("Should have thrown UnsupportedOperationException");
488 } catch (UnsupportedOperationException e) {
489 ;
490 }
491 try {
492 map.put("ckey3", "XXX");
493 fail("Should have thrown UnsupportedOperationException");
494 } catch (UnsupportedOperationException e) {
495 ;
496 }
497 try {
498 map.putAll(new HashMap());
499 fail("Should have thrown UnsupportedOperationException");
500 } catch (UnsupportedOperationException e) {
501 ;
502 }
503 try {
504 map.remove("ckey1");
505 fail("Should have thrown UnsupportedOperationException");
506 } catch (UnsupportedOperationException e) {
507 ;
508 }
509
510 }
511
512
513 public void testPristine() {
514
515 super.testPristine();
516 ServletWebContext swcontext = (ServletWebContext) context;
517
518
519 assertNotNull(swcontext.getApplicationScope());
520 assertNotNull(swcontext.getHeader());
521 assertNotNull(swcontext.getHeaderValues());
522 assertNotNull(swcontext.getInitParam());
523 assertNotNull(swcontext.getParam());
524 assertNotNull(swcontext.getParamValues());
525 assertNotNull(swcontext.getCookies());
526 assertNotNull(swcontext.getRequestScope());
527 assertNotNull(swcontext.getSessionScope());
528
529
530 assertTrue(swcontext.getApplicationScope() ==
531 swcontext.get("applicationScope"));
532 assertTrue(swcontext.getHeader() ==
533 swcontext.get("header"));
534 assertTrue(swcontext.getHeaderValues() ==
535 swcontext.get("headerValues"));
536 assertTrue(swcontext.getInitParam() ==
537 swcontext.get("initParam"));
538 assertTrue(swcontext.getParam() ==
539 swcontext.get("param"));
540 assertTrue(swcontext.getParamValues() ==
541 swcontext.get("paramValues"));
542 assertTrue(swcontext.getCookies() ==
543 swcontext.get("cookies"));
544 assertTrue(swcontext.getRequestScope() ==
545 swcontext.get("requestScope"));
546 assertTrue(swcontext.getSessionScope() ==
547 swcontext.get("sessionScope"));
548
549 }
550
551
552
553 public void testRelease() {
554
555 ServletWebContext swcontext = (ServletWebContext) context;
556 swcontext.release();
557
558
559 assertNull(swcontext.getApplicationScope());
560 assertNull(swcontext.getHeader());
561 assertNull(swcontext.getHeaderValues());
562 assertNull(swcontext.getInitParam());
563 assertNull(swcontext.getParam());
564 assertNull(swcontext.getParamValues());
565 assertNull(swcontext.getCookies());
566 assertNull(swcontext.getRequestScope());
567 assertNull(swcontext.getSessionScope());
568
569
570 assertNull(swcontext.get("applicationScope"));
571 assertNull(swcontext.get("header"));
572 assertNull(swcontext.get("headerValues"));
573 assertNull(swcontext.get("initParam"));
574 assertNull(swcontext.get("param"));
575 assertNull(swcontext.get("paramValues"));
576 assertNull(swcontext.get("cookies"));
577 assertNull(swcontext.get("requestScope"));
578 assertNull(swcontext.get("sessionScope"));
579
580 }
581
582
583
584 public void testRequestScope() {
585
586 Map map = ((WebContext) context).getRequestScope();
587 assertNotNull(map);
588
589
590 checkMapSize(map, 2);
591 assertEquals("rvalue1", (String) map.get("rkey1"));
592 assertEquals("rvalue2", (String) map.get("rkey2"));
593
594
595 checkEntrySet(map, true);
596
597
598 request.removeAttribute("rkey1");
599 checkMapSize(map, 1);
600 assertNull(map.get("rkey1"));
601
602
603 map.remove("rkey2");
604 checkMapSize(map, 0);
605 assertNull(request.getAttribute("rkey2"));
606
607
608 request.setAttribute("rkeyA", "rvalueA");
609 checkMapSize(map, 1);
610 assertEquals("rvalueA", (String) map.get("rkeyA"));
611
612
613 map.put("rkeyB", "rvalueB");
614 checkMapSize(map, 2);
615 assertEquals("rvalueB", (String) request.getAttribute("rkeyB"));
616
617
618 request.setAttribute("rkeyA", "newvalueA");
619 checkMapSize(map, 2);
620 assertEquals("newvalueA", (String) map.get("rkeyA"));
621
622
623 map.put("rkeyB", "newvalueB");
624 checkMapSize(map, 2);
625 assertEquals("newvalueB", (String) request.getAttribute("rkeyB"));
626
627
628 map.clear();
629 checkMapSize(map, 0);
630
631 }
632
633
634
635 public void testSessionScope() {
636
637 Map map = ((WebContext) context).getSessionScope();
638 assertNotNull(map);
639
640
641 checkMapSize(map, 3);
642 assertEquals("svalue1", (String) map.get("skey1"));
643 assertEquals("svalue2", (String) map.get("skey2"));
644 assertEquals("svalue3", (String) map.get("skey3"));
645
646
647 checkEntrySet(map, true);
648
649
650 session.removeAttribute("skey1");
651 checkMapSize(map, 2);
652 assertNull(map.get("skey1"));
653
654
655 map.remove("skey2");
656 checkMapSize(map, 1);
657 assertNull(session.getAttribute("skey2"));
658
659
660 session.setAttribute("skeyA", "svalueA");
661 checkMapSize(map, 2);
662 assertEquals("svalueA", (String) map.get("skeyA"));
663
664
665 map.put("skeyB", "svalueB");
666 checkMapSize(map, 3);
667 assertEquals("svalueB", (String) session.getAttribute("skeyB"));
668
669
670 session.setAttribute("skeyA", "newvalueA");
671 checkMapSize(map, 3);
672 assertEquals("newvalueA", (String) map.get("skeyA"));
673
674
675 map.put("skeyB", "newvalueB");
676 checkMapSize(map, 3);
677 assertEquals("newvalueB", (String) session.getAttribute("skeyB"));
678
679
680 map.clear();
681 checkMapSize(map, 0);
682
683 }
684
685
686
687 public void testSessionScopeWithoutSession() {
688
689
690 ServletWebContext ctx = new ServletWebContext(scontext,
691 new MockHttpServletRequest(), response);
692 assertNull("Session(A)", ctx.getRequest().getSession(false));
693
694
695 Map sessionMap = ctx.getSessionScope();
696 assertNull("Session(B)", ctx.getRequest().getSession(false));
697 assertNotNull("Session Map(A)", sessionMap);
698
699
700 sessionMap.clear();
701 assertNull("Session(C)", ctx.getRequest().getSession(false));
702
703
704 assertFalse("containsKey()", sessionMap.containsKey("ABC"));
705 assertNull("Session(D)", ctx.getRequest().getSession(false));
706
707
708 assertFalse("containsValue()", sessionMap.containsValue("ABC"));
709 assertNull("Session(E)", ctx.getRequest().getSession(false));
710
711
712 Set entrySet = sessionMap.entrySet();
713 assertNotNull("entrySet", entrySet);
714 assertEquals("entrySet Size", 0, entrySet.size());
715 assertNull("Session(F)", ctx.getRequest().getSession(false));
716
717
718 assertFalse("equals()", sessionMap.equals("ABC"));
719 assertNull("Session(G)", ctx.getRequest().getSession(false));
720
721
722 assertNull("get()", sessionMap.get("ABC"));
723 assertNull("Session(H)", ctx.getRequest().getSession(false));
724
725
726 sessionMap.hashCode();
727 assertNull("Session(I)", ctx.getRequest().getSession(false));
728
729
730 assertTrue("isEmpty()", sessionMap.isEmpty());
731 assertNull("Session(J)", ctx.getRequest().getSession(false));
732
733
734 Set keySet = sessionMap.keySet();
735 assertNotNull("keySet", keySet);
736 assertEquals("keySet Size", 0, keySet.size());
737 assertNull("Session(K)", ctx.getRequest().getSession(false));
738
739
740 sessionMap.putAll(new HashMap());
741 assertNull("Session(L)", ctx.getRequest().getSession(false));
742
743
744 assertNull("remove()", sessionMap.remove("ABC"));
745 assertNull("Session(M)", ctx.getRequest().getSession(false));
746
747
748 assertEquals("size() Size", 0, sessionMap.size());
749 assertNull("Session(N)", ctx.getRequest().getSession(false));
750
751
752 Collection values = sessionMap.values();
753 assertNotNull("values", values);
754 assertEquals("values Size", 0, values.size());
755 assertNull("Session(O)", ctx.getRequest().getSession(false));
756
757
758 try {
759 assertNull("put()", sessionMap.put("ABC", "XYZ"));
760 assertNotNull("Session(P)", ctx.getRequest().getSession(false));
761 } catch(UnsupportedOperationException ex) {
762
763
764 }
765
766 }
767
768
769
770
771
772 protected void checkMapSize(Map map, int size) {
773
774 assertEquals(size, map.size());
775
776 int nk = 0;
777 Iterator keys = map.keySet().iterator();
778 while (keys.hasNext()) {
779 keys.next();
780 nk++;
781 }
782 assertEquals(size, nk);
783
784 int nv = 0;
785 Iterator values = map.entrySet().iterator();
786 while (values.hasNext()) {
787 values.next();
788 nv++;
789 }
790 assertEquals(size, nv);
791
792 assertEquals(size, map.values().size());
793 }
794
795
796 protected void checkEntrySet(Map map, boolean modifiable) {
797 assertTrue(map.size() > 1);
798 Set entries = map.entrySet();
799 assertTrue(map.size() == entries.size());
800 Object o = entries.iterator().next();
801
802 assertTrue(o instanceof Map.Entry);
803
804 if (!modifiable) {
805 try {
806 ((Map.Entry)o).setValue(new Object());
807 fail("Should have thrown UnsupportedOperationException");
808 } catch (UnsupportedOperationException e) {
809 ;
810 }
811 } else {
812
813 Map.Entry e = (Map.Entry)o;
814 e.setValue(e.setValue(new Object()));
815 }
816 }
817
818
819 protected Context createContext() {
820 return (new ServletWebContext(scontext, request, response));
821 }
822
823
824 }