001: /*
002: * Copyright 2002-2007 the original author or authors.
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:
017: package org.springframework.mock.web;
018:
019: import java.io.Serializable;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpSession;
028: import javax.servlet.http.HttpSessionBindingEvent;
029: import javax.servlet.http.HttpSessionBindingListener;
030: import javax.servlet.http.HttpSessionContext;
031:
032: import org.springframework.util.Assert;
033:
034: /**
035: * Mock implementation of the {@link javax.servlet.http.HttpSession} interface.
036: * Supports the Servlet 2.4 API level.
037: *
038: * <p>Used for testing the web framework; also useful for testing
039: * application controllers.
040: *
041: * @author Juergen Hoeller
042: * @author Rod Johnson
043: * @since 1.0.2
044: */
045: public class MockHttpSession implements HttpSession {
046:
047: public static final String SESSION_COOKIE_NAME = "JSESSION";
048:
049: private static int nextId = 1;
050:
051: private final String id = Integer.toString(nextId++);
052:
053: private final long creationTime = System.currentTimeMillis();
054:
055: private int maxInactiveInterval;
056:
057: private long lastAccessedTime = System.currentTimeMillis();
058:
059: private final ServletContext servletContext;
060:
061: private final Hashtable attributes = new Hashtable();
062:
063: private boolean invalid = false;
064:
065: private boolean isNew = true;
066:
067: /**
068: * Create a new MockHttpSession with a default {@link MockServletContext}.
069: * @see MockServletContext
070: */
071: public MockHttpSession() {
072: this (null);
073: }
074:
075: /**
076: * Create a new MockHttpSession.
077: * @param servletContext the ServletContext that the session runs in
078: */
079: public MockHttpSession(ServletContext servletContext) {
080: this .servletContext = (servletContext != null ? servletContext
081: : new MockServletContext());
082: }
083:
084: public long getCreationTime() {
085: return this .creationTime;
086: }
087:
088: public String getId() {
089: return this .id;
090: }
091:
092: public void access() {
093: this .lastAccessedTime = System.currentTimeMillis();
094: this .isNew = false;
095: }
096:
097: public long getLastAccessedTime() {
098: return this .lastAccessedTime;
099: }
100:
101: public ServletContext getServletContext() {
102: return this .servletContext;
103: }
104:
105: public void setMaxInactiveInterval(int interval) {
106: this .maxInactiveInterval = interval;
107: }
108:
109: public int getMaxInactiveInterval() {
110: return this .maxInactiveInterval;
111: }
112:
113: public HttpSessionContext getSessionContext() {
114: throw new UnsupportedOperationException("getSessionContext");
115: }
116:
117: public Object getAttribute(String name) {
118: Assert.notNull(name, "Attribute name must not be null");
119: return this .attributes.get(name);
120: }
121:
122: public Object getValue(String name) {
123: return getAttribute(name);
124: }
125:
126: public Enumeration getAttributeNames() {
127: return this .attributes.keys();
128: }
129:
130: public String[] getValueNames() {
131: return (String[]) this .attributes.keySet().toArray(
132: new String[this .attributes.size()]);
133: }
134:
135: public void setAttribute(String name, Object value) {
136: Assert.notNull(name, "Attribute name must not be null");
137: if (value != null) {
138: this .attributes.put(name, value);
139: if (value instanceof HttpSessionBindingListener) {
140: ((HttpSessionBindingListener) value)
141: .valueBound(new HttpSessionBindingEvent(this ,
142: name, value));
143: }
144: } else {
145: removeAttribute(name);
146: }
147: }
148:
149: public void putValue(String name, Object value) {
150: setAttribute(name, value);
151: }
152:
153: public void removeAttribute(String name) {
154: Assert.notNull(name, "Attribute name must not be null");
155: Object value = this .attributes.remove(name);
156: if (value instanceof HttpSessionBindingListener) {
157: ((HttpSessionBindingListener) value)
158: .valueUnbound(new HttpSessionBindingEvent(this ,
159: name, value));
160: }
161: }
162:
163: public void removeValue(String name) {
164: removeAttribute(name);
165: }
166:
167: /**
168: * Clear all of this session's attributes.
169: */
170: public void clearAttributes() {
171: for (Iterator it = this .attributes.entrySet().iterator(); it
172: .hasNext();) {
173: Map.Entry entry = (Map.Entry) it.next();
174: String name = (String) entry.getKey();
175: Object value = entry.getValue();
176: it.remove();
177: if (value instanceof HttpSessionBindingListener) {
178: ((HttpSessionBindingListener) value)
179: .valueUnbound(new HttpSessionBindingEvent(this ,
180: name, value));
181: }
182: }
183: }
184:
185: public void invalidate() {
186: this .invalid = true;
187: clearAttributes();
188: }
189:
190: public boolean isInvalid() {
191: return this .invalid;
192: }
193:
194: public void setNew(boolean value) {
195: this .isNew = value;
196: }
197:
198: public boolean isNew() {
199: return this .isNew;
200: }
201:
202: /**
203: * Serialize the attributes of this session into an object that can
204: * be turned into a byte array with standard Java serialization.
205: * @return a representation of this session's serialized state
206: */
207: public Serializable serializeState() {
208: HashMap state = new HashMap();
209: for (Iterator it = this .attributes.entrySet().iterator(); it
210: .hasNext();) {
211: Map.Entry entry = (Map.Entry) it.next();
212: String name = (String) entry.getKey();
213: Object value = entry.getValue();
214: it.remove();
215: if (value instanceof Serializable) {
216: state.put(name, value);
217: } else {
218: // Not serializable... Servlet containers usually automatically
219: // unbind the attribute in this case.
220: if (value instanceof HttpSessionBindingListener) {
221: ((HttpSessionBindingListener) value)
222: .valueUnbound(new HttpSessionBindingEvent(
223: this , name, value));
224: }
225: }
226: }
227: return state;
228: }
229:
230: /**
231: * Deserialize the attributes of this session from a state object
232: * created by {@link #serializeState()}.
233: * @param state a representation of this session's serialized state
234: */
235: public void deserializeState(Serializable state) {
236: Assert.isTrue(state instanceof Map,
237: "Serialized state needs to be of type [java.util.Map]");
238: this .attributes.putAll((Map) state);
239: }
240:
241: }
|