KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > servletunit > HttpSessionSimulator


1 package servletunit;
2
3 import javax.servlet.ServletContext JavaDoc;
4 import javax.servlet.http.HttpSession JavaDoc;
5 import javax.servlet.http.HttpSessionContext JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.Hashtable JavaDoc;
8
9 // StrutsTestCase - a JUnit extension for testing Struts actions
10
// within the context of the ActionServlet.
11
// Copyright (C) 2002 Deryl Seale
12
//
13
// This library is free software; you can redistribute it and/or
14
// modify it under the terms of the Apache Software License as
15
// published by the Apache Software Foundation; either version 1.1
16
// of the License, or (at your option) any later version.
17
//
18
// This library is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// Apache Software Foundation Licens for more details.
22
//
23
// You may view the full text here: http://www.apache.org/LICENSE.txt
24

25 /**
26  * This class simulates an HttpSession object. You can actually work with sessions. The simulation is done using a static
27  * session object inside HttpServletRequest.
28  */

29 public class HttpSessionSimulator implements HttpSession JavaDoc
30 {
31     private Hashtable JavaDoc values;
32     private boolean valid = true;
33     private ServletContext JavaDoc context;
34
35     public HttpSessionSimulator(ServletContext JavaDoc context)
36     {
37         this.context = context;
38         values = new Hashtable JavaDoc();
39     }
40
41     public Object JavaDoc getAttribute(String JavaDoc s) throws IllegalStateException JavaDoc
42     {
43         checkValid();
44         return values.get(s);
45     }
46
47     public Enumeration JavaDoc getAttributeNames() throws IllegalStateException JavaDoc
48     {
49         checkValid();
50         return values.keys();
51     }
52
53     public long getCreationTime() throws IllegalStateException JavaDoc
54     {
55         checkValid();
56         return -1;
57     }
58
59     public String JavaDoc getId()
60     {
61         return "-9999";
62     }
63
64     public long getLastAccessedTime()
65     {
66         return -1;
67     }
68
69     public int getMaxInactiveInterval() throws IllegalStateException JavaDoc
70     {
71         checkValid();
72         return -1;
73     }
74
75     /**
76      * This method is not supported.
77      */

78     public HttpSessionContext JavaDoc getSessionContext()
79     {
80         throw new UnsupportedOperationException JavaDoc("getSessionContext not supported!");
81     }
82
83     public Object JavaDoc getValue(String JavaDoc s) throws IllegalStateException JavaDoc
84     {
85         checkValid();
86         return values.get(s);
87     }
88
89     public String JavaDoc[] getValueNames() throws IllegalStateException JavaDoc
90     {
91         checkValid();
92         return (String JavaDoc[]) values.keySet().toArray();
93     }
94
95     public void invalidate() throws IllegalStateException JavaDoc
96     {
97         checkValid();
98         this.valid = false;
99     }
100
101     public boolean isNew() throws IllegalStateException JavaDoc
102     {
103         checkValid();
104         return false;
105     }
106
107     public void putValue(String JavaDoc s, Object JavaDoc obj) throws IllegalStateException JavaDoc
108     {
109         checkValid();
110         values.put(s,obj);
111     }
112
113     public void removeAttribute(String JavaDoc s) throws IllegalStateException JavaDoc
114     {
115         checkValid();
116         values.remove(s);
117     }
118
119     public void removeValue(String JavaDoc s) throws IllegalStateException JavaDoc
120     {
121         checkValid();
122         values.remove(s);
123     }
124
125     public void setAttribute(String JavaDoc s, Object JavaDoc obj) throws IllegalStateException JavaDoc
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 JavaDoc getServletContext() {
139         return this.context;
140     }
141
142     private void checkValid() throws IllegalStateException JavaDoc {
143         if (!valid)
144             throw new IllegalStateException JavaDoc("session has been invalidated!");
145     }
146
147     protected boolean isValid() {
148         return valid;
149     }
150 }
151
Popular Tags