Home | History | Annotate | Download | only in io
      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 
     18 package libcore.java.io;
     19 
     20 import java.io.CharArrayReader;
     21 import java.io.IOException;
     22 
     23 public class OldCharArrayReaderTest extends junit.framework.TestCase {
     24 
     25     char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
     26 
     27     CharArrayReader cr;
     28 
     29     /**
     30      * java.io.CharArrayReader#CharArrayReader(char[])
     31      */
     32     public void test_Constructor$C() {
     33         // Test for method java.io.CharArrayReader(char [])
     34 
     35         try {
     36             cr = new CharArrayReader(hw);
     37             assertTrue("Failed to create reader", cr.ready());
     38         } catch (IOException e) {
     39             fail("Exception determining ready state : " + e.getMessage());
     40         }
     41     }
     42 
     43     /**
     44      * java.io.CharArrayReader#CharArrayReader(char[], int, int)
     45      */
     46     public void test_Constructor$CII() throws IOException {
     47         try {
     48             cr = new CharArrayReader(null, 0, 0);
     49             fail("Test 1: NullPointerException expected.");
     50         } catch (NullPointerException e) {
     51             // Expected.
     52         }
     53         try {
     54             cr = new CharArrayReader(hw, -1, 0);
     55             fail("Test 2: IllegalArgumentException expected.");
     56         } catch (IllegalArgumentException e) {
     57             // Expected.
     58         }
     59         try {
     60             cr = new CharArrayReader(hw, 0, -1);
     61             fail("Test 3: IllegalArgumentException expected.");
     62         } catch (IllegalArgumentException e) {
     63             // Expected.
     64         }
     65         try {
     66             cr = new CharArrayReader(hw, hw.length + 1, 1);
     67             fail("Test 4: IllegalArgumentException expected.");
     68         } catch (IllegalArgumentException e) {
     69             // Expected.
     70         }
     71 
     72         cr = new CharArrayReader(hw, 5, 5);
     73         assertTrue("Test 5: Failed to create reader", cr.ready());
     74         assertEquals("Test 6: Incorrect character read;",
     75                 'W', cr.read());
     76     }
     77 
     78     /**
     79      * java.io.CharArrayReader#close()
     80      */
     81     public void test_close() {
     82         cr = new CharArrayReader(hw);
     83         cr.close();
     84         try {
     85             cr.read();
     86             fail("Failed to throw exception on read from closed stream");
     87         } catch (IOException e) {
     88             // Expected.
     89         }
     90 
     91     }
     92 
     93     /**
     94      * java.io.CharArrayReader#mark(int)
     95      */
     96     public void test_markI() throws IOException {
     97         cr = new CharArrayReader(hw);
     98         cr.skip(5L);
     99         cr.mark(100);
    100         cr.read();
    101         cr.reset();
    102         assertEquals("Test 1: Failed to mark correct position;",
    103                 'W', cr.read());
    104 
    105         cr.close();
    106         try {
    107             cr.mark(100);
    108             fail("Test 2: IOException expected.");
    109         } catch (IOException e) {
    110             // Expected.
    111         }
    112     }
    113 
    114     /**
    115      * java.io.CharArrayReader#markSupported()
    116      */
    117     public void test_markSupported() {
    118         cr = new CharArrayReader(hw);
    119         assertTrue("markSupported returned false", cr.markSupported());
    120     }
    121 
    122     /**
    123      * java.io.CharArrayReader#read()
    124      */
    125     public void test_read() throws IOException {
    126         cr = new CharArrayReader(hw);
    127         assertEquals("Test 1: Read returned incorrect char;",
    128                 'H', cr.read());
    129         cr = new CharArrayReader(new char[] { '\u8765' });
    130         assertTrue("Test 2: Incorrect double byte char;",
    131                 cr.read() == '\u8765');
    132 
    133         cr.close();
    134         try {
    135             cr.read();
    136             fail("Test 3: IOException expected.");
    137         } catch (IOException e) {
    138             // Expected.
    139         }
    140     }
    141 
    142     /**
    143      * java.io.CharArrayReader#read(char[], int, int)
    144      */
    145     public void test_read$CII() throws IOException {
    146         // Test for method int java.io.CharArrayReader.read(char [], int, int)
    147         char[] c = new char[11];
    148         cr = new CharArrayReader(hw);
    149         cr.read(c, 1, 10);
    150         assertTrue("Test 1: Read returned incorrect chars.",
    151                 new String(c, 1, 10).equals(new String(hw, 0, 10)));
    152 
    153         // Illegal argument checks.
    154         try {
    155             cr.read(null, 1, 0);
    156             fail("Test 2: NullPointerException expected.");
    157         } catch (NullPointerException e) {
    158             // Expected.
    159         }
    160 
    161         try {
    162             cr.read(c , -1, 1);
    163             fail("Test 3: ArrayIndexOutOfBoundsException expected.");
    164         } catch (IndexOutOfBoundsException e) {
    165             // Expected
    166         }
    167 
    168         try {
    169             cr.read(c , 1, -1);
    170             fail("Test 4: ArrayIndexOutOfBoundsException expected.");
    171         } catch (IndexOutOfBoundsException e) {
    172             // Expected
    173         }
    174 
    175         try {
    176             cr.read(c, 1, c.length);
    177             fail("Test 5: ArrayIndexOutOfBoundsException expected.");
    178         } catch (IndexOutOfBoundsException e) {
    179             // Expected
    180         }
    181 
    182         cr.close();
    183         try {
    184             cr.read(c, 1, 1);
    185             fail("Test 6: IOException expected.");
    186         } catch (IOException e) {
    187             // Expected.
    188         }
    189     }
    190 
    191     public void test_ready() {
    192         // Test for method boolean java.io.CharArrayReader.ready()
    193         cr = new CharArrayReader(hw);
    194         boolean expectException = false;
    195         try {
    196             assertTrue("ready returned false", cr.ready());
    197             cr.skip(1000);
    198             assertTrue("ready returned true", !cr.ready());
    199             cr.close();
    200             expectException = true;
    201             cr.ready();
    202             fail("No exception 1");
    203         } catch (IOException e) {
    204             if (!expectException)
    205                 fail("Unexpected: " + e);
    206         }
    207         try {
    208             cr = new CharArrayReader(hw);
    209             cr.close();
    210             cr.ready();
    211             fail("No exception 2");
    212         } catch (IOException e) {
    213         }
    214     }
    215 
    216     /**
    217      * java.io.CharArrayReader#reset()
    218      */
    219     public void test_reset() throws IOException {
    220         cr = new CharArrayReader(hw);
    221         cr.skip(5L);
    222         cr.mark(100);
    223         cr.read();
    224         cr.reset();
    225         assertEquals("Test 1: Reset failed to return to marker position.",
    226                 'W', cr.read());
    227 
    228         cr.close();
    229         try {
    230             cr.reset();
    231             fail("Test 2: IOException expected.");
    232         } catch (IOException e) {
    233             // Expected.
    234         }
    235     }
    236 
    237     public void test_skipJ() throws IOException {
    238         long skipped = 0;
    239         cr = new CharArrayReader(hw);
    240         skipped = cr.skip(5L);
    241         assertEquals("Test 1: Failed to skip correct number of chars;",
    242                 5L, skipped);
    243         assertEquals("Test 2: Skip skipped wrong chars;",
    244                 'W', cr.read());
    245 
    246         cr.close();
    247         try {
    248             cr.skip(1);
    249             fail("Test 3: IOException expected.");
    250         } catch (IOException e) {
    251             // Expected.
    252         }
    253     }
    254 
    255     protected void tearDown() {
    256         if (cr != null)
    257             cr.close();
    258     }
    259 }
    260