001: /*
002: * $Id: PortletSessionMapTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet;
022:
023: import java.util.Arrays;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import javax.portlet.PortletRequest;
031: import javax.portlet.PortletSession;
032:
033: import org.jmock.Mock;
034: import org.jmock.MockObjectTestCase;
035: import org.jmock.core.Constraint;
036:
037: /**
038: * PortletSessionMapTest. Insert description.
039: *
040: */
041: public class PortletSessionMapTest extends MockObjectTestCase {
042:
043: public void testPut() {
044:
045: Mock mockSession = mock(PortletSession.class);
046: Mock mockRequest = mock(PortletRequest.class);
047:
048: PortletRequest req = (PortletRequest) mockRequest.proxy();
049: PortletSession session = (PortletSession) mockSession.proxy();
050:
051: mockRequest.expects(once()).method("getPortletSession").will(
052: returnValue(session));
053: Constraint[] params = new Constraint[] { eq("testAttribute1"),
054: eq("testValue1") };
055: mockSession.expects(once()).method("setAttribute").with(params);
056: mockSession.expects(once()).method("getAttribute").with(
057: eq("testAttribute1")).will(returnValue("testValue1"));
058: params = new Constraint[] { eq("testAttribute2"),
059: eq("testValue2") };
060: mockSession.expects(once()).method("setAttribute").with(params);
061: mockSession.expects(once()).method("getAttribute").with(
062: eq("testAttribute2")).will(returnValue("testValue2"));
063:
064: PortletSessionMap map = new PortletSessionMap(req);
065: map.put("testAttribute1", "testValue1");
066: map.put("testAttribute2", "testValue2");
067:
068: }
069:
070: public void testGet() {
071: Mock mockSession = mock(PortletSession.class);
072: Mock mockRequest = mock(PortletRequest.class);
073:
074: PortletRequest req = (PortletRequest) mockRequest.proxy();
075: PortletSession session = (PortletSession) mockSession.proxy();
076:
077: mockRequest.expects(once()).method("getPortletSession").will(
078: returnValue(session));
079: mockSession.expects(once()).method("getAttribute").with(
080: eq("testAttribute1")).will(returnValue("testValue1"));
081: mockSession.expects(once()).method("getAttribute").with(
082: eq("testAttribute2")).will(returnValue("testValue2"));
083:
084: PortletSessionMap map = new PortletSessionMap(req);
085: Object val1 = map.get("testAttribute1");
086: Object val2 = map.get("testAttribute2");
087: assertEquals("testValue1", val1);
088: assertEquals("testValue2", val2);
089: }
090:
091: public void testClear() {
092: Mock mockSession = mock(PortletSession.class);
093: Mock mockRequest = mock(PortletRequest.class);
094:
095: PortletRequest req = (PortletRequest) mockRequest.proxy();
096: PortletSession session = (PortletSession) mockSession.proxy();
097:
098: mockRequest.expects(once()).method("getPortletSession").will(
099: returnValue(session));
100: mockSession.expects(once()).method("invalidate");
101:
102: PortletSessionMap map = new PortletSessionMap(req);
103: map.clear();
104: }
105:
106: public void testRemove() {
107: Mock mockSession = mock(PortletSession.class);
108: Mock mockRequest = mock(PortletRequest.class);
109:
110: PortletRequest req = (PortletRequest) mockRequest.proxy();
111: PortletSession session = (PortletSession) mockSession.proxy();
112:
113: mockRequest.expects(once()).method("getPortletSession").will(
114: returnValue(session));
115: mockSession.stubs().method("getAttribute").with(eq("dummyKey"))
116: .will(returnValue("dummyValue"));
117: mockSession.expects(once()).method("removeAttribute").with(
118: eq("dummyKey"));
119:
120: PortletSessionMap map = new PortletSessionMap(req);
121: Object ret = map.remove("dummyKey");
122: assertEquals("dummyValue", ret);
123: }
124:
125: public void testEntrySet() {
126: Mock mockSession = mock(PortletSession.class);
127: Mock mockRequest = mock(PortletRequest.class);
128:
129: PortletRequest req = (PortletRequest) mockRequest.proxy();
130: PortletSession session = (PortletSession) mockSession.proxy();
131:
132: Enumeration names = new Enumeration() {
133:
134: List keys = Arrays.asList(new Object[] { "key1", "key2" });
135: Iterator it = keys.iterator();
136:
137: public boolean hasMoreElements() {
138: return it.hasNext();
139: }
140:
141: public Object nextElement() {
142: return it.next();
143: }
144:
145: };
146:
147: mockSession.stubs().method("getAttributeNames").will(
148: returnValue(names));
149: mockSession.stubs().method("getAttribute").with(eq("key1"))
150: .will(returnValue("value1"));
151: mockSession.stubs().method("getAttribute").with(eq("key2"))
152: .will(returnValue("value2"));
153:
154: mockRequest.expects(once()).method("getPortletSession").will(
155: returnValue(session));
156:
157: PortletSessionMap map = new PortletSessionMap(req);
158: Set entries = map.entrySet();
159:
160: assertEquals(2, entries.size());
161: Iterator it = entries.iterator();
162: Map.Entry entry = (Map.Entry) it.next();
163: assertEquals("key2", entry.getKey());
164: assertEquals("value2", entry.getValue());
165: entry = (Map.Entry) it.next();
166: assertEquals("key1", entry.getKey());
167: assertEquals("value1", entry.getValue());
168:
169: }
170:
171: }
|