KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockServletContext


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.Servlet JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24
25
26 /**
27  * This class is a mock servlet context
28  *
29  * @author Brian Pontarelli
30  * @since 2.0
31  * @version 2.0
32  */

33 public class MockServletContext implements ServletContext JavaDoc {
34
35     private Map JavaDoc attributes;
36     private Map JavaDoc initParameters;
37
38
39     /**
40      * Constructs a new <code>MockServletContext</code> object
41      */

42     public MockServletContext() {
43         attributes = new HashMap JavaDoc();
44         initParameters = new HashMap JavaDoc();
45     }
46
47
48     /**
49      * Returns the attribute stored under the given name
50      *
51      * @param name The name of the attribute
52      * @return The attribute or null
53      */

54     public Object JavaDoc getAttribute(String JavaDoc name) {
55         return attributes.get(name);
56     }
57
58     /**
59      * Returns an enumeration of all the attribute names
60      *
61      * @return An Enumeration of all the attribute names
62      */

63     public Enumeration JavaDoc getAttributeNames(){
64         final Iterator JavaDoc iter = attributes.keySet().iterator();
65         return new Enumeration JavaDoc() {
66             public boolean hasMoreElements() {
67                 return iter.hasNext();
68             }
69             public java.lang.Object JavaDoc nextElement() {
70                 return iter.next();
71             }
72         };
73     }
74
75     /**
76      * @throws UnsupportedOperationException Always
77      */

78     public ServletContext JavaDoc getContext(String JavaDoc uri) {
79         throw new UnsupportedOperationException JavaDoc();
80     }
81
82     /**
83      * Returns the init parameter (context parameter) stored under the given name
84      *
85      * @param name The name of the context parameter
86      * @return The context parameter stored under the given name, or null
87      */

88     public String JavaDoc getInitParameter(String JavaDoc name) {
89         return (String JavaDoc) initParameters.get(name);
90     }
91
92     /**
93      */

94     public Enumeration JavaDoc getInitParameterNames() {
95         final Iterator JavaDoc iter = initParameters.keySet().iterator();
96         return new Enumeration JavaDoc() {
97             public boolean hasMoreElements() {
98                 return iter.hasNext();
99             }
100             public java.lang.Object JavaDoc nextElement() {
101                 return iter.next();
102             }
103         };
104     }
105
106     /**
107      */

108     public int getMajorVersion() {
109         return 2;
110     }
111
112     /**
113      */

114     public String JavaDoc getMimeType(String JavaDoc file) {
115         return null;
116     }
117
118     /**
119      */

120     public int getMinorVersion() {
121         return 3;
122     }
123
124     /**
125      */

126     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc uri) {
127         return getRequestDispatcher(uri);
128     }
129
130     /**
131      */

132     public String JavaDoc getRealPath(String JavaDoc path) {
133         File JavaDoc file = new File JavaDoc(path);
134         return file.getAbsolutePath();
135     }
136
137     /**
138      */

139     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc uri) {
140         return new MockRequestDispatcher(uri);
141     }
142
143     /**
144      * This creates a new java.io.File using the given path and then converts
145      * that File to a URL. If the File doesn't exist, this returns null.
146      * The path can contain any type of separator and it will be converted into
147      * the correct version.
148      */

149     public URL JavaDoc getResource(String JavaDoc path) {
150         File JavaDoc file = new File JavaDoc(convertPath(path));
151
152         if (file.exists() && file.isFile()) {
153             try {
154                 return file.toURL();
155             } catch (MalformedURLException JavaDoc murle) {
156                 return null;
157             }
158         }
159
160         return null;
161     }
162
163     /**
164      * Returns the stream that is an InputStream to the File (if it exists)
165      * constructed using the given path. If it doesn't exist, this returns null
166      * The path can contain any type of separator and it will be converted into
167      * the correct version.
168      */

169     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
170         File JavaDoc file = new File JavaDoc(convertPath(path));
171
172         if (file.exists() && file.isFile()) {
173             try {
174                 return new FileInputStream JavaDoc(file);
175             } catch (FileNotFoundException JavaDoc fnfe) {
176                 return null;
177             }
178         }
179
180         return null;
181     }
182
183     /**
184      */

185     public Set JavaDoc getResourcePaths(String JavaDoc path) {
186         throw new UnsupportedOperationException JavaDoc();
187     }
188
189     /**
190      */

191     public String JavaDoc getServerInfo(){
192         return "local";
193     }
194
195     /**
196      * @deprecated
197      */

198     public Servlet JavaDoc getServlet(String JavaDoc name) {
199         throw new UnsupportedOperationException JavaDoc();
200     }
201
202     /**
203      */

204     public String JavaDoc getServletContextName() {
205         return "local";
206     }
207
208     /**
209      * @deprecated
210      */

211     public Enumeration JavaDoc getServletNames() {
212         throw new UnsupportedOperationException JavaDoc();
213     }
214
215     /**
216      * @deprecated
217      */

218     public Enumeration JavaDoc getServlets() {
219         throw new UnsupportedOperationException JavaDoc();
220     }
221
222     /**
223      * @deprecated
224      */

225     public void log(Exception JavaDoc exception, String JavaDoc string) {
226         throw new UnsupportedOperationException JavaDoc();
227     }
228
229     /**
230      */

231     public void log(String JavaDoc msg) {
232         System.out.println(msg);
233     }
234
235     /**
236      */

237     public void log(String JavaDoc msg, Throwable JavaDoc t) {
238         System.out.println(msg);
239         t.printStackTrace(System.out);
240     }
241
242     /**
243      */

244     public void removeAttribute(String JavaDoc name) {
245         attributes.remove(name);
246     }
247
248     /**
249      */

250     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
251         attributes.put(name, value);
252     }
253
254
255     //-------------------------------------------------------------------------
256
// Other methods
257
//-------------------------------------------------------------------------
258

259
260     /**
261      * Sets an init parameter with the given name to the given value
262      */

263     public void setInitParameter(String JavaDoc name, String JavaDoc value) {
264         initParameters.put(name, value);
265     }
266
267     /**
268      * Returns the system-dependent version of the given file or path. This is
269      * done by converting all the slash characters to the correct slash for the
270      * current operating system
271      *
272      * @param path The path to convert to the current operating system
273      * @return The converted path
274      */

275     public String JavaDoc convertPath(String JavaDoc path) {
276
277         if (File.separatorChar == '\\') {
278             return path.replace('/', '\\');
279         }
280
281         return path.replace('\\', '/');
282     }
283 }
284
Popular Tags