1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.test.misc.TestLogChute;
34
35
36
37
38
39
40
41
42
43
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, TestLogChute.class.getName());
62
63 Velocity.init();
64 }
65
66 public static Test suite()
67 {
68 return new TestSuite(EncodingTestCase.class);
69 }
70
71
72
73
74 public void testChineseEncoding ()
75 throws Exception
76 {
77 VelocityContext context = new VelocityContext();
78
79 assureResultsDirectoryExists(RESULT_DIR);
80
81
82
83
84
85
86
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
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
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
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