KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > mock > MockContext


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test.mock;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.servlet.RequestDispatcher JavaDoc;
29 import javax.servlet.Servlet JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32
33 /**
34  * Mock implementation of {@link javax.servlet.ServletContext}.
35  *
36  * @author Howard Lewis Ship
37  * @since 4.0
38  */

39
40 public class MockContext extends AttributeHolder implements ServletContext JavaDoc, InitParameterHolder
41 {
42     private MockSession _session;
43
44     private static final Map JavaDoc _suffixToContentType = new HashMap JavaDoc();
45
46     static {
47         _suffixToContentType.put("html", "text/html");
48         _suffixToContentType.put("gif", "image/gif");
49         _suffixToContentType.put("png", "image/png");
50     }
51
52     private String JavaDoc _rootDirectory;
53     private String JavaDoc _servletContextName = "test";
54     private Map JavaDoc _initParameters = new HashMap JavaDoc();
55
56     public MockContext()
57     {
58     }
59
60     public MockContext(String JavaDoc testDirectory)
61     {
62         _rootDirectory = testDirectory + "/context";
63     }
64
65     public ServletContext JavaDoc getContext(String JavaDoc name)
66     {
67         return null;
68     }
69
70     public int getMajorVersion()
71     {
72         return 2;
73     }
74
75     public int getMinorVersion()
76     {
77         return 1;
78     }
79
80     public String JavaDoc getMimeType(String JavaDoc path)
81     {
82         int lastx = path.lastIndexOf('.');
83         String JavaDoc suffix = path.substring(lastx + 1);
84
85         return (String JavaDoc) _suffixToContentType.get(suffix);
86     }
87
88     public Set JavaDoc getResourcePaths(String JavaDoc arg0)
89     {
90         return null;
91     }
92
93     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc
94     {
95         if (path == null || !path.startsWith("/"))
96             throw new MalformedURLException JavaDoc("Not a valid context path.");
97
98         String JavaDoc fullPath = _rootDirectory + path;
99
100         File JavaDoc file = new File JavaDoc(fullPath);
101
102         if (file.exists())
103             return file.toURL();
104
105         return null;
106     }
107
108     public InputStream JavaDoc getResourceAsStream(String JavaDoc path)
109     {
110         try
111         {
112             URL JavaDoc url = getResource(path);
113
114             if (url == null)
115                 return null;
116
117             return url.openStream();
118         }
119         catch (MalformedURLException JavaDoc ex)
120         {
121             return null;
122         }
123         catch (IOException JavaDoc ex)
124         {
125             return null;
126         }
127     }
128
129     /**
130      * Gets a dispatcher for the given path. Path should be a relative path (relative
131      * to the context). A special case: "NULL" returns null (i.e., when a
132      * dispatcher can't be found).
133      *
134      **/

135
136     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path)
137     {
138         if (path.endsWith("/NULL"))
139             return null;
140
141         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(_rootDirectory);
142         buffer.append(path);
143
144         // Simulate the handling of directories by serving the index.html
145
// in the directory.
146

147         if (path.endsWith("/"))
148             buffer.append("index.html");
149
150         return new MockRequestDispatcher(buffer.toString());
151     }
152
153     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name)
154     {
155         return null;
156     }
157
158     public Servlet JavaDoc getServlet(String JavaDoc name) throws ServletException JavaDoc
159     {
160         return null;
161     }
162
163     public Enumeration JavaDoc getServlets()
164     {
165         return null;
166     }
167
168     public Enumeration JavaDoc getServletNames()
169     {
170         return null;
171     }
172
173     public void log(String JavaDoc message)
174     {
175         log(message, null);
176     }
177
178     public void log(Exception JavaDoc exception, String JavaDoc message)
179     {
180         log(message, exception);
181     }
182
183     public void log(String JavaDoc message, Throwable JavaDoc exception)
184     {
185     }
186
187     public String JavaDoc getRealPath(String JavaDoc arg0)
188     {
189         return null;
190     }
191
192     public String JavaDoc getServerInfo()
193     {
194         return "Tapestry Mock Objects";
195     }
196
197     public String JavaDoc getInitParameter(String JavaDoc name)
198     {
199         return (String JavaDoc) _initParameters.get(name);
200     }
201
202     public Enumeration JavaDoc getInitParameterNames()
203     {
204         return Collections.enumeration(_initParameters.keySet());
205     }
206
207     public void setInitParameter(String JavaDoc name, String JavaDoc value)
208     {
209         _initParameters.put(name, value);
210     }
211
212     public String JavaDoc getServletContextName()
213     {
214         return _servletContextName;
215     }
216
217     public MockSession createSession()
218     {
219         if (_session == null)
220         {
221             String JavaDoc id = Long.toHexString(System.currentTimeMillis());
222
223             _session = new MockSession(this, id);
224         }
225
226         return _session;
227     }
228
229     public MockSession getSession()
230     {
231         return _session;
232     }
233
234     public void setServletContextName(String JavaDoc servletContextName)
235     {
236         _servletContextName = servletContextName;
237     }
238
239     public String JavaDoc getRootDirectory()
240     {
241         return _rootDirectory;
242     }
243
244     public void setRootDirectory(String JavaDoc rootDirectory)
245     {
246         _rootDirectory = rootDirectory;
247     }
248
249 }
250
Popular Tags