1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.chain.web.servlet;
18
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22 import org.apache.commons.chain.Context;
23 import org.apache.commons.chain.impl.ContextBaseTestCase;
24 import org.apache.commons.chain.web.WebContext;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30 import javax.servlet.http.Cookie;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.Collection;
36
37
38
39
40
41
42
43 public class ServletWebContextTestCase extends ContextBaseTestCase {
44
45
46
47
48
49
50
51
52
53 public ServletWebContextTestCase(String name) {
54 super(name);
55 }
56
57
58
59
60
61
62 protected ServletContext scontext = null;
63 protected HttpServletRequest request = null;
64 protected HttpServletResponse response = null;
65 protected HttpSession session = null;
66
67
68
69
70
71
72
73
74 public void setUp() {
75 scontext = new MockServletContext();
76 scontext.setAttribute("akey1", "avalue1");
77 scontext.setAttribute("akey2", "avalue2");
78 scontext.setAttribute("akey3", "avalue3");
79 scontext.setAttribute("akey4", "avalue4");
80 ((MockServletContext) scontext).addInitParameter("ikey1", "ivalue1");
81 ((MockServletContext) scontext).addInitParameter("ikey2", "ivalue2");
82 ((MockServletContext) scontext).addInitParameter("ikey3", "ivalue3");
83 session = new MockHttpSession(scontext);
84 session.setAttribute("skey1", "svalue1");
85 session.setAttribute("skey2", "svalue2");
86 session.setAttribute("skey3", "svalue3");
87 request = new MockHttpServletRequest("/context", "/servlet",
88 "/path/info", "a=b&c=d",
89 session);
90 request.setAttribute("rkey1", "rvalue1");
91 request.setAttribute("rkey2", "rvalue2");
92 ((MockHttpServletRequest) request).addHeader("hkey1", "hvalue1");
93 ((MockHttpServletRequest) request).addHeader("hkey2", "hvalue2a");
94 ((MockHttpServletRequest) request).addHeader("hkey2", "hvalue2b");
95 ((MockHttpServletRequest) request).addParameter("pkey1", "pvalue1");
96 ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2a");
97 ((MockHttpServletRequest) request).addParameter("pkey2", "pvalue2b");
98 ((MockHttpServletRequest) request).addCookie("ckey1", "cvalue1");
99 ((MockHttpServletRequest) request).addCookie("ckey2", "cvalue2");
100 response = new MockHttpServletResponse();
101 context = createContext();
102 }
103
104
105
106
107
108 public static Test suite() {
109 return (new TestSuite(ServletWebContextTestCase.class));
110 }
111
112
113
114
115
116 public void tearDown() {
117 scontext = null;
118 session = null;
119 request = null;
120 response = null;
121 context = null;
122 }
123
124
125
126
127
128
129 public void testApplicationScope() {
130
131 Map map = ((WebContext) context).getApplicationScope();
132 assertNotNull(map);
133
134
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
142 checkEntrySet(map, true);
143
144
145 scontext.removeAttribute("akey1");
146 checkMapSize(map, 3);
147 assertNull(map.get("akey1"));
148
149
150 map.remove("akey2");
151 checkMapSize(map, 2);
152 assertNull(scontext.getAttribute("akey2"));
153
154
155 scontext.setAttribute("akeyA", "avalueA");
156 checkMapSize(map, 3);
157 assertEquals("avalueA", (String) map.get("akeyA"));
158
159
160 map.put("akeyB", "avalueB");
161 checkMapSize(map, 4);
162 assertEquals("avalueB", (String) scontext.getAttribute("akeyB"));
163
164
165 scontext.setAttribute("akeyA", "newvalueA");
166 checkMapSize(map, 4);
167 assertEquals("newvalueA", (String) map.get("akeyA"));
168
169
170 map.put("akeyB", "newvalueB");
171 assertEquals(4, map.size());
172 assertEquals("newvalueB", (String) scontext.getAttribute("akeyB"));
173
174
175 map.clear();
176 checkMapSize(map, 0);
177
178
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
190
191 public void testEquals() {
192
193
194
195
196 assertTrue(context.equals(context));
197 assertTrue(context.hashCode() == context.hashCode());
198
199
200 Context other = new ServletWebContext(scontext, request, response);
201
202 assertTrue(context.hashCode() == other.hashCode());
203
204
205 other.put("bop", "bop value");
206
207 assertTrue(context.hashCode() != other.hashCode());
208
209
210 other = new ServletWebContext(scontext, request, response);
211 context.put("bop", "bop value");
212
213 assertTrue(context.hashCode() != other.hashCode());
214
215 }
216
217
218
219 public void testHeader() {
220
221 Map map = ((WebContext) context).getHeader();
222 assertNotNull(map);
223
224
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
234 checkEntrySet(map, false);
235
236
237 try {
238 map.clear();
239 fail("Should have thrown UnsupportedOperationException");
240 } catch (UnsupportedOperationException e) {
241 ;
242 }
243 try {
244 map.put("hkey3", "hvalue3");
245 fail("Should have thrown UnsupportedOperationException");
246 } catch (UnsupportedOperationException e) {
247 ;
248 }
249 try {
250 map.putAll(new HashMap());
251 fail("Should have thrown UnsupportedOperationException");
252 } catch (UnsupportedOperationException e) {
253 ;
254 }
255 try {
256 map.remove("hkey1");
257 fail("Should have thrown UnsupportedOperationException");
258 } catch (UnsupportedOperationException e) {
259 ;
260 }
261
262 }
263
264
265
266 public void testHeaderValues() {
267
268 Map map = ((WebContext) context).getHeaderValues();
269 assertNotNull(map);
270
271
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
292 checkEntrySet(map, false);
293
294
295 try {
296 map.clear();
297 fail("Should have thrown UnsupportedOperationException");
298 } catch (UnsupportedOperationException e) {
299 ;
300 }
301 try {
302 map.put("hkey3", values2);
303 fail("Should have thrown UnsupportedOperationException");
304 } catch (UnsupportedOperationException e) {
305 ;
306 }
307 try {
308 map.putAll(new HashMap());
309 fail("Should have thrown UnsupportedOperationException");
310 } catch (UnsupportedOperationException e) {
311 ;
312 }
313 try {
314 map.remove("hkey1");
315 fail("Should have thrown UnsupportedOperationException");
316 } catch (UnsupportedOperationException e) {
317 ;
318 }
319
320 }
321
322
323
324 public void testInitParam() {
325
326 Map map = ((WebContext) context).getInitParam();
327 assertNotNull(map);
328
329
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
342 checkEntrySet(map, false);
343
344
345 try {
346 map.clear();
347 fail("Should have thrown UnsupportedOperationException");
348 } catch (UnsupportedOperationException e) {
349 ;
350 }
351 try {
352 map.put("ikey4", "ivalue4");
353 fail("Should have thrown UnsupportedOperationException");
354 } catch (UnsupportedOperationException e) {
355 ;
356 }
357 try {
358 map.putAll(new HashMap());
359 fail("Should have thrown UnsupportedOperationException");
360 } catch (UnsupportedOperationException e) {
361 ;
362 }
363 try {
364 map.remove("ikey1");
365 fail("Should have thrown UnsupportedOperationException");
366 } catch (UnsupportedOperationException e) {
367 ;
368 }
369
370 }
371
372
373
374 public void testParam() {
375
376 Map map = ((WebContext) context).getParam();
377 assertNotNull(map);
378
379
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
391 try {
392 map.clear();
393 fail("Should have thrown UnsupportedOperationException");
394 } catch (UnsupportedOperationException e) {
395 ;
396 }
397 try {
398 map.put("pkey3", "pvalue3");
399 fail("Should have thrown UnsupportedOperationException");
400 } catch (UnsupportedOperationException e) {
401 ;
402 }
403 try {
404 map.putAll(new HashMap());
405 fail("Should have thrown UnsupportedOperationException");
406 } catch (UnsupportedOperationException e) {
407 ;
408 }
409 try {
410 map.remove("pkey1");
411 fail("Should have thrown UnsupportedOperationException");
412 } catch (UnsupportedOperationException e) {
413 ;
414 }
415
416 }
417
418
419
420 public void testParamValues() {
421
422 Map map = ((WebContext) context).getParamValues();
423 assertNotNull(map);
424
425
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
446 try {
447 map.clear();
448 fail("Should have thrown UnsupportedOperationException");
449 } catch (UnsupportedOperationException e) {
450 ;
451 }
452 try {
453 map.put("pkey3", values2);
454 fail("Should have thrown UnsupportedOperationException");
455 } catch (UnsupportedOperationException e) {
456 ;
457 }
458 try {
459 map.putAll(new HashMap());
460 fail("Should have thrown UnsupportedOperationException");
461 } catch (UnsupportedOperationException e) {
462 ;
463 }
464 try {
465 map.remove("pkey1");
466 fail("Should have thrown UnsupportedOperationException");
467 } catch (UnsupportedOperationException e) {
468 ;
469 }
470
471 }
472
473
474
475 public void testCookies() {
476
477 Map map = ((WebContext) context).getCookies();
478 assertNotNull(map);
479
480
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
494 try {
495 map.clear();
496 fail("Should have thrown UnsupportedOperationException");
497 } catch (UnsupportedOperationException e) {
498 ;
499 }
500 try {
501 map.put("ckey3", "XXX");
502 fail("Should have thrown UnsupportedOperationException");
503 } catch (UnsupportedOperationException e) {
504 ;
505 }
506 try {
507 map.putAll(new HashMap());
508 fail("Should have thrown UnsupportedOperationException");
509 } catch (UnsupportedOperationException e) {
510 ;
511 }
512 try {
513 map.remove("ckey1");
514 fail("Should have thrown UnsupportedOperationException");
515 } catch (UnsupportedOperationException e) {
516 ;
517 }
518
519 }
520
521
522 public void testPristine() {
523
524 super.testPristine();
525 ServletWebContext swcontext = (ServletWebContext) context;
526
527
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
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
562 public void testRelease() {
563
564 ServletWebContext swcontext = (ServletWebContext) context;
565 swcontext.release();
566
567
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
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
593 public void testRequestScope() {
594
595 Map map = ((WebContext) context).getRequestScope();
596 assertNotNull(map);
597
598
599 checkMapSize(map, 2);
600 assertEquals("rvalue1", (String) map.get("rkey1"));
601 assertEquals("rvalue2", (String) map.get("rkey2"));
602
603
604 checkEntrySet(map, true);
605
606
607 request.removeAttribute("rkey1");
608 checkMapSize(map, 1);
609 assertNull(map.get("rkey1"));
610
611
612 map.remove("rkey2");
613 checkMapSize(map, 0);
614 assertNull(request.getAttribute("rkey2"));
615
616
617 request.setAttribute("rkeyA", "rvalueA");
618 checkMapSize(map, 1);
619 assertEquals("rvalueA", (String) map.get("rkeyA"));
620
621
622 map.put("rkeyB", "rvalueB");
623 checkMapSize(map, 2);
624 assertEquals("rvalueB", (String) request.getAttribute("rkeyB"));
625
626
627 request.setAttribute("rkeyA", "newvalueA");
628 checkMapSize(map, 2);
629 assertEquals("newvalueA", (String) map.get("rkeyA"));
630
631
632 map.put("rkeyB", "newvalueB");
633 checkMapSize(map, 2);
634 assertEquals("newvalueB", (String) request.getAttribute("rkeyB"));
635
636
637 map.clear();
638 checkMapSize(map, 0);
639
640
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
653 public void testSessionScope() {
654
655 Map map = ((WebContext) context).getSessionScope();
656 assertNotNull(map);
657
658
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
665 checkEntrySet(map, true);
666
667
668 session.removeAttribute("skey1");
669 checkMapSize(map, 2);
670 assertNull(map.get("skey1"));
671
672
673 map.remove("skey2");
674 checkMapSize(map, 1);
675 assertNull(session.getAttribute("skey2"));
676
677
678 session.setAttribute("skeyA", "svalueA");
679 checkMapSize(map, 2);
680 assertEquals("svalueA", (String) map.get("skeyA"));
681
682
683 map.put("skeyB", "svalueB");
684 checkMapSize(map, 3);
685 assertEquals("svalueB", (String) session.getAttribute("skeyB"));
686
687
688 session.setAttribute("skeyA", "newvalueA");
689 checkMapSize(map, 3);
690 assertEquals("newvalueA", (String) map.get("skeyA"));
691
692
693 map.put("skeyB", "newvalueB");
694 checkMapSize(map, 3);
695 assertEquals("newvalueB", (String) session.getAttribute("skeyB"));
696
697
698 map.clear();
699 checkMapSize(map, 0);
700
701
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
714 public void testSessionScopeWithoutSession() {
715
716
717 ServletWebContext ctx = new ServletWebContext(scontext,
718 new MockHttpServletRequest(), response);
719 assertNull("Session(A)", ctx.getRequest().getSession(false));
720
721
722 Map sessionMap = ctx.getSessionScope();
723 assertNull("Session(B)", ctx.getRequest().getSession(false));
724 assertNotNull("Session Map(A)", sessionMap);
725
726
727 sessionMap.clear();
728 assertNull("Session(C)", ctx.getRequest().getSession(false));
729
730
731 assertFalse("containsKey()", sessionMap.containsKey("ABC"));
732 assertNull("Session(D)", ctx.getRequest().getSession(false));
733
734
735 assertFalse("containsValue()", sessionMap.containsValue("ABC"));
736 assertNull("Session(E)", ctx.getRequest().getSession(false));
737
738
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
745 assertFalse("equals()", sessionMap.equals("ABC"));
746 assertNull("Session(G)", ctx.getRequest().getSession(false));
747
748
749 assertNull("get()", sessionMap.get("ABC"));
750 assertNull("Session(H)", ctx.getRequest().getSession(false));
751
752
753 sessionMap.hashCode();
754 assertNull("Session(I)", ctx.getRequest().getSession(false));
755
756
757 assertTrue("isEmpty()", sessionMap.isEmpty());
758 assertNull("Session(J)", ctx.getRequest().getSession(false));
759
760
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
767 sessionMap.putAll(new HashMap());
768 assertNull("Session(L)", ctx.getRequest().getSession(false));
769
770
771 assertNull("remove()", sessionMap.remove("ABC"));
772 assertNull("Session(M)", ctx.getRequest().getSession(false));
773
774
775 assertEquals("size() Size", 0, sessionMap.size());
776 assertNull("Session(N)", ctx.getRequest().getSession(false));
777
778
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
785 try {
786 assertNull("put()", sessionMap.put("ABC", "XYZ"));
787 assertNotNull("Session(P)", ctx.getRequest().getSession(false));
788 } catch(UnsupportedOperationException ex) {
789
790
791 }
792
793 }
794
795
796
797
798
799 protected void checkMapSize(Map map, int size) {
800
801 assertEquals(size, map.size());
802
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
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
819 assertEquals(size, map.values().size());
820 }
821
822
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 ;
837 }
838 } else {
839
840 Map.Entry e = (Map.Entry)o;
841 e.setValue(e.setValue(new Object()));
842 }
843 }
844
845
846 protected Context createContext() {
847 return (new ServletWebContext(scontext, request, response));
848 }
849
850
851 }