1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     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
18package libcore.java.io;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.OutputStreamWriter;
26import java.io.UnsupportedEncodingException;
27import java.util.Arrays;
28import junit.framework.TestCase;
29import tests.support.Support_ASimpleInputStream;
30
31public class OldInputStreamReaderTest extends TestCase {
32
33    private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
34
35    private InputStream in;
36
37    private InputStreamReader reader;
38
39    private InputStreamReader is;
40
41    private InputStream fis;
42
43    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\n";
44
45    protected void setUp() throws Exception {
46        super.setUp();
47
48        in = new ByteArrayInputStream(source.getBytes("UTF-8"));
49        reader = new InputStreamReader(in, "UTF-8");
50
51        ByteArrayOutputStream bos = new ByteArrayOutputStream();
52        OutputStreamWriter osw = new OutputStreamWriter(bos);
53        char[] buf = new char[fileString.length()];
54        fileString.getChars(0, fileString.length(), buf, 0);
55        osw.write(buf);
56        osw.close();
57        fis = new ByteArrayInputStream(bos.toByteArray());
58        is = new InputStreamReader(fis);
59    }
60
61    protected void tearDown() throws Exception {
62        try {
63            in.close();
64            is.close();
65            fis.close();
66        } catch (IOException e) {
67        }
68
69        super.tearDown();
70    }
71
72    public void testReadcharArrayintint() throws IOException {
73        try {
74            reader.read(new char[3], -1, 0);
75            fail("Should throw IndexOutOfBoundsException");
76        } catch (IndexOutOfBoundsException e) {
77            //expected
78        }
79        try {
80            reader.read(new char[3], 0, -1);
81            fail("Should throw IndexOutOfBoundsException");
82        } catch (IndexOutOfBoundsException e) {
83            //expected
84        }
85        try {
86            reader.read(new char[3], 4, 0);
87            fail("Should throw IndexOutOfBoundsException");
88        } catch (IndexOutOfBoundsException e) {
89            //expected
90        }
91        try {
92            reader.read(new char[3], 3, 1);
93            fail("Should throw IndexOutOfBoundsException");
94        } catch (IndexOutOfBoundsException e) {
95            //expected
96        }
97        try {
98            reader.read(new char[3], 1, 3);
99            fail("Should throw IndexOutOfBoundsException");
100        } catch (IndexOutOfBoundsException e) {
101            //expected
102        }
103        try {
104            reader.read(new char[3], 0, 4);
105            fail("Should throw IndexOutOfBoundsException");
106        } catch (IndexOutOfBoundsException e) {
107            //expected
108        }
109
110        try {
111            reader.read(null, 0, 0);
112            fail("Should throw NullPointerException");
113        } catch (NullPointerException e) {
114            //expected
115        }
116
117        assertEquals(0, reader.read(new char[3], 3, 0));
118        char[] chars = new char[source.length()];
119        assertEquals(0, reader.read(chars, 0, 0));
120        assertEquals(0, chars[0]);
121        assertEquals(3, reader.read(chars, 0, 3));
122        assertEquals(5, reader.read(chars, 3, 5));
123        assertEquals(source.length() - 8, reader.read(chars, 8,
124                chars.length - 8));
125        assertTrue(Arrays.equals(chars, source.toCharArray()));
126        assertEquals(-1, reader.read(chars, 0, chars.length));
127        assertTrue(Arrays.equals(chars, source.toCharArray()));
128    }
129
130    public void testReadcharArrayintint2() throws IOException {
131        char[] chars = new char[source.length()];
132        assertEquals(source.length() - 3, reader.read(chars, 0,
133                chars.length - 3));
134        assertEquals(3, reader.read(chars, 0, 10));
135    }
136
137    public void testReady() throws IOException {
138        assertTrue(reader.ready());
139        reader.read(new char[source.length()]);
140        assertFalse(reader.ready());
141    }
142
143    public void testInputStreamReaderSuccessiveReads() throws IOException {
144        byte[] data = new byte[8192 * 2];
145        Arrays.fill(data, (byte) 116); // 116 = ISO-8859-1 value for 't'
146        ByteArrayInputStream bis = new ByteArrayInputStream(data);
147        InputStreamReader isr = new InputStreamReader(bis, "ISO-8859-1");
148
149        // One less than the InputStreamReader.BUFFER_SIZE
150        char[] buf = new char[8191];
151        int bytesRead = isr.read(buf, 0, buf.length);
152        if (bytesRead == -1) {
153            throw new RuntimeException();
154        }
155        bytesRead = isr.read(buf, 0, buf.length);
156        if (bytesRead == -1) {
157            throw new RuntimeException();
158        }
159    }
160
161    public void test_ConstructorLjava_io_InputStream() {
162        // Test for method java.io.InputStreamReader(java.io.InputStream)
163        assertTrue("Used to test other methods", true);
164    }
165
166    public void test_ConstructorLjava_io_InputStreamLjava_lang_String() {
167        // Test for method java.io.InputStreamReader(java.io.InputStream,
168        // java.lang.String)
169        try {
170            is = new InputStreamReader(fis, "8859_1");
171        } catch (UnsupportedEncodingException e) {
172            fail("Unable to create input stream : " + e.getMessage());
173        }
174
175        try {
176            is = new InputStreamReader(fis, "Bogus");
177        } catch (UnsupportedEncodingException e) {
178            return;
179        }
180        fail("Failed to throw Unsupported Encoding exception");
181    }
182
183    public void test_close() {
184        // Test for method void java.io.InputStreamReader.close()
185        try {
186            is.close();
187        } catch (IOException e) {
188            fail("Failed to close reader : " + e.getMessage());
189        }
190        try {
191            is.read();
192            fail("Test 1: IOException expected.");
193        } catch (IOException e) {
194            // Exception means read failed due to close
195        }
196
197        is = new InputStreamReader(new Support_ASimpleInputStream(true));
198        try {
199            is.read();
200            fail("Test 2: IOException expected.");
201        } catch (IOException e) {
202            // Expected.
203        }
204
205    }
206
207    public void testClose() throws IOException {
208        reader.close();
209        try {
210            reader.ready();
211            fail("Should throw IOException");
212        } catch (IOException e) {
213        }
214        reader.close();
215    }
216
217     public void test_read$CII() {
218        // Test for method int java.io.InputStreamReader.read(char [], int, int)
219        try {
220            char[] rbuf = new char[100];
221            char[] sbuf = new char[100];
222            fileString.getChars(0, 100, sbuf, 0);
223            is.read(rbuf, 0, 100);
224            for (int i = 0; i < rbuf.length; i++)
225                assertTrue("returned incorrect chars", rbuf[i] == sbuf[i]);
226        } catch (IOException e) {
227            fail("Exception during read test : " + e.getMessage());
228        }
229    }
230
231    public void test_ready() {
232        // Test for method boolean java.io.InputStreamReader.ready()
233        try {
234            assertTrue("Ready test failed", is.ready());
235            is.read();
236            assertTrue("More chars, but not ready", is.ready());
237        } catch (IOException e) {
238            fail("Exception during ready test : " + e.getMessage());
239        }
240    }
241
242    /**
243     * Test for regression of a bug that dropped characters when
244     * multibyte encodings spanned buffer boundaries.
245     */
246    public void test_readWhenCharacterSpansBuffer() {
247        final byte[] suffix = {
248            (byte) 0x93, (byte) 0xa1, (byte) 0x8c, (byte) 0xb4,
249            (byte) 0x97, (byte) 0x43, (byte) 0x88, (byte) 0xea,
250            (byte) 0x98, (byte) 0x59
251        };
252        final char[] decodedSuffix = {
253            (char) 0x85e4, (char) 0x539f, (char) 0x4f51, (char) 0x4e00,
254            (char) 0x90ce
255        };
256        final int prefixLength = 8189;
257
258        byte[] bytes = new byte[prefixLength + 10];
259        Arrays.fill(bytes, (byte) ' ');
260        System.arraycopy(suffix, 0, bytes, prefixLength, suffix.length);
261        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
262
263        try {
264            InputStreamReader isr = new InputStreamReader(is, "SHIFT_JIS");
265            char[] chars = new char[8192];
266            int at = 0;
267
268            for (;;) {
269                int amt = isr.read(chars);
270                if (amt <= 0) {
271                    break;
272                }
273
274                for (int i = 0; i < amt; i++) {
275                    char c = chars[i];
276                    if (at < prefixLength) {
277                        if (c != ' ') {
278                            fail("Found bad prefix character " +
279                                    (int) c + " at " + at);
280                        }
281                    } else {
282                        char decoded = decodedSuffix[at - prefixLength];
283                        if (c != decoded) {
284                            fail("Found mismatched character " +
285                                    (int) c + " at " + at);
286                        }
287                    }
288                    at++;
289                }
290            }
291        } catch (IOException ex) {
292            throw new RuntimeException("unexpected exception", ex);
293        }
294    }
295}
296