1 package servletunit;
2
3 import javax.servlet.ServletContext;
4 import javax.servlet.http.HttpSession;
5 import javax.servlet.http.HttpSessionContext;
6 import java.util.Enumeration;
7 import java.util.Hashtable;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class HttpSessionSimulator implements HttpSession
30 {
31 private Hashtable values;
32 private boolean valid = true;
33 private ServletContext context;
34
35 public HttpSessionSimulator(ServletContext context)
36 {
37 this.context = context;
38 values = new Hashtable();
39 }
40
41 public Object getAttribute(String s) throws IllegalStateException
42 {
43 checkValid();
44 return values.get(s);
45 }
46
47 public Enumeration getAttributeNames() throws IllegalStateException
48 {
49 checkValid();
50 return values.keys();
51 }
52
53 public long getCreationTime() throws IllegalStateException
54 {
55 checkValid();
56 return -1;
57 }
58
59 public String getId()
60 {
61 return "-9999";
62 }
63
64 public long getLastAccessedTime()
65 {
66 return -1;
67 }
68
69 public int getMaxInactiveInterval() throws IllegalStateException
70 {
71 checkValid();
72 return -1;
73 }
74
75
76
77
78 public HttpSessionContext getSessionContext()
79 {
80 throw new UnsupportedOperationException("getSessionContext not supported!");
81 }
82
83 public Object getValue(String s) throws IllegalStateException
84 {
85 checkValid();
86 return values.get(s);
87 }
88
89 public String[] getValueNames() throws IllegalStateException
90 {
91 checkValid();
92 return (String[]) values.keySet().toArray();
93 }
94
95 public void invalidate() throws IllegalStateException
96 {
97 checkValid();
98 this.valid = false;
99 }
100
101 public boolean isNew() throws IllegalStateException
102 {
103 checkValid();
104 return false;
105 }
106
107 public void putValue(String s, Object obj) throws IllegalStateException
108 {
109 checkValid();
110 values.put(s,obj);
111 }
112
113 public void removeAttribute(String s) throws IllegalStateException
114 {
115 checkValid();
116 values.remove(s);
117 }
118
119 public void removeValue(String s) throws IllegalStateException
120 {
121 checkValid();
122 values.remove(s);
123 }
124
125 public void setAttribute(String s, Object obj) throws IllegalStateException
126 {
127 checkValid();
128 if (obj == null)
129 removeValue(s);
130 else
131 values.put(s,obj);
132 }
133
134 public void setMaxInactiveInterval(int i)
135 {
136 }
137
138 public ServletContext getServletContext() {
139 return this.context;
140 }
141
142 private void checkValid() throws IllegalStateException {
143 if (!valid)
144 throw new IllegalStateException("session has been invalidated!");
145 }
146
147 protected boolean isValid() {
148 return valid;
149 }
150 }