1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
57 public MockHttpSession() {
58 super();
59 }
60
61 public MockHttpSession(ServletContext servletContext) {
62 super();
63 setServletContext(servletContext);
64 }
65
66
67 public void setServletContext(ServletContext servletContext) {
68 this.servletContext = servletContext;
69 }
70
71
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 }