Home | History | Annotate | Download | only in io
      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 
     17 package org.apache.harmony.luni.tests.java.io;
     18 
     19 import java.io.IOException;
     20 import java.io.Reader;
     21 import java.nio.CharBuffer;
     22 
     23 import junit.framework.TestCase;
     24 
     25 public class ReaderTest extends TestCase {
     26 
     27     public void test_Reader_CharBuffer_null() throws IOException {
     28         String s = "MY TEST STRING";
     29         MockReader mockReader = new MockReader(s.toCharArray());
     30         CharBuffer charBuffer = null;
     31         try {
     32             mockReader.read(charBuffer);
     33             fail("Should throw NullPointerException");
     34         } catch (NullPointerException e) {
     35             //expected;
     36         }
     37     }
     38 
     39     public void test_Reader_CharBuffer_ZeroChar() throws IOException {
     40         //the charBuffer has the capacity of 0, then there the number of char read
     41         // to the CharBuffer is 0. Furthermore, the MockReader is intact in its content.
     42         String s = "MY TEST STRING";
     43         char[] srcBuffer = s.toCharArray();
     44         MockReader mockReader = new MockReader(srcBuffer);
     45         CharBuffer charBuffer = CharBuffer.allocate(0);
     46         int result = mockReader.read(charBuffer);
     47         assertEquals(0, result);
     48         char[] destBuffer = new char[srcBuffer.length];
     49         mockReader.read(destBuffer);
     50         assertEquals(s, String.valueOf(destBuffer));
     51     }
     52 
     53     public void test_Reader_CharBufferChar() throws IOException {
     54         String s = "MY TEST STRING";
     55         char[] srcBuffer = s.toCharArray();
     56         final int CHARBUFFER_SIZE = 10;
     57         MockReader mockReader = new MockReader(srcBuffer);
     58         CharBuffer charBuffer = CharBuffer.allocate(CHARBUFFER_SIZE);
     59         charBuffer.append('A');
     60         final int CHARBUFFER_REMAINING = charBuffer.remaining();
     61         int result = mockReader.read(charBuffer);
     62         assertEquals(CHARBUFFER_REMAINING, result);
     63         charBuffer.rewind();
     64         assertEquals(s.substring(0, CHARBUFFER_REMAINING), charBuffer
     65                 .subSequence(CHARBUFFER_SIZE - CHARBUFFER_REMAINING,
     66                         CHARBUFFER_SIZE).toString());
     67         char[] destBuffer = new char[srcBuffer.length - CHARBUFFER_REMAINING];
     68         mockReader.read(destBuffer);
     69         assertEquals(s.substring(CHARBUFFER_REMAINING), String
     70                 .valueOf(destBuffer));
     71     }
     72 
     73     /**
     74      * @tests {@link java.io.Reader#mark(int)}
     75      */
     76     public void test_mark() {
     77         MockReader mockReader = new MockReader();
     78         try {
     79             mockReader.mark(0);
     80             fail("Should throw IOException for Reader do not support mark");
     81         } catch (IOException e) {
     82             // Excepted
     83         }
     84     }
     85 
     86     /**
     87      * @tests {@link java.io.Reader#read()}
     88      */
     89     public void test_read() throws IOException {
     90         MockReader reader = new MockReader();
     91 
     92         // return -1 when the stream is null;
     93         assertEquals("Should be equal to -1", -1, reader.read());
     94 
     95         String string = "MY TEST STRING";
     96         char[] srcBuffer = string.toCharArray();
     97         MockReader mockReader = new MockReader(srcBuffer);
     98 
     99         // normal read
    100         for (char c : srcBuffer) {
    101             assertEquals("Should be equal to \'" + c + "\'", c, mockReader
    102                     .read());
    103         }
    104 
    105         // return -1 when read Out of Index
    106         mockReader.read();
    107         assertEquals("Should be equal to -1", -1, reader.read());
    108 
    109     }
    110 
    111     /**
    112      * @tests {@link java.io.Reader#ready()}
    113      */
    114     public void test_ready() throws IOException {
    115         MockReader mockReader = new MockReader();
    116         assertFalse("Should always return false", mockReader.ready());
    117 
    118     }
    119 
    120     /**
    121      * @tests {@link java.io.Reader#reset()}
    122      */
    123     public void test_reset() {
    124         MockReader mockReader = new MockReader();
    125         try {
    126             mockReader.reset();
    127             fail("Should throw IOException");
    128         } catch (IOException e) {
    129             // Excepted
    130         }
    131     }
    132 
    133     /**
    134      * @tests {@link java.io.Reader#skip(long)}
    135      */
    136     public void test_skip() throws IOException {
    137         String string = "MY TEST STRING";
    138         char[] srcBuffer = string.toCharArray();
    139         int length = srcBuffer.length;
    140         MockReader mockReader = new MockReader(srcBuffer);
    141         assertEquals("Should be equal to \'M\'", 'M', mockReader.read());
    142 
    143         // normal skip
    144         mockReader.skip(length / 2);
    145         assertEquals("Should be equal to \'S\'", 'S', mockReader.read());
    146 
    147         // try to skip a bigger number of characters than the total
    148         // Should do nothing
    149         mockReader.skip(length);
    150 
    151         // try to skip a negative number of characters throw IllegalArgumentException
    152         try {
    153             mockReader.skip(-1);
    154             fail("Should throw IllegalArgumentException");
    155         } catch (IllegalArgumentException e) {
    156             // Excepted
    157         }
    158     }
    159 
    160     class MockReader extends Reader {
    161 
    162         private char[] contents;
    163 
    164         private int current_offset = 0;
    165 
    166         private int length = 0;
    167 
    168         public MockReader() {
    169             super();
    170         }
    171 
    172         public MockReader(char[] data) {
    173             contents = data;
    174             length = contents.length;
    175         }
    176 
    177         @Override
    178         public void close() throws IOException {
    179 
    180             contents = null;
    181         }
    182 
    183         @Override
    184         public int read(char[] buf, int offset, int count) throws IOException {
    185 
    186             if (null == contents) {
    187                 return -1;
    188             }
    189             if (length <= current_offset) {
    190                 return -1;
    191             }
    192             if (buf.length < offset + count) {
    193                 throw new IndexOutOfBoundsException();
    194             }
    195 
    196             count = Math.min(count, length - current_offset);
    197             for (int i = 0; i < count; i++) {
    198                 buf[offset + i] = contents[current_offset + i];
    199             }
    200             current_offset += count;
    201             return count;
    202         }
    203 
    204     }
    205 }
    206