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 e) {
223            // Expected
224        }
225        try {
226            cw.write(target, 0, -1);
227            fail("Test 2: IndexOutOfBoundsException expected.");
228        } catch (IndexOutOfBoundsException e) {
229            // Expected
230        }
231        try {
232            cw.write(target, 1, target.length);
233            fail("Test 3: IndexOutOfBoundsException expected.");
234        } catch (IndexOutOfBoundsException e) {
235            // Expected
236        }
237        try {
238            cw.write((char[]) null, 1, 1);
239            fail("Test 4: NullPointerException expected.");
240        } catch (NullPointerException e) {
241            // Expected.
242        }
243    }
244
245    /**
246     * @tests java.io.CharArrayWriter#write(int)
247     */
248    @TestTargetNew(
249        level = TestLevel.COMPLETE,
250        notes = "Verifies write(int) method.",
251        method = "write",
252        args = {int.class}
253    )
254    public void test_writeI() {
255        // Test for method void java.io.CharArrayWriter.write(int)
256        cw.write('T');
257        cr = new CharArrayReader(cw.toCharArray());
258        try {
259            assertEquals("Writer failed to write char", 'T', cr.read());
260        } catch (IOException e) {
261            fail("Exception during write test : " + e.getMessage());
262        }
263    }
264
265    /**
266     * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
267     */
268    @TestTargetNew(
269        level = TestLevel.COMPLETE,
270        notes = "Verifies write(java.lang.String, int, int) method. [Need to check different strings?]",
271        method = "write",
272        args = {java.lang.String.class, int.class, int.class}
273    )
274    public void test_writeLjava_lang_StringII() {
275        // Test for method void java.io.CharArrayWriter.write(java.lang.String,
276        // int, int)
277        cw.write("HelloWorld", 5, 5);
278        cr = new CharArrayReader(cw.toCharArray());
279        try {
280            char[] c = new char[100];
281            cr.read(c, 0, 5);
282            assertEquals("Writer failed to write correct chars",
283                         "World", new String(c, 0, 5));
284        } catch (IOException e) {
285            fail("Exception during write test : " + e.getMessage());
286        }
287    }
288
289    /**
290     * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
291     * Regression for HARMONY-387
292     */
293    @TestTargetNew(
294        level = TestLevel.PARTIAL,
295        notes = "Regression for write(java.lang.String, int, int) method.",
296        method = "write",
297        args = {java.lang.String.class, int.class, int.class}
298    )
299    public void test_writeLjava_lang_StringII_2() throws StringIndexOutOfBoundsException {
300        CharArrayWriter obj = new CharArrayWriter();
301        try {
302            obj.write((String) null, -1, 0);
303            fail("NullPointerException expected");
304        } catch (NullPointerException t) {
305        }
306    }
307
308    /**
309     * @tests java.io.CharArrayWriter#writeTo(java.io.Writer)
310     */
311    @TestTargetNew(
312        level = TestLevel.COMPLETE,
313        method = "writeTo",
314        args = {java.io.Writer.class}
315    )
316    public void test_writeToLjava_io_Writer() {
317        Support_ASimpleWriter ssw = new Support_ASimpleWriter(true);
318        cw.write("HelloWorld", 0, 10);
319        StringWriter sw = new StringWriter();
320        try {
321            cw.writeTo(sw);
322            assertEquals("Test 1: Writer failed to write correct chars;",
323                         "HelloWorld", sw.toString());
324        } catch (IOException e) {
325            fail("Exception during writeTo test : " + e.getMessage());
326        }
327
328        try {
329            cw.writeTo(ssw);
330            fail("Test 2: IOException expected.");
331        } catch (IOException e) {
332            // Expected.
333        }
334    }
335
336    /**
337     * Sets up the fixture, for example, open a network connection. This method
338     * is called before a test is executed.
339     */
340    protected void setUp() {
341        cw = new CharArrayWriter();
342    }
343
344    /**
345     * Tears down the fixture, for example, close a network connection. This
346     * method is called after a test is executed.
347     */
348    protected void tearDown() {
349        if (cr != null)
350            cr.close();
351        cw.close();
352    }
353
354    /**
355     * @tests java.io.CharArrayWriter#append(char)
356     */
357    @TestTargetNew(
358        level = TestLevel.COMPLETE,
359        notes = "Verifies append(char c) method.",
360        method = "append",
361        args = {char.class}
362    )
363    public void test_appendChar() throws IOException {
364        char testChar = ' ';
365        CharArrayWriter writer = new CharArrayWriter(10);
366        writer.append(testChar);
367        writer.flush();
368        assertEquals(String.valueOf(testChar), writer.toString());
369        writer.close();
370    }
371
372    /**
373     * @tests java.io.CharArrayWriter#append(CharSequence)
374     */
375    @TestTargetNew(
376        level = TestLevel.COMPLETE,
377        notes = "Verifies append(CharSequence csq) method.",
378        method = "append",
379        args = {java.lang.CharSequence.class}
380    )
381    public void test_appendCharSequence() {
382
383        String testString = "My Test String";
384        CharArrayWriter writer = new CharArrayWriter(10);
385        writer.append(testString);
386        writer.flush();
387        assertEquals(testString, writer.toString());
388        writer.close();
389    }
390
391    /**
392     * @tests java.io.CharArrayWriter#append(CharSequence, int, int)
393     */
394    @TestTargetNew(
395        level = TestLevel.COMPLETE,
396        method = "append",
397        args = {java.lang.CharSequence.class, int.class, int.class}
398    )
399    public void test_appendLjava_langCharSequenceII() {
400        String testString = "My Test String";
401        CharArrayWriter writer = new CharArrayWriter(10);
402
403        // Illegal argument checks.
404        try {
405            writer.append(testString, -1, 0);
406            fail("Test 1: IndexOutOfBoundsException expected.");
407        } catch (IndexOutOfBoundsException e) {
408            // Expected.
409        }
410        try {
411            writer.append(testString, 0, -1);
412            fail("Test 2: IndexOutOfBoundsException expected.");
413        } catch (IndexOutOfBoundsException e) {
414            // Expected.
415        }
416        try {
417            writer.append(testString, 1, 0);
418            fail("Test 3: IndexOutOfBoundsException expected.");
419        } catch (IndexOutOfBoundsException e) {
420            // Expected.
421        }
422        try {
423            writer.append(testString, 1, testString.length() + 1);
424            fail("Test 4: IndexOutOfBoundsException expected.");
425        } catch (IndexOutOfBoundsException e) {
426            // Expected.
427        }
428
429        writer.append(testString, 1, 3);
430        writer.flush();
431        assertEquals("Test 5: Appending failed;",
432                testString.substring(1, 3), writer.toString());
433        writer.close();
434    }
435
436}
437