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 tests.api.java.io; 19 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.FilterOutputStream; 23import java.io.IOException; 24 25import tests.support.Support_OutputStream; 26import dalvik.annotation.TestLevel; 27import dalvik.annotation.TestTargetClass; 28import dalvik.annotation.TestTargetNew; 29 30@TestTargetClass(java.io.FilterOutputStream.class) 31public class FilterOutputStreamTest extends junit.framework.TestCase { 32 33 private java.io.FilterOutputStream os; 34 35 java.io.ByteArrayOutputStream bos; 36 37 java.io.ByteArrayInputStream bis; 38 39 byte[] ibuf = new byte[4096]; 40 41 private final 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_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_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 private final int testLength = fileString.length(); 44 45 46 /** 47 * @tests java.io.FilterOutputStream#FilterOutputStream(java.io.OutputStream) 48 */ 49 @TestTargetNew( 50 level = TestLevel.COMPLETE, 51 notes = "Verifies FilterOutputStream(java.io.OutputStream) constructor.", 52 method = "FilterOutputStream", 53 args = {java.io.OutputStream.class} 54 ) 55 public void test_ConstructorLjava_io_OutputStream() { 56 // Test for method java.io.FilterOutputStream(java.io.OutputStream) 57 try { 58 bos = new ByteArrayOutputStream(); 59 os = new FilterOutputStream(bos); 60 os.write('t'); 61 } catch (java.io.IOException e) { 62 fail("Constructor test failed : " + e.getMessage()); 63 } 64 } 65 66 /** 67 * @tests java.io.FilterOutputStream#close() 68 */ 69 @TestTargetNew( 70 level = TestLevel.COMPLETE, 71 method = "close", 72 args = {} 73 ) 74 public void test_close() throws IOException { 75 Support_OutputStream sos = new Support_OutputStream(); 76 os = new FilterOutputStream(sos); 77 os.close(); 78 79 try { 80 os.write(42); 81 } catch (java.io.IOException e) { 82 fail("Test 1: Unexpected IOException."); 83 } 84 85 sos.setThrowsException(true); 86 try { 87 os.write(42); 88 fail("Test 2: IOException expected."); 89 } catch (java.io.IOException e) { 90 // Expected. 91 } 92 93 os = new FilterOutputStream(sos); 94 try { 95 os.close(); 96 fail("Test 3: IOException expected."); 97 } catch (IOException e) { 98 // Expected. 99 } 100 } 101 102 /** 103 * @tests java.io.FilterOutputStream#flush() 104 */ 105 @TestTargetNew( 106 level = TestLevel.COMPLETE, 107 method = "flush", 108 args = {} 109 ) 110 public void test_flush() throws IOException { 111 Support_OutputStream sos = new Support_OutputStream(550); 112 os = new FilterOutputStream(sos); 113 os.write(fileString.getBytes(), 0, 500); 114 os.flush(); 115 assertEquals("Test 1: Bytes not written after flush;", 116 500, sos.size()); 117 118 sos.setThrowsException(true); 119 try { 120 os.flush(); 121 fail("Test 2: IOException expected."); 122 } catch (IOException e) { 123 // Expected. 124 } 125 sos.setThrowsException(false); 126 } 127 128 /** 129 * @tests java.io.FilterOutputStream#write(byte[]) 130 */ 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 method = "write", 134 args = {byte[].class} 135 ) 136 public void test_write$B() throws IOException { 137 Support_OutputStream sos = new Support_OutputStream(testLength); 138 os = new FilterOutputStream(sos); 139 os.write(fileString.getBytes()); 140 141 bis = new ByteArrayInputStream(sos.toByteArray()); 142 assertTrue("Test 1: Bytes have not been written.", 143 bis.available() == testLength); 144 byte[] wbytes = new byte[testLength]; 145 bis.read(wbytes, 0, testLength); 146 assertTrue("Test 2: Incorrect bytes written or read.", 147 fileString.equals(new String(wbytes))); 148 149 try { 150 // Support_OutputStream throws an IOException if the internal 151 // buffer is full, which it should be now. 152 os.write(42); 153 fail("Test 2: IOException expected."); 154 } catch (IOException e) { 155 // Expected. 156 } 157 } 158 159 /** 160 * @tests java.io.FilterOutputStream#write(byte[], int, int) 161 */ 162 @TestTargetNew( 163 level = TestLevel.COMPLETE, 164 method = "write", 165 args = {byte[].class, int.class, int.class} 166 ) 167 public void test_write$BII() throws IOException { 168 Support_OutputStream sos = new Support_OutputStream(testLength); 169 os = new FilterOutputStream(sos); 170 os.write(fileString.getBytes(), 10, testLength - 10); 171 172 bis = new ByteArrayInputStream(sos.toByteArray()); 173 assertTrue("Test 1: Bytes have not been written.", 174 bis.available() == testLength - 10); 175 byte[] wbytes = new byte[testLength - 10]; 176 bis.read(wbytes); 177 assertTrue("Test 2: Incorrect bytes written or read.", 178 fileString.substring(10).equals(new String(wbytes))); 179 180 try { 181 // Support_OutputStream throws an IOException if the internal 182 // buffer is full, which it should be eventually. 183 os.write(fileString.getBytes()); 184 fail("Test 2: IOException expected."); 185 } catch (IOException e) { 186 // Expected. 187 } 188 } 189 190 /** 191 * @tests java.io.FilterOutputStream#write(byte[], int, int) 192 */ 193 @TestTargetNew( 194 level = TestLevel.COMPLETE, 195 method = "write", 196 args = {byte[].class, int.class, int.class} 197 ) 198 public void test_write$BII_Exception() throws IOException { 199 Support_OutputStream sos = new Support_OutputStream(testLength); 200 os = new FilterOutputStream(sos); 201 byte[] buf = new byte[10]; 202 203 try { 204 os.write(buf, -1, 1); 205 fail("IndexOutOfBoundsException expected."); 206 } catch (IndexOutOfBoundsException e) { 207 // Expected. 208 } 209 210 try { 211 os.write(buf, 0, -1); 212 fail("IndexOutOfBoundsException expected."); 213 } catch (IndexOutOfBoundsException e) { 214 // Expected. 215 } 216 217 try { 218 os.write(buf, 10, 1); 219 fail("IndexOutOfBoundsException expected."); 220 } catch (IndexOutOfBoundsException e) { 221 // Expected. 222 } 223 } 224 225 /** 226 * @tests java.io.FilterOutputStream#write(int) 227 */ 228 @TestTargetNew( 229 level = TestLevel.COMPLETE, 230 method = "write", 231 args = {int.class} 232 ) 233 public void test_writeI() throws IOException { 234 Support_OutputStream sos = new Support_OutputStream(1); 235 os = new FilterOutputStream(sos); 236 os.write(42); 237 238 bis = new ByteArrayInputStream(sos.toByteArray()); 239 assertTrue("Test 1: Byte has not been written.", 240 bis.available() == 1); 241 assertEquals("Test 2: Incorrect byte written or read;", 242 42, bis.read()); 243 244 try { 245 // Support_OutputStream throws an IOException if the internal 246 // buffer is full, which it should be now. 247 os.write(42); 248 fail("Test 2: IOException expected."); 249 } catch (IOException e) { 250 // Expected. 251 } 252 } 253 254 /** 255 * Tears down the fixture, for example, close a network connection. This 256 * method is called after a test is executed. 257 */ 258 protected void tearDown() { 259 try { 260 if (bos != null) 261 bos.close(); 262 if (bis != null) 263 bis.close(); 264 if (os != null) 265 os.close(); 266 } catch (Exception e) { 267 } 268 } 269} 270