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 tests.api.java.io; 19 20 import java.io.BufferedOutputStream; 21 import java.io.ByteArrayOutputStream; 22 import java.io.IOException; 23 import java.io.OutputStream; 24 25 import tests.support.Support_OutputStream; 26 import dalvik.annotation.TestLevel; 27 import dalvik.annotation.TestTargetClass; 28 import dalvik.annotation.TestTargetNew; 29 30 @TestTargetClass(BufferedOutputStream.class) 31 public class BufferedOutputStreamTest extends junit.framework.TestCase { 32 33 private java.io.OutputStream os; 34 35 java.io.ByteArrayOutputStream baos; 36 37 java.io.ByteArrayInputStream bais; 38 39 Support_OutputStream sos; 40 41 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"; 42 43 /** 44 * @tests java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream) 45 */ 46 @TestTargetNew( 47 level = TestLevel.COMPLETE, 48 method = "BufferedOutputStream", 49 args = {java.io.OutputStream.class} 50 ) 51 public void test_ConstructorLjava_io_OutputStream() { 52 try { 53 baos = new java.io.ByteArrayOutputStream(); 54 os = new java.io.BufferedOutputStream(baos); 55 os.write(fileString.getBytes(), 0, 500); 56 } catch (java.io.IOException e) { 57 fail("Constrcutor test failed"); 58 } 59 60 } 61 62 /** 63 * @tests java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream, 64 * int) 65 */ 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "IllegalArgumentException checking missed.", 69 method = "BufferedOutputStream", 70 args = {java.io.OutputStream.class, int.class} 71 ) 72 public void test_ConstructorLjava_io_OutputStreamI() { 73 baos = new java.io.ByteArrayOutputStream(); 74 75 try { 76 os = new java.io.BufferedOutputStream(baos, -1); 77 fail("Test 1: IllegalArgumentException expected."); 78 } catch (IllegalArgumentException e) { 79 // Expected. 80 } 81 try { 82 os = new java.io.BufferedOutputStream(baos, 1024); 83 os.write(fileString.getBytes(), 0, 500); 84 } catch (java.io.IOException e) { 85 fail("Test 2: Unexpected IOException."); 86 } 87 } 88 89 /** 90 * @tests java.io.BufferedOutputStream#flush() 91 */ 92 @TestTargetNew( 93 level = TestLevel.COMPLETE, 94 method = "flush", 95 args = {} 96 ) 97 public void test_flush() throws IOException { 98 baos = new ByteArrayOutputStream(); 99 os = new java.io.BufferedOutputStream(baos, 600); 100 os.write(fileString.getBytes(), 0, 500); 101 os.flush(); 102 assertEquals("Test 1: Bytes not written after flush;", 103 500, ((ByteArrayOutputStream) baos).size()); 104 os.close(); 105 106 sos = new Support_OutputStream(true); 107 os = new BufferedOutputStream(sos, 10); 108 try { 109 os.flush(); 110 fail("Test 2: IOException expected."); 111 } catch (IOException e) { 112 // Expected. 113 } 114 // To avoid exception during tearDown(). 115 sos.setThrowsException(false); 116 } 117 118 /** 119 * @tests java.io.BufferedOutputStream#write(byte[], int, int) 120 */ 121 @TestTargetNew( 122 level = TestLevel.PARTIAL_COMPLETE, 123 method = "write", 124 args = {byte[].class, int.class, int.class} 125 ) 126 public void test_write$BII() throws IOException { 127 os = new java.io.BufferedOutputStream( 128 baos = new java.io.ByteArrayOutputStream(),512); 129 os.write(fileString.getBytes(), 0, 500); 130 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 131 assertEquals("Test 1: Bytes written, not buffered;", 132 0, bais.available()); 133 os.flush(); 134 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 135 assertEquals("Test 2: Bytes not written after flush;", 136 500, bais.available()); 137 os.write(fileString.getBytes(), 500, 513); 138 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 139 assertTrue("Test 3: Bytes not written when buffer full.", 140 bais.available() >= 1000); 141 byte[] wbytes = new byte[1013]; 142 bais.read(wbytes, 0, 1013); 143 assertTrue("Test 4: Incorrect bytes written or read.", 144 fileString.substring(0, 1013).equals( 145 new String(wbytes, 0, wbytes.length))); 146 os.close(); 147 148 sos = new Support_OutputStream(true); 149 os = new BufferedOutputStream(sos, 10); 150 try { 151 os.write(fileString.getBytes(), 0, 500); 152 fail("Test 5: IOException expected."); 153 } catch (IOException e) { 154 // Expected. 155 } 156 // To avoid exception during tearDown(). 157 sos.setThrowsException(false); 158 } 159 160 /** 161 * @tests java.io.BufferedOutputStream#write(byte[], int, int) 162 */ 163 @TestTargetNew( 164 level = TestLevel.PARTIAL_COMPLETE, 165 method = "write", 166 args = {byte[].class, int.class, int.class} 167 ) 168 public void test_write$BII_Exception() throws IOException { 169 OutputStream bos = new BufferedOutputStream(new ByteArrayOutputStream()); 170 byte[] nullByteArray = null; 171 byte[] byteArray = new byte[10]; 172 173 try { 174 bos.write(nullByteArray, 0, 1); 175 fail("Test 1: NullPointerException expected."); 176 } catch (NullPointerException e) { 177 // Expected. 178 } 179 180 try { 181 bos.write(byteArray, -1, 1); 182 fail("Test 2: IndexOutOfBoundsException expected."); 183 } catch (IndexOutOfBoundsException e) { 184 // Expected 185 } 186 187 try { 188 bos.write(byteArray, 0, -1); 189 fail("Test 3: IndexOutOfBoundsException expected."); 190 } catch (IndexOutOfBoundsException e) { 191 // Expected 192 } 193 194 try { 195 bos.write(byteArray, 1, 10); 196 fail("Test 4: IndexOutOfBoundsException expected."); 197 } catch (IndexOutOfBoundsException e) { 198 // Expected 199 } 200 } 201 202 /** 203 * @tests java.io.BufferedOutputStream#write(int) 204 */ 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 method = "write", 208 args = {int.class} 209 ) 210 public void test_writeI() throws IOException { 211 baos = new java.io.ByteArrayOutputStream(); 212 os = new java.io.BufferedOutputStream(baos); 213 os.write('t'); 214 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 215 assertEquals("Test 1: Byte written, not buffered;", 216 0, bais.available()); 217 os.flush(); 218 bais = new java.io.ByteArrayInputStream(baos.toByteArray()); 219 assertEquals("Test 2: Byte not written after flush;", 220 1, bais.available()); 221 byte[] wbytes = new byte[1]; 222 bais.read(wbytes, 0, 1); 223 assertEquals("Test 3: Incorrect byte written or read;", 224 't', wbytes[0]); 225 os.close(); 226 227 sos = new Support_OutputStream(true); 228 os = new BufferedOutputStream(sos, 1); 229 os.write('t'); 230 try { 231 // Exception is only thrown when the buffer is flushed. 232 os.write('e'); 233 fail("Test 4: IOException expected."); 234 } catch (IOException e) { 235 // Expected. 236 } 237 // To avoid exception during tearDown(). 238 sos.setThrowsException(false); 239 } 240 241 protected void tearDown() { 242 try { 243 if (bais != null) 244 bais.close(); 245 if (os != null) 246 os.close(); 247 if (baos != null) 248 baos.close(); 249 } catch (Exception e) { 250 System.out.println("Exception during tearDown" + e.toString()); 251 } 252 } 253 } 254