1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
35 |
| |
36 |
| |
37 |
| |
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 |
41
| public MockContext(String testDirectory)
|
61 |
| { |
62 |
41
| _rootDirectory = testDirectory + "/context";
|
63 |
| } |
64 |
| |
65 |
0
| public ServletContext getContext(String name)
|
66 |
| { |
67 |
0
| return null;
|
68 |
| } |
69 |
| |
70 |
21
| public int getMajorVersion()
|
71 |
| { |
72 |
21
| return 2;
|
73 |
| } |
74 |
| |
75 |
21
| public int getMinorVersion()
|
76 |
| { |
77 |
21
| 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 |
1189
| public URL getResource(String path) throws MalformedURLException
|
94 |
| { |
95 |
1189
| if (path == null || !path.startsWith("/"))
|
96 |
0
| throw new MalformedURLException("Not a valid context path.");
|
97 |
| |
98 |
1189
| String fullPath = _rootDirectory + path;
|
99 |
| |
100 |
1189
| File file = new File(fullPath);
|
101 |
| |
102 |
1189
| if (file.exists())
|
103 |
269
| return file.toURL();
|
104 |
| |
105 |
920
| 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 |
| |
131 |
| |
132 |
| |
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 |
| |
145 |
| |
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 |
41
| public void log(String message)
|
174 |
| { |
175 |
41
| log(message, null);
|
176 |
| } |
177 |
| |
178 |
0
| public void log(Exception exception, String message)
|
179 |
| { |
180 |
0
| log(message, exception);
|
181 |
| } |
182 |
| |
183 |
41
| 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 |
21
| public String getServerInfo()
|
193 |
| { |
194 |
21
| return "Tapestry Mock Objects";
|
195 |
| } |
196 |
| |
197 |
1234
| public String getInitParameter(String name)
|
198 |
| { |
199 |
1234
| return (String) _initParameters.get(name);
|
200 |
| } |
201 |
| |
202 |
21
| public Enumeration getInitParameterNames()
|
203 |
| { |
204 |
21
| 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 |
143
| public String getServletContextName()
|
213 |
| { |
214 |
143
| return _servletContextName;
|
215 |
| } |
216 |
| |
217 |
10
| public MockSession createSession()
|
218 |
| { |
219 |
10
| if (_session == null)
|
220 |
| { |
221 |
10
| String id = Long.toHexString(System.currentTimeMillis());
|
222 |
| |
223 |
10
| _session = new MockSession(this, id);
|
224 |
| } |
225 |
| |
226 |
10
| return _session;
|
227 |
| } |
228 |
| |
229 |
143
| public MockSession getSession()
|
230 |
| { |
231 |
143
| return _session;
|
232 |
| } |
233 |
| |
234 |
52
| public void setServletContextName(String servletContextName)
|
235 |
| { |
236 |
52
| _servletContextName = servletContextName;
|
237 |
| } |
238 |
| |
239 |
0
| public String getRootDirectory()
|
240 |
| { |
241 |
0
| return _rootDirectory;
|
242 |
| } |
243 |
| |
244 |
26
| public void setRootDirectory(String rootDirectory)
|
245 |
| { |
246 |
26
| _rootDirectory = rootDirectory;
|
247 |
| } |
248 |
| |
249 |
| } |