KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockHttpSession


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.servlet.http.HttpSessionContext JavaDoc;
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.http.HttpSession JavaDoc;
17
18
19 /**
20  * This is a Mock Session
21  *
22  * @author Brian Pontarelli
23  * @since 2.0
24  * @version 2.0
25  */

26 public class MockHttpSession implements HttpSession JavaDoc {
27
28     private Map JavaDoc attributes;
29     private long creation;
30     private int interval;
31     private long lastAccessed;
32     private ServletContext JavaDoc context;
33
34     /**
35      * Constructs a new MockHttpSession
36      */

37     public MockHttpSession() {
38         attributes = new HashMap JavaDoc();
39         creation = System.currentTimeMillis();
40         interval = 1800;
41         lastAccessed = System.currentTimeMillis();
42         context = new MockServletContext();
43     }
44
45     /**
46      */

47     public long getCreationTime() {
48         lastAccessed = System.currentTimeMillis();
49         return creation;
50     }
51
52     /**
53      */

54     public String JavaDoc getId() {
55         lastAccessed = System.currentTimeMillis();
56         return "non-valid";
57     }
58
59     /**
60      */

61     public int getMaxInactiveInterval() {
62         lastAccessed = System.currentTimeMillis();
63         return interval;
64     }
65
66     /**
67      */

68     public void setMaxInactiveInterval(int interval) {
69         this.interval = interval;
70         lastAccessed = System.currentTimeMillis();
71     }
72
73     /**
74      */

75     public long getLastAccessedTime() {
76         lastAccessed = System.currentTimeMillis();
77         return lastAccessed;
78     }
79
80     /**
81      */

82     public ServletContext JavaDoc getServletContext() {
83         lastAccessed = System.currentTimeMillis();
84         return context;
85     }
86
87     /**
88      * @deprecated
89      */

90     public HttpSessionContext JavaDoc getSessionContext() {
91         throw new UnsupportedOperationException JavaDoc();
92     }
93
94     /**
95      */

96     public Object JavaDoc getAttribute(String JavaDoc name) {
97         lastAccessed = System.currentTimeMillis();
98         return attributes.get(name);
99     }
100
101     /**
102      * @deprecated
103      */

104     public Object JavaDoc getValue(String JavaDoc name) {
105         throw new UnsupportedOperationException JavaDoc();
106     }
107
108     /**
109      */

110     public void removeAttribute(String JavaDoc name) {
111         lastAccessed = System.currentTimeMillis();
112         attributes.remove(name);
113     }
114
115     /**
116      */

117     public Enumeration JavaDoc getAttributeNames() {
118         lastAccessed = System.currentTimeMillis();
119         final Iterator JavaDoc iter = attributes.keySet().iterator();
120         return new Enumeration JavaDoc() {
121             public boolean hasMoreElements() {
122                 return iter.hasNext();
123             }
124             public Object JavaDoc nextElement() {
125                 return iter.next();
126             }
127         };
128     }
129
130     /**
131      * @deprecated
132      */

133     public String JavaDoc[] getValueNames() {
134         throw new UnsupportedOperationException JavaDoc();
135     }
136
137     /**
138      */

139     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
140         lastAccessed = System.currentTimeMillis();
141         attributes.put(name, value);
142     }
143
144     /**
145      * @deprecated
146      */

147     public void putValue(String JavaDoc name, Object JavaDoc value) {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150
151     /**
152      * @deprecated
153      */

154     public void removeValue(String JavaDoc A) {
155         throw new UnsupportedOperationException JavaDoc();
156     }
157
158     /**
159      */

160     public void invalidate() {
161         lastAccessed = System.currentTimeMillis();
162     }
163
164     /**
165      */

166     public boolean isNew() {
167         lastAccessed = System.currentTimeMillis();
168         return false;
169     }
170 }
171
Popular Tags