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.BufferedWriter;
023: import java.io.FileOutputStream;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026:
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: import org.apache.velocity.Template;
031: import org.apache.velocity.VelocityContext;
032: import org.apache.velocity.app.Velocity;
033: import org.apache.velocity.runtime.log.NullLogChute;
034:
035: /**
036: * Tests input encoding handling. The input target is UTF-8, having
037: * chinese and and a spanish enyay (n-twiddle)
038: *
039: * Thanks to Kent Johnson for the example input file.
040: *
041: *
042: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
043: * @version $Id: EncodingTestCase.java 477002 2006-11-20 01:07:43Z henning $
044: */
045: public class EncodingTestCase extends BaseTestCase implements
046: TemplateTestBase {
047: public EncodingTestCase(String name) {
048: super (name);
049: }
050:
051: public void setUp() throws Exception {
052: Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
053: FILE_RESOURCE_LOADER_PATH);
054:
055: Velocity.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
056:
057: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
058: NullLogChute.class.getName());
059:
060: Velocity.init();
061: }
062:
063: public static Test suite() {
064: return new TestSuite(EncodingTestCase.class);
065: }
066:
067: /**
068: * Runs the test.
069: */
070: public void testChineseEncoding() throws Exception {
071: VelocityContext context = new VelocityContext();
072:
073: assureResultsDirectoryExists(RESULT_DIR);
074:
075: /*
076: * get the template and the output
077: */
078:
079: /*
080: * Chinese and spanish
081: */
082:
083: Template template = Velocity.getTemplate(getFileName(null,
084: "encodingtest", TMPL_FILE_EXT), "UTF-8");
085:
086: FileOutputStream fos = new FileOutputStream(getFileName(
087: RESULT_DIR, "encodingtest", RESULT_FILE_EXT));
088:
089: Writer writer = new BufferedWriter(new OutputStreamWriter(fos,
090: "UTF-8"));
091:
092: template.merge(context, writer);
093: writer.flush();
094: writer.close();
095:
096: if (!isMatch(RESULT_DIR, COMPARE_DIR, "encodingtest",
097: RESULT_FILE_EXT, CMP_FILE_EXT)) {
098: fail("Output 1 incorrect.");
099: }
100: }
101:
102: public void testHighByteChinese() throws Exception {
103: VelocityContext context = new VelocityContext();
104:
105: assureResultsDirectoryExists(RESULT_DIR);
106:
107: /*
108: * a 'high-byte' chinese example from Michael Zhou
109: */
110:
111: Template template = Velocity.getTemplate(getFileName(null,
112: "encodingtest2", TMPL_FILE_EXT), "UTF-8");
113:
114: FileOutputStream fos = new FileOutputStream(getFileName(
115: RESULT_DIR, "encodingtest2", RESULT_FILE_EXT));
116:
117: Writer writer = new BufferedWriter(new OutputStreamWriter(fos,
118: "UTF-8"));
119:
120: template.merge(context, writer);
121: writer.flush();
122: writer.close();
123:
124: if (!isMatch(RESULT_DIR, COMPARE_DIR, "encodingtest2",
125: RESULT_FILE_EXT, CMP_FILE_EXT)) {
126: fail("Output 2 incorrect.");
127: }
128:
129: }
130:
131: public void testHighByteChinese2() throws Exception {
132: VelocityContext context = new VelocityContext();
133:
134: assureResultsDirectoryExists(RESULT_DIR);
135:
136: /*
137: * a 'high-byte' chinese from Ilkka
138: */
139:
140: Template template = Velocity.getTemplate(getFileName(null,
141: "encodingtest3", TMPL_FILE_EXT), "GBK");
142:
143: FileOutputStream fos = new FileOutputStream(getFileName(
144: RESULT_DIR, "encodingtest3", RESULT_FILE_EXT));
145:
146: Writer writer = new BufferedWriter(new OutputStreamWriter(fos,
147: "GBK"));
148:
149: template.merge(context, writer);
150: writer.flush();
151: writer.close();
152:
153: if (!isMatch(RESULT_DIR, COMPARE_DIR, "encodingtest3",
154: RESULT_FILE_EXT, CMP_FILE_EXT)) {
155: fail("Output 3 incorrect.");
156: }
157: }
158:
159: public void testRussian() throws Exception {
160: VelocityContext context = new VelocityContext();
161:
162: assureResultsDirectoryExists(RESULT_DIR);
163:
164: /*
165: * Russian example from Vitaly Repetenko
166: */
167:
168: Template template = Velocity.getTemplate(getFileName(null,
169: "encodingtest_KOI8-R", TMPL_FILE_EXT), "KOI8-R");
170:
171: FileOutputStream fos = new FileOutputStream(getFileName(
172: RESULT_DIR, "encodingtest_KOI8-R", RESULT_FILE_EXT));
173:
174: Writer writer = new BufferedWriter(new OutputStreamWriter(fos,
175: "KOI8-R"));
176:
177: template.merge(context, writer);
178: writer.flush();
179: writer.close();
180:
181: if (!isMatch(RESULT_DIR, COMPARE_DIR, "encodingtest_KOI8-R",
182: RESULT_FILE_EXT, CMP_FILE_EXT)) {
183: fail("Output 4 incorrect.");
184: }
185: }
186: }
|