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 org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import javax.servlet.RequestDispatcher;
24 import javax.servlet.Servlet;
25 import javax.servlet.ServletContext;
26
27 import java.io.InputStream;
28
29 import java.net.URL;
30
31 import java.util.Enumeration;
32 import java.util.HashMap;
33 import java.util.Set;
34
35 /***
36 * <p>Mock <strong>ServletContext</strong> object for low-level unit tests of
37 * Struts controller components. Coarser grained tests should be implemented
38 * in terms of the Cactus framework, instead of the mock object classes.</p>
39 *
40 * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
41 * create unit tests is provided, plus additional methods to configure this
42 * object as necessary. Methods for unsupported operations will throw
43 * <code>UnsupportedOperationException</code>.</p>
44 *
45 * <p><strong>WARNING</strong> - Because unit tests operate in a single
46 * threaded environment, no synchronization is performed.</p>
47 *
48 * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
49 * $
50 */
51 public class MockServletContext implements ServletContext {
52
53
54 /***
55 * <p> The set of servlet context attributes. </p>
56 */
57 protected HashMap attributes = new HashMap();
58
59 /***
60 * <p> Default destination for <code>LOG()</code> output. </p>
61 */
62 protected Log log = LogFactory.getLog(MockServletContext.class);
63
64 /***
65 * <p> The set of context initialization parameters. </p>
66 */
67 protected HashMap parameters = new HashMap();
68
69
70 public void addInitParameter(String name, String value) {
71 parameters.put(name, value);
72 }
73
74 public void setLog(Log log) {
75 this.log = log;
76 }
77
78
79 public Object getAttribute(String name) {
80 return (attributes.get(name));
81 }
82
83 public Enumeration getAttributeNames() {
84 return (new MockEnumeration(attributes.keySet().iterator()));
85 }
86
87 public ServletContext getContext(String uripath) {
88 throw new UnsupportedOperationException();
89 }
90
91 public String getInitParameter(String name) {
92 return ((String) parameters.get(name));
93 }
94
95 public Enumeration getInitParameterNames() {
96 return (new MockEnumeration(parameters.keySet().iterator()));
97 }
98
99 public int getMajorVersion() {
100 return (2);
101 }
102
103 public String getMimeType(String file) {
104 throw new UnsupportedOperationException();
105 }
106
107 public int getMinorVersion() {
108 return (3);
109 }
110
111 public RequestDispatcher getNamedDispatcher(String name) {
112 throw new UnsupportedOperationException();
113 }
114
115 public String getRealPath(String path) {
116 throw new UnsupportedOperationException();
117 }
118
119 public RequestDispatcher getRequestDispatcher(String path) {
120 throw new UnsupportedOperationException();
121 }
122
123 public URL getResource(String path) {
124 return this.getClass().getResource(path);
125
126
127 }
128
129 public InputStream getResourceAsStream(String path) {
130 return this.getClass().getResourceAsStream(path);
131
132
133 }
134
135 public Set getResourcePaths(String path) {
136 throw new UnsupportedOperationException();
137 }
138
139 public String getServerInfo() {
140 return ("MockServletContext/$Version$");
141 }
142
143 public Servlet getServlet(String name) {
144 throw new UnsupportedOperationException();
145 }
146
147 public String getServletContextName() {
148 return (getServerInfo());
149 }
150
151 public Enumeration getServletNames() {
152 throw new UnsupportedOperationException();
153 }
154
155 public Enumeration getServlets() {
156 throw new UnsupportedOperationException();
157 }
158
159 public void log(Exception exception, String message) {
160 log(message, exception);
161 }
162
163 public void log(String message) {
164 log.info(message);
165 }
166
167 public void log(String message, Throwable throwable) {
168 log.error(message, throwable);
169 }
170
171 public void removeAttribute(String name) {
172 attributes.remove(name);
173 }
174
175 public void setAttribute(String name, Object value) {
176 if (value == null) {
177 attributes.remove(name);
178 } else {
179 attributes.put(name, value);
180 }
181 }
182 }