001: /*
002: * Copyright 2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.portlet;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020: import org.apache.commons.chain.Context;
021: import org.apache.commons.chain.impl.ContextBaseTestCase;
022: import org.apache.commons.chain.web.WebContext;
023:
024: import javax.portlet.PortletContext;
025: import javax.portlet.PortletRequest;
026: import javax.portlet.PortletResponse;
027: import javax.portlet.PortletSession;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Set;
032: import java.util.Collection;
033:
034: /**
035: * Extension of <code>ContextBaseTestCase</code> to validate the
036: * extra functionality of this implementation.
037: */
038:
039: public class PortletWebContextTestCase extends ContextBaseTestCase {
040:
041: // ---------------------------------------------------------- Constructors
042:
043: /**
044: * Construct a new instance of this test case.
045: *
046: * @param name Name of the test case
047: */
048: public PortletWebContextTestCase(String name) {
049: super (name);
050: }
051:
052: // ----------------------------------------------------- Instance Variables
053:
054: // Portlet API Objects
055: protected PortletContext pcontext = null;
056: protected PortletRequest request = null;
057: protected PortletResponse response = null;
058: protected PortletSession session = null;
059:
060: // -------------------------------------------------- Overall Test Methods
061:
062: /**
063: * Set up instance variables required by this test case.
064: */
065: public void setUp() {
066: pcontext = new MockPortletContext();
067: pcontext.setAttribute("akey1", "avalue1");
068: pcontext.setAttribute("akey2", "avalue2");
069: pcontext.setAttribute("akey3", "avalue3");
070: pcontext.setAttribute("akey4", "avalue4");
071: ((MockPortletContext) pcontext).addInitParameter("ikey1",
072: "ivalue1");
073: ((MockPortletContext) pcontext).addInitParameter("ikey2",
074: "ivalue2");
075: ((MockPortletContext) pcontext).addInitParameter("ikey3",
076: "ivalue3");
077: session = new MockPortletSession(pcontext);
078: session.setAttribute("skey1", "svalue1");
079: session.setAttribute("skey2", "svalue2");
080: session.setAttribute("skey3", "svalue3");
081: request = new MockPortletRequest(null, pcontext, session);
082: request.setAttribute("rkey1", "rvalue1");
083: request.setAttribute("rkey2", "rvalue2");
084: ((MockPortletRequest) request).addParameter("pkey1", "pvalue1");
085: ((MockPortletRequest) request)
086: .addParameter("pkey2", "pvalue2a");
087: ((MockPortletRequest) request)
088: .addParameter("pkey2", "pvalue2b");
089: context = createContext();
090: }
091:
092: /**
093: * Return the tests included in this test suite.
094: */
095: public static Test suite() {
096: return (new TestSuite(PortletWebContextTestCase.class));
097: }
098:
099: /**
100: * Tear down instance variables required by this test case.
101: */
102: public void tearDown() {
103: pcontext = null;
104: session = null;
105: request = null;
106: response = null;
107: context = null;
108: }
109:
110: // ------------------------------------------------ Individual Test Methods
111:
112: // Test getApplicationScope()
113: public void testApplicationScope() {
114:
115: Map map = ((WebContext) context).getApplicationScope();
116: assertNotNull(map);
117:
118: // Initial contents
119: checkMapSize(map, 4);
120: assertEquals("avalue1", (String) map.get("akey1"));
121: assertEquals("avalue2", (String) map.get("akey2"));
122: assertEquals("avalue3", (String) map.get("akey3"));
123: assertEquals("avalue4", (String) map.get("akey4"));
124:
125: // Transparency - entrySet()
126: checkEntrySet(map, true);
127:
128: // Transparency - removal via web object
129: pcontext.removeAttribute("akey1");
130: checkMapSize(map, 3);
131: assertNull(map.get("akey1"));
132:
133: // Transparency - removal via map
134: map.remove("akey2");
135: checkMapSize(map, 2);
136: assertNull(pcontext.getAttribute("akey2"));
137:
138: // Transparency - addition via web object
139: pcontext.setAttribute("akeyA", "avalueA");
140: checkMapSize(map, 3);
141: assertEquals("avalueA", (String) map.get("akeyA"));
142:
143: // Transparency - addition via map
144: map.put("akeyB", "avalueB");
145: checkMapSize(map, 4);
146: assertEquals("avalueB", (String) pcontext.getAttribute("akeyB"));
147:
148: // Transparency - replacement via web object
149: pcontext.setAttribute("akeyA", "newvalueA");
150: checkMapSize(map, 4);
151: assertEquals("newvalueA", (String) map.get("akeyA"));
152:
153: // Transparency - replacement via map
154: map.put("akeyB", "newvalueB");
155: assertEquals(4, map.size());
156: assertEquals("newvalueB", (String) pcontext
157: .getAttribute("akeyB"));
158:
159: // Clearing the map
160: map.clear();
161: checkMapSize(map, 0);
162:
163: }
164:
165: // Test equals() and hashCode()
166: // Copied from ContextBaseTestCase with customized creation of "other"
167: public void testEquals() {
168:
169: // Compare to self
170: assertTrue(context.equals(context));
171: assertTrue(context.hashCode() == context.hashCode());
172:
173: // Compare to equivalent instance
174: Context other = new PortletWebContext(pcontext, request,
175: response);
176: // assertTrue(context.equals(other));
177: assertTrue(context.hashCode() == other.hashCode());
178:
179: // Compare to non-equivalent instance - other modified
180: other.put("bop", "bop value");
181: // assertTrue(!context.equals(other));
182: assertTrue(context.hashCode() != other.hashCode());
183:
184: // Compare to non-equivalent instance - self modified
185: other = new PortletWebContext(pcontext, request, response);
186: context.put("bop", "bop value");
187: // assertTrue(!context.equals(other));
188: assertTrue(context.hashCode() != other.hashCode());
189:
190: }
191:
192: // Test getHeader()
193: public void testHeader() {
194:
195: Map map = ((WebContext) context).getHeader();
196: assertNotNull("Header Map Null", map);
197:
198: // Initial contents
199: checkMapSize(map, 0);
200:
201: try {
202: map.put("hkey3", "hvalue3");
203: fail("Should have thrown UnsupportedOperationException");
204: } catch (UnsupportedOperationException e) {
205: ; // expected result
206: }
207:
208: }
209:
210: // Test getHeaderValues()
211: public void testHeaderValues() {
212:
213: Map map = ((WebContext) context).getHeaderValues();
214: assertNotNull("HeaderValues Map Null", map);
215:
216: // Initial contents
217: checkMapSize(map, 0);
218:
219: try {
220: map.put("hkey3", "ABC");
221: fail("Should have thrown UnsupportedOperationException");
222: } catch (UnsupportedOperationException e) {
223: ; // expected result
224: }
225:
226: }
227:
228: // Test getInitParam()
229: public void testInitParam() {
230:
231: Map map = ((WebContext) context).getInitParam();
232: assertNotNull(map);
233:
234: // Initial contents
235: checkMapSize(map, 3);
236: assertEquals("ivalue1", (String) map.get("ikey1"));
237: assertEquals("ivalue2", (String) map.get("ikey2"));
238: assertEquals("ivalue3", (String) map.get("ikey3"));
239: assertTrue(map.containsKey("ikey1"));
240: assertTrue(map.containsKey("ikey2"));
241: assertTrue(map.containsKey("ikey3"));
242: assertTrue(map.containsValue("ivalue1"));
243: assertTrue(map.containsValue("ivalue2"));
244: assertTrue(map.containsValue("ivalue3"));
245:
246: // Transparency - entrySet()
247: checkEntrySet(map, false);
248:
249: // Unsupported operations on read-only map
250: try {
251: map.clear();
252: fail("Should have thrown UnsupportedOperationException");
253: } catch (UnsupportedOperationException e) {
254: ; // expected result
255: }
256: try {
257: map.put("ikey4", "ivalue4");
258: fail("Should have thrown UnsupportedOperationException");
259: } catch (UnsupportedOperationException e) {
260: ; // expected result
261: }
262: try {
263: map.putAll(new HashMap());
264: fail("Should have thrown UnsupportedOperationException");
265: } catch (UnsupportedOperationException e) {
266: ; // expected result
267: }
268: try {
269: map.remove("ikey1");
270: fail("Should have thrown UnsupportedOperationException");
271: } catch (UnsupportedOperationException e) {
272: ; // expected result
273: }
274:
275: }
276:
277: // Test getParam()
278: public void testParam() {
279:
280: Map map = ((WebContext) context).getParam();
281: assertNotNull(map);
282:
283: // Initial contents
284: checkMapSize(map, 2);
285: assertEquals("pvalue1", (String) map.get("pkey1"));
286: assertEquals("pvalue2a", (String) map.get("pkey2"));
287: assertTrue(map.containsKey("pkey1"));
288: assertTrue(map.containsKey("pkey2"));
289: assertTrue(map.containsValue("pvalue1"));
290: assertTrue(map.containsValue("pvalue2a"));
291:
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("pkey3", "pvalue3");
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("pkey1");
315: fail("Should have thrown UnsupportedOperationException");
316: } catch (UnsupportedOperationException e) {
317: ; // expected result
318: }
319:
320: }
321:
322: // Test getParamValues()
323: public void testParamValues() {
324:
325: Map map = ((WebContext) context).getParamValues();
326: assertNotNull(map);
327:
328: // Initial contents
329: checkMapSize(map, 2);
330: Object value1 = map.get("pkey1");
331: assertNotNull(value1);
332: assertTrue(value1 instanceof String[]);
333: String values1[] = (String[]) value1;
334: assertEquals(1, values1.length);
335: assertEquals("pvalue1", values1[0]);
336: Object value2 = map.get("pkey2");
337: assertNotNull(value2);
338: assertTrue(value2 instanceof String[]);
339: String values2[] = (String[]) value2;
340: assertEquals(2, values2.length);
341: assertEquals("pvalue2a", values2[0]);
342: assertEquals("pvalue2b", values2[1]);
343: assertTrue(map.containsKey("pkey1"));
344: assertTrue(map.containsKey("pkey2"));
345: assertTrue(map.containsValue(values1));
346: assertTrue(map.containsValue(values2));
347:
348: // Unsupported operations on read-only map
349: try {
350: map.clear();
351: fail("Should have thrown UnsupportedOperationException");
352: } catch (UnsupportedOperationException e) {
353: ; // expected result
354: }
355: try {
356: map.put("pkey3", values2);
357: fail("Should have thrown UnsupportedOperationException");
358: } catch (UnsupportedOperationException e) {
359: ; // expected result
360: }
361: try {
362: map.putAll(new HashMap());
363: fail("Should have thrown UnsupportedOperationException");
364: } catch (UnsupportedOperationException e) {
365: ; // expected result
366: }
367: try {
368: map.remove("pkey1");
369: fail("Should have thrown UnsupportedOperationException");
370: } catch (UnsupportedOperationException e) {
371: ; // expected result
372: }
373:
374: }
375:
376: // Test getCookies()
377: public void testCookies() {
378:
379: Map map = ((WebContext) context).getCookies();
380: assertNotNull(map);
381:
382: // Initial contents
383: checkMapSize(map, 0);
384:
385: try {
386: map.put("ckey3", "XXX");
387: fail("map.put() Should have thrown UnsupportedOperationException");
388: } catch (UnsupportedOperationException e) {
389: ; // expected result
390: }
391: }
392:
393: // Test state of newly created instance
394: public void testPristine() {
395:
396: super .testPristine();
397: PortletWebContext pwcontext = (PortletWebContext) context;
398:
399: // Properties should all be non-null
400: assertNotNull(pwcontext.getApplicationScope());
401: assertNotNull(pwcontext.getHeader());
402: assertNotNull(pwcontext.getHeaderValues());
403: assertNotNull(pwcontext.getInitParam());
404: assertNotNull(pwcontext.getParam());
405: assertNotNull(pwcontext.getParamValues());
406: assertNotNull(pwcontext.getCookies());
407: assertNotNull(pwcontext.getRequestScope());
408: assertNotNull(pwcontext.getSessionScope());
409:
410: // Attribute-property transparency
411: assertTrue(pwcontext.getApplicationScope() == pwcontext
412: .get("applicationScope"));
413: assertTrue(pwcontext.getHeader() == pwcontext.get("header"));
414: assertTrue(pwcontext.getHeaderValues() == pwcontext
415: .get("headerValues"));
416: assertTrue(pwcontext.getInitParam() == pwcontext
417: .get("initParam"));
418: assertTrue(pwcontext.getParam() == pwcontext.get("param"));
419: assertTrue(pwcontext.getParamValues() == pwcontext
420: .get("paramValues"));
421: assertTrue(pwcontext.getCookies() == pwcontext.get("cookies"));
422: assertTrue(pwcontext.getRequestScope() == pwcontext
423: .get("requestScope"));
424: assertTrue(pwcontext.getSessionScope() == pwcontext
425: .get("sessionScope"));
426:
427: }
428:
429: // Test release()
430: public void testRelease() {
431:
432: PortletWebContext pwcontext = (PortletWebContext) context;
433: pwcontext.release();
434:
435: // Properties should all be null
436: assertNull("getApplicationScope()", pwcontext
437: .getApplicationScope());
438: assertNull("getHeader()", pwcontext.getHeader());
439: assertNull("getHeaderValues()", pwcontext.getHeaderValues());
440: assertNull("getInitParam()", pwcontext.getInitParam());
441: assertNull("getParam()", pwcontext.getParam());
442: assertNull("getParamValues()", pwcontext.getParamValues());
443: assertNull("getRequestScope()", pwcontext.getRequestScope());
444: assertNull("getSessionScope()", pwcontext.getSessionScope());
445:
446: // Attributes should all be null
447: assertNull("applicationScope", pwcontext
448: .get("applicationScope"));
449: assertNull("header", pwcontext.get("header"));
450: assertNull("headerValues", pwcontext.get("headerValues"));
451: assertNull("initParam", pwcontext.get("initParam"));
452: assertNull("param", pwcontext.get("param"));
453: assertNull("paramValues", pwcontext.get("paramValues"));
454: assertNull("requestScope", pwcontext.get("requestScope"));
455: assertNull("sessionScope", pwcontext.get("sessionScope"));
456:
457: }
458:
459: // Test getRequestScope()
460: public void testRequestScope() {
461:
462: Map map = ((WebContext) context).getRequestScope();
463: assertNotNull(map);
464:
465: // Initial contents
466: checkMapSize(map, 2);
467: assertEquals("rvalue1", (String) map.get("rkey1"));
468: assertEquals("rvalue2", (String) map.get("rkey2"));
469:
470: // Transparency - entrySet()
471: checkEntrySet(map, true);
472:
473: // Transparency - removal via web object
474: request.removeAttribute("rkey1");
475: checkMapSize(map, 1);
476: assertNull(map.get("rkey1"));
477:
478: // Transparency - removal via map
479: map.remove("rkey2");
480: checkMapSize(map, 0);
481: assertNull(request.getAttribute("rkey2"));
482:
483: // Transparency - addition via web object
484: request.setAttribute("rkeyA", "rvalueA");
485: checkMapSize(map, 1);
486: assertEquals("rvalueA", (String) map.get("rkeyA"));
487:
488: // Transparency - addition via map
489: map.put("rkeyB", "rvalueB");
490: checkMapSize(map, 2);
491: assertEquals("rvalueB", (String) request.getAttribute("rkeyB"));
492:
493: // Transparency - replacement via web object
494: request.setAttribute("rkeyA", "newvalueA");
495: checkMapSize(map, 2);
496: assertEquals("newvalueA", (String) map.get("rkeyA"));
497:
498: // Transparency - replacement via map
499: map.put("rkeyB", "newvalueB");
500: checkMapSize(map, 2);
501: assertEquals("newvalueB", (String) request
502: .getAttribute("rkeyB"));
503:
504: // Clearing the map
505: map.clear();
506: checkMapSize(map, 0);
507:
508: }
509:
510: // Test getSessionScope()
511: public void testSessionScope() {
512:
513: Map map = ((WebContext) context).getSessionScope();
514: assertNotNull(map);
515:
516: // Initial contents
517: checkMapSize(map, 3);
518: assertEquals("svalue1", (String) map.get("skey1"));
519: assertEquals("svalue2", (String) map.get("skey2"));
520: assertEquals("svalue3", (String) map.get("skey3"));
521:
522: // Transparency - entrySet()
523: checkEntrySet(map, true);
524:
525: // Transparency - removal via web object
526: session.removeAttribute("skey1");
527: checkMapSize(map, 2);
528: assertNull(map.get("skey1"));
529:
530: // Transparency - removal via map
531: map.remove("skey2");
532: checkMapSize(map, 1);
533: assertNull(session.getAttribute("skey2"));
534:
535: // Transparency - addition via web object
536: session.setAttribute("skeyA", "svalueA");
537: checkMapSize(map, 2);
538: assertEquals("svalueA", (String) map.get("skeyA"));
539:
540: // Transparency - addition via map
541: map.put("skeyB", "svalueB");
542: checkMapSize(map, 3);
543: assertEquals("svalueB", (String) session.getAttribute("skeyB"));
544:
545: // Transparency - replacement via web object
546: session.setAttribute("skeyA", "newvalueA");
547: checkMapSize(map, 3);
548: assertEquals("newvalueA", (String) map.get("skeyA"));
549:
550: // Transparency - replacement via map
551: map.put("skeyB", "newvalueB");
552: checkMapSize(map, 3);
553: assertEquals("newvalueB", (String) session
554: .getAttribute("skeyB"));
555:
556: // Clearing the map
557: map.clear();
558: checkMapSize(map, 0);
559:
560: }
561:
562: // Test getSessionScope() without Session
563: public void testSessionScopeWithoutSession() {
564:
565: // Create a Context without a session
566: PortletWebContext ctx = new PortletWebContext(pcontext,
567: new MockPortletRequest(), response);
568: assertNull("Session(A)", ctx.getRequest().getPortletSession(
569: false));
570:
571: // Get the session Map & check session doesn't exist
572: Map sessionMap = ctx.getSessionScope();
573: assertNull("Session(B)", ctx.getRequest().getPortletSession(
574: false));
575: assertNotNull("Session Map(A)", sessionMap);
576:
577: // test clear()
578: sessionMap.clear();
579: assertNull("Session(C)", ctx.getRequest().getPortletSession(
580: false));
581:
582: // test containsKey()
583: assertFalse("containsKey()", sessionMap.containsKey("ABC"));
584: assertNull("Session(D)", ctx.getRequest().getPortletSession(
585: false));
586:
587: // test containsValue()
588: assertFalse("containsValue()", sessionMap.containsValue("ABC"));
589: assertNull("Session(E)", ctx.getRequest().getPortletSession(
590: false));
591:
592: // test entrySet()
593: Set entrySet = sessionMap.entrySet();
594: assertNotNull("entrySet", entrySet);
595: assertEquals("entrySet Size", 0, entrySet.size());
596: assertNull("Session(F)", ctx.getRequest().getPortletSession(
597: false));
598:
599: // test equals()
600: assertFalse("equals()", sessionMap.equals("ABC"));
601: assertNull("Session(G)", ctx.getRequest().getPortletSession(
602: false));
603:
604: // test get()
605: assertNull("get()", sessionMap.get("ABC"));
606: assertNull("Session(H)", ctx.getRequest().getPortletSession(
607: false));
608:
609: // test hashCode()
610: sessionMap.hashCode();
611: assertNull("Session(I)", ctx.getRequest().getPortletSession(
612: false));
613:
614: // test isEmpty()
615: assertTrue("isEmpty()", sessionMap.isEmpty());
616: assertNull("Session(J)", ctx.getRequest().getPortletSession(
617: false));
618:
619: // test keySet()
620: Set keySet = sessionMap.keySet();
621: assertNotNull("keySet", keySet);
622: assertEquals("keySet Size", 0, keySet.size());
623: assertNull("Session(K)", ctx.getRequest().getPortletSession(
624: false));
625:
626: // test putAll() with an empty Map
627: sessionMap.putAll(new HashMap());
628: assertNull("Session(L)", ctx.getRequest().getPortletSession(
629: false));
630:
631: // test remove()
632: assertNull("remove()", sessionMap.remove("ABC"));
633: assertNull("Session(M)", ctx.getRequest().getPortletSession(
634: false));
635:
636: // test size()
637: assertEquals("size() Size", 0, sessionMap.size());
638: assertNull("Session(N)", ctx.getRequest().getPortletSession(
639: false));
640:
641: // test values()
642: Collection values = sessionMap.values();
643: assertNotNull("values", values);
644: assertEquals("values Size", 0, values.size());
645: assertNull("Session(O)", ctx.getRequest().getPortletSession(
646: false));
647:
648: // test put()
649: try {
650: assertNull("put()", sessionMap.put("ABC", "XYZ"));
651: assertNotNull("Session(P)", ctx.getRequest()
652: .getPortletSession(false));
653: } catch (UnsupportedOperationException ex) {
654: // expected: currently MockPortletRequest throws this
655: // when trying to create a PortletSession
656: }
657:
658: }
659:
660: // ------------------------------------------------------- Protected Methods
661:
662: protected void checkMapSize(Map map, int size) {
663: // Check reported size of the map
664: assertEquals("checkMapSize(A)", size, map.size());
665: // Iterate over key set
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: // Iterate over entry set
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: // Count the values
682: assertEquals("checkMapSize(D)", size, map.values().size());
683: }
684:
685: // Test to ensure proper entrySet() and are modifiable optionally
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: ; // expected result
700: }
701: } else {
702: // Should pass and not throw UnsupportedOperationException
703: Map.Entry e = (Map.Entry) o;
704: e.setValue(e.setValue(new Object()));
705: }
706: }
707:
708: // Create a new instance of the appropriate Context type for this test case
709: protected Context createContext() {
710: return (new PortletWebContext(pcontext, request, response));
711: }
712:
713: }
|