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.CharArrayReader;
21import java.io.CharArrayWriter;
22import java.io.IOException;
23import java.io.StringWriter;
24import tests.support.Support_ASimpleWriter;
25
26public class OldCharArrayWriterTest extends junit.framework.TestCase {
27
28    CharArrayWriter cw;
29    CharArrayReader cr;
30
31    public void test_ConstructorI() {
32        // Test for method java.io.CharArrayWriter(int)
33        cw = new CharArrayWriter(90);
34        assertEquals("Test 1: Incorrect writer created.", 0, cw.size());
35
36        try {
37            cw = new CharArrayWriter(-1);
38            fail("IllegalArgumentException expected.");
39        } catch (IllegalArgumentException e) {
40            // Expected.
41        }
42    }
43
44    public void test_write$CII_Exception() {
45        char[] target = new char[10];
46        cw = new CharArrayWriter();
47        try {
48            cw.write(target, -1, 1);
49            fail("Test 1: IndexOutOfBoundsException expected.");
50        } catch (IndexOutOfBoundsException e) {
51            // Expected
52        }
53        try {
54            cw.write(target, 0, -1);
55            fail("Test 2: IndexOutOfBoundsException expected.");
56        } catch (IndexOutOfBoundsException e) {
57            // Expected
58        }
59        try {
60            cw.write(target, 1, target.length);
61            fail("Test 3: IndexOutOfBoundsException expected.");
62        } catch (IndexOutOfBoundsException e) {
63            // Expected
64        }
65        try {
66            cw.write((char[]) null, 1, 1);
67            fail("Test 4: NullPointerException expected.");
68        } catch (NullPointerException e) {
69            // Expected.
70        }
71    }
72
73    public void test_writeToLjava_io_Writer() {
74        Support_ASimpleWriter ssw = new Support_ASimpleWriter(true);
75        cw.write("HelloWorld", 0, 10);
76        StringWriter sw = new StringWriter();
77        try {
78            cw.writeTo(sw);
79            assertEquals("Test 1: Writer failed to write correct chars;",
80                         "HelloWorld", sw.toString());
81        } catch (IOException e) {
82            fail("Exception during writeTo test : " + e.getMessage());
83        }
84
85        try {
86            cw.writeTo(ssw);
87            fail("Test 2: IOException expected.");
88        } catch (IOException e) {
89            // Expected.
90        }
91    }
92
93    public void test_appendLjava_langCharSequenceII() {
94        String testString = "My Test String";
95        CharArrayWriter writer = new CharArrayWriter(10);
96
97        // Illegal argument checks.
98        try {
99            writer.append(testString, -1, 0);
100            fail("Test 1: IndexOutOfBoundsException expected.");
101        } catch (IndexOutOfBoundsException e) {
102            // Expected.
103        }
104        try {
105            writer.append(testString, 0, -1);
106            fail("Test 2: IndexOutOfBoundsException expected.");
107        } catch (IndexOutOfBoundsException e) {
108            // Expected.
109        }
110        try {
111            writer.append(testString, 1, 0);
112            fail("Test 3: IndexOutOfBoundsException expected.");
113        } catch (IndexOutOfBoundsException e) {
114            // Expected.
115        }
116        try {
117            writer.append(testString, 1, testString.length() + 1);
118            fail("Test 4: IndexOutOfBoundsException expected.");
119        } catch (IndexOutOfBoundsException e) {
120            // Expected.
121        }
122
123        writer.append(testString, 1, 3);
124        writer.flush();
125        assertEquals("Test 5: Appending failed;",
126                testString.substring(1, 3), writer.toString());
127        writer.close();
128    }
129
130    protected void setUp() {
131        cw = new CharArrayWriter();
132    }
133
134    protected void tearDown() {
135        if (cr != null) {
136            cr.close();
137        }
138        cw.close();
139    }
140}
141