001: /*
002: * $Id: SessionMapTest.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.dispatcher;
022:
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032:
033: import junit.framework.TestCase;
034:
035: import com.mockobjects.constraint.Constraint;
036: import com.mockobjects.constraint.IsAnything;
037: import com.mockobjects.constraint.IsEqual;
038: import com.mockobjects.dynamic.Mock;
039:
040: /**
041: */
042: public class SessionMapTest extends TestCase {
043:
044: private Mock requestMock;
045: private Mock sessionMock;
046:
047: public void testClearInvalidatesTheSession() throws Exception {
048: List<String> attributeNames = new ArrayList<String>();
049: attributeNames.add("test");
050: attributeNames.add("anotherTest");
051: Enumeration attributeNamesEnum = Collections
052: .enumeration(attributeNames);
053:
054: MockSessionMap sessionMap = new MockSessionMap(
055: (HttpServletRequest) requestMock.proxy());
056: sessionMock.expect("setAttribute", new Constraint[] {
057: new IsEqual("test"), new IsEqual("test value") });
058: sessionMock.expect("setAttribute", new Constraint[] {
059: new IsEqual("anotherTest"),
060: new IsEqual("another test value") });
061: sessionMock.expectAndReturn("getAttributeNames",
062: attributeNamesEnum);
063: sessionMock.expect("removeAttribute",
064: new Constraint[] { new IsEqual("test") });
065: sessionMock.expect("removeAttribute",
066: new Constraint[] { new IsEqual("anotherTest") });
067: sessionMap.put("test", "test value");
068: sessionMap.put("anotherTest", "another test value");
069: sessionMap.clear();
070: assertNull(sessionMap.get("test"));
071: assertNull(sessionMap.get("anotherTest"));
072: sessionMock.verify();
073: }
074:
075: public void testGetOnSessionMapUsesWrappedSessionsGetAttribute()
076: throws Exception {
077: Object value = new Object();
078: sessionMock.expectAndReturn("getAttribute",
079: new Constraint[] { new IsEqual("KEY") }, value);
080:
081: SessionMap sessionMap = new SessionMap(
082: (HttpServletRequest) requestMock.proxy());
083: assertEquals(
084: "Expected the get using KEY to return the value object setup in the mockSession",
085: value, sessionMap.get("KEY"));
086: sessionMock.verify();
087: }
088:
089: public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute()
090: throws Exception {
091: Object value = new Object();
092: sessionMock.expect("getAttribute",
093: new Constraint[] { new IsAnything() });
094: sessionMock.expect("setAttribute", new Constraint[] {
095: new IsEqual("KEY"), new IsEqual(value) });
096:
097: SessionMap sessionMap = new SessionMap(
098: (HttpServletRequest) requestMock.proxy());
099: sessionMap.put("KEY", value);
100: sessionMock.verify();
101: }
102:
103: public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey()
104: throws Exception {
105: Object value = new Object();
106: sessionMock.expectAndReturn("getAttribute",
107: new Constraint[] { new IsEqual("KEY") }, null);
108: sessionMock.expect("setAttribute", new Constraint[] {
109: new IsEqual("KEY"), new IsEqual(value) });
110:
111: SessionMap sessionMap = new SessionMap(
112: (HttpServletRequest) requestMock.proxy());
113: assertNull(
114: "should be null, as the contract for Map says that put returns the previous value in the map for the key",
115: sessionMap.put("KEY", value));
116: sessionMock.verify();
117: }
118:
119: public void testPuttingObjectInMapReturnsPreviousValueForKey()
120: throws Exception {
121: Object originalValue = new Object();
122: Object value = new Object();
123: sessionMock.expectAndReturn("getAttribute",
124: new Constraint[] { new IsEqual("KEY") }, null);
125: sessionMock.expect("setAttribute", new Constraint[] {
126: new IsEqual("KEY"), new IsEqual(originalValue) });
127: sessionMock.expectAndReturn("getAttribute",
128: new Constraint[] { new IsEqual("KEY") }, originalValue);
129: sessionMock.expect("setAttribute", new Constraint[] {
130: new IsEqual("KEY"), new IsEqual(value) });
131:
132: SessionMap sessionMap = new SessionMap(
133: (HttpServletRequest) requestMock.proxy());
134: sessionMap.put("KEY", originalValue);
135: assertEquals(
136: "should be the OriginalValue, as the contract for Map says that put returns the previous value in the map for the key",
137: originalValue, sessionMap.put("KEY", value));
138: sessionMock.verify();
139: }
140:
141: public void testRemovePassThroughCallToRemoveAttribute()
142: throws Exception {
143: Object value = new Object();
144: sessionMock.expectAndReturn("getAttribute",
145: new Constraint[] { new IsEqual("KEY") }, value);
146: sessionMock.expect("removeAttribute",
147: new Constraint[] { new IsEqual("KEY") });
148:
149: SessionMap sessionMap = new SessionMap(
150: (HttpServletRequest) requestMock.proxy());
151: assertEquals(value, sessionMap.remove("KEY"));
152: sessionMock.verify();
153: }
154:
155: protected void setUp() throws Exception {
156: sessionMock = new Mock(HttpSession.class);
157: requestMock = new Mock(HttpServletRequest.class);
158: requestMock.matchAndReturn("getSession",
159: new Constraint[] { new IsEqual(Boolean.FALSE) },
160: sessionMock.proxy());
161: }
162:
163: /**
164: * class that extends session map, making the values available in a local map -- useful
165: * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
166: * putting new data into the map.
167: */
168: private class MockSessionMap extends SessionMap {
169:
170: private static final long serialVersionUID = 8783604360786273764L;
171:
172: private Map map = new HashMap();
173:
174: public MockSessionMap(HttpServletRequest request) {
175: super (request);
176: }
177:
178: public Object get(Object key) {
179: return map.get(key);
180: }
181:
182: public Object put(Object key, Object value) {
183: Object originalValue = super .put(key, value);
184: map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
185:
186: return originalValue;
187: }
188:
189: public void clear() {
190: super.clear();
191: map.clear();
192: }
193: }
194: }
|