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.IOException;
     21 import java.io.SequenceInputStream;
     22 import tests.support.Support_ASimpleInputStream;
     23 
     24 public class OldSequenceInputStreamTest extends junit.framework.TestCase {
     25 
     26     Support_ASimpleInputStream simple1, simple2;
     27     SequenceInputStream si;
     28     String s1 = "Hello";
     29     String s2 = "World";
     30 
     31     public void test_available() throws IOException {
     32         assertEquals("Returned incorrect number of bytes!", s1.length(), si.available());
     33         simple2.throwExceptionOnNextUse = true;
     34         assertTrue("IOException on second stream should not affect at this time!",
     35                 si.available() == s1.length());
     36         simple1.throwExceptionOnNextUse = true;
     37         try {
     38             si.available();
     39             fail("IOException not thrown!");
     40         } catch (IOException e) {
     41             // expected
     42         }
     43     }
     44 
     45     public void test_close2() throws IOException {
     46         simple1.throwExceptionOnNextUse = true;
     47         try {
     48             si.close();
     49             fail("IOException not thrown!");
     50         } catch (IOException e) {
     51             // expected
     52         }
     53     }
     54 
     55     public void test_read() throws IOException {
     56         si.read();
     57         assertEquals("Test 1: Incorrect char read;",
     58                 s1.charAt(1), (char) si.read());
     59 
     60         // We are still reading from the first input stream, should be ok.
     61         simple2.throwExceptionOnNextUse = true;
     62         try {
     63             assertEquals("Test 2: Incorrect char read;",
     64                     s1.charAt(2), (char) si.read());
     65         } catch (IOException e) {
     66             fail("Test 3: Unexpected IOException.");
     67         }
     68 
     69         simple1.throwExceptionOnNextUse = true;
     70         try {
     71             si.read();
     72             fail("Test 4: IOException expected.");
     73         } catch (IOException e) {
     74             // Expected.
     75         }
     76         simple1.throwExceptionOnNextUse = false;
     77 
     78         // Reading bytes 4 and 5 of the first input stream should be ok again.
     79         si.read();
     80         si.read();
     81 
     82         // Reading the first byte of the second input stream should fail.
     83         try {
     84             si.read();
     85             fail("Test 5: IOException expected.");
     86         } catch (IOException e) {
     87             // Expected.
     88         }
     89 
     90         // Reading from the second input stream should be ok now.
     91         simple2.throwExceptionOnNextUse = false;
     92         try {
     93             assertEquals("Test 6: Incorrect char read;",
     94                     s2.charAt(0), (char) si.read());
     95         } catch (IOException e) {
     96             fail("Test 7: Unexpected IOException.");
     97         }
     98 
     99         si.close();
    100         assertTrue("Test 8: -1 expected when reading from a closed " +
    101                    "sequence input stream.", si.read() == -1);
    102     }
    103 
    104     public void test_read_exc() throws IOException {
    105         simple2.throwExceptionOnNextUse = true;
    106         assertEquals("IOException on second stream should not affect at this time!", 72, si.read());
    107         simple1.throwExceptionOnNextUse = true;
    108         try {
    109             si.read();
    110             fail("IOException not thrown!");
    111         } catch (IOException e) {
    112             // expected
    113         }
    114     }
    115 
    116     public void test_read$BII_Excpetion() throws IOException {
    117         byte[] buf = new byte[4];
    118         si.read(buf, 0, 2);
    119         si.read(buf, 2, 1);
    120         simple2.throwExceptionOnNextUse = true;
    121         si.read(buf, 3, 1);
    122         assertEquals("Wrong stuff read!", "Hell", new String(buf));
    123         simple1.throwExceptionOnNextUse = true;
    124         try {
    125             si.read(buf, 3, 1);
    126             fail("IOException not thrown!");
    127         } catch (IOException e) {
    128             // expected
    129         }
    130 
    131         buf = new byte[10];
    132         simple1 = new Support_ASimpleInputStream(s1);
    133         simple2 = new Support_ASimpleInputStream(s2);
    134         si = new SequenceInputStream(simple1, simple2);
    135         try {
    136             si.read(buf, -1, 1);
    137             fail("IndexOutOfBoundsException was not thrown");
    138         } catch (IndexOutOfBoundsException e) {
    139             // Expected
    140         }
    141         try {
    142             si.read(buf, 0, -1);
    143             fail("IndexOutOfBoundsException was not thrown");
    144         } catch (IndexOutOfBoundsException e) {
    145             // Expected
    146         }
    147         try {
    148             si.read(buf, 1, 10);
    149             fail("IndexOutOfBoundsException was not thrown");
    150         } catch (IndexOutOfBoundsException e) {
    151             // Expected
    152         }
    153     }
    154 
    155     protected void setUp() {
    156         simple1 = new Support_ASimpleInputStream(s1);
    157         simple2 = new Support_ASimpleInputStream(s2);
    158         si = new SequenceInputStream(simple1, simple2);
    159     }
    160 }
    161