001: package org.apache.velocity.io;
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.ByteArrayInputStream;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.lang.ArrayUtils;
031:
032: /**
033: * Test the UnicodeInputStream.
034: *
035: * @author $author$
036: * @version $Revision: 500889 $, $Date: 2007-01-28 13:30:04 -0800 (Sun, 28 Jan 2007) $
037: */
038: public class UnicodeInputStreamTestCase extends TestCase {
039:
040: public UnicodeInputStreamTestCase(final String name) {
041: super (name);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(UnicodeInputStreamTestCase.class);
046: }
047:
048: public void testSimpleStream() throws Exception {
049: testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", true);
050: testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", false);
051: }
052:
053: public void testSimpleUTF8() throws Exception {
054: testRun(null, "Ich bin zwei Oeltanks", "UTF-8", true);
055: testRun(null, "Ich bin zwei Oeltanks", "UTF-8", false);
056: }
057:
058: public void testRealUTF8() throws Exception {
059: testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", true);
060: testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", false);
061: }
062:
063: public void testRealUTF8WithBOM() throws Exception {
064: testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
065: "UTF-8", true);
066: testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
067: "UTF-8", false);
068: }
069:
070: public void testRealUTF16BEWithBOM() throws Exception {
071: testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
072: "UTF-16BE", true);
073: testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
074: "UTF-16BE", false);
075: }
076:
077: public void testRealUTF16LEWithBOM() throws Exception {
078: testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
079: "UTF-16LE", true);
080: testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
081: "UTF-16LE", false);
082: }
083:
084: public void testRealUTF32BEWithBOM() throws Exception {
085: testRun(UnicodeInputStream.UTF32BE_BOM, null, "UTF-32BE", true);
086: testRun(UnicodeInputStream.UTF32BE_BOM, null, "UTF-32BE", false);
087: }
088:
089: public void testRealUTF32LEWithBOM() throws Exception {
090: testRun(UnicodeInputStream.UTF32LE_BOM, null, "UTF-32LE", true);
091: testRun(UnicodeInputStream.UTF32LE_BOM, null, "UTF-32LE", false);
092: }
093:
094: protected void testRun(final UnicodeInputStream.UnicodeBOM bom,
095: final String str, final String testEncoding,
096: final boolean skipBOM) throws Exception {
097:
098: byte[] testString = buildTestString(bom, str, testEncoding,
099: skipBOM);
100:
101: InputStream is = null;
102: UnicodeInputStream uis = null;
103:
104: try {
105: is = createInputStream(bom, str, testEncoding);
106: uis = new UnicodeInputStream(is, skipBOM);
107:
108: assertEquals("BOM Skipping problem", skipBOM, uis
109: .isSkipBOM());
110:
111: if (bom != null) {
112: assertEquals("Wrong Encoding detected", testEncoding,
113: uis.getEncodingFromStream());
114: }
115:
116: byte[] result = readAllBytes(uis, testEncoding);
117:
118: assertNotNull(testString);
119: assertNotNull(result);
120: assertEquals("Wrong result length", testString.length,
121: result.length);
122:
123: for (int i = 0; i < result.length; i++) {
124: assertEquals("Wrong Byte at " + i, testString[i],
125: result[i]);
126: }
127: } finally {
128:
129: if (uis != null) {
130: uis.close();
131: }
132:
133: if (is != null) {
134: is.close();
135: }
136: }
137: }
138:
139: protected InputStream createInputStream(
140: final UnicodeInputStream.UnicodeBOM bom, final String str,
141: final String enc) throws Exception {
142:
143: if (bom == null) {
144: if (str != null) {
145: return new ByteArrayInputStream(str.getBytes(enc));
146: } else {
147: return new ByteArrayInputStream(new byte[0]);
148: }
149: } else {
150: if (str != null) {
151: return new ByteArrayInputStream(ArrayUtils.addAll(bom
152: .getBytes(), str.getBytes(enc)));
153: } else {
154: return new ByteArrayInputStream(ArrayUtils.addAll(bom
155: .getBytes(), new byte[0]));
156: }
157: }
158: }
159:
160: protected byte[] buildTestString(
161: final UnicodeInputStream.UnicodeBOM bom, final String str,
162: final String enc, final boolean skipBOM) throws Exception {
163:
164: byte[] strBytes = (str != null) ? str.getBytes(enc)
165: : new byte[0];
166:
167: if ((bom == null) || skipBOM) {
168: return strBytes;
169: } else {
170: return ArrayUtils.addAll(bom.getBytes(), strBytes);
171: }
172: }
173:
174: protected byte[] readAllBytes(final InputStream inputStream,
175: final String enc) throws Exception {
176: InputStreamReader isr = null;
177:
178: byte[] res = new byte[0];
179:
180: try {
181: byte[] buf = new byte[1024];
182: int read = 0;
183:
184: while ((read = inputStream.read(buf)) >= 0) {
185: res = ArrayUtils.addAll(res, ArrayUtils.subarray(buf,
186: 0, read));
187: }
188: } finally {
189:
190: if (isr != null) {
191: isr.close();
192: }
193: }
194:
195: return res;
196: }
197:
198: }
|