StringWriterTest.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 dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestLevel;
24
25import java.io.IOException;
26import java.io.StringWriter;
27
28@TestTargetClass(StringWriter.class)
29public class StringWriterTest extends junit.framework.TestCase {
30
31    StringWriter sw;
32
33    /**
34     * @tests java.io.StringWriter#StringWriter()
35     */
36    @TestTargets({
37        @TestTargetNew(
38            level = TestLevel.COMPLETE,
39            notes = "",
40            method = "StringWriter",
41            args = {}
42        ),
43        @TestTargetNew(
44            level = TestLevel.COMPLETE,
45            notes = "",
46            method = "StringWriter",
47            args = {int.class}
48        )
49    })
50    public void test_Constructor() {
51        new StringWriter();
52        new StringWriter(10);
53        new StringWriter(10000);
54    }
55
56    /**
57     * @tests java.io.StringWriter#close()
58     */
59    @TestTargetNew(
60        level = TestLevel.COMPLETE,
61        notes = "There won't be IOException, ever!",
62        method = "close",
63        args = {}
64    )
65    public void test_close() {
66        // Test for method void java.io.StringWriter.close()
67        try {
68            sw.close();
69        } catch (IOException e) {
70            fail("IOException closing StringWriter : " + e.getMessage());
71        }
72    }
73
74    /**
75     * @tests java.io.StringWriter#flush()
76     */
77    @TestTargetNew(
78        level = TestLevel.COMPLETE,
79        notes = "",
80        method = "flush",
81        args = {}
82    )
83    public void test_flush() {
84        // Test for method void java.io.StringWriter.flush()
85        sw.flush();
86        sw.write('c');
87        assertEquals("Failed to flush char", "c", sw.toString());
88    }
89
90    /**
91     * @tests java.io.StringWriter#getBuffer()
92     */
93    @TestTargetNew(
94        level = TestLevel.COMPLETE,
95        notes = "",
96        method = "getBuffer",
97        args = {}
98    )
99    public void test_getBuffer() {
100        // Test for method java.lang.StringBuffer
101        // java.io.StringWriter.getBuffer()
102
103        sw.write("This is a test string");
104        StringBuffer sb = sw.getBuffer();
105        assertEquals("Incorrect buffer returned",
106                "This is a test string", sb.toString());
107    }
108
109    /**
110     * @tests java.io.StringWriter#toString()
111     */
112    @TestTargetNew(
113        level = TestLevel.COMPLETE,
114        notes = "",
115        method = "toString",
116        args = {}
117    )
118    public void test_toString() {
119        // Test for method java.lang.String java.io.StringWriter.toString()
120        sw.write("This is a test string");
121        assertEquals("Incorrect string returned",
122                "This is a test string", sw.toString());
123    }
124
125    /**
126     * @tests java.io.StringWriter#write(char[], int, int)
127     */
128    @TestTargetNew(
129        level = TestLevel.PARTIAL_COMPLETE,
130        notes = "",
131        method = "write",
132        args = {char[].class, int.class, int.class}
133    )
134    public void test_write$CII() {
135        // Test for method void java.io.StringWriter.write(char [], int, int)
136        char[] c = new char[1000];
137        "This is a test string".getChars(0, 21, c, 0);
138        sw.write(c, 4, 14);
139        assertEquals("Chars not written properly",
140                " is a test str", sw.toString());
141    }
142
143    /**
144     * @tests java.io.StringWriter#write(char[], int, int)
145     * Regression for HARMONY-387
146     */
147    @TestTargetNew(
148        level = TestLevel.PARTIAL_COMPLETE,
149        notes = "",
150        method = "write",
151        args = {char[].class, int.class, int.class}
152    )
153    public void test_write$CII_2() {
154        StringWriter obj = null;
155        try {
156            obj = new StringWriter();
157            obj.write(new char[0], (int) 0, (int) -1);
158            fail("IndexOutOfBoundsException expected");
159        } catch (IndexOutOfBoundsException t) {
160            assertEquals(
161                    "IndexOutOfBoundsException rather than a subclass expected",
162                    IndexOutOfBoundsException.class, t.getClass());
163        }
164    }
165
166    /**
167     * @tests java.io.StringWriter#write(char[], int, int)
168     */
169    @TestTargetNew(
170        level = TestLevel.PARTIAL_COMPLETE,
171        notes = "",
172        method = "write",
173        args = {char[].class, int.class, int.class}
174    )
175    public void test_write$CII_3() {
176        StringWriter obj = null;
177        try {
178            obj = new StringWriter();
179            obj.write(new char[0], (int) -1, (int) 0);
180            fail("IndexOutOfBoundsException expected");
181        } catch (ArrayIndexOutOfBoundsException t) {
182            fail("IndexOutOfBoundsException expected");
183        } catch (IndexOutOfBoundsException t) {
184        }
185    }
186
187    /**
188     * @tests java.io.StringWriter#write(char[], int, int)
189     */
190    @TestTargetNew(
191        level = TestLevel.PARTIAL_COMPLETE,
192        notes = "",
193        method = "write",
194        args = {char[].class, int.class, int.class}
195    )
196    public void test_write$CII_4() {
197        StringWriter obj = null;
198        try {
199            obj = new StringWriter();
200            obj.write(new char[0], (int) -1, (int) -1);
201            fail("IndexOutOfBoundsException expected");
202        } catch (ArrayIndexOutOfBoundsException t) {
203            fail("IndexOutOfBoundsException expected");
204        } catch (IndexOutOfBoundsException t) {
205        }
206    }
207
208    /**
209     * @tests java.io.StringWriter#write(int)
210     */
211    @TestTargetNew(
212        level = TestLevel.COMPLETE,
213        notes = "",
214        method = "write",
215        args = {int.class}
216    )
217    public void test_writeI() {
218        // Test for method void java.io.StringWriter.write(int)
219        sw.write('c');
220        assertEquals("Char not written properly", "c", sw.toString());
221    }
222
223    /**
224     * @tests java.io.StringWriter#write(java.lang.String)
225     */
226    @TestTargetNew(
227        level = TestLevel.COMPLETE,
228        notes = "",
229        method = "write",
230        args = {java.lang.String.class}
231    )
232    public void test_writeLjava_lang_String() {
233        // Test for method void java.io.StringWriter.write(java.lang.String)
234        sw.write("This is a test string");
235        assertEquals("String not written properly",
236                "This is a test string", sw.toString());
237    }
238
239    /**
240     * @tests java.io.StringWriter#write(java.lang.String, int, int)
241     */
242    @TestTargetNew(
243        level = TestLevel.COMPLETE,
244        notes = "",
245        method = "write",
246        args = {java.lang.String.class, int.class, int.class}
247    )
248    public void test_writeLjava_lang_StringII() {
249        // Test for method void java.io.StringWriter.write(java.lang.String,
250        // int, int)
251        sw.write("This is a test string", 2, 2);
252        assertEquals("String not written properly", "is", sw.toString());
253    }
254
255    /**
256     * @tests java.io.StringWriter#append(char)
257     */
258    @TestTargetNew(
259        level = TestLevel.COMPLETE,
260        notes = "",
261        method = "append",
262        args = {char.class}
263    )
264    public void test_appendChar() throws IOException {
265        char testChar = ' ';
266        StringWriter stringWriter = new StringWriter(20);
267        stringWriter.append(testChar);
268        assertEquals(String.valueOf(testChar), stringWriter.toString());
269        stringWriter.close();
270    }
271
272    /**
273     * @tests java.io.PrintWriter#append(CharSequence)
274     */
275    @TestTargetNew(
276        level = TestLevel.COMPLETE,
277        notes = "",
278        method = "append",
279        args = {java.lang.CharSequence.class}
280    )
281    public void test_appendCharSequence() throws IOException {
282
283        String testString = "My Test String";
284        StringWriter stringWriter = new StringWriter(20);
285        stringWriter.append(testString);
286        assertEquals(String.valueOf(testString), stringWriter.toString());
287        stringWriter.close();
288    }
289
290    /**
291     * @tests java.io.PrintWriter#append(CharSequence, int, int)
292     */
293    @TestTargetNew(
294        level = TestLevel.COMPLETE,
295        notes = "",
296        method = "append",
297        args = {java.lang.CharSequence.class, int.class, int.class}
298    )
299    public void test_appendCharSequenceIntInt() throws IOException {
300        String testString = "My Test String";
301        StringWriter stringWriter = new StringWriter(20);
302        stringWriter.append(testString, 1, 3);
303        assertEquals(testString.substring(1, 3), stringWriter.toString());
304        stringWriter.close();
305
306        try {
307            StringWriter tobj = new StringWriter(9);
308            tobj.append("01234567890123456789", 19, 2);
309            fail("IndexOutOfBoundsException not thrown!");
310        } catch (IndexOutOfBoundsException e) {
311            // expected
312        }
313        try {
314            StringWriter tobj = new StringWriter(9);
315            tobj.append("01234567890123456789", 29, 2);
316            fail("IndexOutOfBoundsException not thrown!");
317        } catch (IndexOutOfBoundsException e) {
318            // expected
319        }
320    }
321
322    /**
323     * Sets up the fixture, for example, open a network connection. This method
324     * is called before a test is executed.
325     */
326    protected void setUp() {
327
328        sw = new StringWriter();
329    }
330
331    /**
332     * Tears down the fixture, for example, close a network connection. This
333     * method is called after a test is executed.
334     */
335    protected void tearDown() {
336    }
337}
338