1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Enumeration;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import javax.servlet.RequestDispatcher;
31  import javax.servlet.Servlet;
32  import javax.servlet.ServletConfig;
33  import javax.servlet.ServletContext;
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import junit.framework.Test;
39  import junit.framework.TestCase;
40  import junit.framework.TestSuite;
41  
42  import org.apache.velocity.runtime.RuntimeConstants;
43  import org.apache.velocity.runtime.RuntimeSingleton;
44  import org.apache.velocity.servlet.VelocityServlet;
45  
46  
47  /**
48   * Tests our VelocityServlet implementation.
49   *
50   * @author <a href="mailto:dlr@apache.org">Daniel Rall</a>
51   */
52  public class VelocityServletTestCase extends TestCase
53  {
54      /**
55       * Default constructor.
56       */
57      public VelocityServletTestCase(String name)
58      {
59          super(name);
60      }
61  
62      public static Test suite ()
63      {
64          return new TestSuite(VelocityServletTestCase.class);
65      }
66  
67      /**
68       * Runs the test.
69       */
70      public void testVelocityServlet()
71              throws Exception
72      {
73          /*
74           * Assure we have the encoding we think we should.
75           */
76  
77          MockVelocityServlet servlet = new MockVelocityServlet();
78          servlet.init(new MockServletConfig());
79  
80          System.out.println(RuntimeConstants.OUTPUT_ENCODING + "=" +
81                             RuntimeSingleton.getProperty
82                             (RuntimeConstants.OUTPUT_ENCODING));
83          HttpServletResponse res = new MockHttpServletResponse();
84          servlet.visibleSetContentType(null, res);
85          assertEquals("Character encoding not set to UTF-8",
86                       "UTF-8", res.getCharacterEncoding());
87      }
88  
89  
90      class MockVelocityServlet extends VelocityServlet
91      {
92          void visibleSetContentType(HttpServletRequest req,
93                                     HttpServletResponse res)
94          {
95              setContentType(req, res);
96          }
97  
98          protected Properties loadConfiguration(ServletConfig config)
99              throws IOException
100         {
101             Properties p = new Properties();
102             p.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
103             return p;
104         }
105 
106         public ServletConfig getServletConfig()
107         {
108             return new MockServletConfig();
109         }
110     }
111 
112     static class MockServletConfig implements ServletConfig
113     {
114         public String getInitParameter(String ignored)
115         {
116             return null;
117         }
118 
119         public Enumeration getInitParameterNames()
120         {
121             return null;
122         }
123 
124         public ServletContext getServletContext()
125         {
126             return new MockServletContext();
127         }
128 
129         public String getServletName()
130         {
131             return "VelocityServlet";
132         }
133     }
134 
135     static class MockServletContext implements ServletContext
136     {
137         public Object getAttribute(String ignored)
138         {
139             return null;
140         }
141 
142         public Enumeration getAttributeNames()
143         {
144             return null;
145         }
146 
147         public ServletContext getContext(String ignored)
148         {
149             return this;
150         }
151 
152         public String getServletContextName()
153         {
154             return "VelocityTestContext";
155         }
156 
157         public String getInitParameter(String ignored)
158         {
159             return null;
160         }
161 
162         public Enumeration getInitParameterNames()
163         {
164             return null;
165         }
166 
167         public int getMajorVersion()
168         {
169             return -1;
170         }
171 
172         public String getMimeType(String ignored)
173         {
174             return null;
175         }
176 
177         public Set getResourcePaths(String string)
178         {
179             return null;
180         }
181 
182         public int getMinorVersion()
183         {
184             return -1;
185         }
186 
187         public RequestDispatcher getNamedDispatcher(String ignored)
188         {
189             return null;
190         }
191 
192         public String getRealPath(String ignored)
193         {
194             return null;
195         }
196 
197         public RequestDispatcher getRequestDispatcher(String ignored)
198         {
199             return null;
200         }
201 
202         public URL getResource(String ignored)
203             throws MalformedURLException
204         {
205             return null;
206         }
207 
208         public InputStream getResourceAsStream(String ignored)
209         {
210             return null;
211         }
212 
213         public String getServerInfo()
214         {
215             return "Velocity Test Suite";
216         }
217 
218         public Servlet getServlet(String ignored)
219             throws ServletException
220         {
221             return null;
222         }
223 
224         public Enumeration getServletNames()
225         {
226             return null;
227         }
228 
229         public Enumeration getServlets()
230         {
231             return null;
232         }
233 
234         public void log(Exception e, String msg)
235         {
236         }
237 
238         public void log(String msg)
239         {
240         }
241 
242         public void log(String msg, Throwable t)
243         {
244         }
245 
246         public void removeAttribute(String name)
247         {
248         }
249 
250         public void setAttribute(String name, Object value)
251         {
252         }
253     }
254 
255     static class MockHttpServletResponse implements HttpServletResponse
256     {
257         private String encoding;
258 
259         // ---- ServletResponse implementation -----------------------------
260 
261         public void flushBuffer() throws IOException
262         {
263         }
264 
265         public void resetBuffer()
266         {
267         }
268 
269         public int getBufferSize()
270         {
271             return -1;
272         }
273 
274         public String getCharacterEncoding()
275         {
276             return (encoding != null ? encoding : "ISO-8859-1");
277         }
278 
279         public java.util.Locale getLocale()
280         {
281             return null;
282         }
283 
284         public javax.servlet.ServletOutputStream getOutputStream()
285             throws IOException
286         {
287             return null;
288         }
289 
290         public java.io.PrintWriter getWriter() throws IOException
291         {
292             return null;
293         }
294 
295         public boolean isCommitted()
296         {
297             return false;
298         }
299 
300         public void reset()
301         {
302         }
303 
304         public void setBufferSize(int i)
305         {
306         }
307 
308         public void setContentLength(int i)
309         {
310         }
311 
312         /**
313          * Records the character encoding.
314          */
315         public void setContentType(String contentType)
316         {
317             if (contentType != null)
318             {
319                 int index = contentType.lastIndexOf(';') + 1;
320                 if (0 <= index || index < contentType.length())
321                 {
322                     index = contentType.indexOf("charset=", index);
323                     if (index != -1)
324                     {
325                         index += 8;
326                         this.encoding = contentType.substring(index).trim();
327                     }
328                 }
329             }
330         }
331 
332         public void setLocale(java.util.Locale l)
333         {
334         }
335 
336 
337         // ---- HttpServletResponse implementation -------------------------
338 
339         public void addCookie(javax.servlet.http.Cookie c)
340         {
341         }
342 
343         public void addDateHeader(String s, long l)
344         {
345         }
346 
347         public void addHeader(String name, String value)
348         {
349         }
350 
351         public void addIntHeader(String name, int value)
352         {
353         }
354 
355         public boolean containsHeader(String name)
356         {
357             return false;
358         }
359 
360         public String encodeRedirectURL(String url)
361         {
362             return url;
363         }
364 
365         public String encodeRedirectUrl(String url)
366         {
367             return url;
368         }
369 
370         public String encodeURL(String url)
371         {
372             return url;
373         }
374 
375         public String encodeUrl(String url)
376         {
377             return url;
378         }
379 
380         public void sendError(int i) throws IOException
381         {
382         }
383 
384         public void sendError(int i, String s) throws IOException
385         {
386         }
387 
388         public void sendRedirect(String s) throws IOException
389         {
390         }
391 
392         public void setDateHeader(String s, long l)
393         {
394         }
395 
396         public void setHeader(String name, String value)
397         {
398         }
399 
400         public void setIntHeader(String s, int i)
401         {
402         }
403 
404         public void setStatus(int i)
405         {
406         }
407 
408         public void setStatus(int i , String s)
409         {
410         }
411     }
412 }