1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.io;
18
19import java.io.IOException;
20import java.io.Reader;
21import java.nio.CharBuffer;
22
23import junit.framework.TestCase;
24import tests.support.Support_ASimpleReader;
25import dalvik.annotation.TestLevel;
26import dalvik.annotation.TestTargetClass;
27import dalvik.annotation.TestTargetNew;
28import dalvik.annotation.TestTargets;
29
30@TestTargetClass(Reader.class)
31public class ReaderTest extends TestCase {
32
33    @TestTargetNew(
34            level = TestLevel.COMPLETE,
35            method = "Reader",
36            args = {}
37        )
38    public void test_Reader() {
39        MockReader r = new MockReader();
40        assertTrue("Test 1: Lock has not been set correctly.", r.lockSet(r));
41    }
42
43    @TestTargetNew(
44        level = TestLevel.PARTIAL_COMPLETE,
45        notes = "",
46        method = "Reader",
47        args = {java.lang.Object.class}
48    )
49    public void test_Reader_CharBuffer_null() throws IOException {
50        String s = "MY TEST STRING";
51        MockReader mockReader = new MockReader(s.toCharArray());
52        CharBuffer charBuffer = null;
53        try {
54            mockReader.read(charBuffer);
55            fail("Should throw NullPointerException");
56        } catch (NullPointerException e) {
57            //expected;
58        }
59    }
60
61    @TestTargets ({
62        @TestTargetNew(
63                level = TestLevel.PARTIAL_COMPLETE,
64                notes = "Functional test.",
65                method = "Reader",
66                args = {java.lang.Object.class}
67        ),
68        @TestTargetNew(
69                level = TestLevel.PARTIAL_COMPLETE,
70                notes = "Functional test.",
71                method = "read",
72                args = {java.nio.CharBuffer.class}
73        )
74    })
75    public void test_Reader_CharBuffer_ZeroChar() throws IOException {
76        // If the charBuffer has a capacity of 0, then the number of char read
77        // to the CharBuffer is 0. Furthermore, the MockReader is intact in
78        // its content.
79        String s = "MY TEST STRING";
80        char[] srcBuffer = s.toCharArray();
81        MockReader mockReader = new MockReader(srcBuffer);
82        CharBuffer charBuffer = CharBuffer.allocate(0);
83        int result = mockReader.read(charBuffer);
84        assertEquals(0, result);
85        char[] destBuffer = new char[srcBuffer.length];
86        mockReader.read(destBuffer);
87        assertEquals(s, String.valueOf(destBuffer));
88    }
89
90    @TestTargets ({
91        @TestTargetNew(
92                level = TestLevel.PARTIAL_COMPLETE,
93                notes = "Functional test.",
94                method = "Reader",
95                args = {java.lang.Object.class}
96        ),
97        @TestTargetNew(
98                level = TestLevel.PARTIAL_COMPLETE,
99                notes = "Functional test.",
100                method = "read",
101                args = {java.nio.CharBuffer.class}
102        )
103    })
104    public void test_Reader_CharBufferChar() throws IOException {
105        String s = "MY TEST STRING";
106        char[] srcBuffer = s.toCharArray();
107        final int CHARBUFFER_SIZE = 10;
108        MockReader mockReader = new MockReader(srcBuffer);
109        CharBuffer charBuffer = CharBuffer.allocate(CHARBUFFER_SIZE);
110        charBuffer.append('A');
111        final int CHARBUFFER_REMAINING = charBuffer.remaining();
112        int result = mockReader.read(charBuffer);
113        assertEquals(CHARBUFFER_REMAINING, result);
114        charBuffer.rewind();
115        assertEquals(s.substring(0, CHARBUFFER_REMAINING), charBuffer
116                .subSequence(CHARBUFFER_SIZE - CHARBUFFER_REMAINING,
117                        CHARBUFFER_SIZE).toString());
118        char[] destBuffer = new char[srcBuffer.length - CHARBUFFER_REMAINING];
119        mockReader.read(destBuffer);
120        assertEquals(s.substring(CHARBUFFER_REMAINING), String
121                .valueOf(destBuffer));
122
123        Support_ASimpleReader simple;
124        simple = new Support_ASimpleReader("Bla bla, what else?");
125        CharBuffer buf = CharBuffer.allocate(4);
126        assertEquals("Wrong return value!", 4, simple.read(buf));
127        buf.rewind();
128        assertEquals("Wrong stuff read!", "Bla ", String.valueOf(buf));
129        simple.read(buf);
130        buf.rewind();
131        assertEquals("Wrong stuff read!", "bla,", String.valueOf(buf));
132        simple.throwExceptionOnNextUse = true;
133        try {
134            simple.read(buf);
135            fail("IOException not thrown!");
136        } catch (IOException e) {
137            // expected
138        }
139    }
140
141    @TestTargetNew(
142        level = TestLevel.COMPLETE,
143        notes = "",
144        method = "read",
145        args = {char[].class}
146    )
147    public void test_Read_$C() throws IOException {
148        Support_ASimpleReader simple;
149        simple = new Support_ASimpleReader("Bla bla, what else?");
150        char[] buf = new char[4];
151        assertEquals("Wrong return value!", 4, simple.read(buf));
152        assertEquals("Wrong stuff read!", "Bla ", new String(buf));
153        simple.read(buf);
154        assertEquals("Wrong stuff read!", "bla,", new String(buf));
155        simple.throwExceptionOnNextUse = true;
156        try {
157            simple.read(buf);
158            fail("IOException not thrown!");
159        } catch (IOException e) {
160            // expected
161        }
162    }
163
164    /**
165     * @tests {@link java.io.Reader#mark(int)}
166     */
167    @TestTargetNew(
168        level = TestLevel.COMPLETE,
169        notes = "",
170        method = "mark",
171        args = {int.class}
172    )
173    public void test_mark() {
174        MockReader mockReader = new MockReader();
175        try {
176            mockReader.mark(0);
177            fail("Should throw IOException for Reader do not support mark");
178        } catch (IOException e) {
179            // Excepted
180        }
181    }
182
183    @TestTargetNew(
184        level = TestLevel.COMPLETE,
185        notes = "",
186        method = "markSupported",
187        args = {}
188    )
189    public void test_markSupported() {
190        assertFalse("markSupported must return false", new MockReader().markSupported());
191    }
192
193    /**
194     * @tests {@link java.io.Reader#read()}
195     */
196    @TestTargetNew(
197        level = TestLevel.COMPLETE,
198        notes = "",
199        method = "read",
200        args = {}
201    )
202    public void test_read() throws IOException {
203        MockReader reader = new MockReader();
204
205        // return -1 when the stream is null;
206        assertEquals("Should be equal to -1", -1, reader.read());
207
208        String string = "MY TEST STRING";
209        char[] srcBuffer = string.toCharArray();
210        MockReader mockReader = new MockReader(srcBuffer);
211
212        // normal read
213        for (char c : srcBuffer) {
214            assertEquals("Should be equal to \'" + c + "\'", c, mockReader
215                    .read());
216        }
217
218        // return -1 when read Out of Index
219        mockReader.read();
220        assertEquals("Should be equal to -1", -1, reader.read());
221
222        Support_ASimpleReader simple;
223        simple = new Support_ASimpleReader("Bla bla, what else?");
224        int res;
225        res = simple.read();
226        assertEquals("Wrong stuff read!", 'B', res);
227        res = simple.read();
228        assertEquals("Wrong stuff read!", 'l', res);
229        simple.throwExceptionOnNextUse = true;
230        try {
231            simple.read();
232            fail("IOException not thrown!");
233        } catch (IOException e) {
234            // expected
235        }
236    }
237
238    /**
239     * @tests {@link java.io.Reader#ready()}
240     */
241    @TestTargetNew(
242        level = TestLevel.COMPLETE,
243        notes = "",
244        method = "ready",
245        args = {}
246    )
247    public void test_ready() throws IOException {
248        MockReader mockReader = new MockReader();
249        assertFalse("Should always return false", mockReader.ready());
250
251        Support_ASimpleReader simple;
252        simple = new Support_ASimpleReader("Bla bla, what else?");
253        simple.throwExceptionOnNextUse = true;
254        try {
255            simple.ready();
256            fail("IOException not thrown!");
257        } catch (IOException e) {
258            // expected
259        }
260    }
261
262    /**
263     * @throws IOException
264     * @tests {@link java.io.Reader#reset()}
265     */
266    @TestTargetNew(
267        level = TestLevel.COMPLETE,
268        notes = "",
269        method = "reset",
270        args = {}
271    )
272    public void test_reset() throws IOException {
273        MockReader mockReader = new MockReader();
274        try {
275            mockReader.reset();
276            fail("Should throw IOException");
277        } catch (IOException e) {
278            // Excepted
279        }
280    }
281
282    /**
283     * @tests {@link java.io.Reader#skip(long)}
284     */
285    @TestTargetNew(
286        level = TestLevel.COMPLETE,
287        notes = "",
288        method = "skip",
289        args = {long.class}
290    )
291    public void test_skip() throws IOException {
292        String string = "MY TEST STRING";
293        char[] srcBuffer = string.toCharArray();
294        int length = srcBuffer.length;
295        MockReader mockReader = new MockReader(srcBuffer);
296        assertEquals("Should be equal to \'M\'", 'M', mockReader.read());
297
298        // normal skip
299        mockReader.skip(length / 2);
300        assertEquals("Should be equal to \'S\'", 'S', mockReader.read());
301
302        // try to skip a bigger number of characters than the total
303        // Should do nothing
304        mockReader.skip(length);
305
306        // try to skip a negative number of characters throw IllegalArgumentException
307        try {
308            mockReader.skip(-1);
309            fail("Should throw IllegalArgumentException");
310        } catch (IllegalArgumentException e) {
311            // Excepted
312        }
313
314        Support_ASimpleReader simple;
315        simple = new Support_ASimpleReader("Bla bla, what else?");
316        char[] buf = new char[4];
317        simple.read(buf);
318        assertEquals("Wrong stuff read!", "Bla ", new String(buf));
319        simple.skip(5);
320        simple.read(buf);
321        assertEquals("Wrong stuff read!", "what", new String(buf));
322        simple.throwExceptionOnNextUse = true;
323        try {
324            simple.skip(1);
325            fail("IOException not thrown!");
326        } catch (IOException e) {
327            // expected
328        }
329    }
330
331    class MockReader extends Reader {
332
333        private char[] contents;
334
335        private int current_offset = 0;
336
337        private int length = 0;
338
339        public MockReader() {
340            super();
341        }
342
343        public MockReader(char[] data) {
344            contents = data;
345            length = contents.length;
346        }
347
348        @Override
349        public void close() throws IOException {
350
351            contents = null;
352        }
353
354        @Override
355        public int read(char[] buf, int offset, int count) throws IOException {
356
357            if (null == contents) {
358                return -1;
359            }
360            if (length <= current_offset) {
361                return -1;
362            }
363            if (buf.length < offset + count) {
364                throw new IndexOutOfBoundsException();
365            }
366
367            count = Math.min(count, length - current_offset);
368            for (int i = 0; i < count; i++) {
369                buf[offset + i] = contents[current_offset + i];
370            }
371            current_offset += count;
372            return count;
373        }
374
375        public boolean lockSet(Object o) {
376            return (lock == o);
377        }
378    }
379}
380