1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.huihoo.jfox.soaf.util.filter;
18
19 import java.util.Enumeration;
20 import java.util.Hashtable;
21
22 import javax.servlet.ServletContext;
23 import javax.servlet.http.HttpSession;
24 import javax.servlet.http.HttpSessionContext;
25
26
27 /***
28 * Mock implementation of the HttpSession interface.
29 *
30 * <p>Used for testing the web framework; also useful
31 * for testing application controllers.
32 *
33 * @author Rod Johnson
34 * @author Juergen Hoeller
35 * @since 1.0.2
36 */
37 public class MockHttpSession implements HttpSession {
38
39 public static final String SESSION_COOKIE_NAME = "JSESSION";
40
41 private static int nextId = 1;
42
43
44 private final String id = Integer.toString(nextId++);
45
46 private final long creationTime = System.currentTimeMillis();
47
48 private int maxInactiveInterval;
49
50 private long lastAccessedTime = System.currentTimeMillis();
51
52 private final ServletContext servletContext;
53
54 private final Hashtable attributes = new Hashtable();
55
56 private boolean invalid = false;
57
58 private boolean isNew = true;
59
60
61 /***
62 * Create a new MockHttpSession.
63 * @param servletContext the ServletContext that the session runs in
64 */
65 public MockHttpSession(ServletContext servletContext) {
66 this.servletContext = servletContext;
67 }
68
69 /***
70 * Create a new MockHttpSession with a MockServletContext.
71 * @see MockServletContext
72 */
73 public MockHttpSession() {
74 this(new MockServletContext());
75 }
76
77
78 public long getCreationTime() {
79 return creationTime;
80 }
81
82 public String getId() {
83 return id;
84 }
85
86 public void access() {
87 this.lastAccessedTime = System.currentTimeMillis();
88 this.isNew = false;
89 }
90
91 public long getLastAccessedTime() {
92 return lastAccessedTime;
93 }
94
95 public ServletContext getServletContext() {
96 return servletContext;
97 }
98
99 public void setMaxInactiveInterval(int interval) {
100 maxInactiveInterval = interval;
101 }
102
103 public int getMaxInactiveInterval() {
104 return maxInactiveInterval;
105 }
106
107 /***
108 * @deprecated
109 */
110 public HttpSessionContext getSessionContext() {
111 throw new UnsupportedOperationException("getSessionContext");
112 }
113
114 public Object getAttribute(String name) {
115 return this.attributes.get(name);
116 }
117
118 public Object getValue(String name) {
119 return getAttribute(name);
120 }
121
122 public Enumeration getAttributeNames() {
123 return this.attributes.keys();
124 }
125
126 public String[] getValueNames() {
127 return (String[]) this.attributes.keySet().toArray(new String[this.attributes.size()]);
128 }
129
130 public void setAttribute(String name, Object value) {
131 if (value != null) {
132 this.attributes.put(name, value);
133 }
134 else {
135 this.attributes.remove(name);
136 }
137 }
138
139 public void putValue(String name, Object value) {
140 setAttribute(name, value);
141 }
142
143 public void removeAttribute(String name) {
144 this.attributes.remove(name);
145 }
146
147 public void removeValue(String name) {
148 removeAttribute(name);
149 }
150
151 public void invalidate() {
152 this.invalid = true;
153 this.attributes.clear();
154 }
155
156 public boolean isInvalid() {
157 return invalid;
158 }
159
160 public void setNew(boolean value) {
161 isNew = value;
162 }
163
164 public boolean isNew() {
165 return isNew;
166 }
167
168 }