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 18package libcore.java.io; 19 20import java.io.BufferedInputStream; 21import java.io.File; 22import java.io.FilterInputStream; 23import java.io.IOException; 24import java.util.Arrays; 25import tests.support.Support_ASimpleInputStream; 26 27public class OldFilterInputStreamTest extends junit.framework.TestCase { 28 29 static class MyFilterInputStream extends java.io.FilterInputStream { 30 public MyFilterInputStream(java.io.InputStream is) { 31 super(is); 32 } 33 } 34 35 private String fileName; 36 37 private FilterInputStream is; 38 39 private static final String INPUT = "Lorem ipsum dolor sit amet,\n" + 40 "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" + 41 "labore et dolore magna aliqua.\n"; 42 43 private static final int INPUT_LENGTH = INPUT.length(); 44 45 public void test_Constructor() { 46 // The FilterInputStream object has already been created in setUp(). 47 // If anything has gone wrong, closing it should throw a 48 // NullPointerException. 49 try { 50 is.close(); 51 } catch (IOException e) { 52 fail("Unexpected IOException: " + e.getMessage()); 53 } catch (NullPointerException npe) { 54 fail("Unexpected NullPointerException."); 55 } 56 } 57 58 public void test_available() throws IOException { 59 assertEquals("Test 1: Returned incorrect number of available bytes;", 60 INPUT_LENGTH, is.available()); 61 62 is.close(); 63 try { 64 is.available(); 65 fail("Test 2: IOException expected."); 66 } catch (IOException e) { 67 // Expected. 68 } 69 } 70 71 public void test_close() throws IOException { 72 is.close(); 73 74 try { 75 is.read(); 76 fail("Test 1: Read from closed stream succeeded."); 77 } catch (IOException e) { 78 // Expected. 79 } 80 81 Support_ASimpleInputStream sis = new Support_ASimpleInputStream(true); 82 is = new MyFilterInputStream(sis); 83 try { 84 is.close(); 85 fail("Test 2: IOException expected."); 86 } catch (IOException e) { 87 // Expected. 88 } 89 sis.throwExceptionOnNextUse = false; 90 } 91 92 public void test_markI() throws Exception { 93 // Test for method void java.io.FilterInputStream.mark(int) 94 final int bufSize = 10; 95 byte[] buf1 = new byte[bufSize]; 96 byte[] buf2 = new byte[bufSize]; 97 98 // Purpose 1: Check that mark() does nothing if the filtered stream 99 // is a FileInputStream. 100 is.read(buf1, 0, bufSize); 101 is.mark(2 * bufSize); 102 is.read(buf1, 0, bufSize); 103 try { 104 is.reset(); 105 } catch (IOException e) { 106 // Expected 107 } 108 is.read(buf2, 0, bufSize); 109 assertFalse("Test 1: mark() should have no effect.", 110 Arrays.equals(buf1, buf2)); 111 is.close(); 112 113 // Purpose 2: Check that mark() in combination with reset() works if 114 // the filtered stream is a BufferedInputStream. 115 is = new MyFilterInputStream(new BufferedInputStream( 116 new java.io.FileInputStream(fileName), 100)); 117 is.read(buf1, 0, bufSize); 118 is.mark(2 * bufSize); 119 is.read(buf1, 0, bufSize); 120 is.reset(); 121 is.read(buf2, 0, bufSize); 122 assertTrue("Test 2: mark() or reset() has failed.", 123 Arrays.equals(buf1, buf2)); 124 } 125 126 public void test_markSupported() throws Exception { 127 // Test for method boolean java.io.FilterInputStream.markSupported() 128 129 // Test 1: Check that markSupported() returns false for a filtered 130 // input stream that is known to not support mark(). 131 assertFalse("Test 1: markSupported() incorrectly returned true " + 132 "for a FileInputStream.", is.markSupported()); 133 is.close(); 134 // Test 2: Check that markSupported() returns true for a filtered 135 // input stream that is known to support mark(). 136 is = new MyFilterInputStream(new BufferedInputStream( 137 new java.io.FileInputStream(fileName), 100)); 138 assertTrue("Test 2: markSupported() incorrectly returned false " + 139 "for a BufferedInputStream.", is.markSupported()); 140 } 141 142 public void test_read() throws IOException { 143 int c = is.read(); 144 assertEquals("Test 1: Read returned incorrect char;", 145 INPUT.charAt(0), c); 146 147 is.close(); 148 try { 149 is.read(); 150 fail("Test 2: IOException expected."); 151 } catch (IOException e) { 152 // Expected. 153 } 154 } 155 156 public void test_read$B() throws IOException { 157 // Test for method int java.io.FilterInputStream.read(byte []) 158 byte[] buf1 = new byte[100]; 159 is.read(buf1); 160 assertTrue("Test 1: Failed to read correct data.", 161 new String(buf1, 0, buf1.length).equals( 162 INPUT.substring(0, 100))); 163 164 is.close(); 165 try { 166 is.read(buf1); 167 fail("Test 2: IOException expected."); 168 } catch (IOException e) { 169 // Expected. 170 } 171 } 172 173 public void test_read$BII_Exception() throws IOException { 174 byte[] buf = null; 175 try { 176 is.read(buf, -1, 0); 177 fail("Test 1: NullPointerException expected."); 178 } catch (NullPointerException e) { 179 // Expected. 180 } 181 182 buf = new byte[1000]; 183 try { 184 is.read(buf, -1, 0); 185 fail("Test 2: IndexOutOfBoundsException expected."); 186 } catch (IndexOutOfBoundsException e) { 187 // Expected. 188 } 189 190 try { 191 is.read(buf, 0, -1); 192 fail("Test 3: IndexOutOfBoundsException expected."); 193 } catch (IndexOutOfBoundsException e) { 194 // Expected. 195 } 196 197 try { 198 is.read(buf, -1, -1); 199 fail("Test 4: IndexOutOfBoundsException expected."); 200 } catch (IndexOutOfBoundsException e) { 201 // Expected. 202 } 203 204 try { 205 is.read(buf, 0, 1001); 206 fail("Test 5: IndexOutOfBoundsException expected."); 207 } catch (IndexOutOfBoundsException e) { 208 // Expected. 209 } 210 211 try { 212 is.read(buf, 1001, 0); 213 fail("Test 6: IndexOutOfBoundsException expected."); 214 } catch (IndexOutOfBoundsException e) { 215 // Expected. 216 } 217 218 try { 219 is.read(buf, 500, 501); 220 fail("Test 7: IndexOutOfBoundsException expected."); 221 } catch (IndexOutOfBoundsException e) { 222 // Expected. 223 } 224 225 is.close(); 226 try { 227 is.read(buf, 0, 100); 228 fail("Test 8: IOException expected."); 229 } catch (IOException e) { 230 // Expected. 231 } 232 } 233 234 public void test_reset() throws Exception { 235 // Test for method void java.io.FilterInputStream.reset() 236 237 // Test 1: Check that reset() throws an IOException if the 238 // filtered stream is a FileInputStream. 239 try { 240 is.reset(); 241 fail("Test 1: IOException expected."); 242 } catch (IOException e) { 243 // expected 244 } 245 246 // Test 2: Check that reset() throws an IOException if the 247 // filtered stream is a BufferedInputStream but mark() has not 248 // yet been called. 249 is = new MyFilterInputStream(new BufferedInputStream( 250 new java.io.FileInputStream(fileName), 100)); 251 try { 252 is.reset(); 253 fail("Test 2: IOException expected."); 254 } catch (IOException e) { 255 // expected 256 } 257 258 // Test 3: Check that reset() in combination with mark() 259 // works correctly. 260 final int bufSize = 10; 261 byte[] buf1 = new byte[bufSize]; 262 byte[] buf2 = new byte[bufSize]; 263 is.read(buf1, 0, bufSize); 264 is.mark(2 * bufSize); 265 is.read(buf1, 0, bufSize); 266 try { 267 is.reset(); 268 } catch (IOException e) { 269 fail("Test 3: Unexpected IOException."); 270 } 271 is.read(buf2, 0, bufSize); 272 assertTrue("Test 4: mark() or reset() has failed.", 273 Arrays.equals(buf1, buf2)); 274 } 275 276 public void test_skipJ() throws IOException { 277 byte[] buf1 = new byte[10]; 278 is.skip(10); 279 is.read(buf1, 0, buf1.length); 280 assertTrue("Test 1: Failed to skip to the correct position.", 281 new String(buf1, 0, buf1.length).equals( 282 INPUT.substring(10, 20))); 283 284 is.close(); 285 try { 286 is.read(); 287 fail("Test 2: IOException expected."); 288 } catch (IOException e) { 289 // Expected. 290 } 291 } 292 293 protected void setUp() throws Exception { 294 File f = File.createTempFile("OldFilterInputStreamTest", "tst"); 295 fileName = f.getAbsolutePath(); 296 java.io.OutputStream fos = new java.io.FileOutputStream(fileName); 297 fos.write(INPUT.getBytes()); 298 fos.close(); 299 is = new MyFilterInputStream(new java.io.FileInputStream(fileName)); 300 } 301 302 protected void tearDown() { 303 try { 304 is.close(); 305 } catch (Exception ignored) { 306 } 307 } 308} 309