KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > VelocityServletTest


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

18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Properties JavaDoc;
25 import javax.servlet.RequestDispatcher JavaDoc;
26 import javax.servlet.Servlet JavaDoc;
27 import javax.servlet.ServletConfig JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.velocity.app.Velocity;
34 import org.apache.velocity.runtime.RuntimeSingleton;
35 import org.apache.velocity.runtime.RuntimeConstants;
36 import org.apache.velocity.servlet.VelocityServlet;
37
38 import junit.framework.TestCase;
39
40 /**
41  * Tests our VelocityServlet implementation.
42  *
43  * @author <a HREF="mailto:dlr@apache.org">Daniel Rall</a>
44  */

45 public class VelocityServletTest extends TestCase
46 {
47     /**
48      * Default constructor.
49      */

50     public VelocityServletTest()
51     {
52         super("VelocityServletTest");
53     }
54
55     public static junit.framework.Test suite ()
56     {
57         return new VelocityServletTest();
58     }
59
60     /**
61      * Runs the test.
62      */

63     public void runTest()
64     {
65         /*
66          * Assure we have the encoding we think we should.
67          */

68
69         MockVelocityServlet servlet = new MockVelocityServlet();
70         try
71         {
72             servlet.init(new MockServletConfig());
73         }
74         catch (ServletException JavaDoc e)
75         {
76             e.printStackTrace();
77         }
78         System.out.println(RuntimeConstants.OUTPUT_ENCODING + "=" +
79                            RuntimeSingleton.getProperty
80                            (RuntimeConstants.OUTPUT_ENCODING));
81         HttpServletResponse JavaDoc res = new MockHttpServletResponse();
82         servlet.visibleSetContentType(null, res);
83         assertEquals("Character encoding not set to UTF-8",
84                      "UTF-8", res.getCharacterEncoding());
85     }
86
87     class MockVelocityServlet extends VelocityServlet
88     {
89         void visibleSetContentType(HttpServletRequest JavaDoc req,
90                                    HttpServletResponse JavaDoc res)
91         {
92             setContentType(req, res);
93         }
94
95         protected Properties JavaDoc loadConfiguration(ServletConfig JavaDoc config)
96             throws IOException JavaDoc
97         {
98             Properties JavaDoc p = new Properties JavaDoc();
99             p.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
100             return p;
101         }
102
103         public ServletConfig JavaDoc getServletConfig()
104         {
105             return new MockServletConfig();
106         }
107     }
108
109     static class MockServletConfig implements ServletConfig JavaDoc
110     {
111         public String JavaDoc getInitParameter(String JavaDoc ignored)
112         {
113             return null;
114         }
115
116         public Enumeration JavaDoc getInitParameterNames()
117         {
118             return null;
119         }
120
121         public ServletContext JavaDoc getServletContext()
122         {
123             return new MockServletContext();
124         }
125
126         public String JavaDoc getServletName()
127         {
128             return "VelocityServlet";
129         }
130     }
131
132     static class MockServletContext implements ServletContext JavaDoc
133     {
134         public Object JavaDoc getAttribute(String JavaDoc ignored)
135         {
136             return null;
137         }
138
139         public Enumeration JavaDoc getAttributeNames()
140         {
141             return null;
142         }
143
144         public ServletContext JavaDoc getContext(String JavaDoc ignored)
145         {
146             return this;
147         }
148
149         public String JavaDoc getInitParameter(String JavaDoc ignored)
150         {
151             return null;
152         }
153
154         public Enumeration JavaDoc getInitParameterNames()
155         {
156             return null;
157         }
158
159         public int getMajorVersion()
160         {
161             return -1;
162         }
163
164         public String JavaDoc getMimeType(String JavaDoc ignored)
165         {
166             return null;
167         }
168
169         public int getMinorVersion()
170         {
171             return -1;
172         }
173
174         public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc ignored)
175         {
176             return null;
177         }
178
179         public String JavaDoc getRealPath(String JavaDoc ignored)
180         {
181             return null;
182         }
183
184         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc ignored)
185         {
186             return null;
187         }
188
189         public URL JavaDoc getResource(String JavaDoc ignored)
190             throws MalformedURLException JavaDoc
191         {
192             return null;
193         }
194
195         public InputStream JavaDoc getResourceAsStream(String JavaDoc ignored)
196         {
197             return null;
198         }
199
200         public String JavaDoc getServerInfo()
201         {
202             return "Velocity Test Suite";
203         }
204
205         public Servlet JavaDoc getServlet(String JavaDoc ignored)
206             throws ServletException JavaDoc
207         {
208             return null;
209         }
210
211         public Enumeration JavaDoc getServletNames()
212         {
213             return null;
214         }
215
216         public Enumeration JavaDoc getServlets()
217         {
218             return null;
219         }
220
221         public void log(Exception JavaDoc e, String JavaDoc msg)
222         {
223         }
224
225         public void log(String JavaDoc msg)
226         {
227         }
228
229         public void log(String JavaDoc msg, Throwable JavaDoc t)
230         {
231         }
232
233         public void removeAttribute(String JavaDoc name)
234         {
235         }
236
237         public void setAttribute(String JavaDoc name, Object JavaDoc value)
238         {
239         }
240     }
241
242     static class MockHttpServletResponse implements HttpServletResponse JavaDoc
243     {
244         private String JavaDoc encoding;
245
246         // ---- ServletResponse implementation -----------------------------
247

248         public void flushBuffer() throws IOException JavaDoc
249         {
250         }
251
252         public int getBufferSize()
253         {
254             return -1;
255         }
256
257         public String JavaDoc getCharacterEncoding()
258         {
259             return (encoding != null ? encoding : "ISO-8859-1");
260         }
261
262         public java.util.Locale JavaDoc getLocale()
263         {
264             return null;
265         }
266
267         public javax.servlet.ServletOutputStream JavaDoc getOutputStream()
268             throws IOException JavaDoc
269         {
270             return null;
271         }
272
273         public java.io.PrintWriter JavaDoc getWriter() throws IOException JavaDoc
274         {
275             return null;
276         }
277
278         public boolean isCommitted()
279         {
280             return false;
281         }
282
283         public void reset()
284         {
285         }
286
287         public void setBufferSize(int i)
288         {
289         }
290
291         public void setContentLength(int i)
292         {
293         }
294
295         /**
296          * Records the character encoding.
297          */

298         public void setContentType(String JavaDoc contentType)
299         {
300             if (contentType != null)
301             {
302                 int index = contentType.lastIndexOf(';') + 1;
303                 if (0 <= index || index < contentType.length())
304                 {
305                     index = contentType.indexOf("charset=", index);
306                     if (index != -1)
307                     {
308                         index += 8;
309                         this.encoding = contentType.substring(index).trim();
310                     }
311                 }
312             }
313         }
314
315         public void setLocale(java.util.Locale JavaDoc l)
316         {
317         }
318
319
320         // ---- HttpServletResponse implementation -------------------------
321

322         public void addCookie(javax.servlet.http.Cookie JavaDoc c)
323         {
324         }
325
326         public void addDateHeader(String JavaDoc s, long l)
327         {
328         }
329
330         public void addHeader(String JavaDoc name, String JavaDoc value)
331         {
332         }
333
334         public void addIntHeader(String JavaDoc name, int value)
335         {
336         }
337
338         public boolean containsHeader(String JavaDoc name)
339         {
340             return false;
341         }
342
343         public String JavaDoc encodeRedirectURL(String JavaDoc url)
344         {
345             return url;
346         }
347
348         public String JavaDoc encodeRedirectUrl(String JavaDoc url)
349         {
350             return url;
351         }
352
353         public String JavaDoc encodeURL(String JavaDoc url)
354         {
355             return url;
356         }
357
358         public String JavaDoc encodeUrl(String JavaDoc url)
359         {
360             return url;
361         }
362
363         public void sendError(int i) throws IOException JavaDoc
364         {
365         }
366
367         public void sendError(int i, String JavaDoc s) throws IOException JavaDoc
368         {
369         }
370
371         public void sendRedirect(String JavaDoc s) throws IOException JavaDoc
372         {
373         }
374
375         public void setDateHeader(String JavaDoc s, long l)
376         {
377         }
378
379         public void setHeader(String JavaDoc name, String JavaDoc value)
380         {
381         }
382
383         public void setIntHeader(String JavaDoc s, int i)
384         {
385         }
386
387         public void setStatus(int i)
388         {
389         }
390
391         public void setStatus(int i , String JavaDoc s)
392         {
393         }
394     }
395 }
396
Popular Tags