2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.util.ArrayList;
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpSession;
28  import javax.servlet.http.HttpSessionAttributeListener;
29  import javax.servlet.http.HttpSessionBindingEvent;
30  import javax.servlet.http.HttpSessionContext;
31  
32  /***
33   * <p>Mock implementation of <code>HttpSession</code>.</p>
34   *
35   * $Id$
36   */
37  
38  public class MockHttpSession implements HttpSession {
39  
40  
41      // ------------------------------------------------------------ Constructors
42  
43  
44      /***
45       * <p>Configure a default instance.</p>
46       */
47      public MockHttpSession() {
48  
49          super();
50  
51      }
52  
53  
54      /***
55       * <p>Configure a session instance associated with the specified
56       * servlet context.</p>
57       *
58       * @param servletContext The associated servlet context
59       */
60      public MockHttpSession(ServletContext servletContext) {
61  
62          super();
63          setServletContext(servletContext);
64  
65      }
66  
67  
68      // ----------------------------------------------------- Mock Object Methods
69  
70  
71      /***
72       * <p>Add a new listener instance that should be notified about
73       * attribute changes.</p>
74       *
75       * @param listener The new listener to be added
76       */
77      public void addAttributeListener(HttpSessionAttributeListener listener) {
78          attributeListeners.add(listener);
79      }
80  
81  
82      /***
83       * <p>Set the ServletContext associated with this session.</p>
84       *
85       * @param servletContext The associated servlet context
86       */
87      public void setServletContext(ServletContext servletContext) {
88  
89          this.servletContext = servletContext;
90  
91      }
92  
93  
94      // ------------------------------------------------------ Instance Variables
95  
96  
97      private List attributeListeners = new ArrayList();
98      private HashMap attributes = new HashMap();
99      private String id = "123";
100     private ServletContext servletContext = null;
101 
102 
103     // ---------------------------------------------------------- Public Methods
104 
105 
106     /***
107      * <p>Set the session identifier of this session.</p>
108      *
109      * @param id The new session identifier
110      */
111     public void setId(String id) {
112         this.id = id;
113     }
114 
115 
116     // ----------------------------------------------------- HttpSession Methods
117 
118 
119     /*** {@inheritDoc} */
120     public Object getAttribute(String name) {
121 
122         return attributes.get(name);
123 
124     }
125 
126 
127     /*** {@inheritDoc} */
128     public Enumeration getAttributeNames() {
129 
130         return new MockEnumeration(attributes.keySet().iterator());
131 
132     }
133 
134 
135     /*** {@inheritDoc} */
136     public long getCreationTime() {
137 
138         throw new UnsupportedOperationException();
139 
140     }
141 
142 
143     /*** {@inheritDoc} */
144     public String getId() {
145 
146         return this.id;
147 
148     }
149 
150 
151     /*** {@inheritDoc} */
152     public long getLastAccessedTime() {
153 
154         throw new UnsupportedOperationException();
155 
156     }
157 
158 
159     /*** {@inheritDoc} */
160     public int getMaxInactiveInterval() {
161 
162         throw new UnsupportedOperationException();
163 
164     }
165 
166 
167     /*** {@inheritDoc} */
168     public ServletContext getServletContext() {
169 
170         return this.servletContext;
171 
172     }
173 
174 
175     /*** {@inheritDoc} */
176     public HttpSessionContext getSessionContext() {
177 
178         throw new UnsupportedOperationException();
179 
180     }
181 
182 
183     /*** {@inheritDoc} */
184     public Object getValue(String name) {
185 
186         throw new UnsupportedOperationException();
187 
188     }
189 
190 
191     /*** {@inheritDoc} */
192     public String[] getValueNames() {
193 
194         throw new UnsupportedOperationException();
195 
196     }
197 
198 
199     /*** {@inheritDoc} */
200     public void invalidate() {
201 
202         throw new UnsupportedOperationException();
203 
204     }
205 
206 
207     /*** {@inheritDoc} */
208     public boolean isNew() {
209 
210         throw new UnsupportedOperationException();
211 
212     }
213 
214 
215     /*** {@inheritDoc} */
216     public void putValue(String name, Object value) {
217 
218         throw new UnsupportedOperationException();
219 
220     }
221 
222 
223     /*** {@inheritDoc} */
224     public void removeAttribute(String name) {
225 
226         if (attributes.containsKey(name)) {
227             Object value = attributes.remove(name);
228             fireAttributeRemoved(name, value);
229         }
230 
231     }
232 
233 
234     /*** {@inheritDoc} */
235     public void removeValue(String name) {
236 
237         throw new UnsupportedOperationException();
238 
239     }
240 
241 
242     /*** {@inheritDoc} */
243     public void setAttribute(String name, Object value) {
244 
245         if (name == null) {
246             throw new IllegalArgumentException("Attribute name cannot be null");
247         }
248         if (value == null) {
249             removeAttribute(name);
250             return;
251         }
252         if (attributes.containsKey(name)) {
253             Object oldValue = attributes.get(name);
254             attributes.put(name, value);
255             fireAttributeReplaced(name, oldValue);
256         } else {
257             attributes.put(name, value);
258             fireAttributeAdded(name, value);
259         }
260 
261     }
262 
263 
264     /*** {@inheritDoc} */
265     public void setMaxInactiveInterval(int interval) {
266 
267         throw new UnsupportedOperationException();
268 
269     }
270 
271 
272     // --------------------------------------------------------- Support Methods
273 
274 
275     /***
276      * <p>Fire an attribute added event to interested listeners.</p>
277      *
278      * @param key Attribute whose value was added
279      * @param value The new value
280      */
281     private void fireAttributeAdded(String key, Object value) {
282         if (attributeListeners.size() < 1) {
283             return;
284         }
285         HttpSessionBindingEvent event =
286                 new HttpSessionBindingEvent(this, key, value);
287         Iterator listeners = attributeListeners.iterator();
288         while (listeners.hasNext()) {
289             HttpSessionAttributeListener listener =
290                     (HttpSessionAttributeListener) listeners.next();
291             listener.attributeAdded(event);
292         }
293     }
294 
295 
296     /***
297      * <p>Fire an attribute removed event to interested listeners.</p>
298      *
299      * @param key Attribute whose value was removed
300      * @param value The removed value
301      */
302     private void fireAttributeRemoved(String key, Object value) {
303         if (attributeListeners.size() < 1) {
304             return;
305         }
306         HttpSessionBindingEvent event =
307                 new HttpSessionBindingEvent(this, key, value);
308         Iterator listeners = attributeListeners.iterator();
309         while (listeners.hasNext()) {
310             HttpSessionAttributeListener listener =
311                     (HttpSessionAttributeListener) listeners.next();
312             listener.attributeRemoved(event);
313         }
314     }
315 
316 
317     /***
318      * <p>Fire an attribute replaced event to interested listeners.</p>
319      *
320      * @param key Attribute whose value was replaced
321      * @param value The original value
322      */
323     private void fireAttributeReplaced(String key, Object value) {
324         if (attributeListeners.size() < 1) {
325             return;
326         }
327         HttpSessionBindingEvent event =
328                 new HttpSessionBindingEvent(this, key, value);
329         Iterator listeners = attributeListeners.iterator();
330         while (listeners.hasNext()) {
331             HttpSessionAttributeListener listener =
332                     (HttpSessionAttributeListener) listeners.next();
333             listener.attributeReplaced(event);
334         }
335     }
336 
337 
338 }