1 package org.apache.velocity.test; 2 3 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Enumeration ; 24 import java.util.Properties ; 25 import javax.servlet.RequestDispatcher ; 26 import javax.servlet.Servlet ; 27 import javax.servlet.ServletConfig ; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 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 45 public class VelocityServletTest extends TestCase 46 { 47 50 public VelocityServletTest() 51 { 52 super("VelocityServletTest"); 53 } 54 55 public static junit.framework.Test suite () 56 { 57 return new VelocityServletTest(); 58 } 59 60 63 public void runTest() 64 { 65 68 69 MockVelocityServlet servlet = new MockVelocityServlet(); 70 try 71 { 72 servlet.init(new MockServletConfig()); 73 } 74 catch (ServletException e) 75 { 76 e.printStackTrace(); 77 } 78 System.out.println(RuntimeConstants.OUTPUT_ENCODING + "=" + 79 RuntimeSingleton.getProperty 80 (RuntimeConstants.OUTPUT_ENCODING)); 81 HttpServletResponse 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 req, 90 HttpServletResponse res) 91 { 92 setContentType(req, res); 93 } 94 95 protected Properties loadConfiguration(ServletConfig config) 96 throws IOException 97 { 98 Properties p = new Properties (); 99 p.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8"); 100 return p; 101 } 102 103 public ServletConfig getServletConfig() 104 { 105 return new MockServletConfig(); 106 } 107 } 108 109 static class MockServletConfig implements ServletConfig 110 { 111 public String getInitParameter(String ignored) 112 { 113 return null; 114 } 115 116 public Enumeration getInitParameterNames() 117 { 118 return null; 119 } 120 121 public ServletContext getServletContext() 122 { 123 return new MockServletContext(); 124 } 125 126 public String getServletName() 127 { 128 return "VelocityServlet"; 129 } 130 } 131 132 static class MockServletContext implements ServletContext 133 { 134 public Object getAttribute(String ignored) 135 { 136 return null; 137 } 138 139 public Enumeration getAttributeNames() 140 { 141 return null; 142 } 143 144 public ServletContext getContext(String ignored) 145 { 146 return this; 147 } 148 149 public String getInitParameter(String ignored) 150 { 151 return null; 152 } 153 154 public Enumeration getInitParameterNames() 155 { 156 return null; 157 } 158 159 public int getMajorVersion() 160 { 161 return -1; 162 } 163 164 public String getMimeType(String ignored) 165 { 166 return null; 167 } 168 169 public int getMinorVersion() 170 { 171 return -1; 172 } 173 174 public RequestDispatcher getNamedDispatcher(String ignored) 175 { 176 return null; 177 } 178 179 public String getRealPath(String ignored) 180 { 181 return null; 182 } 183 184 public RequestDispatcher getRequestDispatcher(String ignored) 185 { 186 return null; 187 } 188 189 public URL getResource(String ignored) 190 throws MalformedURLException 191 { 192 return null; 193 } 194 195 public InputStream getResourceAsStream(String ignored) 196 { 197 return null; 198 } 199 200 public String getServerInfo() 201 { 202 return "Velocity Test Suite"; 203 } 204 205 public Servlet getServlet(String ignored) 206 throws ServletException 207 { 208 return null; 209 } 210 211 public Enumeration getServletNames() 212 { 213 return null; 214 } 215 216 public Enumeration getServlets() 217 { 218 return null; 219 } 220 221 public void log(Exception e, String msg) 222 { 223 } 224 225 public void log(String msg) 226 { 227 } 228 229 public void log(String msg, Throwable t) 230 { 231 } 232 233 public void removeAttribute(String name) 234 { 235 } 236 237 public void setAttribute(String name, Object value) 238 { 239 } 240 } 241 242 static class MockHttpServletResponse implements HttpServletResponse 243 { 244 private String encoding; 245 246 248 public void flushBuffer() throws IOException 249 { 250 } 251 252 public int getBufferSize() 253 { 254 return -1; 255 } 256 257 public String getCharacterEncoding() 258 { 259 return (encoding != null ? encoding : "ISO-8859-1"); 260 } 261 262 public java.util.Locale getLocale() 263 { 264 return null; 265 } 266 267 public javax.servlet.ServletOutputStream getOutputStream() 268 throws IOException 269 { 270 return null; 271 } 272 273 public java.io.PrintWriter getWriter() throws IOException 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 298 public void setContentType(String 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 l) 316 { 317 } 318 319 320 322 public void addCookie(javax.servlet.http.Cookie c) 323 { 324 } 325 326 public void addDateHeader(String s, long l) 327 { 328 } 329 330 public void addHeader(String name, String value) 331 { 332 } 333 334 public void addIntHeader(String name, int value) 335 { 336 } 337 338 public boolean containsHeader(String name) 339 { 340 return false; 341 } 342 343 public String encodeRedirectURL(String url) 344 { 345 return url; 346 } 347 348 public String encodeRedirectUrl(String url) 349 { 350 return url; 351 } 352 353 public String encodeURL(String url) 354 { 355 return url; 356 } 357 358 public String encodeUrl(String url) 359 { 360 return url; 361 } 362 363 public void sendError(int i) throws IOException 364 { 365 } 366 367 public void sendError(int i, String s) throws IOException 368 { 369 } 370 371 public void sendRedirect(String s) throws IOException 372 { 373 } 374 375 public void setDateHeader(String s, long l) 376 { 377 } 378 379 public void setHeader(String name, String value) 380 { 381 } 382 383 public void setIntHeader(String s, int i) 384 { 385 } 386 387 public void setStatus(int i) 388 { 389 } 390 391 public void setStatus(int i , String s) 392 { 393 } 394 } 395 } 396 | Popular Tags |