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 org.apache.harmony.tests.java.io;
19
20import java.io.CharArrayReader;
21import java.io.CharArrayWriter;
22import java.io.IOException;
23import java.io.StringWriter;
24
25public class CharArrayWriterTest extends junit.framework.TestCase {
26
27    char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
28
29    CharArrayWriter cw;
30
31    CharArrayReader cr;
32
33    /**
34     * java.io.CharArrayWriter#CharArrayWriter()
35     */
36    public void test_Constructor() {
37        cw = new CharArrayWriter(90);
38        assertEquals("Created incorrect writer", 0, cw.size());
39    }
40
41    /**
42     * java.io.CharArrayWriter#CharArrayWriter(int)
43     */
44    public void test_ConstructorI() {
45        cw = new CharArrayWriter();
46        assertEquals("Created incorrect writer", 0, cw.size());
47    }
48
49    /**
50     * java.io.CharArrayWriter#close()
51     */
52    public void test_close() {
53        cw.close();
54    }
55
56    /**
57     * java.io.CharArrayWriter#flush()
58     */
59    public void test_flush() {
60        cw.flush();
61    }
62
63    /**
64     * java.io.CharArrayWriter#reset()
65     */
66    public void test_reset() throws IOException {
67        cw.write("HelloWorld", 5, 5);
68        cw.reset();
69        cw.write("HelloWorld", 0, 5);
70        cr = new CharArrayReader(cw.toCharArray());
71        char[] c = new char[100];
72        cr.read(c, 0, 5);
73        assertEquals("Reset failed to reset buffer", "Hello", new String(c, 0,
74                5));
75    }
76
77    /**
78     * java.io.CharArrayWriter#size()
79     */
80    public void test_size() {
81        assertEquals("Returned incorrect size", 0, cw.size());
82        cw.write(hw, 5, 5);
83        assertEquals("Returned incorrect size", 5, cw.size());
84    }
85
86    /**
87     * java.io.CharArrayWriter#toCharArray()
88     */
89    public void test_toCharArray() throws IOException {
90        cw.write("HelloWorld", 0, 10);
91        cr = new CharArrayReader(cw.toCharArray());
92        char[] c = new char[100];
93        cr.read(c, 0, 10);
94        assertEquals("toCharArray failed to return correct array",
95                "HelloWorld", new String(c, 0, 10));
96    }
97
98    /**
99     * java.io.CharArrayWriter#toString()
100     */
101    public void test_toString() {
102        cw.write("HelloWorld", 5, 5);
103        cr = new CharArrayReader(cw.toCharArray());
104        assertEquals("Returned incorrect string", "World", cw.toString());
105    }
106
107    /**
108     * java.io.CharArrayWriter#write(char[], int, int)
109     */
110    public void test_write$CII() throws IOException {
111        cw.write(hw, 5, 5);
112        cr = new CharArrayReader(cw.toCharArray());
113        char[] c = new char[100];
114        cr.read(c, 0, 5);
115        assertEquals("Writer failed to write correct chars", "World",
116                new String(c, 0, 5));
117    }
118
119    /**
120     * java.io.CharArrayWriter#write(char[], int, int)
121     */
122    public void test_write$CII_2() {
123        // Regression for HARMONY-387
124        CharArrayWriter obj = new CharArrayWriter();
125        try {
126            obj.write(new char[] { '0' }, 0, -1);
127            fail();
128        } catch (IndexOutOfBoundsException expected) {
129        }
130    }
131
132    /**
133     * java.io.CharArrayWriter#write(int)
134     */
135    public void test_writeI() throws IOException {
136        cw.write('T');
137        cr = new CharArrayReader(cw.toCharArray());
138        assertEquals("Writer failed to write char", 'T', cr.read());
139    }
140
141    /**
142     * java.io.CharArrayWriter#write(java.lang.String, int, int)
143     */
144    public void test_writeLjava_lang_StringII() throws IOException {
145        cw.write("HelloWorld", 5, 5);
146        cr = new CharArrayReader(cw.toCharArray());
147        char[] c = new char[100];
148        cr.read(c, 0, 5);
149        assertEquals("Writer failed to write correct chars", "World",
150                new String(c, 0, 5));
151    }
152
153    /**
154     * java.io.CharArrayWriter#write(java.lang.String, int, int)
155     */
156    public void test_writeLjava_lang_StringII_2()
157            throws StringIndexOutOfBoundsException {
158        // Regression for HARMONY-387
159        CharArrayWriter obj = new CharArrayWriter();
160        try {
161            obj.write((String) null, -1, 0);
162            fail("NullPointerException expected");
163        } catch (NullPointerException t) {
164            // Expected
165        }
166    }
167
168    /**
169     * java.io.CharArrayWriter#writeTo(java.io.Writer)
170     */
171    public void test_writeToLjava_io_Writer() throws IOException {
172        cw.write("HelloWorld", 0, 10);
173        StringWriter sw = new StringWriter();
174        cw.writeTo(sw);
175        assertEquals("Writer failed to write correct chars", "HelloWorld", sw
176                .toString());
177    }
178
179    /**
180     * Sets up the fixture, for example, open a network connection. This method
181     * is called before a test is executed.
182     */
183    protected void setUp() {
184        cw = new CharArrayWriter();
185    }
186
187    /**
188     * Tears down the fixture, for example, close a network connection. This
189     * method is called after a test is executed.
190     */
191    protected void tearDown() {
192        if (cr != null) {
193            cr.close();
194        }
195        cw.close();
196    }
197
198    /**
199     * java.io.CharArrayWriter#append(char)
200     */
201    public void test_appendChar() throws IOException {
202        char testChar = ' ';
203        CharArrayWriter writer = new CharArrayWriter(10);
204        writer.append(testChar);
205        writer.flush();
206        assertEquals(String.valueOf(testChar), writer.toString());
207        writer.close();
208    }
209
210    /**
211     * java.io.CharArrayWriter#append(CharSequence)
212     */
213    public void test_appendCharSequence() {
214        String testString = "My Test String";
215        CharArrayWriter writer = new CharArrayWriter(10);
216        writer.append(testString);
217        writer.flush();
218        assertEquals(testString, writer.toString());
219        writer.close();
220    }
221
222    /**
223     * java.io.CharArrayWriter#append(CharSequence, int, int)
224     */
225    public void test_appendCharSequenceIntInt() {
226        String testString = "My Test String";
227        CharArrayWriter writer = new CharArrayWriter(10);
228        writer.append(testString, 1, 3);
229        writer.flush();
230        assertEquals(testString.substring(1, 3), writer.toString());
231        writer.close();
232    }
233}
234