BufferedWriterTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 dalvik.annotation.TestInfo;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTarget;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.BufferedWriter;
26import java.io.IOException;
27
28import tests.support.Support_StringWriter;
29
30@TestTargetClass(BufferedWriter.class)
31public class BufferedWriterTest extends junit.framework.TestCase {
32
33    BufferedWriter bw;
34
35    Support_StringWriter sw;
36
37    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";
38
39    /**
40     * @tests java.io.BufferedWriter#BufferedWriter(java.io.Writer)
41     */
42    @TestInfo(
43            level = TestLevel.COMPLETE,
44            purpose = "The test verifies BufferedWriter(Writer out) constructor.",
45            targets = { @TestTarget(methodName = "BufferedWriter",
46                                    methodArgs = {java.io.Writer.class})
47            }
48    )
49    public void test_ConstructorLjava_io_Writer() {
50        // Test for method java.io.BufferedWriter(java.io.Writer)
51
52        sw = new Support_StringWriter();
53        bw = new BufferedWriter(sw);
54        sw.write("Hi");
55        assertEquals("Constructor failed", "Hi", sw.toString());
56
57    }
58
59    /**
60     * @tests java.io.BufferedWriter#BufferedWriter(java.io.Writer, int)
61     */
62    @TestInfo(
63            level = TestLevel.PARTIAL,
64            purpose = "IllegalArgumentException checking missed. See setUp for info.",
65            targets = { @TestTarget(methodName = "BufferedWriter",
66                                    methodArgs = {java.io.Writer.class, int.class})
67            }
68    )
69    public void test_ConstructorLjava_io_WriterI() {
70        // Test for method java.io.BufferedWriter(java.io.Writer, int)
71        assertTrue("Used in tests", true);
72    }
73
74    /**
75     * @tests java.io.BufferedWriter#close()
76     */
77    @TestInfo(
78            level = TestLevel.PARTIAL,
79            purpose = "IOException checking missed.",
80            targets = { @TestTarget(methodName = "close",
81                                    methodArgs = {})
82            }
83    )
84    public void test_close() {
85        // Test for method void java.io.BufferedWriter.close()
86        try {
87            bw.close();
88            bw.write(testString);
89        } catch (IOException e) {
90        }
91        assertTrue("Write after close", !sw.toString().equals(testString));
92    }
93
94    /**
95     * @tests java.io.BufferedWriter#flush()
96     */
97    @TestInfo(
98            level = TestLevel.PARTIAL,
99            purpose = "IOException checking missed.",
100            targets = { @TestTarget(methodName = "flush",
101                                    methodArgs = {})
102            }
103    )
104    public void test_flush() {
105        // Test for method void java.io.BufferedWriter.flush()
106        try {
107            bw.write("This should not cause a flush");
108            assertTrue("Bytes written without flush", sw.toString().equals(""));
109            bw.flush();
110            assertEquals("Bytes not flushed",
111                    "This should not cause a flush", sw.toString());
112        } catch (Exception e) {
113            fail("Exception during flush test");
114        }
115    }
116
117    /**
118     * @tests java.io.BufferedWriter#newLine()
119     */
120    @TestInfo(
121            level = TestLevel.PARTIAL,
122            purpose = "IOException checking missed",
123            targets = { @TestTarget(methodName = "newLine",
124                                    methodArgs = {})
125            }
126    )
127    public void test_newLine() {
128        // Test for method void java.io.BufferedWriter.newLine()
129        try {
130            String separator = System.getProperty("line.separator");
131            bw.write("Hello");
132            bw.newLine();
133            bw.write("World");
134            bw.flush();
135            assertTrue("Incorrect string written: " + sw.toString(), sw
136                    .toString().equals("Hello" + separator + "World"));
137        } catch (Exception e) {
138            fail("Exception during write test");
139        }
140    }
141
142    /**
143     * @tests java.io.BufferedWriter#write(char[], int, int)
144     */
145    @TestInfo(
146            level = TestLevel.PARTIAL_OK,
147            purpose = "IOException checking missed",
148            targets = { @TestTarget(methodName = "write",
149                                    methodArgs = {char[].class, int.class, int.class})
150            }
151    )
152    public void test_write$CII() {
153        // Test for method void java.io.BufferedWriter.write(char [], int, int)
154        try {
155            char[] testCharArray = testString.toCharArray();
156            bw.write(testCharArray, 500, 1000);
157            bw.flush();
158            assertTrue("Incorrect string written", sw.toString().equals(
159                    testString.substring(500, 1500)));
160
161            int idx = sw.toString().length();
162            bw.write(testCharArray, 0, testCharArray.length);
163            assertEquals(idx + testCharArray.length, sw.toString().length());
164            bw.write(testCharArray, 0, 0);
165            assertEquals(idx + testCharArray.length, sw.toString().length());
166            bw.write(testCharArray, testCharArray.length, 0);
167            assertEquals(idx + testCharArray.length, sw.toString().length());
168        } catch (Exception e) {
169            fail("Exception during write test");
170        }
171
172    }
173
174    /**
175     * @tests java.io.BufferedWriter#write(char[], int, int)
176     */
177    @TestInfo(
178            level = TestLevel.PARTIAL_OK,
179            purpose = "The test verifies that write(char[] cbuf, int off, int len) " +
180                    "method throws exceptions in appropriate cases.",
181            targets = { @TestTarget(methodName = "write",
182                                    methodArgs = {char[].class, int.class, int.class})
183            }
184    )
185    public void test_write_$CII_Exception() throws IOException {
186        char[] nullCharArray = null;
187        char[] charArray = testString.toCharArray();
188
189        try {
190            bw.write(nullCharArray, 0, 0);
191            fail("should throw NullPointerException");
192        } catch (NullPointerException e) {
193            // expected
194        }
195
196        try {
197            bw.write(charArray, -1, 0);
198            fail("should throw IndexOutOfBoundsException");
199        } catch (IndexOutOfBoundsException e) {
200            // expected
201        }
202
203        try {
204            bw.write(charArray, 0, -1);
205            fail("should throw IndexOutOfBoundsException");
206        } catch (IndexOutOfBoundsException e) {
207            // expected
208        }
209
210        try {
211            bw.write(charArray, charArray.length + 1, 0);
212            fail("should throw IndexOutOfBoundsException");
213        } catch (IndexOutOfBoundsException e) {
214            //expected
215        }
216
217        try {
218            bw.write(charArray, charArray.length, 1);
219            fail("should throw IndexOutOfBoundsException");
220        } catch (IndexOutOfBoundsException e) {
221            //expected
222        }
223
224        try {
225            bw.write(charArray, 0, charArray.length + 1);
226            fail("should throw IndexOutOfBoundsException");
227        } catch (IndexOutOfBoundsException e) {
228            //expected
229        }
230
231        try {
232            bw.write(charArray, 1, charArray.length);
233            fail("should throw IndexOutOfBoundsException");
234        } catch (IndexOutOfBoundsException e) {
235            //expected
236        }
237
238        bw.close();
239
240        try {
241            bw.write(nullCharArray, -1, -1);
242            fail("should throw IOException");
243        } catch (IOException e) {
244            // expected
245        }
246    }
247
248    /**
249     * @tests java.io.BufferedWriter#write(int)
250     */
251    @TestInfo(
252            level = TestLevel.PARTIAL,
253            purpose = "IOException checking missed.",
254            targets = { @TestTarget(methodName = "write",
255                                    methodArgs = {int.class})
256            }
257    )
258    public void test_writeI() {
259        // Test for method void java.io.BufferedWriter.write(int)
260        try {
261            bw.write('T');
262            assertTrue("Char written without flush", sw.toString().equals(""));
263            bw.flush();
264            assertEquals("Incorrect char written", "T", sw.toString());
265        } catch (Exception e) {
266            fail("Exception during write test");
267        }
268    }
269
270    /**
271     * @tests java.io.BufferedWriter#write(java.lang.String, int, int)
272     */
273    @TestInfo(
274            level = TestLevel.PARTIAL,
275            purpose = "IOException checking missed.",
276            targets = { @TestTarget(methodName = "write",
277                                    methodArgs = {java.lang.String.class, int.class, int.class})
278            }
279    )
280    public void test_writeLjava_lang_StringII() {
281        // Test for method void java.io.BufferedWriter.write(java.lang.String,
282        // int, int)
283        try {
284            bw.write(testString);
285            bw.flush();
286            assertTrue("Incorrect string written", sw.toString().equals(
287                    testString));
288        } catch (Exception e) {
289            fail("Exception during write test");
290        }
291    }
292
293    /**
294     * @tests java.io.BufferedWriter#write(java.lang.String, int, int)
295     */
296    @TestInfo(
297            level = TestLevel.COMPLETE,
298            purpose = "The test verifies that write(String s, int off, int len) " +
299                    "method throws exceptions in appropriate cases.",
300            targets = { @TestTarget(methodName = "write",
301                                    methodArgs = {java.lang.String.class, int.class, int.class})
302            }
303    )
304    public void test_write_LStringII_Exception() throws IOException {
305
306        bw.write((String) null , -1, -1);
307        bw.write((String) null , -1, 0);
308        bw.write((String) null , 0 , -1);
309        bw.write((String) null , 0 , 0);
310
311        try {
312            bw.write((String) null , -1, 1);
313            fail("should throw NullPointerException");
314        } catch (NullPointerException e) {
315            // expected
316        }
317
318        bw.write(testString, 0, 0);
319        bw.write(testString, testString.length(), 0);
320        bw.write(testString, testString.length() + 1, 0);
321
322        try {
323            bw.write(testString, testString.length() + 1, 1);
324            fail("should throw StringIndexOutOfBoundsException");
325        } catch (StringIndexOutOfBoundsException e) {
326            // expected
327        }
328
329        bw.close();
330
331        try {
332            bw.write((String) null , -1, -1);
333            fail("should throw IOException");
334        } catch (IOException e) {
335            // expected
336        }
337
338        try {
339            bw.write((String) null , -1, 1);
340            fail("should throw IOException");
341        } catch (IOException e) {
342            // expected
343        }
344
345        try {
346            bw.write(testString , -1, -1);
347            fail("should throw IOException");
348        } catch (IOException e) {
349            // expected
350        }
351    }
352
353    /**
354     * Sets up the fixture, for example, open a network connection. This method
355     * is called before a test is executed.
356     */
357    protected void setUp() {
358        sw = new Support_StringWriter();
359        bw = new BufferedWriter(sw, 500);
360    }
361
362    /**
363     * Tears down the fixture, for example, close a network connection. This
364     * method is called after a test is executed.
365     */
366    protected void tearDown() {
367
368        try {
369            bw.close();
370        } catch (Exception e) {
371        }
372    }
373}
374