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.File; 21import java.io.FileDescriptor; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.FileOutputStream; 25import java.io.IOException; 26import java.nio.channels.FileChannel; 27import junit.framework.TestCase; 28import tests.support.Support_PlatformFile; 29 30public class OldFileInputStreamTest extends TestCase { 31 32 public String fileName; 33 private FileInputStream is; 34 public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n"; 35 36 public void test_ConstructorLjava_io_File() { 37 // Test for method FileInputStream(File) 38 try { 39 File f = new File(fileName); 40 is = new FileInputStream(f); 41 is.close(); 42 } catch (Exception e) { 43 fail("Failed to create FileInputStream : " + e.getMessage()); 44 } 45 File f2 = new File("ImprobableFile.42"); 46 try { 47 is = new FileInputStream(f2); 48 is.close(); 49 f2.delete(); 50 fail("FileNotFoundException expected."); 51 } catch (FileNotFoundException e) { 52 // Expected. 53 } catch (IOException e) { 54 fail("Unexpected IOException: " + e.getMessage()); 55 } 56 } 57 58 public void test_ConstructorLjava_io_FileDescriptor() { 59 try { 60 FileInputStream fis = new FileInputStream((FileDescriptor) null); 61 fis.close(); 62 fail("NullPointerException expected."); 63 } catch (NullPointerException e) { 64 // Expected. 65 } catch (IOException e) { 66 fail("Unexpected IOException: " + e.getMessage()); 67 } 68 } 69 70 public void test_ConstructorLjava_lang_String() { 71 // Test for method FileInputStream(java.lang.String) 72 try { 73 is = new FileInputStream(fileName); 74 is.close(); 75 } catch (Exception e) { 76 fail("Failed to create FileInputStream : " + e.getMessage()); 77 } 78 try { 79 is = new FileInputStream("ImprobableFile.42"); 80 is.close(); 81 new File("ImprobableFile.42").delete(); 82 fail("FileNotFoundException expected."); 83 } catch (FileNotFoundException e) { 84 // Expected. 85 } catch (IOException e) { 86 fail("Unexpected IOException: " + e.getMessage()); 87 } 88 } 89 90 public void test_available() throws IOException { 91 is = new FileInputStream(fileName); 92 assertEquals("Test 1: Returned incorrect number of available bytes;", 93 fileString.length(), is.available()); 94 is.close(); 95 try { 96 is.available(); 97 fail("Test 2: IOException expected."); 98 } catch (IOException e) { 99 // Expected. 100 } 101 } 102 103 public void test_getChannel() { 104 // Test for method FileChannel FileInputStream.getChannel() 105 FileChannel channel; 106 byte[] buffer = new byte[100]; 107 byte[] stringBytes; 108 final int offset = 5; 109 boolean equal = true; 110 111 try { 112 FileInputStream fis = new FileInputStream(fileName); 113 channel = fis.getChannel(); 114 assertNotNull(channel); 115 assertTrue("Channel is closed.", channel.isOpen()); 116 117 // Check that the channel is associated with the input stream. 118 channel.position(offset); 119 fis.read(buffer, 0, 10); 120 stringBytes = fileString.getBytes(); 121 for (int i = 0; i < 10; i++) { 122 equal &= (buffer[i] == stringBytes[i + offset]); 123 } 124 assertTrue("Channel is not associated with this stream.", equal); 125 126 fis.close(); 127 assertFalse("Channel has not been closed.", channel.isOpen()); 128 } catch (FileNotFoundException e) { 129 fail("Could not find : " + fileName); 130 } 131 132 catch (IOException e) { 133 fail("Exception during test : " + e.getMessage()); 134 } 135 } 136 137 public void test_read() throws IOException { 138 is = new FileInputStream(fileName); 139 int c = is.read(); 140 assertEquals("Test 1: Read returned incorrect char;", 141 fileString.charAt(0), c); 142 143 is.close(); 144 try { 145 is.read(); 146 fail("Test 2: IOException expected."); 147 } catch (IOException e) { 148 // Expected. 149 } 150 } 151 152 public void test_read$B() throws IOException { 153 byte[] buf1 = new byte[100]; 154 is = new FileInputStream(fileName); 155 is.skip(3000); 156 is.read(buf1); 157 is.close(); 158 assertTrue("Test 1: Failed to read correct data.", 159 new String(buf1, 0, buf1.length).equals( 160 fileString.substring(3000, 3100))); 161 162 is.close(); 163 try { 164 is.read(buf1); 165 fail("Test 2: IOException expected."); 166 } catch (IOException e) { 167 // Expected. 168 } 169 } 170 171 public void test_skipJ() throws IOException { 172 byte[] buf1 = new byte[10]; 173 is = new FileInputStream(fileName); 174 is.skip(1000); 175 is.read(buf1, 0, buf1.length); 176 assertTrue("Test 1: Failed to skip to correct position.", 177 new String(buf1, 0, buf1.length).equals( 178 fileString.substring(1000, 1010))); 179 180 is.close(); 181 try { 182 is.read(); 183 fail("Test 2: IOException expected."); 184 } catch (IOException e) { 185 // Expected. 186 } 187 } 188 189 protected void setUp() throws Exception { 190 fileName = System.getProperty("java.io.tmpdir"); 191 String separator = System.getProperty("file.separator"); 192 if (fileName.charAt(fileName.length() - 1) == separator.charAt(0)) 193 fileName = Support_PlatformFile.getNewPlatformFile(fileName, 194 "input.tst"); 195 else 196 fileName = Support_PlatformFile.getNewPlatformFile(fileName 197 + separator, "input.tst"); 198 java.io.OutputStream fos = new FileOutputStream(fileName); 199 fos.write(fileString.getBytes()); 200 fos.close(); 201 } 202 203 protected void tearDown() throws Exception { 204 if (is != null) { 205 is.close(); 206 } 207 new File(fileName).delete(); 208 } 209} 210