01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.tools.ant.util;
19:
20: import java.io.*;
21:
22: import junit.framework.TestCase;
23:
24: /**
25: * Test for ReaderInputStream
26: */
27: public class ReaderInputStreamTest extends TestCase {
28: public ReaderInputStreamTest(String s) {
29: super (s);
30: }
31:
32: public void testSimple() throws Exception {
33: compareBytes("abc", "utf-8");
34: }
35:
36: public void testSimple16() throws Exception {
37: compareBytes("a", "utf-16");
38: }
39:
40: public void testSimpleAbc16() throws Exception {
41: // THIS WILL FAIL.
42: //compareBytes("abc", "utf-16");
43: byte[] bytes = new byte[40];
44: int pos = 0;
45: ReaderInputStream r = new ReaderInputStream(new StringReader(
46: "abc"), "utf-16");
47: for (int i = 0; true; ++i) {
48: int res = r.read();
49: if (res == -1) {
50: break;
51: }
52: bytes[pos++] = (byte) res;
53: }
54: bytes = "abc".getBytes("utf-16");
55: // String n = new String(bytes, 0, pos, "utf-16");
56: String n = new String(bytes, 0, bytes.length, "utf-16");
57: System.out.println(n);
58: }
59:
60: public void testReadZero() throws Exception {
61: ReaderInputStream r = new ReaderInputStream(new StringReader(
62: "abc"));
63: byte[] bytes = new byte[30];
64: // First read in zero bytes
65: r.read(bytes, 0, 0);
66: // Now read in the string
67: int readin = r.read(bytes, 0, 10);
68: // Make sure that the counts are the same
69: assertEquals("abc".getBytes().length, readin);
70: }
71:
72: public void testPreample() throws Exception {
73: byte[] bytes = "".getBytes("utf-16");
74: System.out.println("Preample len is " + bytes.length);
75: }
76:
77: private void compareBytes(String s, String encoding)
78: throws Exception {
79: byte[] expected = s.getBytes(encoding);
80:
81: ReaderInputStream r = new ReaderInputStream(
82: new StringReader(s), encoding);
83: for (int i = 0; i < expected.length; ++i) {
84: int expect = expected[i] & 0xFF;
85: int read = r.read();
86: if (expect != read) {
87: fail("Mismatch in ReaderInputStream at index " + i
88: + " expecting " + expect + " got " + read
89: + " for string " + s + " with encoding "
90: + encoding);
91: }
92: }
93: if (r.read() != -1) {
94: fail("Mismatch in ReaderInputStream - EOF not seen for string "
95: + s + " with encoding " + encoding);
96: }
97: }
98: }
|