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.BufferedWriter;
23  import java.io.FileOutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.Template;
31  import org.apache.velocity.VelocityContext;
32  import org.apache.velocity.app.Velocity;
33  import org.apache.velocity.runtime.log.NullLogChute;
34  
35  /**
36   * Tests input encoding handling.  The input target is UTF-8, having
37   * chinese and and a spanish enyay (n-twiddle)
38   *
39   *  Thanks to Kent Johnson for the example input file.
40   *
41   *
42   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
43   * @version $Id: EncodingTestCase.java 477002 2006-11-20 01:07:43Z henning $
44   */
45  public class EncodingTestCase extends BaseTestCase implements TemplateTestBase
46  {
47      public EncodingTestCase(String name)
48      {
49          super(name);
50      }
51  
52      public void setUp()
53              throws Exception
54      {
55          Velocity.setProperty(
56                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
57  
58          Velocity.setProperty( Velocity.INPUT_ENCODING, "UTF-8" );
59  
60          Velocity.setProperty(
61                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
62  
63          Velocity.init();
64      }
65  
66      public static Test suite()
67      {
68          return new TestSuite(EncodingTestCase.class);
69      }
70  
71      /**
72       * Runs the test.
73       */
74      public void testChineseEncoding ()
75              throws Exception
76      {
77          VelocityContext context = new VelocityContext();
78  
79          assureResultsDirectoryExists(RESULT_DIR);
80  
81          /*
82           *  get the template and the output
83           */
84  
85          /*
86           *  Chinese and spanish
87           */
88  
89          Template template = Velocity.getTemplate(
90              getFileName(null, "encodingtest", TMPL_FILE_EXT), "UTF-8");
91  
92          FileOutputStream fos =
93              new FileOutputStream (
94                  getFileName(RESULT_DIR, "encodingtest", RESULT_FILE_EXT));
95  
96          Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
97  
98          template.merge(context, writer);
99          writer.flush();
100         writer.close();
101 
102         if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest",
103                 RESULT_FILE_EXT,CMP_FILE_EXT) )
104         {
105             fail("Output 1 incorrect.");
106         }
107     }
108 
109     public void testHighByteChinese()
110             throws Exception
111     {
112         VelocityContext context = new VelocityContext();
113 
114         assureResultsDirectoryExists(RESULT_DIR);
115 
116         /*
117          *  a 'high-byte' chinese example from Michael Zhou
118          */
119 
120         Template template = Velocity.getTemplate(
121               getFileName( null, "encodingtest2", TMPL_FILE_EXT), "UTF-8");
122 
123         FileOutputStream fos =
124             new FileOutputStream (
125                 getFileName(RESULT_DIR, "encodingtest2", RESULT_FILE_EXT));
126 
127         Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
128 
129         template.merge(context, writer);
130         writer.flush();
131         writer.close();
132 
133         if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest2",
134                 RESULT_FILE_EXT,CMP_FILE_EXT) )
135         {
136             fail("Output 2 incorrect.");
137         }
138 
139     }
140 
141     public void testHighByteChinese2()
142             throws Exception
143     {
144         VelocityContext context = new VelocityContext();
145 
146         assureResultsDirectoryExists(RESULT_DIR);
147 
148         /*
149          *  a 'high-byte' chinese from Ilkka
150          */
151 
152         Template template = Velocity.getTemplate(
153               getFileName( null, "encodingtest3", TMPL_FILE_EXT), "GBK");
154 
155         FileOutputStream fos =
156             new FileOutputStream (
157                 getFileName(RESULT_DIR, "encodingtest3", RESULT_FILE_EXT));
158 
159         Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "GBK"));
160 
161         template.merge(context, writer);
162         writer.flush();
163         writer.close();
164 
165         if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest3",
166                 RESULT_FILE_EXT,CMP_FILE_EXT) )
167         {
168             fail("Output 3 incorrect.");
169         }
170     }
171 
172     public void testRussian()
173             throws Exception
174     {
175         VelocityContext context = new VelocityContext();
176 
177         assureResultsDirectoryExists(RESULT_DIR);
178 
179         /*
180          *  Russian example from Vitaly Repetenko
181          */
182 
183         Template template = Velocity.getTemplate(
184               getFileName( null, "encodingtest_KOI8-R", TMPL_FILE_EXT), "KOI8-R");
185 
186         FileOutputStream fos =
187             new FileOutputStream (
188                 getFileName(RESULT_DIR, "encodingtest_KOI8-R", RESULT_FILE_EXT));
189 
190         Writer writer = new BufferedWriter(new OutputStreamWriter(fos, "KOI8-R"));
191 
192         template.merge(context, writer);
193         writer.flush();
194         writer.close();
195 
196         if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest_KOI8-R",
197                 RESULT_FILE_EXT,CMP_FILE_EXT) )
198         {
199             fail("Output 4 incorrect.");
200         }
201     }
202 }
203 
204 
205