View Javadoc

1   /*
2    * $Id: MockHttpSession.java 421119 2006-07-12 04:49:11Z wsmoak $
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  package org.apache.struts.mock;
19  
20  import javax.servlet.ServletContext;
21  import javax.servlet.http.HttpSession;
22  import javax.servlet.http.HttpSessionContext;
23  
24  import java.util.Enumeration;
25  import java.util.HashMap;
26  
27  /***
28   * <p>Mock <strong>HttpSession</strong> object for low-level unit tests of
29   * Struts controller components.  Coarser grained tests should be implemented
30   * in terms of the Cactus framework, instead of the mock object classes.</p>
31   *
32   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
33   * create unit tests is provided, plus additional methods to configure this
34   * object as necessary.  Methods for unsupported operations will throw
35   * <code>UnsupportedOperationException</code>.</p>
36   *
37   * <p><strong>WARNING</strong> - Because unit tests operate in a single
38   * threaded environment, no synchronization is performed.</p>
39   *
40   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
41   *          $
42   */
43  public class MockHttpSession implements HttpSession {
44      // ----------------------------------------------------- Instance Variables
45  
46      /***
47       * <p> The set of session attributes. </p>
48       */
49      protected HashMap attributes = new HashMap();
50  
51      /***
52       * <p> The ServletContext with which we are associated. </p>
53       */
54      protected ServletContext servletContext = null;
55  
56      // ----------------------------------------------------------- Constructors
57      public MockHttpSession() {
58          super();
59      }
60  
61      public MockHttpSession(ServletContext servletContext) {
62          super();
63          setServletContext(servletContext);
64      }
65  
66      // --------------------------------------------------------- Public Methods
67      public void setServletContext(ServletContext servletContext) {
68          this.servletContext = servletContext;
69      }
70  
71      // ---------------------------------------------------- HttpSession Methods
72      public Object getAttribute(String name) {
73          return (attributes.get(name));
74      }
75  
76      public Enumeration getAttributeNames() {
77          return (new MockEnumeration(attributes.keySet().iterator()));
78      }
79  
80      public long getCreationTime() {
81          throw new UnsupportedOperationException();
82      }
83  
84      public String getId() {
85          throw new UnsupportedOperationException();
86      }
87  
88      public long getLastAccessedTime() {
89          throw new UnsupportedOperationException();
90      }
91  
92      public int getMaxInactiveInterval() {
93          throw new UnsupportedOperationException();
94      }
95  
96      public ServletContext getServletContext() {
97          return (this.servletContext);
98      }
99  
100     public HttpSessionContext getSessionContext() {
101         throw new UnsupportedOperationException();
102     }
103 
104     public Object getValue(String name) {
105         throw new UnsupportedOperationException();
106     }
107 
108     public String[] getValueNames() {
109         throw new UnsupportedOperationException();
110     }
111 
112     public void invalidate() {
113         throw new UnsupportedOperationException();
114     }
115 
116     public boolean isNew() {
117         throw new UnsupportedOperationException();
118     }
119 
120     public void putValue(String name, Object value) {
121         throw new UnsupportedOperationException();
122     }
123 
124     public void removeAttribute(String name) {
125         attributes.remove(name);
126     }
127 
128     public void removeValue(String name) {
129         throw new UnsupportedOperationException();
130     }
131 
132     public void setAttribute(String name, Object value) {
133         if (value == null) {
134             attributes.remove(name);
135         } else {
136             attributes.put(name, value);
137         }
138     }
139 
140     public void setMaxInactiveInterval(int interval) {
141         throw new UnsupportedOperationException();
142     }
143 }