aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/io/UnicodeInputStreamTestCase.java
blob: 68c108422af7f070c78c33f2f2d60b3f96c6a490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package org.apache.velocity.io;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.lang3.ArrayUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 * Test the UnicodeInputStream.
 *
 * @author  $author$
 * @version  $Revision$, $Date$
 */
public class UnicodeInputStreamTestCase
    extends TestCase
{

    public UnicodeInputStreamTestCase(final String name)
    {
        super(name);
    }

    public static Test suite()
    {
        return new TestSuite(UnicodeInputStreamTestCase.class);
    }

    public void testSimpleStream()
        throws Exception
    {
        testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", true);
        testRun(null, "Ich bin zwei Oeltanks", "US-ASCII", false);
    }

    public void testSimpleUTF8()
        throws Exception
    {
        testRun(null, "Ich bin zwei Oeltanks", "UTF-8", true);
        testRun(null, "Ich bin zwei Oeltanks", "UTF-8", false);
    }

    public void testRealUTF8()
        throws Exception
    {
        testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", true);
        testRun(null, "Ich bin zwei \u00d6ltanks", "UTF-8", false);
    }

    public void testRealUTF8WithBOM()
        throws Exception
    {
        testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
                "UTF-8", true);
        testRun(UnicodeInputStream.UTF8_BOM, "Ich bin ein Test",
                "UTF-8", false);
    }

    public void testRealUTF16BEWithBOM()
        throws Exception
    {
        testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
                "UTF-16BE", true);
        testRun(UnicodeInputStream.UTF16BE_BOM, "Ich bin ein Test",
                "UTF-16BE", false);
    }

    public void testRealUTF16LEWithBOM()
        throws Exception
    {
        testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
                "UTF-16LE", true);
        testRun(UnicodeInputStream.UTF16LE_BOM, "Ich bin ein Test",
                "UTF-16LE", false);
    }

    public void testRealUTF32BEWithBOM()
        throws Exception
    {
        testRun(UnicodeInputStream.UTF32BE_BOM, null,
                "UTF-32BE", true);
        testRun(UnicodeInputStream.UTF32BE_BOM, null,
                "UTF-32BE", false);
    }

    public void testRealUTF32LEWithBOM()
        throws Exception
    {
        testRun(UnicodeInputStream.UTF32LE_BOM, null,
                "UTF-32LE", true);
        testRun(UnicodeInputStream.UTF32LE_BOM, null,
                "UTF-32LE", false);
    }


    protected void testRun(final UnicodeInputStream.UnicodeBOM bom, final String str, final String testEncoding, final boolean skipBOM)
        throws Exception
    {

        byte [] testString = buildTestString(bom, str, testEncoding, skipBOM);

        InputStream is = null;
        UnicodeInputStream uis = null;

        try
        {
            is = createInputStream(bom, str, testEncoding);
            uis = new UnicodeInputStream(is, skipBOM);

            assertEquals("BOM Skipping problem", skipBOM, uis.isSkipBOM());

            if (bom != null)
            {
                assertEquals("Wrong Encoding detected", testEncoding, uis.getEncodingFromStream());
            }

            byte [] result = readAllBytes(uis, testEncoding);

            assertNotNull(testString);
            assertNotNull(result);
            assertEquals("Wrong result length", testString.length, result.length);

            for (int i = 0; i < result.length; i++)
            {
                assertEquals("Wrong Byte at " + i, testString[i], result[i]);
            }
        }
        finally
        {

            if (uis != null)
            {
                uis.close();
            }

            if (is != null)
            {
                is.close();
            }
        }
    }

    protected InputStream createInputStream(final UnicodeInputStream.UnicodeBOM bom, final String str, final String enc)
        throws Exception
    {

        if (bom == null)
        {
            if (str != null)
            {
                return new ByteArrayInputStream(str.getBytes(enc));
            }
            else
            {
                return new ByteArrayInputStream(new byte[0]);
            }
        }
        else
        {
            if (str != null)
            {
                return new ByteArrayInputStream(ArrayUtils.addAll(bom.getBytes(), str.getBytes(enc)));
            }
            else
            {
                return new ByteArrayInputStream(ArrayUtils.addAll(bom.getBytes(), new byte[0]));
            }
        }
    }

    protected byte [] buildTestString(final UnicodeInputStream.UnicodeBOM bom, final String str, final String enc, final boolean skipBOM)
        throws Exception
    {

        byte [] strBytes = (str != null) ? str.getBytes(enc) : new byte[0];

        if ((bom == null) || skipBOM)
        {
                return strBytes;
        }
        else
        {
            return ArrayUtils.addAll(bom.getBytes(), strBytes);
        }
    }

    protected byte [] readAllBytes(final InputStream inputStream, final String enc)
        throws Exception
    {
        InputStreamReader isr = null;

        byte [] res = new byte[0];

        try
        {
            byte[] buf = new byte[1024];
            int read = 0;

            while ((read = inputStream.read(buf)) >= 0)
            {
                res = ArrayUtils.addAll(res, ArrayUtils.subarray(buf, 0, read));
            }
        }
        finally
        {

            if (isr != null)
            {
                isr.close();
            }
        }

        return res;
    }

}