1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/* 2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * Licensed to the Apache Software Foundation (ASF) under one or more 3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * contributor license agreements. See the NOTICE file distributed with 4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * this work for additional information regarding copyright ownership. 5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * The ASF licenses this file to You under the Apache License, Version 2.0 6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * (the "License"); you may not use this file except in compliance with 7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * the License. You may obtain a copy of the License at 8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * 9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * 11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * Unless required by applicable law or agreed to in writing, software 12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, 13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * See the License for the specific language governing permissions and 15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * limitations under the License. 16cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 17cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 18ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.io; 19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 20cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.File; 212b7c83942a5e28c53698232182193d5118028e6cNarayan Kamathimport java.io.FileOutputStream; 22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.FilterInputStream; 23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.IOException; 24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.InputStream; 252b7c83942a5e28c53698232182193d5118028e6cNarayan Kamathimport java.io.OutputStream; 262b7c83942a5e28c53698232182193d5118028e6cNarayan Kamathimport java.nio.charset.StandardCharsets; 27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport junit.framework.TestCase; 28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class FilterInputStreamTest extends TestCase { 30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath static class MyFilterInputStream extends FilterInputStream { 32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public MyFilterInputStream(InputStream is) { 33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath super(is); 34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 35cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 36cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 37cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath private String fileName; 38cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath private InputStream is; 392b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath private static final String INPUT = 402b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_All_Tests\n" + 412b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_BufferedInputStream\n" + 422b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_BufferedOutputStream\n" + 432b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_ByteArrayInputStream\n" + 442b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_ByteArrayOutputStream\n" + 452b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_DataInputStream\n" + 462b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_File\n" + 472b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileDescriptor\n" + 482b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileInputStream\n" + 492b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileNotFoundException\n" + 502b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileOutputStream\n" + 512b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FilterInputStream\n" + 522b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FilterOutputStream\n" + 532b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_InputStream\n" + 542b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_IOException\n" + 552b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_OutputStream\n" + 562b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_PrintStream\n" + 572b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_RandomAccessFile\n" + 582b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_SyncFailedException\n" + 592b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_AbstractMethodError\n" + 602b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArithmeticException\n" + 612b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArrayIndexOutOfBoundsException\n" + 622b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArrayStoreException\n" + 632b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Boolean\n" + 642b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Byte\n" + 652b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Character\n" + 662b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_All_Tests\n" + 672b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_BufferedInputStream\n" + 682b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_BufferedOutputStream\n" + 692b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_ByteArrayInputStream\n" + 702b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_ByteArrayOutputStream\n" + 712b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_DataInputStream\n" + 722b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_File\n" + 732b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileDescriptor\n" + 742b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileInputStream\n" + 752b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileNotFoundException\n" + 762b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FileOutputStream\n" + 772b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FilterInputStream\n" + 782b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_FilterOutputStream\n" + 792b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_InputStream\n" + 802b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_IOException\n" + 812b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_OutputStream\n" + 822b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_PrintStream\n" + 832b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_RandomAccessFile\n" + 842b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_io_SyncFailedException\n" + 852b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_AbstractMethodError\n" + 862b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArithmeticException\n" + 872b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArrayIndexOutOfBoundsException\n" + 882b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_ArrayStoreException\n" + 892b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Boolean\n" + 902b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Byte\n" + 912b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath "Test_java_lang_Character\n"; 92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 932b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath /** 942b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath * Sets up the fixture, for example, open a network connection. This method 952b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath * is called before a test is executed. 962b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath */ 972b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath @Override 982b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath protected void setUp() throws IOException { 992b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath File temp = File.createTempFile("FilterInputStreamTest", "tst"); 1002b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath fileName = temp.getAbsolutePath(); 1012b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath OutputStream fos = new FileOutputStream(temp.getAbsolutePath()); 1022b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath fos.write(INPUT.getBytes(StandardCharsets.US_ASCII)); 1032b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath fos.close(); 1042b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath is = new MyFilterInputStream(new java.io.FileInputStream(fileName)); 1052b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath } 106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 1072b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath /** 1082b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath * Tears down the fixture, for example, close a network connection. This 1092b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath * method is called after a test is executed. 1102b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath */ 1112b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath @Override 1122b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath protected void tearDown() { 1132b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath try { 1142b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath is.close(); 1152b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath } catch (Exception e) { 1162b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath // Ignored 1172b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath } 1182b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath new File(fileName).delete(); 1192b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath } 120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#available() 123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_available() throws IOException { 125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("Returned incorrect number of available bytes", is 1262b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath .available() == INPUT.length()); 127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#close() 131cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 132cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_close() throws IOException { 133cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.close(); 134cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 135cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 136cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.read(); 137cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("Able to read from closed stream"); 138cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (java.io.IOException e) { 139cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // Expected 140cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 141cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 142cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 143cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 144cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#mark(int) 145cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 146cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_markI() { 147cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("Mark not supported by parent InputStream", true); 148cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 149cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 150cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 151cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#markSupported() 152cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 153cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_markSupported() { 154cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("markSupported returned true", !is.markSupported()); 155cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 156cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 157cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 158cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#read() 159cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 160cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_read() throws Exception { 161cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath int c = is.read(); 1622b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath assertTrue("read returned incorrect char", c == INPUT.charAt(0)); 163cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 164cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 165cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 166cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#read(byte[]) 167cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 168cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_read$B() throws Exception { 169cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath byte[] buf1 = new byte[100]; 170cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.read(buf1); 171cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("Failed to read correct data", new String(buf1, 0, 1722b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath buf1.length, "UTF-8").equals(INPUT.substring(0, 100))); 173cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 174cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 175cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 176cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#read(byte[], int, int) 177cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 178cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_read$BII() throws Exception { 179cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath byte[] buf1 = new byte[100]; 1802b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath is.skip(500); 181cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.mark(1000); 182cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.read(buf1, 0, buf1.length); 183cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("Failed to read correct data", new String(buf1, 0, 1842b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath buf1.length, "UTF-8").equals(INPUT.substring(500, 600))); 185cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 186cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 187cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 188cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#reset() 189cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 190cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_reset() { 191cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 192cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.reset(); 193cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("should throw IOException"); 194cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (IOException e) { 195cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // expected 196cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 197cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 198cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 199cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 200cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.io.FilterInputStream#skip(long) 201cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 202cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_skipJ() throws Exception { 203cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath byte[] buf1 = new byte[10]; 204cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.skip(1000); 205cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath is.read(buf1, 0, buf1.length); 206cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue("Failed to skip to correct position", new String(buf1, 0, 2072b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath buf1.length, "UTF-8").equals(INPUT.substring(1000, 1010))); 208cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 209cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath} 210