001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet;
006:
007: import java.util.ArrayList;
008: import java.util.Arrays;
009: import java.util.Enumeration;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Map;
013: import java.util.Set;
014:
015: import javax.portlet.PortletRequest;
016: import javax.portlet.PortletSession;
017:
018: import org.jmock.Mock;
019: import org.jmock.MockObjectTestCase;
020: import org.jmock.core.Constraint;
021:
022: /**
023: * PortletSessionMapTest. Insert description.
024: *
025: * @author Nils-Helge Garli
026: * @version $Revision: 2350 $ $Date: 2006-03-10 10:39:24 +0100 (Fri, 10 Mar 2006) $
027: */
028: public class PortletSessionMapTest extends MockObjectTestCase {
029:
030: public void testPut() {
031:
032: Mock mockSession = mock(PortletSession.class);
033: Mock mockRequest = mock(PortletRequest.class);
034:
035: PortletRequest req = (PortletRequest) mockRequest.proxy();
036: PortletSession session = (PortletSession) mockSession.proxy();
037:
038: mockRequest.expects(once()).method("getPortletSession").will(
039: returnValue(session));
040: Constraint[] params = new Constraint[] { eq("testAttribute1"),
041: eq("testValue1") };
042: mockSession.expects(once()).method("setAttribute").with(params);
043: mockSession.expects(once()).method("getAttribute").with(
044: eq("testAttribute1")).will(returnValue("testValue1"));
045: params = new Constraint[] { eq("testAttribute2"),
046: eq("testValue2") };
047: mockSession.expects(once()).method("setAttribute").with(params);
048: mockSession.expects(once()).method("getAttribute").with(
049: eq("testAttribute2")).will(returnValue("testValue2"));
050:
051: PortletSessionMap map = new PortletSessionMap(req);
052: map.put("testAttribute1", "testValue1");
053: map.put("testAttribute2", "testValue2");
054:
055: }
056:
057: public void testGet() {
058: Mock mockSession = mock(PortletSession.class);
059: Mock mockRequest = mock(PortletRequest.class);
060:
061: PortletRequest req = (PortletRequest) mockRequest.proxy();
062: PortletSession session = (PortletSession) mockSession.proxy();
063:
064: mockRequest.expects(once()).method("getPortletSession").will(
065: returnValue(session));
066: mockSession.expects(once()).method("getAttribute").with(
067: eq("testAttribute1")).will(returnValue("testValue1"));
068: mockSession.expects(once()).method("getAttribute").with(
069: eq("testAttribute2")).will(returnValue("testValue2"));
070:
071: PortletSessionMap map = new PortletSessionMap(req);
072: Object val1 = map.get("testAttribute1");
073: Object val2 = map.get("testAttribute2");
074: assertEquals("testValue1", val1);
075: assertEquals("testValue2", val2);
076: }
077:
078: public void testClear() {
079: Mock mockSession = mock(PortletSession.class);
080: Mock mockRequest = mock(PortletRequest.class);
081:
082: PortletRequest req = (PortletRequest) mockRequest.proxy();
083: PortletSession session = (PortletSession) mockSession.proxy();
084:
085: mockRequest.expects(once()).method("getPortletSession").will(
086: returnValue(session));
087: mockSession.expects(once()).method("invalidate");
088:
089: PortletSessionMap map = new PortletSessionMap(req);
090: map.clear();
091: }
092:
093: public void testRemove() {
094: Mock mockSession = mock(PortletSession.class);
095: Mock mockRequest = mock(PortletRequest.class);
096:
097: PortletRequest req = (PortletRequest) mockRequest.proxy();
098: PortletSession session = (PortletSession) mockSession.proxy();
099:
100: mockRequest.expects(once()).method("getPortletSession").will(
101: returnValue(session));
102: mockSession.stubs().method("getAttribute").with(eq("dummyKey"))
103: .will(returnValue("dummyValue"));
104: mockSession.expects(once()).method("removeAttribute").with(
105: eq("dummyKey"));
106:
107: PortletSessionMap map = new PortletSessionMap(req);
108: Object ret = map.remove("dummyKey");
109: assertEquals("dummyValue", ret);
110: }
111:
112: public void testEntrySet() {
113: Mock mockSession = mock(PortletSession.class);
114: Mock mockRequest = mock(PortletRequest.class);
115:
116: PortletRequest req = (PortletRequest) mockRequest.proxy();
117: PortletSession session = (PortletSession) mockSession.proxy();
118:
119: Enumeration names = new Enumeration() {
120:
121: List keys = Arrays.asList(new Object[] { "key1", "key2" });
122: Iterator it = keys.iterator();
123:
124: public boolean hasMoreElements() {
125: return it.hasNext();
126: }
127:
128: public Object nextElement() {
129: return it.next();
130: }
131:
132: };
133:
134: mockSession.stubs().method("getAttributeNames").will(
135: returnValue(names));
136: mockSession.stubs().method("getAttribute").with(eq("key1"))
137: .will(returnValue("value1"));
138: mockSession.stubs().method("getAttribute").with(eq("key2"))
139: .will(returnValue("value2"));
140:
141: mockRequest.expects(once()).method("getPortletSession").will(
142: returnValue(session));
143:
144: PortletSessionMap map = new PortletSessionMap(req);
145: Set entries = map.entrySet();
146:
147: assertEquals(2, entries.size());
148: Iterator it = entries.iterator();
149: Map.Entry entry = (Map.Entry) it.next();
150: assertEquals("key2", entry.getKey());
151: assertEquals("value2", entry.getValue());
152: entry = (Map.Entry) it.next();
153: assertEquals("key1", entry.getKey());
154: assertEquals("value1", entry.getValue());
155:
156: }
157:
158: }
|