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.BufferedOutputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.IOException; 23import java.io.OutputStream; 24import tests.support.Support_OutputStream; 25 26public class OldBufferedOutputStreamTest extends junit.framework.TestCase { 27 28 private java.io.OutputStream os; 29 30 java.io.ByteArrayOutputStream baos; 31 32 java.io.ByteArrayInputStream bais; 33 34 Support_OutputStream sos; 35 36 public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_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"; 37 38 public void test_ConstructorLjava_io_OutputStream() { 39 try { 40 baos = new java.io.ByteArrayOutputStream(); 41 os = new java.io.BufferedOutputStream(baos); 42 os.write(fileString.getBytes(), 0, 500); 43 } catch (java.io.IOException e) { 44 fail("Constructor test failed"); 45 } 46 47 } 48 49 public void test_ConstructorLjava_io_OutputStreamI() { 50 baos = new java.io.ByteArrayOutputStream(); 51 52 try { 53 os = new java.io.BufferedOutputStream(baos, -1); 54 fail("Test 1: IllegalArgumentException expected."); 55 } catch (IllegalArgumentException e) { 56 // Expected. 57 } 58 try { 59 os = new java.io.BufferedOutputStream(baos, 1024); 60 os.write(fileString.getBytes(), 0, 500); 61 } catch (java.io.IOException e) { 62 fail("Test 2: Unexpected IOException."); 63 } 64 } 65 66 public void test_flush() throws IOException { 67 baos = new ByteArrayOutputStream(); 68 os = new java.io.BufferedOutputStream(baos, 600); 69 os.write(fileString.getBytes(), 0, 500); 70 os.flush(); 71 assertEquals("Test 1: Bytes not written after flush;", 72 500, ((ByteArrayOutputStream) baos).size()); 73 os.close(); 74 75 sos = new Support_OutputStream(true); 76 os = new BufferedOutputStream(sos, 10); 77 try { 78 os.flush(); 79 fail("Test 2: IOException expected."); 80 } catch (IOException e) { 81 // Expected. 82 } 83 // To avoid exception during tearDown(). 84 sos.setThrowsException(false); 85 } 86 87 public void test_write$BII() throws IOException { 88 os = new java.io.BufferedOutputStream( 89 baos = new java.io.ByteArrayOutputStream(),512); 90 os.write(fileString.getBytes(), 0, 500); 91 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 92 assertEquals("Test 1: Bytes written, not buffered;", 93 0, bais.available()); 94 os.flush(); 95 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 96 assertEquals("Test 2: Bytes not written after flush;", 97 500, bais.available()); 98 os.write(fileString.getBytes(), 500, 513); 99 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 100 assertTrue("Test 3: Bytes not written when buffer full.", 101 bais.available() >= 1000); 102 byte[] wbytes = new byte[1013]; 103 bais.read(wbytes, 0, 1013); 104 assertTrue("Test 4: Incorrect bytes written or read.", 105 fileString.substring(0, 1013).equals( 106 new String(wbytes, 0, wbytes.length))); 107 os.close(); 108 109 sos = new Support_OutputStream(true); 110 os = new BufferedOutputStream(sos, 10); 111 try { 112 os.write(fileString.getBytes(), 0, 500); 113 fail("Test 5: IOException expected."); 114 } catch (IOException e) { 115 // Expected. 116 } 117 // To avoid exception during tearDown(). 118 sos.setThrowsException(false); 119 } 120 121 public void test_write$BII_Exception() throws IOException { 122 OutputStream bos = new BufferedOutputStream(new ByteArrayOutputStream()); 123 byte[] nullByteArray = null; 124 byte[] byteArray = new byte[10]; 125 126 try { 127 bos.write(nullByteArray, 0, 1); 128 fail("Test 1: NullPointerException expected."); 129 } catch (NullPointerException e) { 130 // Expected. 131 } 132 133 try { 134 bos.write(byteArray, -1, 1); 135 fail("Test 2: IndexOutOfBoundsException expected."); 136 } catch (IndexOutOfBoundsException e) { 137 // Expected 138 } 139 140 try { 141 bos.write(byteArray, 0, -1); 142 fail("Test 3: IndexOutOfBoundsException expected."); 143 } catch (IndexOutOfBoundsException e) { 144 // Expected 145 } 146 147 try { 148 bos.write(byteArray, 1, 10); 149 fail("Test 4: IndexOutOfBoundsException expected."); 150 } catch (IndexOutOfBoundsException e) { 151 // Expected 152 } 153 } 154 155 public void test_writeI() throws IOException { 156 baos = new java.io.ByteArrayOutputStream(); 157 os = new java.io.BufferedOutputStream(baos); 158 os.write('t'); 159 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 160 assertEquals("Test 1: Byte written, not buffered;", 161 0, bais.available()); 162 os.flush(); 163 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 164 assertEquals("Test 2: Byte not written after flush;", 165 1, bais.available()); 166 byte[] wbytes = new byte[1]; 167 bais.read(wbytes, 0, 1); 168 assertEquals("Test 3: Incorrect byte written or read;", 169 't', wbytes[0]); 170 os.close(); 171 172 sos = new Support_OutputStream(true); 173 os = new BufferedOutputStream(sos, 1); 174 os.write('t'); 175 try { 176 // Exception is only thrown when the buffer is flushed. 177 os.write('e'); 178 fail("Test 4: IOException expected."); 179 } catch (IOException e) { 180 // Expected. 181 } 182 // To avoid exception during tearDown(). 183 sos.setThrowsException(false); 184 } 185 186 protected void tearDown() { 187 try { 188 if (bais != null) 189 bais.close(); 190 if (os != null) 191 os.close(); 192 if (baos != null) 193 baos.close(); 194 } catch (Exception e) { 195 System.out.println("Exception during tearDown" + e.toString()); 196 } 197 } 198} 199