001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package za.org.coefficient.util.testing;
020:
021: import java.util.Enumeration;
022: import java.util.Map;
023: import java.util.Vector;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.http.HttpSession;
027: import javax.servlet.http.HttpSessionContext;
028:
029: /**
030: * <p>Project: coefficient</p>
031: * <p>Description: This is an implementation of the HttpSession
032: * interace that is backed by a Map for the session data - it
033: * exists completely independently of any container - this makes
034: * it useful for running containerless unit tests on modules.</p>
035: * <p>Copyright: Copyright (c) 2003</p>
036: * <p>Company: CSIR</p>
037: * @author tfogwill
038: * @version 1.0
039: */
040: public class TestingHttpSession implements HttpSession {
041:
042: private long created = System.currentTimeMillis();
043: private Map data;
044:
045: public TestingHttpSession(Map sessionData) {
046: this .data = sessionData;
047: }
048:
049: /* (non-Javadoc)
050: * @see javax.servlet.http.HttpSession#getCreationTime()
051: */
052: public long getCreationTime() {
053: return created;
054: }
055:
056: /* (non-Javadoc)
057: * @see javax.servlet.http.HttpSession#getId()
058: */
059: public String getId() {
060: return "sessionID";
061: }
062:
063: /* (non-Javadoc)
064: * @see javax.servlet.http.HttpSession#getLastAccessedTime()
065: */
066: public long getLastAccessedTime() {
067: return System.currentTimeMillis() - 2000;
068: }
069:
070: /* (non-Javadoc)
071: * @see javax.servlet.http.HttpSession#getServletContext()
072: */
073: public ServletContext getServletContext() {
074: throw new UnsupportedOperationException();
075: }
076:
077: /* (non-Javadoc)
078: * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
079: */
080: public void setMaxInactiveInterval(int arg0) {
081: throw new UnsupportedOperationException();
082: }
083:
084: /* (non-Javadoc)
085: * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
086: */
087: public int getMaxInactiveInterval() {
088: return 99999;
089: }
090:
091: /* (non-Javadoc)
092: * @see javax.servlet.http.HttpSession#getSessionContext()
093: */
094: public HttpSessionContext getSessionContext() {
095: throw new UnsupportedOperationException();
096: }
097:
098: /* (non-Javadoc)
099: * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
100: */
101: public Object getAttribute(String arg0) {
102: return data.get(arg0);
103: }
104:
105: /* (non-Javadoc)
106: * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
107: */
108: public Object getValue(String arg0) {
109: return getAttribute(arg0);
110: }
111:
112: /* (non-Javadoc)
113: * @see javax.servlet.http.HttpSession#getAttributeNames()
114: */
115: public Enumeration getAttributeNames() {
116: return new Vector(data.keySet()).elements();
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.servlet.http.HttpSession#getValueNames()
121: */
122: public String[] getValueNames() {
123: return (String[]) data.keySet().toArray(new String[] {});
124: }
125:
126: /* (non-Javadoc)
127: * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
128: */
129: public void setAttribute(String arg0, Object arg1) {
130: data.put(arg0, arg1);
131: }
132:
133: /* (non-Javadoc)
134: * @see javax.servlet.http.HttpSession#putValue(java.lang.String, java.lang.Object)
135: */
136: public void putValue(String arg0, Object arg1) {
137: setAttribute(arg0, arg1);
138:
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
143: */
144: public void removeAttribute(String arg0) {
145: data.remove(arg0);
146: }
147:
148: /* (non-Javadoc)
149: * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
150: */
151: public void removeValue(String arg0) {
152: removeAttribute(arg0);
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.http.HttpSession#invalidate()
157: */
158: public void invalidate() {
159: data.clear();
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.http.HttpSession#isNew()
164: */
165: public boolean isNew() {
166: return false;
167: }
168:
169: }
|