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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.harmony.luni.tests.java.io;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.nio.CharBuffer;
23 import junit.framework.TestCase;
24 import tests.support.Support_ASimpleReader;
25 import dalvik.annotation.TestLevel;
26 import dalvik.annotation.TestTargetClass;
27 import dalvik.annotation.TestTargetNew;
28 import dalvik.annotation.TestTargets;
30 @TestTargetClass(Reader.class)
31 public class ReaderTest extends TestCase {
34 level = TestLevel.COMPLETE,
38 public void test_Reader() {
39 MockReader r = new MockReader();
40 assertTrue("Test 1: Lock has not been set correctly.", r.lockSet(r));
44 level = TestLevel.PARTIAL_COMPLETE,
47 args = {java.lang.Object.class}
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;
54 mockReader.read(charBuffer);
55 fail("Should throw NullPointerException");
56 } catch (NullPointerException e) {
63 level = TestLevel.PARTIAL_COMPLETE,
64 notes = "Functional test.",
66 args = {java.lang.Object.class}
69 level = TestLevel.PARTIAL_COMPLETE,
70 notes = "Functional test.",
72 args = {java.nio.CharBuffer.class}
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
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));
92 level = TestLevel.PARTIAL_COMPLETE,
93 notes = "Functional test.",
95 args = {java.lang.Object.class}
98 level = TestLevel.PARTIAL_COMPLETE,
99 notes = "Functional test.",
101 args = {java.nio.CharBuffer.class}
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);
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));
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));
128 assertEquals("Wrong stuff read!", "Bla ", String.valueOf(buf));
131 assertEquals("Wrong stuff read!", "bla,", String.valueOf(buf));
132 simple.throwExceptionOnNextUse = true;
135 fail("IOException not thrown!");
136 } catch (IOException e) {
142 level = TestLevel.COMPLETE,
145 args = {char[].class}
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));
154 assertEquals("Wrong stuff read!", "bla,", new String(buf));
155 simple.throwExceptionOnNextUse = true;
158 fail("IOException not thrown!");
159 } catch (IOException e) {
165 * @tests {@link java.io.Reader#mark(int)}
168 level = TestLevel.COMPLETE,
173 public void test_mark() {
174 MockReader mockReader = new MockReader();
177 fail("Should throw IOException for Reader do not support mark");
178 } catch (IOException e) {
184 level = TestLevel.COMPLETE,
186 method = "markSupported",
189 public void test_markSupported() {
190 assertFalse("markSupported must return false", new MockReader().markSupported());
194 * @tests {@link java.io.Reader#read()}
197 level = TestLevel.COMPLETE,
202 public void test_read() throws IOException {
203 MockReader reader = new MockReader();
205 // return -1 when the stream is null;
206 assertEquals("Should be equal to -1", -1, reader.read());
208 String string = "MY TEST STRING";
209 char[] srcBuffer = string.toCharArray();
210 MockReader mockReader = new MockReader(srcBuffer);
213 for (char c : srcBuffer) {
214 assertEquals("Should be equal to \'" + c + "\'", c, mockReader
218 // return -1 when read Out of Index
220 assertEquals("Should be equal to -1", -1, reader.read());
222 Support_ASimpleReader simple;
223 simple = new Support_ASimpleReader("Bla bla, what else?");
226 assertEquals("Wrong stuff read!", 'B', res);
228 assertEquals("Wrong stuff read!", 'l', res);
229 simple.throwExceptionOnNextUse = true;
232 fail("IOException not thrown!");
233 } catch (IOException e) {
239 * @tests {@link java.io.Reader#ready()}
242 level = TestLevel.COMPLETE,
247 public void test_ready() throws IOException {
248 MockReader mockReader = new MockReader();
249 assertFalse("Should always return false", mockReader.ready());
251 Support_ASimpleReader simple;
252 simple = new Support_ASimpleReader("Bla bla, what else?");
253 simple.throwExceptionOnNextUse = true;
256 fail("IOException not thrown!");
257 } catch (IOException e) {
263 * @throws IOException
264 * @tests {@link java.io.Reader#reset()}
267 level = TestLevel.COMPLETE,
272 public void test_reset() throws IOException {
273 MockReader mockReader = new MockReader();
276 fail("Should throw IOException");
277 } catch (IOException e) {
283 * @tests {@link java.io.Reader#skip(long)}
286 level = TestLevel.COMPLETE,
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());
299 mockReader.skip(length / 2);
300 assertEquals("Should be equal to \'S\'", 'S', mockReader.read());
302 // try to skip a bigger number of characters than the total
304 mockReader.skip(length);
306 // try to skip a negative number of characters throw IllegalArgumentException
309 fail("Should throw IllegalArgumentException");
310 } catch (IllegalArgumentException e) {
314 Support_ASimpleReader simple;
315 simple = new Support_ASimpleReader("Bla bla, what else?");
316 char[] buf = new char[4];
318 assertEquals("Wrong stuff read!", "Bla ", new String(buf));
321 assertEquals("Wrong stuff read!", "what", new String(buf));
322 simple.throwExceptionOnNextUse = true;
325 fail("IOException not thrown!");
326 } catch (IOException e) {
331 class MockReader extends Reader {
333 private char[] contents;
335 private int current_offset = 0;
337 private int length = 0;
339 public MockReader() {
343 public MockReader(char[] data) {
345 length = contents.length;
349 public void close() throws IOException {
355 public int read(char[] buf, int offset, int count) throws IOException {
357 if (null == contents) {
360 if (length <= current_offset) {
363 if (buf.length < offset + count) {
364 throw new IndexOutOfBoundsException();
367 count = Math.min(count, length - current_offset);
368 for (int i = 0; i < count; i++) {
369 buf[offset + i] = contents[current_offset + i];
371 current_offset += count;
375 public boolean lockSet(Object o) {