KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > MockHttpSession


1 /*
2  * $Id: MockHttpSession.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.mock;
21
22
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import javax.servlet.http.HttpSessionContext JavaDoc;
28
29
30
31 /**
32  * <p>Mock <strong>HttpSession</strong> object for low-level unit tests
33  * of Struts controller components. Coarser grained tests should be
34  * implemented in terms of the Cactus framework, instead of the mock
35  * object classes.</p>
36  *
37  * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
38  * create unit tests is provided, plus additional methods to configure this
39  * object as necessary. Methods for unsupported operations will throw
40  * <code>UnsupportedOperationException</code>.</p>
41  *
42  * <p><strong>WARNING</strong> - Because unit tests operate in a single
43  * threaded environment, no synchronization is performed.</p>
44  *
45  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
46  */

47
48 public class MockHttpSession implements HttpSession JavaDoc {
49
50
51
52     // ----------------------------------------------------------- Constructors
53

54
55     public MockHttpSession() {
56         super();
57     }
58
59
60     public MockHttpSession(ServletContext JavaDoc servletContext) {
61         super();
62         setServletContext(servletContext);
63     }
64
65
66
67     // ----------------------------------------------------- Instance Variables
68

69
70     /**
71      * The set of session attributes.
72      */

73     protected HashMap JavaDoc attributes = new HashMap JavaDoc();
74
75
76     /**
77      * The ServletContext with which we are associated.
78      */

79     protected ServletContext JavaDoc servletContext = null;
80
81
82     // --------------------------------------------------------- Public Methods
83

84
85     public void setServletContext(ServletContext JavaDoc servletContext) {
86         this.servletContext = servletContext;
87     }
88
89
90     // ---------------------------------------------------- HttpSession Methods
91

92
93     public Object JavaDoc getAttribute(String JavaDoc name) {
94         return (attributes.get(name));
95     }
96
97
98     public Enumeration JavaDoc getAttributeNames() {
99         return (new MockEnumeration(attributes.keySet().iterator()));
100     }
101
102
103     public long getCreationTime() {
104         throw new UnsupportedOperationException JavaDoc();
105     }
106
107
108     public String JavaDoc getId() {
109         throw new UnsupportedOperationException JavaDoc();
110     }
111
112
113     public long getLastAccessedTime() {
114         throw new UnsupportedOperationException JavaDoc();
115     }
116
117
118     public int getMaxInactiveInterval() {
119         throw new UnsupportedOperationException JavaDoc();
120     }
121
122
123     public ServletContext JavaDoc getServletContext() {
124         return (this.servletContext);
125     }
126
127
128     public HttpSessionContext JavaDoc getSessionContext() {
129         throw new UnsupportedOperationException JavaDoc();
130     }
131
132
133     public Object JavaDoc getValue(String JavaDoc name) {
134         throw new UnsupportedOperationException JavaDoc();
135     }
136
137
138     public String JavaDoc[] getValueNames() {
139         throw new UnsupportedOperationException JavaDoc();
140     }
141
142
143     public void invalidate() {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146
147
148     public boolean isNew() {
149         throw new UnsupportedOperationException JavaDoc();
150     }
151
152
153     public void putValue(String JavaDoc name, Object JavaDoc value) {
154         throw new UnsupportedOperationException JavaDoc();
155     }
156
157
158     public void removeAttribute(String JavaDoc name) {
159         attributes.remove(name);
160     }
161
162
163     public void removeValue(String JavaDoc name) {
164         throw new UnsupportedOperationException JavaDoc();
165     }
166
167
168     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
169         if (value == null) {
170             attributes.remove(name);
171         } else {
172             attributes.put(name, value);
173         }
174     }
175
176
177     public void setMaxInactiveInterval(int interval) {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180
181
182 }
183
Popular Tags