|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MockContext.java | 33.3% | 51.9% | 57.1% | 51.1% |
|
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;
|
|
18 |
import java.io.IOException;
|
|
19 |
import java.io.InputStream;
|
|
20 |
import java.net.MalformedURLException;
|
|
21 |
import java.net.URL;
|
|
22 |
import java.util.Collections;
|
|
23 |
import java.util.Enumeration;
|
|
24 |
import java.util.HashMap;
|
|
25 |
import java.util.Map;
|
|
26 |
import java.util.Set;
|
|
27 |
|
|
28 |
import javax.servlet.RequestDispatcher;
|
|
29 |
import javax.servlet.Servlet;
|
|
30 |
import javax.servlet.ServletContext;
|
|
31 |
import javax.servlet.ServletException;
|
|
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, InitParameterHolder |
|
41 |
{ |
|
42 |
private MockSession _session;
|
|
43 |
|
|
44 |
private static final Map _suffixToContentType = new HashMap(); |
|
45 |
|
|
46 |
static {
|
|
47 | 1 |
_suffixToContentType.put("html", "text/html"); |
48 | 1 |
_suffixToContentType.put("gif", "image/gif"); |
49 | 1 |
_suffixToContentType.put("png", "image/png"); |
50 |
} |
|
51 |
|
|
52 |
private String _rootDirectory;
|
|
53 |
private String _servletContextName = "test"; |
|
54 |
private Map _initParameters = new HashMap(); |
|
55 |
|
|
56 | 11 |
public MockContext()
|
57 |
{ |
|
58 |
} |
|
59 |
|
|
60 | 45 |
public MockContext(String testDirectory)
|
61 |
{ |
|
62 | 45 |
_rootDirectory = testDirectory + "/context";
|
63 |
} |
|
64 |
|
|
65 | 0 |
public ServletContext getContext(String name)
|
66 |
{ |
|
67 | 0 |
return null; |
68 |
} |
|
69 |
|
|
70 | 22 |
public int getMajorVersion() |
71 |
{ |
|
72 | 22 |
return 2;
|
73 |
} |
|
74 |
|
|
75 | 22 |
public int getMinorVersion() |
76 |
{ |
|
77 | 22 |
return 1;
|
78 |
} |
|
79 |
|
|
80 | 1 |
public String getMimeType(String path)
|
81 |
{ |
|
82 | 1 |
int lastx = path.lastIndexOf('.');
|
83 | 1 |
String suffix = path.substring(lastx + 1); |
84 |
|
|
85 | 1 |
return (String) _suffixToContentType.get(suffix);
|
86 |
} |
|
87 |
|
|
88 | 0 |
public Set getResourcePaths(String arg0)
|
89 |
{ |
|
90 | 0 |
return null; |
91 |
} |
|
92 |
|
|
93 | 1314 |
public URL getResource(String path) throws MalformedURLException |
94 |
{ |
|
95 | 1314 |
if (path == null || !path.startsWith("/")) |
96 | 0 |
throw new MalformedURLException("Not a valid context path."); |
97 |
|
|
98 | 1314 |
String fullPath = _rootDirectory + path; |
99 |
|
|
100 | 1314 |
File file = new File(fullPath);
|
101 |
|
|
102 | 1314 |
if (file.exists())
|
103 | 289 |
return file.toURL();
|
104 |
|
|
105 | 1025 |
return null; |
106 |
} |
|
107 |
|
|
108 | 0 |
public InputStream getResourceAsStream(String path)
|
109 |
{ |
|
110 | 0 |
try
|
111 |
{ |
|
112 | 0 |
URL url = getResource(path); |
113 |
|
|
114 | 0 |
if (url == null) |
115 | 0 |
return null; |
116 |
|
|
117 | 0 |
return url.openStream();
|
118 |
} |
|
119 |
catch (MalformedURLException ex)
|
|
120 |
{ |
|
121 | 0 |
return null; |
122 |
} |
|
123 |
catch (IOException ex)
|
|
124 |
{ |
|
125 | 0 |
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 | 0 |
public RequestDispatcher getRequestDispatcher(String path)
|
137 |
{ |
|
138 | 0 |
if (path.endsWith("/NULL")) |
139 | 0 |
return null; |
140 |
|
|
141 | 0 |
StringBuffer buffer = new StringBuffer(_rootDirectory);
|
142 | 0 |
buffer.append(path); |
143 |
|
|
144 |
// Simulate the handling of directories by serving the index.html
|
|
145 |
// in the directory.
|
|
146 |
|
|
147 | 0 |
if (path.endsWith("/")) |
148 | 0 |
buffer.append("index.html");
|
149 |
|
|
150 | 0 |
return new MockRequestDispatcher(buffer.toString()); |
151 |
} |
|
152 |
|
|
153 | 0 |
public RequestDispatcher getNamedDispatcher(String name)
|
154 |
{ |
|
155 | 0 |
return null; |
156 |
} |
|
157 |
|
|
158 | 0 |
public Servlet getServlet(String name) throws ServletException |
159 |
{ |
|
160 | 0 |
return null; |
161 |
} |
|
162 |
|
|
163 | 0 |
public Enumeration getServlets()
|
164 |
{ |
|
165 | 0 |
return null; |
166 |
} |
|
167 |
|
|
168 | 0 |
public Enumeration getServletNames()
|
169 |
{ |
|
170 | 0 |
return null; |
171 |
} |
|
172 |
|
|
173 | 45 |
public void log(String message) |
174 |
{ |
|
175 | 45 |
log(message, null);
|
176 |
} |
|
177 |
|
|
178 | 0 |
public void log(Exception exception, String message) |
179 |
{ |
|
180 | 0 |
log(message, exception); |
181 |
} |
|
182 |
|
|
183 | 45 |
public void log(String message, Throwable exception) |
184 |
{ |
|
185 |
} |
|
186 |
|
|
187 | 0 |
public String getRealPath(String arg0)
|
188 |
{ |
|
189 | 0 |
return null; |
190 |
} |
|
191 |
|
|
192 | 22 |
public String getServerInfo()
|
193 |
{ |
|
194 | 22 |
return "Tapestry Mock Objects"; |
195 |
} |
|
196 |
|
|
197 | 1270 |
public String getInitParameter(String name)
|
198 |
{ |
|
199 | 1270 |
return (String) _initParameters.get(name);
|
200 |
} |
|
201 |
|
|
202 | 22 |
public Enumeration getInitParameterNames()
|
203 |
{ |
|
204 | 22 |
return Collections.enumeration(_initParameters.keySet());
|
205 |
} |
|
206 |
|
|
207 | 0 |
public void setInitParameter(String name, String value) |
208 |
{ |
|
209 | 0 |
_initParameters.put(name, value); |
210 |
} |
|
211 |
|
|
212 | 159 |
public String getServletContextName()
|
213 |
{ |
|
214 | 159 |
return _servletContextName;
|
215 |
} |
|
216 |
|
|
217 | 11 |
public MockSession createSession()
|
218 |
{ |
|
219 | 11 |
if (_session == null) |
220 |
{ |
|
221 | 11 |
String id = Long.toHexString(System.currentTimeMillis()); |
222 |
|
|
223 | 11 |
_session = new MockSession(this, id); |
224 |
} |
|
225 |
|
|
226 | 11 |
return _session;
|
227 |
} |
|
228 |
|
|
229 | 159 |
public MockSession getSession()
|
230 |
{ |
|
231 | 159 |
return _session;
|
232 |
} |
|
233 |
|
|
234 | 56 |
public void setServletContextName(String servletContextName) |
235 |
{ |
|
236 | 56 |
_servletContextName = servletContextName; |
237 |
} |
|
238 |
|
|
239 | 0 |
public String getRootDirectory()
|
240 |
{ |
|
241 | 0 |
return _rootDirectory;
|
242 |
} |
|
243 |
|
|
244 | 28 |
public void setRootDirectory(String rootDirectory) |
245 |
{ |
|
246 | 28 |
_rootDirectory = rootDirectory; |
247 |
} |
|
248 |
|
|
249 |
} |
|
250 |
|
|