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