001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.mockobjects.constraint.Constraint;
008: import com.mockobjects.constraint.IsAnything;
009: import com.mockobjects.constraint.IsEqual;
010: import com.mockobjects.dynamic.Mock;
011: import junit.framework.TestCase;
012:
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpSession;
015:
016: import java.util.ArrayList;
017: import java.util.Collections;
018: import java.util.Enumeration;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: /**
024: * @author Robert Dawson (robert@rojotek.com)
025: * @author tm_jee
026: * @version $Date: 2006-09-16 16:19:37 +0200 (Sat, 16 Sep 2006) $ $Id: SessionMapTest.java 2722 2006-09-16 14:19:37Z tmjee $
027: */
028: public class SessionMapTest extends TestCase {
029:
030: private Mock requestMock;
031: private Mock sessionMock;
032:
033: public void testClearInvalidatesTheSession() throws Exception {
034: List attributeNames = new ArrayList();
035: attributeNames.add("test");
036: attributeNames.add("anotherTest");
037: Enumeration attributeNamesEnum = Collections
038: .enumeration(attributeNames);
039:
040: MockSessionMap sessionMap = new MockSessionMap(
041: (HttpServletRequest) requestMock.proxy());
042: sessionMock.expect("setAttribute", new Constraint[] {
043: new IsEqual("test"), new IsEqual("test value") });
044: sessionMock.expect("setAttribute", new Constraint[] {
045: new IsEqual("anotherTest"),
046: new IsEqual("another test value") });
047: sessionMock.expectAndReturn("getAttributeNames",
048: attributeNamesEnum);
049: sessionMock.expect("removeAttribute",
050: new Constraint[] { new IsEqual("test") });
051: sessionMock.expect("removeAttribute",
052: new Constraint[] { new IsEqual("anotherTest") });
053: sessionMap.put("test", "test value");
054: sessionMap.put("anotherTest", "another test value");
055: sessionMap.clear();
056: assertNull(sessionMap.get("test"));
057: assertNull(sessionMap.get("anotherTest"));
058: sessionMock.verify();
059: }
060:
061: public void testGetOnSessionMapUsesWrappedSessionsGetAttribute()
062: throws Exception {
063: Object value = new Object();
064: sessionMock.expectAndReturn("getAttribute",
065: new Constraint[] { new IsEqual("KEY") }, value);
066:
067: SessionMap sessionMap = new SessionMap(
068: (HttpServletRequest) requestMock.proxy());
069: assertEquals(
070: "Expected the get using KEY to return the value object setup in the mockSession",
071: value, sessionMap.get("KEY"));
072: sessionMock.verify();
073: }
074:
075: public void testPutOnSessionMapUsesWrappedSessionsSetsAttribute()
076: throws Exception {
077: Object value = new Object();
078: sessionMock.expect("getAttribute",
079: new Constraint[] { new IsAnything() });
080: sessionMock.expect("setAttribute", new Constraint[] {
081: new IsEqual("KEY"), new IsEqual(value) });
082:
083: SessionMap sessionMap = new SessionMap(
084: (HttpServletRequest) requestMock.proxy());
085: sessionMap.put("KEY", value);
086: sessionMock.verify();
087: }
088:
089: public void testPuttingObjectInMapReturnsNullForPreviouslyUnusedKey()
090: throws Exception {
091: Object value = new Object();
092: sessionMock.expectAndReturn("getAttribute",
093: new Constraint[] { new IsEqual("KEY") }, null);
094: sessionMock.expect("setAttribute", new Constraint[] {
095: new IsEqual("KEY"), new IsEqual(value) });
096:
097: SessionMap sessionMap = new SessionMap(
098: (HttpServletRequest) requestMock.proxy());
099: assertNull(
100: "should be null, as the contract for Map says that put returns the previous value in the map for the key",
101: sessionMap.put("KEY", value));
102: sessionMock.verify();
103: }
104:
105: public void testPuttingObjectInMapReturnsPreviousValueForKey()
106: throws Exception {
107: Object originalValue = new Object();
108: Object value = new Object();
109: sessionMock.expectAndReturn("getAttribute",
110: new Constraint[] { new IsEqual("KEY") }, null);
111: sessionMock.expect("setAttribute", new Constraint[] {
112: new IsEqual("KEY"), new IsEqual(originalValue) });
113: sessionMock.expectAndReturn("getAttribute",
114: new Constraint[] { new IsEqual("KEY") }, originalValue);
115: sessionMock.expect("setAttribute", new Constraint[] {
116: new IsEqual("KEY"), new IsEqual(value) });
117:
118: SessionMap sessionMap = new SessionMap(
119: (HttpServletRequest) requestMock.proxy());
120: sessionMap.put("KEY", originalValue);
121: assertEquals(
122: "should be the OriginalValue, as the contract for Map says that put returns the previous value in the map for the key",
123: originalValue, sessionMap.put("KEY", value));
124: sessionMock.verify();
125: }
126:
127: public void testRemovePassThroughCallToRemoveAttribute()
128: throws Exception {
129: Object value = new Object();
130: sessionMock.expectAndReturn("getAttribute",
131: new Constraint[] { new IsEqual("KEY") }, value);
132: sessionMock.expect("removeAttribute",
133: new Constraint[] { new IsEqual("KEY") });
134:
135: SessionMap sessionMap = new SessionMap(
136: (HttpServletRequest) requestMock.proxy());
137: assertEquals(value, sessionMap.remove("KEY"));
138: sessionMock.verify();
139: }
140:
141: protected void setUp() throws Exception {
142: sessionMock = new Mock(HttpSession.class);
143: requestMock = new Mock(HttpServletRequest.class);
144: requestMock.matchAndReturn("getSession",
145: new Constraint[] { new IsEqual(Boolean.FALSE) },
146: sessionMock.proxy());
147: }
148:
149: /**
150: * class that extends session map, making the values available in a local map -- useful
151: * for confirming put and get calls in the superclass. ie useful for testing that the get is done before
152: * putting new data into the map.
153: */
154: private class MockSessionMap extends SessionMap {
155: private Map map = new HashMap();
156:
157: public MockSessionMap(HttpServletRequest request) {
158: super (request);
159: }
160:
161: public Object get(Object key) {
162: return map.get(key);
163: }
164:
165: public Object put(Object key, Object value) {
166: Object originalValue = super .put(key, value);
167: map.put(key, value); //put the value into our map after putting it in the superclass map to avoid polluting the get call.
168:
169: return originalValue;
170: }
171:
172: public void clear() {
173: super.clear();
174: map.clear();
175: }
176: }
177: }
|