1   package org.apache.velocity.io;
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.ByteArrayInputStream;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.commons.lang.ArrayUtils;
31  
32  
33  /**
34   * Test the UnicodeInputStream.
35   *
36   * @author  $author$
37   * @version  $Revision: 501574 $, $Date: 2007-01-30 13:32:26 -0800 (Tue, 30 Jan 2007) $
38   */
39  public class UnicodeInputStreamTestCase
40      extends TestCase
41  {
42  
43      public UnicodeInputStreamTestCase(final String name)
44      {
45          super(name);
46      }
47  
48      public static Test suite()
49      {
50          return new TestSuite(UnicodeInputStreamTestCase.class);
51      }
52  
53      public void testSimpleStream()
54          throws Exception
55      {
56          testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", true);
57          testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", false);
58      }
59  
60      public void testSimpleUTF8()
61          throws Exception
62      {
63          testRun(null, "Ich bin zwei Oeltanks", "UTF-8", true);
64          testRun(null, "Ich bin zwei Oeltanks", "UTF-8", false);
65      }
66  
67      public void testRealUTF8()
68          throws Exception
69      {
70          testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", true);
71          testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", false);
72      }
73  
74      public void testRealUTF8WithBOM()
75          throws Exception
76      {
77          testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
78                  "UTF-8", true);
79          testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
80                  "UTF-8", false);
81      }
82  
83      public void testRealUTF16BEWithBOM()
84          throws Exception
85      {
86          testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
87                  "UTF-16BE", true);
88          testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
89                  "UTF-16BE", false);
90      }
91  
92      public void testRealUTF16LEWithBOM()
93          throws Exception
94      {
95          testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
96                  "UTF-16LE", true);
97          testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
98                  "UTF-16LE", false);
99      }
100 
101     public void testRealUTF32BEWithBOM()
102         throws Exception
103     {
104         testRun(UnicodeInputStream.UTF32BE_BOM, null,
105                 "UTF-32BE", true);
106         testRun(UnicodeInputStream.UTF32BE_BOM, null,
107                 "UTF-32BE", false);
108     }
109 
110     public void testRealUTF32LEWithBOM()
111         throws Exception
112     {
113         testRun(UnicodeInputStream.UTF32LE_BOM, null,
114                 "UTF-32LE", true);
115         testRun(UnicodeInputStream.UTF32LE_BOM, null,
116                 "UTF-32LE", false);
117     }
118 
119 
120     protected void testRun(final UnicodeInputStream.UnicodeBOM bom, final String str, final String testEncoding, final boolean skipBOM)
121         throws Exception
122     {
123 
124         byte [] testString = buildTestString(bom, str, testEncoding, skipBOM);
125 
126         InputStream is = null;
127         UnicodeInputStream uis = null;
128 
129         try
130         {
131             is = createInputStream(bom, str, testEncoding);
132             uis = new UnicodeInputStream(is, skipBOM);
133 
134             assertEquals("BOM Skipping problem", skipBOM, uis.isSkipBOM());
135 
136             if (bom != null)
137             {
138                 assertEquals("Wrong Encoding detected", testEncoding, uis.getEncodingFromStream());
139             }
140 
141             byte [] result = readAllBytes(uis, testEncoding);
142 
143             assertNotNull(testString);
144             assertNotNull(result);
145             assertEquals("Wrong result length", testString.length, result.length);
146 
147             for (int i = 0; i < result.length; i++)
148             {
149                 assertEquals("Wrong Byte at " + i, testString[i], result[i]);
150             }
151         }
152         finally
153         {
154 
155             if (uis != null)
156             {
157                 uis.close();
158             }
159 
160             if (is != null)
161             {
162                 is.close();
163             }
164         }
165     }
166 
167     protected InputStream createInputStream(final UnicodeInputStream.UnicodeBOM bom, final String str, final String enc)
168         throws Exception
169     {
170 
171         if (bom == null)
172         {
173             if (str != null)
174             {
175                 return new ByteArrayInputStream(str.getBytes(enc));
176             }
177             else
178             {
179                 return new ByteArrayInputStream(new byte[0]);
180             }
181         }
182         else
183         {
184             if (str != null)
185             {
186                 return new ByteArrayInputStream(ArrayUtils.addAll(bom.getBytes(), str.getBytes(enc)));
187             }
188             else
189             {
190                 return new ByteArrayInputStream(ArrayUtils.addAll(bom.getBytes(), new byte[0]));
191             }
192         }
193     }
194 
195     protected byte [] buildTestString(final UnicodeInputStream.UnicodeBOM bom, final String str, final String enc, final boolean skipBOM)
196         throws Exception
197     {
198 
199         byte [] strBytes = (str != null) ? str.getBytes(enc) : new byte[0];
200 
201         if ((bom == null) || skipBOM)
202         {
203                 return strBytes;
204         }
205         else
206         {
207             return ArrayUtils.addAll(bom.getBytes(), strBytes);
208         }
209     }
210 
211     protected byte [] readAllBytes(final InputStream inputStream, final String enc)
212         throws Exception
213     {
214         InputStreamReader isr = null;
215 
216         byte [] res = new byte[0];
217 
218         try
219         {
220             byte[] buf = new byte[1024];
221             int read = 0;
222 
223             while ((read = inputStream.read(buf)) >= 0)
224             {
225                 res = ArrayUtils.addAll(res, ArrayUtils.subarray(buf, 0, read));
226             }
227         }
228         finally
229         {
230 
231             if (isr != null)
232             {
233                 isr.close();
234             }
235         }
236 
237         return res;
238     }
239 
240 }