CharArrayWriterTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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.CharArrayReader;
21import java.io.CharArrayWriter;
22import java.io.IOException;
23import java.io.StringWriter;
24
25import tests.support.Support_ASimpleWriter;
26import dalvik.annotation.TestLevel;
27import dalvik.annotation.TestTargetClass;
28import dalvik.annotation.TestTargetNew;
29
30@TestTargetClass(CharArrayWriter.class)
31public class CharArrayWriterTest extends junit.framework.TestCase {
32
33    char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
34
35    CharArrayWriter cw;
36
37    CharArrayReader cr;
38
39    /**
40     * @tests java.io.CharArrayWriter#CharArrayWriter(int)
41     */
42    @TestTargetNew(
43        level = TestLevel.COMPLETE,
44        method = "CharArrayWriter",
45        args = {int.class}
46    )
47    public void test_ConstructorI() {
48        // Test for method java.io.CharArrayWriter(int)
49        cw = new CharArrayWriter(90);
50        assertEquals("Test 1: Incorrect writer created.", 0, cw.size());
51
52        try {
53            cw = new CharArrayWriter(-1);
54            fail("IllegalArgumentException expected.");
55        } catch (IllegalArgumentException e) {
56            // Expected.
57        }
58    }
59
60    /**
61     * @tests java.io.CharArrayWriter#CharArrayWriter()
62     */
63    @TestTargetNew(
64        level = TestLevel.COMPLETE,
65        notes = "Verifies CharArrayWriter() method.",
66        method = "CharArrayWriter",
67        args = {}
68    )
69    public void test_Constructor() {
70        // Test for method java.io.CharArrayWriter()
71        cw = new CharArrayWriter();
72        assertEquals("Created incorrect writer", 0, cw.size());
73    }
74
75    /**
76     * @tests java.io.CharArrayWriter#close()
77     */
78    @TestTargetNew(
79        level = TestLevel.COMPLETE,
80        notes = "Verifies close() method.",
81        method = "close",
82        args = {}
83    )
84    public void test_close() {
85        // Test for method void java.io.CharArrayWriter.close()
86        cw.close();
87    }
88
89    /**
90     * @tests java.io.CharArrayWriter#flush()
91     */
92    @TestTargetNew(
93        level = TestLevel.COMPLETE,
94        notes = "Verifies flush() method.",
95        method = "flush",
96        args = {}
97    )
98    public void test_flush() {
99        // Test for method void java.io.CharArrayWriter.flush()
100        cw.flush();
101    }
102
103    /**
104     * @tests java.io.CharArrayWriter#reset()
105     */
106    @TestTargetNew(
107        level = TestLevel.COMPLETE,
108        notes = "Verifies reset() method.",
109        method = "reset",
110        args = {}
111    )
112    public void test_reset() {
113        // Test for method void java.io.CharArrayWriter.reset()
114        cw.write("HelloWorld", 5, 5);
115        cw.reset();
116        cw.write("HelloWorld", 0, 5);
117        cr = new CharArrayReader(cw.toCharArray());
118        try {
119            char[] c = new char[100];
120            cr.read(c, 0, 5);
121            assertEquals("Reset failed to reset buffer",
122                         "Hello", new String(c, 0, 5));
123        } catch (IOException e) {
124            fail("Exception during reset test : " + e.getMessage());
125        }
126    }
127
128    /**
129     * @tests java.io.CharArrayWriter#size()
130     */
131    @TestTargetNew(
132        level = TestLevel.COMPLETE,
133        notes = "Verifies size() method.",
134        method = "size",
135        args = {}
136    )
137    public void test_size() {
138        // Test for method int java.io.CharArrayWriter.size()
139        assertEquals("Returned incorrect size", 0, cw.size());
140        cw.write(hw, 5, 5);
141        assertEquals("Returned incorrect size", 5, cw.size());
142    }
143
144    /**
145     * @tests java.io.CharArrayWriter#toCharArray()
146     */
147    @TestTargetNew(
148        level = TestLevel.COMPLETE,
149        notes = "Verifies toCharArray() method.",
150        method = "toCharArray",
151        args = {}
152    )
153    public void test_toCharArray() {
154        // Test for method char [] java.io.CharArrayWriter.toCharArray()
155        cw.write("HelloWorld", 0, 10);
156        cr = new CharArrayReader(cw.toCharArray());
157        try {
158            char[] c = new char[100];
159            cr.read(c, 0, 10);
160            assertEquals("toCharArray failed to return correct array",
161                         "HelloWorld", new String(c, 0, 10));
162        } catch (IOException e) {
163            fail("Exception during toCharArray test : " + e.getMessage());
164        }
165    }
166
167    /**
168     * @tests java.io.CharArrayWriter#toString()
169     */
170    @TestTargetNew(
171        level = TestLevel.COMPLETE,
172        notes = "Verifies toString() method.",
173        method = "toString",
174        args = {}
175    )
176    public void test_toString() {
177        // Test for method java.lang.String java.io.CharArrayWriter.toString()
178        cw.write("HelloWorld", 5, 5);
179        cr = new CharArrayReader(cw.toCharArray());
180        assertEquals("Returned incorrect string",
181                     "World", cw.toString());
182    }
183
184    /**
185     * @tests java.io.CharArrayWriter#write(char[], int, int)
186     */
187    @TestTargetNew(
188        level = TestLevel.PARTIAL_COMPLETE,
189        method = "write",
190        args = {char[].class, int.class, int.class}
191    )
192    public void test_write$CII() {
193        // Test for method void java.io.CharArrayWriter.write(char [], int, int)
194        cw.write(hw, 5, 5);
195        cr = new CharArrayReader(cw.toCharArray());
196        try {
197            char[] c = new char[100];
198            cr.read(c, 0, 5);
199            assertEquals("Writer failed to write correct chars",
200                         "World", new String(c, 0, 5));
201        } catch (IOException e) {
202            fail("Exception during write test : " + e.getMessage());
203        }
204    }
205
206    /**
207     * @tests java.io.CharArrayWriter#write(char[], int, int)
208     * Regression for HARMONY-387
209     */
210    @TestTargetNew(
211        level = TestLevel.PARTIAL_COMPLETE,
212        notes = "Illegal argument checks.",
213        method = "write",
214        args = {char[].class, int.class, int.class}
215    )
216    public void test_write$CII_Exception() {
217        char[] target = new char[10];
218        cw = new CharArrayWriter();
219        try {
220            cw.write(target, -1, 1);
221            fail("Test 1: IndexOutOfBoundsException expected.");
222        } catch (IndexOutOfBoundsException t) {
223            assertEquals("IndexOutOfBoundsException rather than a subclass expected;",
224                    IndexOutOfBoundsException.class, t.getClass());
225        }
226        try {
227            cw.write(target, 0, -1);
228            fail("Test 2: IndexOutOfBoundsException expected.");
229        } catch (IndexOutOfBoundsException t) {
230            assertEquals("IndexOutOfBoundsException rather than a subclass expected;",
231                    IndexOutOfBoundsException.class, t.getClass());
232        }
233        try {
234            cw.write(target, 1, target.length);
235            fail("Test 3: IndexOutOfBoundsException expected.");
236        } catch (IndexOutOfBoundsException t) {
237            assertEquals("IndexOutOfBoundsException rather than a subclass expected;",
238                    IndexOutOfBoundsException.class, t.getClass());
239        }
240        try {
241            cw.write((char[]) null, 1, 1);
242            fail("Test 4: NullPointerException expected.");
243        } catch (NullPointerException t) {
244            // Expected.
245        }
246    }
247
248    /**
249     * @tests java.io.CharArrayWriter#write(int)
250     */
251    @TestTargetNew(
252        level = TestLevel.COMPLETE,
253        notes = "Verifies write(int) method.",
254        method = "write",
255        args = {int.class}
256    )
257    public void test_writeI() {
258        // Test for method void java.io.CharArrayWriter.write(int)
259        cw.write('T');
260        cr = new CharArrayReader(cw.toCharArray());
261        try {
262            assertEquals("Writer failed to write char", 'T', cr.read());
263        } catch (IOException e) {
264            fail("Exception during write test : " + e.getMessage());
265        }
266    }
267
268    /**
269     * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
270     */
271    @TestTargetNew(
272        level = TestLevel.COMPLETE,
273        notes = "Verifies write(java.lang.String, int, int) method. [Need to check different strings?]",
274        method = "write",
275        args = {java.lang.String.class, int.class, int.class}
276    )
277    public void test_writeLjava_lang_StringII() {
278        // Test for method void java.io.CharArrayWriter.write(java.lang.String,
279        // int, int)
280        cw.write("HelloWorld", 5, 5);
281        cr = new CharArrayReader(cw.toCharArray());
282        try {
283            char[] c = new char[100];
284            cr.read(c, 0, 5);
285            assertEquals("Writer failed to write correct chars",
286                         "World", new String(c, 0, 5));
287        } catch (IOException e) {
288            fail("Exception during write test : " + e.getMessage());
289        }
290    }
291
292    /**
293     * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
294     * Regression for HARMONY-387
295     */
296    @TestTargetNew(
297        level = TestLevel.PARTIAL,
298        notes = "Regression for write(java.lang.String, int, int) method.",
299        method = "write",
300        args = {java.lang.String.class, int.class, int.class}
301    )
302    public void test_writeLjava_lang_StringII_2() throws StringIndexOutOfBoundsException {
303        CharArrayWriter obj = new CharArrayWriter();
304        try {
305            obj.write((String) null, -1, 0);
306            fail("NullPointerException expected");
307        } catch (NullPointerException t) {
308        }
309    }
310
311    /**
312     * @tests java.io.CharArrayWriter#writeTo(java.io.Writer)
313     */
314    @TestTargetNew(
315        level = TestLevel.COMPLETE,
316        method = "writeTo",
317        args = {java.io.Writer.class}
318    )
319    public void test_writeToLjava_io_Writer() {
320        Support_ASimpleWriter ssw = new Support_ASimpleWriter(true);
321        cw.write("HelloWorld", 0, 10);
322        StringWriter sw = new StringWriter();
323        try {
324            cw.writeTo(sw);
325            assertEquals("Test 1: Writer failed to write correct chars;",
326                         "HelloWorld", sw.toString());
327        } catch (IOException e) {
328            fail("Exception during writeTo test : " + e.getMessage());
329        }
330
331        try {
332            cw.writeTo(ssw);
333            fail("Test 2: IOException expected.");
334        } catch (IOException e) {
335            // Expected.
336        }
337    }
338
339    /**
340     * Sets up the fixture, for example, open a network connection. This method
341     * is called before a test is executed.
342     */
343    protected void setUp() {
344        cw = new CharArrayWriter();
345    }
346
347    /**
348     * Tears down the fixture, for example, close a network connection. This
349     * method is called after a test is executed.
350     */
351    protected void tearDown() {
352        if (cr != null)
353            cr.close();
354        cw.close();
355    }
356
357    /**
358     * @tests java.io.CharArrayWriter#append(char)
359     */
360    @TestTargetNew(
361        level = TestLevel.COMPLETE,
362        notes = "Verifies append(char c) method.",
363        method = "append",
364        args = {char.class}
365    )
366    public void test_appendChar() throws IOException {
367        char testChar = ' ';
368        CharArrayWriter writer = new CharArrayWriter(10);
369        writer.append(testChar);
370        writer.flush();
371        assertEquals(String.valueOf(testChar), writer.toString());
372        writer.close();
373    }
374
375    /**
376     * @tests java.io.CharArrayWriter#append(CharSequence)
377     */
378    @TestTargetNew(
379        level = TestLevel.COMPLETE,
380        notes = "Verifies append(CharSequence csq) method.",
381        method = "append",
382        args = {java.lang.CharSequence.class}
383    )
384    public void test_appendCharSequence() {
385
386        String testString = "My Test String";
387        CharArrayWriter writer = new CharArrayWriter(10);
388        writer.append(testString);
389        writer.flush();
390        assertEquals(testString, writer.toString());
391        writer.close();
392    }
393
394    /**
395     * @tests java.io.CharArrayWriter#append(CharSequence, int, int)
396     */
397    @TestTargetNew(
398        level = TestLevel.COMPLETE,
399        method = "append",
400        args = {java.lang.CharSequence.class, int.class, int.class}
401    )
402    public void test_appendLjava_langCharSequenceII() {
403        String testString = "My Test String";
404        CharArrayWriter writer = new CharArrayWriter(10);
405
406        // Illegal argument checks.
407        try {
408            writer.append(testString, -1, 0);
409            fail("Test 1: IndexOutOfBoundsException expected.");
410        } catch (IndexOutOfBoundsException e) {
411            // Expected.
412        }
413        try {
414            writer.append(testString, 0, -1);
415            fail("Test 2: IndexOutOfBoundsException expected.");
416        } catch (IndexOutOfBoundsException e) {
417            // Expected.
418        }
419        try {
420            writer.append(testString, 1, 0);
421            fail("Test 3: IndexOutOfBoundsException expected.");
422        } catch (IndexOutOfBoundsException e) {
423            // Expected.
424        }
425        try {
426            writer.append(testString, 1, testString.length() + 1);
427            fail("Test 4: IndexOutOfBoundsException expected.");
428        } catch (IndexOutOfBoundsException e) {
429            // Expected.
430        }
431
432        writer.append(testString, 1, 3);
433        writer.flush();
434        assertEquals("Test 5: Appending failed;",
435                testString.substring(1, 3), writer.toString());
436        writer.close();
437    }
438
439}
440