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.InputStream; 21 import java.util.Vector; 22 import java.io.IOException; 23 import java.io.SequenceInputStream; 24 import tests.support.Support_ASimpleInputStream; 25 26 public class OldSequenceInputStreamTest extends junit.framework.TestCase { 27 28 Support_ASimpleInputStream simple1, simple2; 29 SequenceInputStream si; 30 final String s1 = "Hello"; 31 final String s2 = "World"; 32 33 public void test_available() throws IOException { 34 assertEquals("Returned incorrect number of bytes!", s1.length(), si.available()); 35 simple2.throwExceptionOnNextUse = true; 36 assertTrue("IOException on second stream should not affect at this time!", 37 si.available() == s1.length()); 38 simple1.throwExceptionOnNextUse = true; 39 try { 40 si.available(); 41 fail("IOException not thrown!"); 42 } catch (IOException e) { 43 // expected 44 } 45 } 46 47 public void test_close2() throws IOException { 48 simple1.throwExceptionOnNextUse = true; 49 try { 50 si.close(); 51 fail("IOException not thrown!"); 52 } catch (IOException e) { 53 // expected 54 } 55 } 56 57 public void test_read() throws IOException { 58 si.read(); 59 assertEquals("Test 1: Incorrect char read;", 60 s1.charAt(1), (char) si.read()); 61 62 // We are still reading from the first input stream, should be ok. 63 simple2.throwExceptionOnNextUse = true; 64 try { 65 assertEquals("Test 2: Incorrect char read;", 66 s1.charAt(2), (char) si.read()); 67 } catch (IOException e) { 68 fail("Test 3: Unexpected IOException."); 69 } 70 71 simple1.throwExceptionOnNextUse = true; 72 try { 73 si.read(); 74 fail("Test 4: IOException expected."); 75 } catch (IOException e) { 76 // Expected. 77 } 78 simple1.throwExceptionOnNextUse = false; 79 80 // Reading bytes 4 and 5 of the first input stream should be ok again. 81 si.read(); 82 si.read(); 83 84 // Reading the first byte of the second input stream should fail. 85 try { 86 si.read(); 87 fail("Test 5: IOException expected."); 88 } catch (IOException e) { 89 // Expected. 90 } 91 92 // Reading from the second input stream should be ok now. 93 simple2.throwExceptionOnNextUse = false; 94 try { 95 assertEquals("Test 6: Incorrect char read;", 96 s2.charAt(0), (char) si.read()); 97 } catch (IOException e) { 98 fail("Test 7: Unexpected IOException."); 99 } 100 101 si.close(); 102 assertTrue("Test 8: -1 expected when reading from a closed " + 103 "sequence input stream.", si.read() == -1); 104 } 105 106 public void test_read_exc() throws IOException { 107 simple2.throwExceptionOnNextUse = true; 108 assertEquals("IOException on second stream should not affect at this time!", 72, si.read()); 109 simple1.throwExceptionOnNextUse = true; 110 try { 111 si.read(); 112 fail("IOException not thrown!"); 113 } catch (IOException e) { 114 // expected 115 } 116 } 117 118 public void test_read$BII_Excpetion() throws IOException { 119 byte[] buf = new byte[4]; 120 si.read(buf, 0, 2); 121 si.read(buf, 2, 1); 122 simple2.throwExceptionOnNextUse = true; 123 si.read(buf, 3, 1); 124 assertEquals("Wrong stuff read!", "Hell", new String(buf)); 125 simple1.throwExceptionOnNextUse = true; 126 try { 127 si.read(buf, 3, 1); 128 fail("IOException not thrown!"); 129 } catch (IOException e) { 130 // expected 131 } 132 133 buf = new byte[10]; 134 simple1 = new Support_ASimpleInputStream(s1); 135 simple2 = new Support_ASimpleInputStream(s2); 136 si = new SequenceInputStream(simple1, simple2); 137 try { 138 si.read(buf, -1, 1); 139 fail("IndexOutOfBoundsException was not thrown"); 140 } catch (IndexOutOfBoundsException e) { 141 // Expected 142 } 143 try { 144 si.read(buf, 0, -1); 145 fail("IndexOutOfBoundsException was not thrown"); 146 } catch (IndexOutOfBoundsException e) { 147 // Expected 148 } 149 try { 150 si.read(buf, 1, 10); 151 fail("IndexOutOfBoundsException was not thrown"); 152 } catch (IndexOutOfBoundsException e) { 153 // Expected 154 } 155 } 156 157 public void test_readStackOVerflow() throws Exception { 158 // 2^16 should be enough to overflow 159 Vector<InputStream> inputs = new Vector<>(); 160 InputStream emptyInputStream = new Support_ASimpleInputStream(new byte[0]); 161 for (int i=0;i < 32768; i++) { 162 inputs.add(emptyInputStream); 163 } 164 165 SequenceInputStream sequenceInputStream = new SequenceInputStream(inputs.elements()); 166 assertEquals(-1, sequenceInputStream.read()); 167 168 byte[] buf = new byte[10]; 169 sequenceInputStream = new SequenceInputStream(inputs.elements()); 170 assertEquals(-1, sequenceInputStream.read(buf, 0, 10)); 171 } 172 173 private SequenceInputStream createSequenceInputStreamWithGaps() { 174 Vector<InputStream> inputs = new Vector<>(); 175 InputStream emptyInputStream = new Support_ASimpleInputStream(new byte[0]); 176 inputs.add(emptyInputStream); 177 inputs.add(simple1); 178 inputs.add(emptyInputStream); 179 inputs.add(simple2); 180 inputs.add(emptyInputStream); 181 return new SequenceInputStream(inputs.elements()); 182 } 183 184 public void test_readArraySkipsEmpty() throws Exception { 185 SequenceInputStream sequenceInputStream1 = createSequenceInputStreamWithGaps(); 186 byte[] buf = new byte[10]; 187 assertEquals(s1.length(), sequenceInputStream1.read(buf, 0, s1.length())); 188 assertEquals(s1, new String(buf, 0, s1.length())); 189 assertEquals(s2.length(), sequenceInputStream1.read(buf, 0, s2.length())); 190 assertEquals(s2, new String(buf, 0, s2.length())); 191 assertEquals(-1, sequenceInputStream1.read(buf, 0, s1.length())); 192 } 193 194 public void test_readSkipsEmpty() throws Exception { 195 SequenceInputStream sequenceInputStream1 = createSequenceInputStreamWithGaps(); 196 for (int i=0;i < s1.length(); i++) { 197 assertEquals(s1.charAt(i), sequenceInputStream1.read()); 198 } 199 for (int i=0;i < s2.length(); i++) { 200 assertEquals(s2.charAt(i), sequenceInputStream1.read()); 201 } 202 assertEquals(-1, sequenceInputStream1.read()); 203 } 204 205 protected void setUp() { 206 simple1 = new Support_ASimpleInputStream(s1); 207 simple2 = new Support_ASimpleInputStream(s2); 208 si = new SequenceInputStream(simple1, simple2); 209 } 210 } 211