1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.io;
18
19import java.io.IOException;
20import java.io.Writer;
21import junit.framework.TestCase;
22import tests.support.Support_ASimpleWriter;
23
24public class OldWriterTest extends TestCase {
25
26    public void test_appendChar() throws IOException {
27        Writer tobj = new Support_ASimpleWriter(2);
28        tobj.append('a');
29        tobj.append('b');
30        assertEquals("Wrong stuff written!", "ab", tobj.toString());
31        try {
32            tobj.append('c');
33            fail("IOException not thrown!");
34        } catch (IOException e) {
35            // expected
36        }
37    }
38
39    public void test_appendCharSequence() throws IOException {
40        String testString = "My Test String";
41        Writer tobj = new Support_ASimpleWriter(20);
42        tobj.append(testString);
43        assertEquals("Wrong stuff written!", testString, tobj.toString());
44        try {
45            tobj.append(testString);
46            fail("IOException not thrown!");
47        } catch (IOException e) {
48            // expected
49        }
50    }
51
52    public void test_appendCharSequenceIntInt() throws IOException {
53        String testString = "My Test String";
54        Writer tobj = new Support_ASimpleWriter(21);
55        testString = "0123456789abcdefghijABCDEFGHIJ";
56        tobj.append(testString, 0, 5);
57        assertEquals("Wrong stuff written!", "01234", tobj.toString());
58        tobj.append(testString, 10, 15);
59        assertEquals("Wrong stuff written!", "01234abcde", tobj.toString());
60        tobj.append(testString, 20, 30);
61        assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString());
62        try {
63            tobj.append(testString, 30, 31);
64            fail("IndexOutOfBoundsException not thrown!");
65        } catch (IndexOutOfBoundsException e) {
66            // expected
67        }
68        tobj.append(testString, 20, 21); // Just fill the writer to its limit!
69        try {
70            tobj.append(testString, 29, 30);
71            fail("IOException not thrown!");
72        } catch (IOException e) {
73            // expected
74        }
75    }
76
77    public void test_appendCharSequenceIntInt_Exception() throws IOException {
78        String testString = "My Test String";
79        Writer tobj = new Support_ASimpleWriter(21);
80        try {
81            tobj.append(testString, 30, 31);
82            fail("IndexOutOfBoundsException not thrown!");
83        } catch (IndexOutOfBoundsException e) {
84            // expected
85        }
86        try {
87            tobj.append(testString, -1, 1);
88            fail("IndexOutOfBoundsException not thrown!");
89        } catch (IndexOutOfBoundsException e) {
90            // expected
91        }
92        try {
93            tobj.append(testString, 0, -1);
94            fail("IndexOutOfBoundsException not thrown!");
95        } catch (IndexOutOfBoundsException e) {
96            // expected
97        }
98    }
99
100    public void test_write$C() throws IOException {
101        Writer tobj = new Support_ASimpleWriter(21);
102        tobj.write("01234".toCharArray());
103        assertEquals("Wrong stuff written!", "01234", tobj.toString());
104        tobj.write("abcde".toCharArray());
105        assertEquals("Wrong stuff written!", "01234abcde", tobj.toString());
106        tobj.write("ABCDEFGHIJ".toCharArray());
107        assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString());
108        tobj.write("z".toCharArray()); // Just fill the writer to its limit!
109        try {
110            tobj.write("LES JEUX SONT FAITS".toCharArray());
111            fail("IOException not thrown!");
112        } catch (IOException e) {
113            // expected
114        }
115    }
116
117    public void test_writeI() throws IOException {
118        Writer tobj = new Support_ASimpleWriter(2);
119        tobj.write('a');
120        tobj.write('b');
121        assertEquals("Wrong stuff written!", "ab", tobj.toString());
122        try {
123            tobj.write('c');
124            fail("IOException not thrown!");
125        } catch (IOException e) {
126            // expected
127        }
128    }
129
130    public void test_writeLjava_lang_String() throws IOException {
131        Writer tobj = new Support_ASimpleWriter(21);
132        tobj.write("01234");
133        assertEquals("Wrong stuff written!", "01234", tobj.toString());
134        tobj.write("abcde");
135        assertEquals("Wrong stuff written!", "01234abcde", tobj.toString());
136        tobj.write("ABCDEFGHIJ");
137        assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString());
138        tobj.write("z"); // Just fill the writer to its limit!
139        try {
140            tobj.write("LES JEUX SONT FAITS");
141            fail("IOException not thrown!");
142        } catch (IOException e) {
143            // expected
144        }
145    }
146
147    public void test_writeLjava_lang_StringII() throws IOException {
148        String testString;
149        Writer tobj = new Support_ASimpleWriter(21);
150        testString = "0123456789abcdefghijABCDEFGHIJ";
151        tobj.write(testString, 0, 5);
152        assertEquals("Wrong stuff written!", "01234", tobj.toString());
153        tobj.write(testString, 10, 5);
154        assertEquals("Wrong stuff written!", "01234abcde", tobj.toString());
155        tobj.write(testString, 20, 10);
156        assertEquals("Wrong stuff written!", "01234abcdeABCDEFGHIJ", tobj.toString());
157        try {
158            tobj.write(testString, 30, 1);
159            fail("IndexOutOfBoundsException not thrown!");
160        } catch (IndexOutOfBoundsException e) {
161            // expected
162        }
163        tobj.write(testString, 20, 1); // Just fill the writer to its limit!
164        try {
165            tobj.write(testString, 29, 1);
166            fail("IOException not thrown!");
167        } catch (IOException e) {
168            // expected
169        }
170    }
171
172    public void test_writeLjava_lang_StringII_Exception() throws IOException {
173        String testString = "My Test String";
174        Writer tobj = new Support_ASimpleWriter(21);
175        try {
176            tobj.write(testString, 30, 31);
177            fail("IndexOutOfBoundsException not thrown!");
178        } catch (IndexOutOfBoundsException e) {
179            // expected
180        }
181        try {
182            tobj.write(testString, -1, 1);
183            fail("IndexOutOfBoundsException not thrown!");
184        } catch (IndexOutOfBoundsException e) {
185            // expected
186        }
187        try {
188            tobj.write(testString, 0, -1);
189            fail("IndexOutOfBoundsException not thrown!");
190        } catch (IndexOutOfBoundsException e) {
191            // expected
192        }
193    }
194}
195