KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > EncodingTestCase


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23
24 import java.util.Vector JavaDoc;
25
26 import org.apache.velocity.VelocityContext;
27
28 import org.apache.velocity.Template;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.test.provider.TestProvider;
31 import org.apache.velocity.util.StringUtils;
32
33 import junit.framework.TestCase;
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,v 1.4.10.1 2004/03/03 23:23:04 geirm Exp $
44  */

45 public class EncodingTestCase extends BaseTestCase implements TemplateTestBase
46 {
47     public EncodingTestCase()
48     {
49         super("EncodingTestCase");
50         
51         try
52         {
53             Velocity.setProperty(
54                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
55             
56             Velocity.setProperty( Velocity.INPUT_ENCODING, "UTF-8" );
57
58             Velocity.init();
59         }
60         catch (Exception JavaDoc e)
61         {
62             System.err.println("Cannot setup EncodingTestCase!");
63             e.printStackTrace();
64             System.exit(1);
65         }
66     }
67
68     public static junit.framework.Test suite()
69     {
70         return new EncodingTestCase();
71     }
72
73     /**
74      * Runs the test.
75      */

76     public void runTest ()
77     {
78         
79         VelocityContext context = new VelocityContext();
80        
81         try
82         {
83             assureResultsDirectoryExists(RESULT_DIR);
84             
85             /*
86              * get the template and the output
87              */

88
89             /*
90              * Chinese and spanish
91              */

92
93             Template template = Velocity.getTemplate(
94                 getFileName(null, "encodingtest", TMPL_FILE_EXT), "UTF-8");
95
96             FileOutputStream JavaDoc fos =
97                 new FileOutputStream JavaDoc (
98                     getFileName(RESULT_DIR, "encodingtest", RESULT_FILE_EXT));
99
100             Writer JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos, "UTF-8"));
101            
102             template.merge(context, writer);
103             writer.flush();
104             writer.close();
105             
106             if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest",
107                     RESULT_FILE_EXT,CMP_FILE_EXT) )
108             {
109                 fail("Output 1 incorrect.");
110             }
111
112             /*
113              * a 'high-byte' chinese example from Michael Zhou
114              */

115
116             template = Velocity.getTemplate(
117                   getFileName( null, "encodingtest2", TMPL_FILE_EXT), "UTF-8");
118
119             fos =
120                 new FileOutputStream JavaDoc (
121                     getFileName(RESULT_DIR, "encodingtest2", RESULT_FILE_EXT));
122
123             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos, "UTF-8"));
124            
125             template.merge(context, writer);
126             writer.flush();
127             writer.close();
128             
129             if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest2",
130                     RESULT_FILE_EXT,CMP_FILE_EXT) )
131             {
132                 fail("Output 2 incorrect.");
133             }
134
135             /*
136              * a 'high-byte' chinese from Ilkka
137              */

138
139             template = Velocity.getTemplate(
140                   getFileName( null, "encodingtest3", TMPL_FILE_EXT), "GBK");
141
142             fos =
143                 new FileOutputStream JavaDoc (
144                     getFileName(RESULT_DIR, "encodingtest3", RESULT_FILE_EXT));
145
146             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos, "GBK"));
147            
148             template.merge(context, writer);
149             writer.flush();
150             writer.close();
151             
152             if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest3",
153                     RESULT_FILE_EXT,CMP_FILE_EXT) )
154             {
155                 fail("Output 3 incorrect.");
156             }
157
158             /*
159              * Russian example from Vitaly Repetenko
160              */

161
162             template = Velocity.getTemplate(
163                   getFileName( null, "encodingtest_KOI8-R", TMPL_FILE_EXT), "KOI8-R");
164
165             fos =
166                 new FileOutputStream JavaDoc (
167                     getFileName(RESULT_DIR, "encodingtest_KOI8-R", RESULT_FILE_EXT));
168
169             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos, "KOI8-R"));
170            
171             template.merge(context, writer);
172             writer.flush();
173             writer.close();
174             
175             if (!isMatch(RESULT_DIR,COMPARE_DIR,"encodingtest_KOI8-R",
176                     RESULT_FILE_EXT,CMP_FILE_EXT) )
177             {
178                 fail("Output 4 incorrect.");
179             }
180         }
181         catch (Exception JavaDoc e)
182         {
183             fail(e.getMessage());
184         }
185     }
186 }
187
188
189
190
Popular Tags