KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > testing > TestingHttpSession


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package za.org.coefficient.util.testing;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.http.HttpSessionContext JavaDoc;
28
29 /**
30  * <p>Project: coefficient</p>
31  * <p>Description: This is an implementation of the HttpSession
32  * interace that is backed by a Map for the session data - it
33  * exists completely independently of any container - this makes
34  * it useful for running containerless unit tests on modules.</p>
35  * <p>Copyright: Copyright (c) 2003</p>
36  * <p>Company: CSIR</p>
37  * @author tfogwill
38  * @version 1.0
39  */

40 public class TestingHttpSession implements HttpSession JavaDoc {
41     
42     private long created = System.currentTimeMillis();
43     private Map JavaDoc data;
44     
45     public TestingHttpSession(Map JavaDoc sessionData){
46         this.data = sessionData;
47     }
48
49     /* (non-Javadoc)
50      * @see javax.servlet.http.HttpSession#getCreationTime()
51      */

52     public long getCreationTime() {
53         return created;
54     }
55
56     /* (non-Javadoc)
57      * @see javax.servlet.http.HttpSession#getId()
58      */

59     public String JavaDoc getId() {
60         return "sessionID";
61     }
62
63     /* (non-Javadoc)
64      * @see javax.servlet.http.HttpSession#getLastAccessedTime()
65      */

66     public long getLastAccessedTime() {
67         return System.currentTimeMillis() - 2000;
68     }
69
70     /* (non-Javadoc)
71      * @see javax.servlet.http.HttpSession#getServletContext()
72      */

73     public ServletContext JavaDoc getServletContext() {
74         throw new UnsupportedOperationException JavaDoc();
75     }
76
77     /* (non-Javadoc)
78      * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
79      */

80     public void setMaxInactiveInterval(int arg0) {
81         throw new UnsupportedOperationException JavaDoc();
82     }
83
84     /* (non-Javadoc)
85      * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
86      */

87     public int getMaxInactiveInterval() {
88         return 99999;
89     }
90
91     /* (non-Javadoc)
92      * @see javax.servlet.http.HttpSession#getSessionContext()
93      */

94     public HttpSessionContext JavaDoc getSessionContext() {
95         throw new UnsupportedOperationException JavaDoc();
96     }
97
98     /* (non-Javadoc)
99      * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
100      */

101     public Object JavaDoc getAttribute(String JavaDoc 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 JavaDoc getValue(String JavaDoc arg0) {
109         return getAttribute(arg0);
110     }
111
112     /* (non-Javadoc)
113      * @see javax.servlet.http.HttpSession#getAttributeNames()
114      */

115     public Enumeration JavaDoc getAttributeNames() {
116         return new Vector JavaDoc(data.keySet()).elements();
117     }
118
119     /* (non-Javadoc)
120      * @see javax.servlet.http.HttpSession#getValueNames()
121      */

122     public String JavaDoc[] getValueNames() {
123         return (String JavaDoc[])data.keySet().toArray(new String JavaDoc[]{});
124     }
125
126     /* (non-Javadoc)
127      * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String, java.lang.Object)
128      */

129     public void setAttribute(String JavaDoc arg0, Object JavaDoc 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 JavaDoc arg0, Object JavaDoc 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 JavaDoc 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 JavaDoc 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 }
170
Popular Tags