001: package com.ecyrd.jspwiki;
002:
003: import java.util.Enumeration;
004: import java.util.HashMap;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.Random;
008: import java.security.SecureRandom;
009: import java.util.Vector;
010:
011: import javax.servlet.ServletContext;
012: import javax.servlet.http.HttpSession;
013:
014: /**
015: * This is a mock HttpSession implementation. It is used for testing.
016: * Most methods work as the should; notable exceptions include
017: * getSessionContext/getServletContext.
018: * @author Andrew R. Jaquith
019: */
020: public class TestHttpSession implements HttpSession {
021:
022: protected final long m_createTime;
023:
024: protected static final Random RANDOM = new SecureRandom();
025:
026: protected final String m_id;
027:
028: protected final long m_lastAccessTime;
029:
030: protected final Map m_attributes;
031:
032: protected int m_inactiveInterval;
033:
034: protected boolean m_invalidated;
035:
036: /**
037: * Constructs a new instance of this class.
038: */
039: public TestHttpSession() {
040: m_createTime = System.currentTimeMillis();
041: m_lastAccessTime = m_createTime;
042: m_id = String.valueOf(RANDOM.nextLong());
043: m_attributes = new HashMap();
044: m_invalidated = false;
045: m_inactiveInterval = 1800;
046: }
047:
048: /**
049: * @see javax.servlet.http.HttpSession#getCreationTime()
050: */
051: public long getCreationTime() {
052: return m_createTime;
053: }
054:
055: /**
056: * @see javax.servlet.http.HttpSession#getId()
057: */
058: public String getId() {
059: return m_id;
060: }
061:
062: /**
063: * @see javax.servlet.http.HttpSession#getLastAccessedTime()
064: */
065: public long getLastAccessedTime() {
066: return m_lastAccessTime;
067: }
068:
069: /**
070: * Always returns null.
071: * @see javax.servlet.http.HttpSession#getServletContext()
072: */
073: public ServletContext getServletContext() {
074: return null;
075: }
076:
077: /**
078: * No-op.
079: * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
080: */
081: public void setMaxInactiveInterval(int arg0) {
082: m_inactiveInterval = arg0;
083: }
084:
085: /**
086: * Returns the number of seconds of allowed inactivity;
087: * returns 1800 seconds (30 minutes) by default.
088: * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
089: */
090: public int getMaxInactiveInterval() {
091: return m_inactiveInterval;
092: }
093:
094: /**
095: * No-op; returns null;
096: * @see javax.servlet.http.HttpSession#getSessionContext()
097: * @deprecated
098: */
099: public javax.servlet.http.HttpSessionContext getSessionContext() {
100: return null;
101: }
102:
103: /**
104: * Returns the attribute with a given name, or null of no object is bound
105: * with that name.
106: * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
107: */
108: public Object getAttribute(String arg0) {
109: return m_attributes.get(arg0);
110: }
111:
112: /**
113: * Delegates to {@link #getAttribute(String)}.
114: * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
115: * @deprecated
116: */
117: public Object getValue(String arg0) {
118: return getAttribute(arg0);
119: }
120:
121: /**
122: * @see javax.servlet.http.HttpSession#getAttributeNames()
123: */
124: public Enumeration getAttributeNames() {
125: Vector items = new Vector();
126: for (Iterator it = m_attributes.keySet().iterator(); it
127: .hasNext();) {
128: items.add(it.next());
129: }
130: return items.elements();
131: }
132:
133: /**
134: * @see javax.servlet.http.HttpSession#getValueNames()
135: * @deprecated
136: */
137: public String[] getValueNames() {
138: Vector items = new Vector();
139: for (Iterator it = m_attributes.keySet().iterator(); it
140: .hasNext();) {
141: items.add(it.next());
142: }
143: return (String[]) items.toArray(new String[items.size()]);
144: }
145:
146: /**
147: * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
148: * java.lang.Object)
149: */
150: public void setAttribute(String arg0, Object arg1) {
151: m_attributes.put(arg0, arg1);
152: }
153:
154: /**
155: * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
156: * java.lang.Object)
157: * @deprecated
158: */
159: public void putValue(String arg0, Object arg1) {
160: setAttribute(arg0, arg1);
161: }
162:
163: /**
164: * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
165: */
166: public void removeAttribute(String arg0) {
167: m_attributes.remove(arg0);
168: }
169:
170: /**
171: * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
172: * @deprecated
173: */
174: public void removeValue(String arg0) {
175: removeAttribute(arg0);
176: }
177:
178: /**
179: * Invalidates the session.
180: * @see javax.servlet.http.HttpSession#invalidate()
181: */
182: public void invalidate() {
183: if (m_invalidated) {
184: throw new IllegalStateException(
185: "Session is already invalidated.");
186: }
187: m_invalidated = true;
188: m_attributes.clear();
189: }
190:
191: /**
192: * Always returns false.
193: * @see javax.servlet.http.HttpSession#isNew()
194: */
195: public boolean isNew() {
196: return false;
197: }
198:
199: }
|