001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.util.Enumeration;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.servlet.RequestDispatcher;
031: import javax.servlet.Servlet;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037:
038: import junit.framework.Test;
039: import junit.framework.TestCase;
040: import junit.framework.TestSuite;
041:
042: import org.apache.velocity.runtime.RuntimeConstants;
043: import org.apache.velocity.runtime.RuntimeSingleton;
044: import org.apache.velocity.servlet.VelocityServlet;
045:
046: /**
047: * Tests our VelocityServlet implementation.
048: *
049: * @author <a href="mailto:dlr@apache.org">Daniel Rall</a>
050: */
051: public class VelocityServletTestCase extends TestCase {
052: /**
053: * Default constructor.
054: */
055: public VelocityServletTestCase(String name) {
056: super (name);
057: }
058:
059: public static Test suite() {
060: return new TestSuite(VelocityServletTestCase.class);
061: }
062:
063: /**
064: * Runs the test.
065: */
066: public void testVelocityServlet() throws Exception {
067: /*
068: * Assure we have the encoding we think we should.
069: */
070:
071: MockVelocityServlet servlet = new MockVelocityServlet();
072: servlet.init(new MockServletConfig());
073:
074: System.out.println(RuntimeConstants.OUTPUT_ENCODING
075: + "="
076: + RuntimeSingleton
077: .getProperty(RuntimeConstants.OUTPUT_ENCODING));
078: HttpServletResponse res = new MockHttpServletResponse();
079: servlet.visibleSetContentType(null, res);
080: assertEquals("Character encoding not set to UTF-8", "UTF-8",
081: res.getCharacterEncoding());
082: }
083:
084: class MockVelocityServlet extends VelocityServlet {
085: void visibleSetContentType(HttpServletRequest req,
086: HttpServletResponse res) {
087: setContentType(req, res);
088: }
089:
090: protected Properties loadConfiguration(ServletConfig config)
091: throws IOException {
092: Properties p = new Properties();
093: p.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
094: return p;
095: }
096:
097: public ServletConfig getServletConfig() {
098: return new MockServletConfig();
099: }
100: }
101:
102: static class MockServletConfig implements ServletConfig {
103: public String getInitParameter(String ignored) {
104: return null;
105: }
106:
107: public Enumeration getInitParameterNames() {
108: return null;
109: }
110:
111: public ServletContext getServletContext() {
112: return new MockServletContext();
113: }
114:
115: public String getServletName() {
116: return "VelocityServlet";
117: }
118: }
119:
120: static class MockServletContext implements ServletContext {
121: public Object getAttribute(String ignored) {
122: return null;
123: }
124:
125: public Enumeration getAttributeNames() {
126: return null;
127: }
128:
129: public ServletContext getContext(String ignored) {
130: return this ;
131: }
132:
133: public String getServletContextName() {
134: return "VelocityTestContext";
135: }
136:
137: public String getInitParameter(String ignored) {
138: return null;
139: }
140:
141: public Enumeration getInitParameterNames() {
142: return null;
143: }
144:
145: public int getMajorVersion() {
146: return -1;
147: }
148:
149: public String getMimeType(String ignored) {
150: return null;
151: }
152:
153: public Set getResourcePaths(String string) {
154: return null;
155: }
156:
157: public int getMinorVersion() {
158: return -1;
159: }
160:
161: public RequestDispatcher getNamedDispatcher(String ignored) {
162: return null;
163: }
164:
165: public String getRealPath(String ignored) {
166: return null;
167: }
168:
169: public RequestDispatcher getRequestDispatcher(String ignored) {
170: return null;
171: }
172:
173: public URL getResource(String ignored)
174: throws MalformedURLException {
175: return null;
176: }
177:
178: public InputStream getResourceAsStream(String ignored) {
179: return null;
180: }
181:
182: public String getServerInfo() {
183: return "Velocity Test Suite";
184: }
185:
186: public Servlet getServlet(String ignored)
187: throws ServletException {
188: return null;
189: }
190:
191: public Enumeration getServletNames() {
192: return null;
193: }
194:
195: public Enumeration getServlets() {
196: return null;
197: }
198:
199: public void log(Exception e, String msg) {
200: }
201:
202: public void log(String msg) {
203: }
204:
205: public void log(String msg, Throwable t) {
206: }
207:
208: public void removeAttribute(String name) {
209: }
210:
211: public void setAttribute(String name, Object value) {
212: }
213: }
214:
215: static class MockHttpServletResponse implements HttpServletResponse {
216: private String encoding;
217:
218: // ---- ServletResponse implementation -----------------------------
219:
220: public void flushBuffer() throws IOException {
221: }
222:
223: public void resetBuffer() {
224: }
225:
226: public int getBufferSize() {
227: return -1;
228: }
229:
230: public String getCharacterEncoding() {
231: return (encoding != null ? encoding : "ISO-8859-1");
232: }
233:
234: public java.util.Locale getLocale() {
235: return null;
236: }
237:
238: public javax.servlet.ServletOutputStream getOutputStream()
239: throws IOException {
240: return null;
241: }
242:
243: public java.io.PrintWriter getWriter() throws IOException {
244: return null;
245: }
246:
247: public boolean isCommitted() {
248: return false;
249: }
250:
251: public void reset() {
252: }
253:
254: public void setBufferSize(int i) {
255: }
256:
257: public void setContentLength(int i) {
258: }
259:
260: /**
261: * Records the character encoding.
262: */
263: public void setContentType(String contentType) {
264: if (contentType != null) {
265: int index = contentType.lastIndexOf(';') + 1;
266: if (0 <= index || index < contentType.length()) {
267: index = contentType.indexOf("charset=", index);
268: if (index != -1) {
269: index += 8;
270: this .encoding = contentType.substring(index)
271: .trim();
272: }
273: }
274: }
275: }
276:
277: public void setLocale(java.util.Locale l) {
278: }
279:
280: // ---- HttpServletResponse implementation -------------------------
281:
282: public void addCookie(javax.servlet.http.Cookie c) {
283: }
284:
285: public void addDateHeader(String s, long l) {
286: }
287:
288: public void addHeader(String name, String value) {
289: }
290:
291: public void addIntHeader(String name, int value) {
292: }
293:
294: public boolean containsHeader(String name) {
295: return false;
296: }
297:
298: public String encodeRedirectURL(String url) {
299: return url;
300: }
301:
302: public String encodeRedirectUrl(String url) {
303: return url;
304: }
305:
306: public String encodeURL(String url) {
307: return url;
308: }
309:
310: public String encodeUrl(String url) {
311: return url;
312: }
313:
314: public void sendError(int i) throws IOException {
315: }
316:
317: public void sendError(int i, String s) throws IOException {
318: }
319:
320: public void sendRedirect(String s) throws IOException {
321: }
322:
323: public void setDateHeader(String s, long l) {
324: }
325:
326: public void setHeader(String name, String value) {
327: }
328:
329: public void setIntHeader(String s, int i) {
330: }
331:
332: public void setStatus(int i) {
333: }
334:
335: public void setStatus(int i, String s) {
336: }
337: }
338: }
|