001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.io;
018:
019: import java.io.IOException;
020: import java.io.Reader;
021: import java.nio.CharBuffer;
022:
023: import junit.framework.TestCase;
024:
025: public class ReaderTest extends TestCase {
026:
027: public void test_Reader_CharBuffer_null() throws IOException {
028: String s = "MY TEST STRING";
029: MockReader mockReader = new MockReader(s.toCharArray());
030: CharBuffer charBuffer = null;
031: try {
032: mockReader.read(charBuffer);
033: fail("Should throw NullPointerException");
034: } catch (NullPointerException e) {
035: //expected;
036: }
037: }
038:
039: public void test_Reader_CharBuffer_ZeroChar() throws IOException {
040: //the charBuffer has the capacity of 0, then there the number of char read
041: // to the CharBuffer is 0. Furthermore, the MockReader is intact in its content.
042: String s = "MY TEST STRING";
043: char[] srcBuffer = s.toCharArray();
044: MockReader mockReader = new MockReader(srcBuffer);
045: CharBuffer charBuffer = CharBuffer.allocate(0);
046: int result = mockReader.read(charBuffer);
047: assertEquals(0, result);
048: char[] destBuffer = new char[srcBuffer.length];
049: mockReader.read(destBuffer);
050: assertEquals(s, String.valueOf(destBuffer));
051: }
052:
053: public void test_Reader_CharBufferChar() throws IOException {
054: String s = "MY TEST STRING";
055: char[] srcBuffer = s.toCharArray();
056: final int CHARBUFFER_SIZE = 10;
057: MockReader mockReader = new MockReader(srcBuffer);
058: CharBuffer charBuffer = CharBuffer.allocate(CHARBUFFER_SIZE);
059: charBuffer.append('A');
060: final int CHARBUFFER_REMAINING = charBuffer.remaining();
061: int result = mockReader.read(charBuffer);
062: assertEquals(CHARBUFFER_REMAINING, result);
063: charBuffer.rewind();
064: assertEquals(s.substring(0, CHARBUFFER_REMAINING), charBuffer
065: .subSequence(CHARBUFFER_SIZE - CHARBUFFER_REMAINING,
066: CHARBUFFER_SIZE).toString());
067: char[] destBuffer = new char[srcBuffer.length
068: - CHARBUFFER_REMAINING];
069: mockReader.read(destBuffer);
070: assertEquals(s.substring(CHARBUFFER_REMAINING), String
071: .valueOf(destBuffer));
072: }
073:
074: class MockReader extends Reader {
075:
076: private char[] contents;
077:
078: private int current_offset = 0;
079:
080: private int length = 0;
081:
082: public MockReader(char[] data) {
083: contents = data;
084: length = contents.length;
085: }
086:
087: @Override
088: public void close() throws IOException {
089:
090: contents = null;
091: }
092:
093: @Override
094: public int read(char[] buf, int offset, int count)
095: throws IOException {
096:
097: if (null == contents) {
098: return -1;
099: }
100: if (length <= current_offset) {
101: return -1;
102: }
103: if (buf.length < offset + count) {
104: throw new IndexOutOfBoundsException();
105: }
106:
107: count = Math.min(count, length - current_offset);
108: for (int i = 0; i < count; i++) {
109: buf[offset + i] = contents[current_offset + i];
110: }
111: current_offset += count;
112: return count;
113: }
114:
115: }
116: }
|