001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: MockSession.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.test;
009:
010: import java.util.Collections;
011: import java.util.Enumeration;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import javax.servlet.ServletContext;
016: import javax.servlet.http.HttpSession;
017: import javax.servlet.http.HttpSessionContext;
018:
019: class MockSession implements HttpSession {
020: final static public String SESSION_COOKIE_NAME = "JSESSION";
021:
022: private static int sNextId = 1;
023:
024: private MockConversation mMockConversation;
025: private String mId = Integer.toString(sNextId++);
026: private long mCreationTime = System.currentTimeMillis();
027: private long mLastAccessTime = System.currentTimeMillis();
028: private int mMaxInactiveInterval;
029: private Map<String, Object> mAttributes = new HashMap<String, Object>();
030: private boolean mIsNew = true;
031: private boolean mInvalid = false;
032:
033: MockSession(MockConversation conversation) {
034: mMockConversation = conversation;
035: }
036:
037: public long getCreationTime() {
038: if (mInvalid)
039: throw new IllegalStateException();
040:
041: return mCreationTime;
042: }
043:
044: public String getId() {
045: if (mInvalid)
046: throw new IllegalStateException();
047:
048: return mId;
049: }
050:
051: public long getLastAccessedTime() {
052: if (mInvalid)
053: throw new IllegalStateException();
054:
055: return mLastAccessTime;
056: }
057:
058: public void setMaxInactiveInterval(int interval) {
059: if (mInvalid)
060: throw new IllegalStateException();
061:
062: mMaxInactiveInterval = interval;
063: }
064:
065: public int getMaxInactiveInterval() {
066: if (mInvalid)
067: throw new IllegalStateException();
068:
069: return mMaxInactiveInterval;
070: }
071:
072: public Object getAttribute(String name) {
073: if (mInvalid)
074: throw new IllegalStateException();
075:
076: return mAttributes.get(name);
077: }
078:
079: public Enumeration getAttributeNames() {
080: if (mInvalid)
081: throw new IllegalStateException();
082:
083: return Collections.enumeration(mAttributes.keySet());
084: }
085:
086: public void setAttribute(String name, Object value) {
087: if (mInvalid)
088: throw new IllegalStateException();
089:
090: mAttributes.put(name, value);
091: }
092:
093: public void removeAttribute(String name) {
094: if (mInvalid)
095: throw new IllegalStateException();
096:
097: mAttributes.remove(name);
098: }
099:
100: public void invalidate() {
101: mMockConversation.removeSession(mId);
102: mInvalid = true;
103: mAttributes.clear();
104: mId = null;
105: }
106:
107: public boolean isNew() {
108: return mIsNew;
109: }
110:
111: public Object getValue(String name) {
112: if (mInvalid)
113: throw new IllegalStateException();
114:
115: return getAttribute(name);
116: }
117:
118: public String[] getValueNames() {
119: if (mInvalid)
120: throw new IllegalStateException();
121:
122: String[] names_array = new String[mAttributes.size()];
123: mAttributes.keySet().toArray(names_array);
124: return names_array;
125: }
126:
127: public void putValue(String name, Object value) {
128: if (mInvalid)
129: throw new IllegalStateException();
130:
131: setAttribute(name, value);
132: }
133:
134: public void removeValue(String name) {
135: if (mInvalid)
136: throw new IllegalStateException();
137:
138: removeAttribute(name);
139: }
140:
141: public ServletContext getServletContext() {
142: return null;
143: }
144:
145: public HttpSessionContext getSessionContext() {
146: return null;
147: }
148:
149: void access() {
150: mIsNew = false;
151: mLastAccessTime = System.currentTimeMillis();
152: }
153:
154: boolean isValid() {
155: return !mInvalid;
156: }
157: }
|