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.CharArrayReader;
021: import java.io.CharArrayWriter;
022: import java.io.IOException;
023: import java.io.StringWriter;
024:
025: public class CharArrayWriterTest extends junit.framework.TestCase {
026:
027: char[] hw = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' };
028:
029: CharArrayWriter cw;
030:
031: CharArrayReader cr;
032:
033: /**
034: * @tests java.io.CharArrayWriter#CharArrayWriter()
035: */
036: public void test_Constructor() {
037: cw = new CharArrayWriter(90);
038: assertEquals("Created incorrect writer", 0, cw.size());
039: }
040:
041: /**
042: * @tests java.io.CharArrayWriter#CharArrayWriter(int)
043: */
044: public void test_ConstructorI() {
045: cw = new CharArrayWriter();
046: assertEquals("Created incorrect writer", 0, cw.size());
047: }
048:
049: /**
050: * @tests java.io.CharArrayWriter#close()
051: */
052: public void test_close() {
053: cw.close();
054: }
055:
056: /**
057: * @tests java.io.CharArrayWriter#flush()
058: */
059: public void test_flush() {
060: cw.flush();
061: }
062:
063: /**
064: * @tests java.io.CharArrayWriter#reset()
065: */
066: public void test_reset() throws IOException {
067: cw.write("HelloWorld", 5, 5);
068: cw.reset();
069: cw.write("HelloWorld", 0, 5);
070: cr = new CharArrayReader(cw.toCharArray());
071: char[] c = new char[100];
072: cr.read(c, 0, 5);
073: assertEquals("Reset failed to reset buffer", "Hello",
074: new String(c, 0, 5));
075: }
076:
077: /**
078: * @tests java.io.CharArrayWriter#size()
079: */
080: public void test_size() {
081: assertEquals("Returned incorrect size", 0, cw.size());
082: cw.write(hw, 5, 5);
083: assertEquals("Returned incorrect size", 5, cw.size());
084: }
085:
086: /**
087: * @tests java.io.CharArrayWriter#toCharArray()
088: */
089: public void test_toCharArray() throws IOException {
090: cw.write("HelloWorld", 0, 10);
091: cr = new CharArrayReader(cw.toCharArray());
092: char[] c = new char[100];
093: cr.read(c, 0, 10);
094: assertEquals("toCharArray failed to return correct array",
095: "HelloWorld", new String(c, 0, 10));
096: }
097:
098: /**
099: * @tests java.io.CharArrayWriter#toString()
100: */
101: public void test_toString() {
102: cw.write("HelloWorld", 5, 5);
103: cr = new CharArrayReader(cw.toCharArray());
104: assertEquals("Returned incorrect string", "World", cw
105: .toString());
106: }
107:
108: /**
109: * @tests java.io.CharArrayWriter#write(char[], int, int)
110: */
111: public void test_write$CII() throws IOException {
112: cw.write(hw, 5, 5);
113: cr = new CharArrayReader(cw.toCharArray());
114: char[] c = new char[100];
115: cr.read(c, 0, 5);
116: assertEquals("Writer failed to write correct chars", "World",
117: new String(c, 0, 5));
118: }
119:
120: /**
121: * @tests java.io.CharArrayWriter#write(char[], int, int)
122: */
123: public void test_write$CII_2() {
124: // Regression for HARMONY-387
125: CharArrayWriter obj = new CharArrayWriter();
126: try {
127: obj.write(new char[] { '0' }, 0, -1);
128: fail("IndexOutOfBoundsException expected");
129: } catch (IndexOutOfBoundsException t) {
130: assertEquals(
131: "IndexOutOfBoundsException rather than a subclass expected",
132: IndexOutOfBoundsException.class, t.getClass());
133: }
134: }
135:
136: /**
137: * @tests java.io.CharArrayWriter#write(int)
138: */
139: public void test_writeI() throws IOException {
140: cw.write('T');
141: cr = new CharArrayReader(cw.toCharArray());
142: assertEquals("Writer failed to write char", 'T', cr.read());
143: }
144:
145: /**
146: * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
147: */
148: public void test_writeLjava_lang_StringII() throws IOException {
149: cw.write("HelloWorld", 5, 5);
150: cr = new CharArrayReader(cw.toCharArray());
151: char[] c = new char[100];
152: cr.read(c, 0, 5);
153: assertEquals("Writer failed to write correct chars", "World",
154: new String(c, 0, 5));
155: }
156:
157: /**
158: * @tests java.io.CharArrayWriter#write(java.lang.String, int, int)
159: */
160: public void test_writeLjava_lang_StringII_2()
161: throws StringIndexOutOfBoundsException {
162: // Regression for HARMONY-387
163: CharArrayWriter obj = new CharArrayWriter();
164: try {
165: obj.write((String) null, -1, 0);
166: fail("NullPointerException expected");
167: } catch (NullPointerException t) {
168: // Expected
169: }
170: }
171:
172: /**
173: * @tests java.io.CharArrayWriter#writeTo(java.io.Writer)
174: */
175: public void test_writeToLjava_io_Writer() throws IOException {
176: cw.write("HelloWorld", 0, 10);
177: StringWriter sw = new StringWriter();
178: cw.writeTo(sw);
179: assertEquals("Writer failed to write correct chars",
180: "HelloWorld", sw.toString());
181: }
182:
183: /**
184: * Sets up the fixture, for example, open a network connection. This method
185: * is called before a test is executed.
186: */
187: protected void setUp() {
188: cw = new CharArrayWriter();
189: }
190:
191: /**
192: * Tears down the fixture, for example, close a network connection. This
193: * method is called after a test is executed.
194: */
195: protected void tearDown() {
196: if (cr != null) {
197: cr.close();
198: }
199: cw.close();
200: }
201:
202: /**
203: * @tests java.io.CharArrayWriter#append(char)
204: */
205: public void test_appendChar() throws IOException {
206: char testChar = ' ';
207: CharArrayWriter writer = new CharArrayWriter(10);
208: writer.append(testChar);
209: writer.flush();
210: assertEquals(String.valueOf(testChar), writer.toString());
211: writer.close();
212: }
213:
214: /**
215: * @tests java.io.CharArrayWriter#append(CharSequence)
216: */
217: public void test_appendCharSequence() {
218: String testString = "My Test String";
219: CharArrayWriter writer = new CharArrayWriter(10);
220: writer.append(testString);
221: writer.flush();
222: assertEquals(testString, writer.toString());
223: writer.close();
224: }
225:
226: /**
227: * @tests java.io.CharArrayWriter#append(CharSequence, int, int)
228: */
229: public void test_appendCharSequenceIntInt() {
230: String testString = "My Test String";
231: CharArrayWriter writer = new CharArrayWriter(10);
232: writer.append(testString, 1, 3);
233: writer.flush();
234: assertEquals(testString.substring(1, 3), writer.toString());
235: writer.close();
236: }
237: }
|