StringWriterTest.java revision 2a68e06b23ceb5b401d5bc784e6882e71c662cae
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.luni.tests.java.io;
19
20import java.io.IOException;
21import java.io.StringWriter;
22
23public class StringWriterTest extends junit.framework.TestCase {
24
25	StringWriter sw;
26
27	/**
28	 * @tests java.io.StringWriter#StringWriter()
29	 */
30	public void test_Constructor() {
31		// Test for method java.io.StringWriter()
32		assertTrue("Used in tests", true);
33	}
34
35	/**
36	 * @tests java.io.StringWriter#close()
37	 */
38	public void test_close() {
39		// Test for method void java.io.StringWriter.close()
40		try {
41			sw.close();
42		} catch (IOException e) {
43			fail("IOException closing StringWriter : " + e.getMessage());
44		}
45	}
46
47	/**
48	 * @tests java.io.StringWriter#flush()
49	 */
50	public void test_flush() {
51		// Test for method void java.io.StringWriter.flush()
52		sw.flush();
53		sw.write('c');
54		assertEquals("Failed to flush char", "c", sw.toString());
55	}
56
57	/**
58	 * @tests java.io.StringWriter#getBuffer()
59	 */
60	public void test_getBuffer() {
61		// Test for method java.lang.StringBuffer
62		// java.io.StringWriter.getBuffer()
63
64		sw.write("This is a test string");
65		StringBuffer sb = sw.getBuffer();
66		assertEquals("Incorrect buffer returned",
67				"This is a test string", sb.toString());
68	}
69
70	/**
71	 * @tests java.io.StringWriter#toString()
72	 */
73	public void test_toString() {
74		// Test for method java.lang.String java.io.StringWriter.toString()
75		sw.write("This is a test string");
76		assertEquals("Incorrect string returned",
77				"This is a test string", sw.toString());
78	}
79
80	/**
81	 * @tests java.io.StringWriter#write(char[], int, int)
82	 */
83	public void test_write$CII() {
84		// Test for method void java.io.StringWriter.write(char [], int, int)
85		char[] c = new char[1000];
86		"This is a test string".getChars(0, 21, c, 0);
87		sw.write(c, 0, 21);
88		assertEquals("Chars not written properly",
89				"This is a test string", sw.toString());
90	}
91
92    /**
93     * @tests java.io.StringWriter#write(char[], int, int)
94     * Regression for HARMONY-387
95     */
96    public void test_write$CII_2() {
97        StringWriter obj = null;
98        try {
99            obj = new StringWriter();
100            obj.write(new char[0], (int) 0, (int) -1);
101            fail();
102        } catch (IndexOutOfBoundsException expected) {
103        }
104    }
105
106    /**
107     * @tests java.io.StringWriter#write(char[], int, int)
108     */
109    public void test_write$CII_3() {
110        StringWriter obj = null;
111        try {
112            obj = new StringWriter();
113            obj.write(new char[0], (int) -1, (int) 0);
114            fail();
115        } catch (IndexOutOfBoundsException expected) {
116        }
117    }
118
119    /**
120     * @tests java.io.StringWriter#write(char[], int, int)
121     */
122    public void test_write$CII_4() {
123        StringWriter obj = null;
124        try {
125            obj = new StringWriter();
126            obj.write(new char[0], (int) -1, (int) -1);
127            fail();
128        } catch (IndexOutOfBoundsException expected) {
129        }
130    }
131
132	/**
133	 * @tests java.io.StringWriter#write(int)
134	 */
135	public void test_writeI() {
136		// Test for method void java.io.StringWriter.write(int)
137		sw.write('c');
138		assertEquals("Char not written properly", "c", sw.toString());
139	}
140
141	/**
142	 * @tests java.io.StringWriter#write(java.lang.String)
143	 */
144	public void test_writeLjava_lang_String() {
145		// Test for method void java.io.StringWriter.write(java.lang.String)
146		sw.write("This is a test string");
147		assertEquals("String not written properly",
148				"This is a test string", sw.toString());
149	}
150
151	/**
152	 * @tests java.io.StringWriter#write(java.lang.String, int, int)
153	 */
154	public void test_writeLjava_lang_StringII() {
155		// Test for method void java.io.StringWriter.write(java.lang.String,
156		// int, int)
157		sw.write("This is a test string", 2, 2);
158		assertEquals("String not written properly", "is", sw.toString());
159	}
160
161	/**
162	 * @tests java.io.StringWriter#append(char)
163	 */
164	public void test_appendChar() throws IOException {
165	    char testChar = ' ';
166	    StringWriter stringWriter = new StringWriter(20);
167	    stringWriter.append(testChar);
168	    assertEquals(String.valueOf(testChar), stringWriter.toString());
169	    stringWriter.close();
170	}
171
172	/**
173	 * @tests java.io.PrintWriter#append(CharSequence)
174	 */
175	public void test_appendCharSequence() throws IOException {
176
177	    String testString = "My Test String";
178	    StringWriter stringWriter = new StringWriter(20);
179	    stringWriter.append(testString);
180	    assertEquals(String.valueOf(testString), stringWriter.toString());
181	    stringWriter.close();
182	}
183
184	/**
185	 * @tests java.io.PrintWriter#append(CharSequence, int, int)
186	 */
187	public void test_appendCharSequenceIntInt() throws IOException {
188	    String testString = "My Test String";
189	    StringWriter stringWriter = new StringWriter(20);
190	    stringWriter.append(testString, 1, 3);
191	    assertEquals(testString.substring(1, 3), stringWriter.toString());
192	    stringWriter.close();
193
194	}
195
196	/**
197     * Sets up the fixture, for example, open a network connection. This method
198     * is called before a test is executed.
199     */
200	protected void setUp() {
201
202		sw = new StringWriter();
203	}
204
205	/**
206	 * Tears down the fixture, for example, close a network connection. This
207	 * method is called after a test is executed.
208	 */
209	protected void tearDown() {
210	}
211}
212