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