001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022:
023: public class StringWriterTest extends junit.framework.TestCase {
024:
025: StringWriter sw;
026:
027: /**
028: * @tests java.io.StringWriter#StringWriter()
029: */
030: public void test_Constructor() {
031: // Test for method java.io.StringWriter()
032: assertTrue("Used in tests", true);
033: }
034:
035: /**
036: * @tests java.io.StringWriter#close()
037: */
038: public void test_close() {
039: // Test for method void java.io.StringWriter.close()
040: try {
041: sw.close();
042: } catch (IOException e) {
043: fail("IOException closing StringWriter : " + e.getMessage());
044: }
045: }
046:
047: /**
048: * @tests java.io.StringWriter#flush()
049: */
050: public void test_flush() {
051: // Test for method void java.io.StringWriter.flush()
052: sw.flush();
053: sw.write('c');
054: assertEquals("Failed to flush char", "c", sw.toString());
055: }
056:
057: /**
058: * @tests java.io.StringWriter#getBuffer()
059: */
060: public void test_getBuffer() {
061: // Test for method java.lang.StringBuffer
062: // java.io.StringWriter.getBuffer()
063:
064: sw.write("This is a test string");
065: StringBuffer sb = sw.getBuffer();
066: assertEquals("Incorrect buffer returned",
067: "This is a test string", sb.toString());
068: }
069:
070: /**
071: * @tests java.io.StringWriter#toString()
072: */
073: public void test_toString() {
074: // Test for method java.lang.String java.io.StringWriter.toString()
075: sw.write("This is a test string");
076: assertEquals("Incorrect string returned",
077: "This is a test string", sw.toString());
078: }
079:
080: /**
081: * @tests java.io.StringWriter#write(char[], int, int)
082: */
083: public void test_write$CII() {
084: // Test for method void java.io.StringWriter.write(char [], int, int)
085: char[] c = new char[1000];
086: "This is a test string".getChars(0, 21, c, 0);
087: sw.write(c, 0, 21);
088: assertEquals("Chars not written properly",
089: "This is a test string", sw.toString());
090: }
091:
092: /**
093: * @tests java.io.StringWriter#write(char[], int, int)
094: * Regression for HARMONY-387
095: */
096: public void test_write$CII_2() {
097: StringWriter obj = null;
098: try {
099: obj = new StringWriter();
100: obj.write(new char[0], (int) 0, (int) -1);
101: fail("IndexOutOfBoundsException expected");
102: } catch (IndexOutOfBoundsException t) {
103: assertEquals(
104: "IndexOutOfBoundsException rather than a subclass expected",
105: IndexOutOfBoundsException.class, t.getClass());
106: }
107: }
108:
109: /**
110: * @tests java.io.StringWriter#write(char[], int, int)
111: */
112: public void test_write$CII_3() {
113: StringWriter obj = null;
114: try {
115: obj = new StringWriter();
116: obj.write(new char[0], (int) -1, (int) 0);
117: fail("IndexOutOfBoundsException expected");
118: } catch (ArrayIndexOutOfBoundsException t) {
119: fail("IndexOutOfBoundsException expected");
120: } catch (IndexOutOfBoundsException t) {
121: }
122: }
123:
124: /**
125: * @tests java.io.StringWriter#write(char[], int, int)
126: */
127: public void test_write$CII_4() {
128: StringWriter obj = null;
129: try {
130: obj = new StringWriter();
131: obj.write(new char[0], (int) -1, (int) -1);
132: fail("IndexOutOfBoundsException expected");
133: } catch (ArrayIndexOutOfBoundsException t) {
134: fail("IndexOutOfBoundsException expected");
135: } catch (IndexOutOfBoundsException t) {
136: }
137: }
138:
139: /**
140: * @tests java.io.StringWriter#write(int)
141: */
142: public void test_writeI() {
143: // Test for method void java.io.StringWriter.write(int)
144: sw.write('c');
145: assertEquals("Char not written properly", "c", sw.toString());
146: }
147:
148: /**
149: * @tests java.io.StringWriter#write(java.lang.String)
150: */
151: public void test_writeLjava_lang_String() {
152: // Test for method void java.io.StringWriter.write(java.lang.String)
153: sw.write("This is a test string");
154: assertEquals("String not written properly",
155: "This is a test string", sw.toString());
156: }
157:
158: /**
159: * @tests java.io.StringWriter#write(java.lang.String, int, int)
160: */
161: public void test_writeLjava_lang_StringII() {
162: // Test for method void java.io.StringWriter.write(java.lang.String,
163: // int, int)
164: sw.write("This is a test string", 2, 2);
165: assertEquals("String not written properly", "is", sw.toString());
166: }
167:
168: /**
169: * @tests java.io.StringWriter#append(char)
170: */
171: public void test_appendChar() throws IOException {
172: char testChar = ' ';
173: StringWriter stringWriter = new StringWriter(20);
174: stringWriter.append(testChar);
175: assertEquals(String.valueOf(testChar), stringWriter.toString());
176: stringWriter.close();
177: }
178:
179: /**
180: * @tests java.io.PrintWriter#append(CharSequence)
181: */
182: public void test_appendCharSequence() throws IOException {
183:
184: String testString = "My Test String";
185: StringWriter stringWriter = new StringWriter(20);
186: stringWriter.append(testString);
187: assertEquals(String.valueOf(testString), stringWriter
188: .toString());
189: stringWriter.close();
190: }
191:
192: /**
193: * @tests java.io.PrintWriter#append(CharSequence, int, int)
194: */
195: public void test_appendCharSequenceIntInt() throws IOException {
196: String testString = "My Test String";
197: StringWriter stringWriter = new StringWriter(20);
198: stringWriter.append(testString, 1, 3);
199: assertEquals(testString.substring(1, 3), stringWriter
200: .toString());
201: stringWriter.close();
202:
203: }
204:
205: /**
206: * Sets up the fixture, for example, open a network connection. This method
207: * is called before a test is executed.
208: */
209: protected void setUp() {
210:
211: sw = new StringWriter();
212: }
213:
214: /**
215: * Tears down the fixture, for example, close a network connection. This
216: * method is called after a test is executed.
217: */
218: protected void tearDown() {
219: }
220: }
|