KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > MockHttpSession


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.web;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
29 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
30 import javax.servlet.http.HttpSessionContext JavaDoc;
31
32 import org.springframework.util.Assert;
33
34 /**
35  * Mock implementation of the {@link javax.servlet.http.HttpSession} interface.
36  * Supports the Servlet 2.4 API level.
37  *
38  * <p>Used for testing the web framework; also useful for testing
39  * application controllers.
40  *
41  * @author Juergen Hoeller
42  * @author Rod Johnson
43  * @since 1.0.2
44  */

45 public class MockHttpSession implements HttpSession JavaDoc {
46
47     public static final String JavaDoc SESSION_COOKIE_NAME = "JSESSION";
48
49     private static int nextId = 1;
50
51
52     private final String JavaDoc id = Integer.toString(nextId++);
53
54     private final long creationTime = System.currentTimeMillis();
55
56     private int maxInactiveInterval;
57
58     private long lastAccessedTime = System.currentTimeMillis();
59
60     private final ServletContext JavaDoc servletContext;
61
62     private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
63
64     private boolean invalid = false;
65
66     private boolean isNew = true;
67
68
69     /**
70      * Create a new MockHttpSession with a default {@link MockServletContext}.
71      * @see MockServletContext
72      */

73     public MockHttpSession() {
74         this(null);
75     }
76
77     /**
78      * Create a new MockHttpSession.
79      * @param servletContext the ServletContext that the session runs in
80      */

81     public MockHttpSession(ServletContext JavaDoc servletContext) {
82         this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
83     }
84
85
86     public long getCreationTime() {
87         return this.creationTime;
88     }
89
90     public String JavaDoc getId() {
91         return this.id;
92     }
93
94     public void access() {
95         this.lastAccessedTime = System.currentTimeMillis();
96         this.isNew = false;
97     }
98
99     public long getLastAccessedTime() {
100         return this.lastAccessedTime;
101     }
102
103     public ServletContext JavaDoc getServletContext() {
104         return this.servletContext;
105     }
106
107     public void setMaxInactiveInterval(int interval) {
108         this.maxInactiveInterval = interval;
109     }
110
111     public int getMaxInactiveInterval() {
112         return this.maxInactiveInterval;
113     }
114
115     public HttpSessionContext JavaDoc getSessionContext() {
116         throw new UnsupportedOperationException JavaDoc("getSessionContext");
117     }
118
119     public Object JavaDoc getAttribute(String JavaDoc name) {
120         Assert.notNull(name, "Attribute name must not be null");
121         return this.attributes.get(name);
122     }
123
124     public Object JavaDoc getValue(String JavaDoc name) {
125         return getAttribute(name);
126     }
127
128     public Enumeration JavaDoc getAttributeNames() {
129         return this.attributes.keys();
130     }
131
132     public String JavaDoc[] getValueNames() {
133         return (String JavaDoc[]) this.attributes.keySet().toArray(new String JavaDoc[this.attributes.size()]);
134     }
135
136     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
137         Assert.notNull(name, "Attribute name must not be null");
138         if (value != null) {
139             this.attributes.put(name, value);
140             if (value instanceof HttpSessionBindingListener JavaDoc) {
141                 ((HttpSessionBindingListener JavaDoc) value).valueBound(new HttpSessionBindingEvent JavaDoc(this, name, value));
142             }
143         }
144         else {
145             removeAttribute(name);
146         }
147     }
148
149     public void putValue(String JavaDoc name, Object JavaDoc value) {
150         setAttribute(name, value);
151     }
152
153     public void removeAttribute(String JavaDoc name) {
154         Assert.notNull(name, "Attribute name must not be null");
155         Object JavaDoc value = this.attributes.remove(name);
156         if (value instanceof HttpSessionBindingListener JavaDoc) {
157             ((HttpSessionBindingListener JavaDoc) value).valueUnbound(new HttpSessionBindingEvent JavaDoc(this, name, value));
158         }
159     }
160
161     public void removeValue(String JavaDoc name) {
162         removeAttribute(name);
163     }
164
165     /**
166      * Clear all of this session's attributes.
167      */

168     public void clearAttributes() {
169         for (Iterator JavaDoc it = this.attributes.entrySet().iterator(); it.hasNext();) {
170             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
171             String JavaDoc name = (String JavaDoc) entry.getKey();
172             Object JavaDoc value = entry.getValue();
173             it.remove();
174             if (value instanceof HttpSessionBindingListener JavaDoc) {
175                 ((HttpSessionBindingListener JavaDoc) value).valueUnbound(new HttpSessionBindingEvent JavaDoc(this, name, value));
176             }
177         }
178     }
179
180     public void invalidate() {
181         this.invalid = true;
182         clearAttributes();
183     }
184
185     public boolean isInvalid() {
186         return this.invalid;
187     }
188
189     public void setNew(boolean value) {
190         this.isNew = value;
191     }
192
193     public boolean isNew() {
194         return this.isNew;
195     }
196
197
198     /**
199      * Serialize the attributes of this session into an object that can
200      * be turned into a byte array with standard Java serialization.
201      * @return a representation of this session's serialized state
202      */

203     public Serializable JavaDoc serializeState() {
204         HashMap JavaDoc state = new HashMap JavaDoc();
205         for (Iterator JavaDoc it = this.attributes.entrySet().iterator(); it.hasNext();) {
206             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
207             String JavaDoc name = (String JavaDoc) entry.getKey();
208             Object JavaDoc value = entry.getValue();
209             it.remove();
210             if (value instanceof Serializable JavaDoc) {
211                 state.put(name, value);
212             }
213             else {
214                 // Not serializable... Servlet containers usually automatically
215
// unbind the attribute in this case.
216
if (value instanceof HttpSessionBindingListener JavaDoc) {
217                     ((HttpSessionBindingListener JavaDoc) value).valueUnbound(new HttpSessionBindingEvent JavaDoc(this, name, value));
218                 }
219             }
220         }
221         return state;
222     }
223
224     /**
225      * Deserialize the attributes of this session from a state object
226      * created by {@link #serializeState()}.
227      * @param state a representation of this session's serialized state
228      */

229     public void deserializeState(Serializable JavaDoc state) {
230         Assert.isTrue(state instanceof Map JavaDoc, "Serialized state needs to be of type [java.util.Map]");
231         this.attributes.putAll((Map JavaDoc) state);
232     }
233
234 }
235
Popular Tags