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.Writer; 21import java.io.BufferedWriter; 22import java.io.IOException; 23import tests.support.Support_ASimpleWriter; 24import tests.support.Support_StringWriter; 25 26public class OldBufferedWriterTest extends junit.framework.TestCase { 27 28 BufferedWriter bw; 29 30 Support_StringWriter sw; 31 32 Support_ASimpleWriter ssw; 33 34 public String testString = "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_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_Writer() { 37 bw = new BufferedWriter(sw); 38 try { 39 bw.write("Hi", 0, 2); 40 assertTrue("Test 1: Buffering failed.", sw.toString().equals("")); 41 bw.flush(); 42 assertEquals("Test 2: Incorrect value;", "Hi", sw.toString()); 43 } catch (IOException e) { 44 fail("Test 3: Unexpected IOException."); 45 } 46 } 47 48 public void test_ConstructorLjava_io_WriterI() { 49 try { 50 bw = new BufferedWriter(sw, 0); 51 fail("Test 1: IllegalArgumentException expected."); 52 } catch (IllegalArgumentException expected) { 53 // Expected. 54 } 55 56 bw = new BufferedWriter(sw, 10); 57 try { 58 bw.write("Hi", 0, 2); 59 assertTrue("Test 2: Buffering failed.", sw.toString().equals("")); 60 bw.flush(); 61 assertEquals("Test 3: Incorrect value;", "Hi", sw.toString()); 62 } catch (IOException e) { 63 fail("Test 4: Unexpected IOException."); 64 } 65 } 66 67 public void test_close() { 68 // Test for method void java.io.BufferedWriter.close() 69 try { 70 bw.close(); 71 bw.write(testString); 72 fail("Test 1: IOException expected."); 73 } catch (IOException e) { 74 // Expected. 75 } 76 assertFalse("Test 2: Write after close.", sw.toString().equals(testString)); 77 78 bw = new BufferedWriter(ssw); 79 try { 80 bw.close(); 81 fail("Test 3: IOException expected."); 82 } catch (IOException e) { 83 // Expected. 84 } 85 } 86 87 public void test_flush() throws IOException { 88 bw.write("This should not cause a flush"); 89 assertTrue("Test 1: Bytes written without flush.", 90 sw.toString().equals("")); 91 bw.flush(); 92 assertEquals("Test 2: Bytes not flushed.", 93 "This should not cause a flush", sw.toString()); 94 95 bw.close(); 96 bw = new BufferedWriter(ssw); 97 try { 98 bw.flush(); 99 fail("Test 3: IOException expected."); 100 } catch (IOException e) { 101 // Expected. 102 } 103 } 104 105 public void test_newLine() throws IOException { 106 String separator = System.getProperty("line.separator"); 107 bw.write("Hello"); 108 bw.newLine(); 109 bw.write("World"); 110 bw.flush(); 111 assertTrue("Test 1: Incorrect string written: " + sw.toString(), 112 sw.toString().equals("Hello" + separator + "World")); 113 114 bw.close(); 115 bw = new BufferedWriter(ssw, 1); 116 try { 117 bw.newLine(); 118 fail("Test 2: IOException expected."); 119 } catch (IOException e) { 120 // Expected. 121 } 122 } 123 124 public void test_write$CII() { 125 // Test for method void java.io.BufferedWriter.write(char [], int, int) 126 try { 127 char[] testCharArray = testString.toCharArray(); 128 bw.write(testCharArray, 500, 1000); 129 bw.flush(); 130 assertTrue("Incorrect string written", sw.toString().equals( 131 testString.substring(500, 1500))); 132 133 int idx = sw.toString().length(); 134 bw.write(testCharArray, 0, testCharArray.length); 135 assertEquals(idx + testCharArray.length, sw.toString().length()); 136 bw.write(testCharArray, 0, 0); 137 assertEquals(idx + testCharArray.length, sw.toString().length()); 138 bw.write(testCharArray, testCharArray.length, 0); 139 assertEquals(idx + testCharArray.length, sw.toString().length()); 140 } catch (Exception e) { 141 fail("Exception during write test"); 142 } 143 144 } 145 146 public void test_write$CII_Exception() throws IOException { 147 char[] nullCharArray = null; 148 char[] charArray = testString.toCharArray(); 149 150 try { 151 bw.write(nullCharArray, 0, 1); 152 fail("Test 1: NullPointerException expected."); 153 } catch (NullPointerException e) { 154 // Expected. 155 } 156 157 try { 158 bw.write(charArray, -1, 0); 159 fail("Test 2: IndexOutOfBoundsException expected."); 160 } catch (IndexOutOfBoundsException e) { 161 // Expected 162 } 163 164 try { 165 bw.write(charArray, 0, -1); 166 fail("Test 3: IndexOutOfBoundsException expected."); 167 } catch (IndexOutOfBoundsException e) { 168 // Expected 169 } 170 171 try { 172 bw.write(charArray, charArray.length + 1, 0); 173 fail("Test 4: IndexOutOfBoundsException expected."); 174 } catch (IndexOutOfBoundsException e) { 175 // Expected 176 } 177 178 try { 179 bw.write(charArray, charArray.length, 1); 180 fail("Test 5: IndexOutOfBoundsException expected."); 181 } catch (IndexOutOfBoundsException e) { 182 // Expected 183 } 184 185 try { 186 bw.write(charArray, 0, charArray.length + 1); 187 fail("Test 6: IndexOutOfBoundsException expected."); 188 } catch (IndexOutOfBoundsException e) { 189 // Expected 190 } 191 192 try { 193 bw.write(charArray, 1, charArray.length); 194 fail("Test 7: IndexOutOfBoundsException expected."); 195 } catch (IndexOutOfBoundsException e) { 196 // Expected 197 } 198 199 bw.close(); 200 201 try { 202 bw.write(charArray, 0, 1); 203 fail("Test 7: IOException expected."); 204 } catch (IOException e) { 205 // Expected. 206 } 207 208 bw = new BufferedWriter(ssw, charArray.length / 2); 209 try { 210 bw.write(charArray, 0, charArray.length); 211 fail("Test 8: IOException expected."); 212 } catch (IOException e) { 213 // Expected. 214 } 215 } 216 217 public void test_writeI() throws IOException { 218 bw.write('T'); 219 assertTrue("Test 1: Char written without flush.", 220 sw.toString().equals("")); 221 bw.flush(); 222 assertEquals("Test 2: Incorrect char written;", 223 "T", sw.toString()); 224 225 bw.close(); 226 try { 227 bw.write('E'); 228 fail("Test 3: IOException expected since the target writer is closed."); 229 } catch (IOException e) { 230 // Expected. 231 } 232 233 // IOException should be thrown when the buffer is full and data is 234 // written out to the target writer. 235 bw = new BufferedWriter(ssw, 1); 236 bw.write('S'); 237 try { 238 bw.write('T'); 239 fail("Test 4: IOException expected since the target writer throws it."); 240 } catch (IOException e) { 241 // Expected. 242 } 243 } 244 245 public void test_writeLjava_lang_StringII() { 246 // Test for method void java.io.BufferedWriter.write(java.lang.String, 247 // int, int) 248 try { 249 bw.write(testString); 250 bw.flush(); 251 assertTrue("Incorrect string written", sw.toString().equals( 252 testString)); 253 } catch (Exception e) { 254 fail("Exception during write test"); 255 } 256 } 257 258 public void test_writeLjava_lang_StringII_Exception() throws IOException { 259 260 bw.write((String) null , -1, -1); 261 bw.write((String) null , -1, 0); 262 bw.write((String) null , 0 , -1); 263 bw.write((String) null , 0 , 0); 264 265 try { 266 bw.write((String) null, 0, 1); 267 fail("Test 1: NullPointerException expected."); 268 } catch (NullPointerException e) { 269 // Expected. 270 } 271 272 try { 273 bw.write(testString, -1, 1); 274 fail("Test 2: StringIndexOutOfBoundsException expected."); 275 } catch (StringIndexOutOfBoundsException e) { 276 // Expected. 277 } 278 279 try { 280 bw.write(testString, 1, testString.length()); 281 fail("Test 3: StringIndexOutOfBoundsException expected."); 282 } catch (StringIndexOutOfBoundsException e) { 283 // Expected. 284 } 285 286 bw.close(); 287 288 try { 289 bw.write(testString, 0, 1); 290 fail("Test 4: IOException expected."); 291 } catch (IOException e) { 292 // Expected. 293 } 294 295 bw = new BufferedWriter(ssw, testString.length() / 2); 296 try { 297 bw.write(testString, 0, testString.length()); 298 fail("Test 5: IOException expected."); 299 } catch (IOException e) { 300 // Expected. 301 } 302 } 303 304 public void test_closeException() throws Exception { 305 final IOException testException = new IOException("kaboom!"); 306 Writer thrower = new Writer() { 307 @Override 308 public void write(char cbuf[], int off, int len) throws IOException { 309 // Not used 310 } 311 312 @Override 313 public void flush() throws IOException { 314 // Not used 315 } 316 317 @Override 318 public void close() throws IOException { 319 throw testException; 320 } 321 }; 322 BufferedWriter bw = new BufferedWriter(thrower); 323 324 try { 325 bw.close(); 326 fail(); 327 } catch(IOException expected) { 328 assertSame(testException, expected); 329 } 330 331 try { 332 // Pre-openJdk8 BufferedWriter#close() with exception wouldn't 333 // reset the output writer to null. This would still allow write() 334 // to succeed. 335 bw.write(1); 336 fail(); 337 } catch(IOException expected) { 338 } 339 } 340 341 protected void setUp() { 342 sw = new Support_StringWriter(); 343 ssw = new Support_ASimpleWriter(true); 344 bw = new BufferedWriter(sw, 500); 345 } 346 347 protected void tearDown() { 348 ssw.throwExceptionOnNextUse = false; 349 try { 350 bw.close(); 351 } catch (Exception e) { 352 } 353 } 354} 355