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.BufferedWriter;
021: import java.io.IOException;
022: import java.io.Writer;
023:
024: import tests.support.Support_StringWriter;
025:
026: public class BufferedWriterTest extends junit.framework.TestCase {
027:
028: BufferedWriter bw;
029:
030: Support_StringWriter sw;
031:
032: public String testString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_java_io_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
033:
034: /**
035: * @tests java.io.BufferedWriter#BufferedWriter(java.io.Writer)
036: */
037: public void test_ConstructorLjava_io_Writer() {
038: sw = new Support_StringWriter();
039: bw = new BufferedWriter(sw);
040: sw.write("Hi");
041: assertEquals("Constructor failed", "Hi", sw.toString());
042: }
043:
044: /**
045: * @tests java.io.BufferedWriter#BufferedWriter(java.io.Writer, int)
046: */
047: public void test_ConstructorLjava_io_WriterI() {
048: assertTrue("Used in tests", true);
049: }
050:
051: private static class MockWriter extends Writer {
052: StringBuffer sb = new StringBuffer();
053: boolean flushCalled = false;
054:
055: public void write(char[] buf, int off, int len)
056: throws IOException {
057: for (int i = off; i < off + len; i++) {
058: sb.append(buf[i]);
059: }
060: }
061:
062: public void close() throws IOException {
063: // Empty
064: }
065:
066: public void flush() throws IOException {
067: flushCalled = true;
068: }
069:
070: public String getWritten() {
071: return sb.toString();
072: }
073:
074: public boolean isFlushCalled() {
075: return flushCalled;
076: }
077: }
078:
079: /**
080: * @tests java.io.BufferedWriter#close()
081: */
082: public void test_close() throws IOException {
083: try {
084: bw.close();
085: bw.write(testString);
086: fail("Writing to a closed stream should throw IOException");
087: } catch (IOException e) {
088: // Expected
089: }
090: assertTrue("Write after close", !sw.toString().equals(
091: testString));
092:
093: // Regression test for HARMONY-4178
094: MockWriter mw = new MockWriter();
095: BufferedWriter bw = new BufferedWriter(mw);
096: bw.write('a');
097: bw.close();
098:
099: // flush should not be called on underlying stream
100: assertFalse("Flush was called in the underlying stream", mw
101: .isFlushCalled());
102:
103: // on the other hand the BufferedWriter itself should flush the
104: // buffer
105: assertEquals("BufferdWriter do not flush itself before close",
106: "a", mw.getWritten());
107: }
108:
109: /**
110: * @tests java.io.BufferedWriter#flush()
111: */
112: public void test_flush() throws Exception {
113: bw.write("This should not cause a flush");
114: assertTrue("Bytes written without flush", sw.toString().equals(
115: ""));
116: bw.flush();
117: assertEquals("Bytes not flushed",
118: "This should not cause a flush", sw.toString());
119: }
120:
121: /**
122: * @tests java.io.BufferedWriter#newLine()
123: */
124: public void test_newLine() throws Exception {
125: String separator = System.getProperty("line.separator");
126: bw.write("Hello");
127: bw.newLine();
128: bw.write("World");
129: bw.flush();
130: assertTrue("Incorrect string written: " + sw.toString(), sw
131: .toString().equals("Hello" + separator + "World"));
132: }
133:
134: /**
135: * @tests java.io.BufferedWriter#write(char[], int, int)
136: */
137: public void test_write$CII() throws Exception {
138: char[] testCharArray = testString.toCharArray();
139: bw.write(testCharArray, 500, 1000);
140: bw.flush();
141: assertTrue("Incorrect string written", sw.toString().equals(
142: testString.substring(500, 1500)));
143: }
144:
145: /**
146: * @tests java.io.BufferedWriter#write(char[], int, int)
147: */
148: public void test_write_$CII_Exception() throws IOException {
149: BufferedWriter bWriter = new BufferedWriter(sw);
150: char[] nullCharArray = null;
151:
152: try {
153: bWriter.write(nullCharArray, -1, -1);
154: fail("should throw IndexOutOfBoundsException");
155: } catch (IndexOutOfBoundsException e) {
156: // expected
157: }
158:
159: try {
160: bWriter.write(nullCharArray, -1, 0);
161: fail("should throw IndexOutOfBoundsException");
162: } catch (IndexOutOfBoundsException e) {
163: // expected
164: }
165:
166: try {
167: bWriter.write(nullCharArray, 0, -1);
168: fail("should throw NullPointerException");
169: } catch (NullPointerException e) {
170: // expected
171: }
172:
173: try {
174: bWriter.write(nullCharArray, 0, 0);
175: fail("should throw NullPointerException");
176: } catch (NullPointerException e) {
177: // expected
178: }
179:
180: char[] testCharArray = testString.toCharArray();
181:
182: bWriter.write(testCharArray, 0, 0);
183:
184: bWriter.write(testCharArray, testCharArray.length, 0);
185:
186: try {
187: bWriter.write(testCharArray, testCharArray.length + 1, 0);
188: fail("should throw IndexOutOfBoundsException");
189: } catch (IndexOutOfBoundsException e) {
190: // expected
191: }
192:
193: bWriter.close();
194:
195: try {
196: bWriter.write(nullCharArray, -1, -1);
197: fail("should throw IOException");
198: } catch (IOException e) {
199: // expected
200: }
201: }
202:
203: /**
204: * @tests java.io.BufferedWriter#write(int)
205: */
206: public void test_writeI() throws Exception {
207: bw.write('T');
208: assertTrue("Char written without flush", sw.toString().equals(
209: ""));
210: bw.flush();
211: assertEquals("Incorrect char written", "T", sw.toString());
212: }
213:
214: /**
215: * @tests java.io.BufferedWriter#write(java.lang.String, int, int)
216: */
217: public void test_writeLjava_lang_StringII() throws Exception {
218: bw.write(testString);
219: bw.flush();
220: assertTrue("Incorrect string written", sw.toString().equals(
221: testString));
222: }
223:
224: /**
225: * @tests java.io.BufferedWriter#write(java.lang.String, int, int)
226: */
227: public void test_write_LStringII_Exception() throws IOException {
228: BufferedWriter bWriter = new BufferedWriter(sw);
229:
230: bWriter.write((String) null, -1, -1);
231: bWriter.write((String) null, -1, 0);
232: bWriter.write((String) null, 0, -1);
233: bWriter.write((String) null, 0, 0);
234:
235: try {
236: bWriter.write((String) null, -1, 1);
237: fail("should throw NullPointerException");
238: } catch (NullPointerException e) {
239: // expected
240: }
241:
242: bWriter.write(testString, 0, 0);
243: bWriter.write(testString, testString.length(), 0);
244: bWriter.write(testString, testString.length() + 1, 0);
245:
246: try {
247: bWriter.write(testString, testString.length() + 1, 1);
248: fail("should throw StringIndexOutOfBoundsException");
249: } catch (StringIndexOutOfBoundsException e) {
250: // expected
251: }
252:
253: bWriter.close();
254:
255: try {
256: bWriter.write((String) null, -1, -1);
257: fail("should throw IOException");
258: } catch (IOException e) {
259: // expected
260: }
261:
262: try {
263: bWriter.write((String) null, -1, 1);
264: fail("should throw IOException");
265: } catch (IOException e) {
266: // expected
267: }
268:
269: try {
270: bWriter.write(testString, -1, -1);
271: fail("should throw IOException");
272: } catch (IOException e) {
273: // expected
274: }
275: }
276:
277: /**
278: * Sets up the fixture, for example, open a network connection. This method
279: * is called before a test is executed.
280: */
281: protected void setUp() {
282: sw = new Support_StringWriter();
283: bw = new BufferedWriter(sw, 500);
284: }
285:
286: /**
287: * Tears down the fixture, for example, close a network connection. This
288: * method is called after a test is executed.
289: */
290: protected void tearDown() {
291: try {
292: bw.close();
293: } catch (Exception e) {
294: }
295: }
296: }
|