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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.luni.tests.java.io;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
23 import java.io.FileInputStream;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.io.OutputStreamWriter;
29 import java.io.UnsupportedEncodingException;
30 import java.nio.charset.Charset;
31 import java.nio.charset.CharsetEncoder;
33 import junit.framework.TestCase;
35 public class OutputStreamWriterTest extends TestCase {
37 private static final int UPPER = 0xd800;
39 private static final int BUFFER_SIZE = 10000;
41 private ByteArrayOutputStream out;
43 private OutputStreamWriter writer;
45 static private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
47 static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII",
48 "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" };
50 OutputStreamWriter osw;
52 InputStreamReader isr;
54 private ByteArrayOutputStream fos;
56 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\n";
59 * @see TestCase#setUp()
62 protected void setUp() throws Exception {
64 out = new ByteArrayOutputStream();
65 writer = new OutputStreamWriter(out, "utf-8");
67 fos = new ByteArrayOutputStream();
68 osw = new OutputStreamWriter(fos);
72 * @see TestCase#tearDown()
75 protected void tearDown() throws Exception {
83 } catch (Exception e) {
90 public void testClose() throws Exception {
96 } catch (IOException e) {
101 public void testFlush() throws Exception {
102 writer.write(source);
104 String result = out.toString("utf-8");
105 assertEquals(source, result);
109 * Class under test for void write(char[], int, int)
111 public void testWritecharArrayintint() throws IOException {
112 char[] chars = source.toCharArray();
114 // Throws IndexOutOfBoundsException if offset is negative
116 writer.write((char[]) null, -1, -1);
117 fail("should throw IndexOutOfBoundsException");
118 } catch (IndexOutOfBoundsException e) {
122 // throws NullPointerException though count is negative
124 writer.write((char[]) null, 1, -1);
125 fail("should throw NullPointerException");
126 } catch (NullPointerException e) {
131 writer.write((char[]) null, 1, 1);
133 } catch (NullPointerException e) {
137 writer.write(new char[0], 0, 1);
139 } catch (IndexOutOfBoundsException e) {
143 writer.write(chars, -1, 1);
145 } catch (IndexOutOfBoundsException e) {
149 writer.write(chars, 0, -1);
151 } catch (IndexOutOfBoundsException e) {
155 writer.write(chars, 1, chars.length);
157 } catch (IndexOutOfBoundsException e) {
160 writer.write(chars, 1, 2);
162 assertEquals("hi", out.toString("utf-8"));
163 writer.write(chars, 0, chars.length);
165 assertEquals("hi" + source, out.toString("utf-8"));
168 // After the stream is closed, should throw IOException first
170 writer.write((char[]) null, -1, -1);
171 fail("should throw IOException");
172 } catch (IOException e) {
178 * Class under test for void write(int)
180 public void testWriteint() throws IOException {
183 String str = new String(out.toByteArray(), "utf-8");
184 assertEquals("\u0001", str);
188 str = new String(out.toByteArray(), "utf-8");
189 assertEquals("\u0001\u0002", str);
193 str = new String(out.toByteArray(), "utf-8");
194 assertEquals("\u0001\u0002\uffff", str);
196 writer.write(0xfedcb);
198 str = new String(out.toByteArray(), "utf-8");
199 assertEquals("\u0001\u0002\uffff\uedcb", str);
202 // After the stream is closed, should throw IOException
205 fail("should throw IOException");
206 } catch (IOException e) {
212 * Class under test for void write(String, int, int)
214 public void testWriteStringintint() throws IOException {
216 writer.write((String) null, 1, 1);
218 } catch (NullPointerException e) {
222 writer.write("", 0, 1);
224 } catch (StringIndexOutOfBoundsException e) {
228 writer.write("abc", -1, 1);
230 } catch (StringIndexOutOfBoundsException e) {
234 writer.write("abc", 0, -1);
236 } catch (IndexOutOfBoundsException e) {
240 writer.write("abc", 1, 3);
242 } catch (StringIndexOutOfBoundsException e) {
246 // Throws IndexOutOfBoundsException before NullPointerException if count
249 writer.write((String) null, -1, -1);
250 fail("should throw IndexOutOfBoundsException");
251 } catch (IndexOutOfBoundsException e) {
255 // Throws NullPointerException before StringIndexOutOfBoundsException
257 writer.write((String) null, -1, 0);
258 fail("should throw NullPointerException");
259 } catch (NullPointerException e) {
263 writer.write("abc", 1, 2);
265 assertEquals("bc", out.toString("utf-8"));
266 writer.write(source, 0, source.length());
268 assertEquals("bc" + source, out.toString("utf-8"));
271 // Throws IndexOutOfBoundsException first if count is negative
273 writer.write((String) null, 0, -1);
274 fail("should throw IndexOutOfBoundsException");
275 } catch (IndexOutOfBoundsException e) {
280 writer.write((String) null, -1, 0);
281 fail("should throw NullPointerException");
282 } catch (NullPointerException e) {
287 writer.write("abc", -1, 0);
288 fail("should throw StringIndexOutOfBoundsException");
289 } catch (StringIndexOutOfBoundsException e) {
293 // Throws IOException
295 writer.write("abc", 0, 1);
296 fail("should throw IOException");
297 } catch (IOException e) {
303 * Class under test for void OutputStreamWriter(OutputStream)
305 public void testOutputStreamWriterOutputStream() throws IOException {
307 writer = new OutputStreamWriter(null);
309 } catch (NullPointerException e) {
312 OutputStreamWriter writer2 = new OutputStreamWriter(out);
317 * Class under test for void OutputStreamWriter(OutputStream, String)
319 public void testOutputStreamWriterOutputStreamString() throws IOException {
321 writer = new OutputStreamWriter(null, "utf-8");
323 } catch (NullPointerException e) {
327 writer = new OutputStreamWriter(out, "");
329 } catch (UnsupportedEncodingException e) {
333 writer = new OutputStreamWriter(out, "badname");
335 } catch (UnsupportedEncodingException e) {
339 writer = new OutputStreamWriter(out, (String) null);
341 } catch (NullPointerException e) {
344 OutputStreamWriter writer2 = new OutputStreamWriter(out, "ascii");
345 assertEquals(Charset.forName("ascii"), Charset.forName(writer2
351 * Class under test for void OutputStreamWriter(OutputStream)
353 public void testOutputStreamWriterOutputStreamCharset() throws IOException {
354 Charset cs = Charset.forName("ascii");
356 writer = new OutputStreamWriter(null, cs);
358 } catch (NullPointerException e) {
362 writer = new OutputStreamWriter(out, (Charset) null);
364 } catch (NullPointerException e) {
367 OutputStreamWriter writer2 = new OutputStreamWriter(out, cs);
368 assertEquals(cs, Charset.forName(writer2.getEncoding()));
373 * Class under test for void OutputStreamWriter(OutputStream, String)
375 public void testOutputStreamWriterOutputStreamCharsetEncoder()
377 Charset cs = Charset.forName("ascii");
378 CharsetEncoder enc = cs.newEncoder();
380 writer = new OutputStreamWriter(null, enc);
382 } catch (NullPointerException e) {
386 writer = new OutputStreamWriter(out, (CharsetEncoder) null);
388 } catch (NullPointerException e) {
391 OutputStreamWriter writer2 = new OutputStreamWriter(out, enc);
392 assertEquals(cs, Charset.forName(writer2.getEncoding()));
396 public void testGetEncoding() {
397 Charset cs = Charset.forName("utf-8");
398 assertEquals(cs, Charset.forName(writer.getEncoding()));
401 public void testHandleEarlyEOFChar_1() throws IOException {
402 String str = "All work and no play makes Jack a dull boy\n";
405 int len = str.length() * NUMBER;
406 char[] strChars = new char[len];
407 for (int i = 0; i < NUMBER; ++i) {
408 for (int k = 0; k < str.length(); ++k) {
409 strChars[j++] = str.charAt(k);
413 File f = File.createTempFile("one", "by_one");
415 FileWriter fw = new FileWriter(f);
418 FileInputStream fis = new FileInputStream(f);
419 InputStreamReader in = new InputStreamReader(fis);
420 for (int offset = 0; offset < strChars.length; ++offset) {
422 assertFalse("Early EOF at offset", -1 == b);
426 public void testHandleEarlyEOFChar_2() throws IOException {
427 int capacity = 65536;
428 byte[] bytes = new byte[capacity];
429 byte[] bs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
430 for (int i = 0; i < bytes.length; i++) {
431 bytes[i] = bs[i / 8192];
433 String inputStr = new String(bytes);
434 int len = inputStr.length();
435 File f = File.createTempFile("FileWriterBugTest ", null);
437 FileWriter writer = new FileWriter(f);
438 writer.write(inputStr);
440 long flen = f.length();
442 FileReader reader = new FileReader(f);
443 char[] outChars = new char[capacity];
444 int outCount = reader.read(outChars);
445 String outStr = new String(outChars, 0, outCount);
447 assertEquals(len, flen);
448 assertEquals(inputStr, outStr);
451 public void testSingleCharIO() throws Exception {
452 InputStreamReader isr = null;
453 for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
455 out = new ByteArrayOutputStream();
456 writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
468 for (int c = 0; c < upper; ++c) {
472 byte[] result = out.toByteArray();
474 isr = new InputStreamReader(new ByteArrayInputStream(result),
475 MINIMAL_CHARSETS[i]);
476 for (int expected = 0; expected < upper; ++expected) {
477 assertEquals("Error when reading bytes in "
478 + MINIMAL_CHARSETS[i], expected, isr.read());
483 } catch (Exception e) {
487 } catch (Exception e) {
493 public void testBlockIO() throws Exception {
494 InputStreamReader isr = null;
495 char[] largeBuffer = new char[BUFFER_SIZE];
496 for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
498 out = new ByteArrayOutputStream();
499 writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
512 for (int c = 0; c < upper; ++c) {
513 largeBuffer[m++] = (char) c;
514 if (m == BUFFER_SIZE) {
515 writer.write(largeBuffer);
519 writer.write(largeBuffer, 0, m);
521 byte[] result = out.toByteArray();
523 isr = new InputStreamReader(new ByteArrayInputStream(result),
524 MINIMAL_CHARSETS[i]);
525 int expected = 0, read = 0, j = 0;
526 while (expected < upper) {
528 read = isr.read(largeBuffer);
531 assertEquals("Error when reading bytes in "
532 + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]);
537 } catch (Exception e) {
541 } catch (Exception e) {
548 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
550 public void test_ConstructorLjava_io_OutputStream() {
551 assertTrue("Used in tests", true);
555 * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
558 public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
559 throws UnsupportedEncodingException {
560 osw = new OutputStreamWriter(fos, "8859_1");
562 osw = new OutputStreamWriter(fos, "Bogus");
563 fail("Failed to throw Unsupported Encoding exception");
564 } catch (UnsupportedEncodingException e) {
570 * @tests java.io.OutputStreamWriter#close()
572 public void test_close() throws IOException {
576 osw.write(testString, 0, testString.length());
577 fail("Chars written after close");
578 } catch (IOException e) {
582 ByteArrayOutputStream bout = new ByteArrayOutputStream();
584 OutputStreamWriter writer = new OutputStreamWriter(bout,
586 writer.write(new char[] { 'a' });
588 // the default is ASCII, there should not be any mode changes
589 String converted = new String(bout.toByteArray(), "ISO8859_1");
590 assertTrue("invalid conversion 1: " + converted, converted
594 writer = new OutputStreamWriter(bout, "ISO2022JP");
595 writer.write(new char[] { '\u3048' });
597 // the byte sequence should not switch to ASCII mode until the
599 converted = new String(bout.toByteArray(), "ISO8859_1");
600 assertTrue("invalid conversion 2: " + converted, converted
601 .equals("\u001b$B$("));
603 converted = new String(bout.toByteArray(), "ISO8859_1");
604 assertTrue("invalid conversion 3: " + converted, converted
605 .equals("\u001b$B$(\u001b(B"));
608 writer = new OutputStreamWriter(bout, "ISO2022JP");
609 writer.write(new char[] { '\u3048' });
610 writer.write(new char[] { '\u3048' });
612 // there should not be a mode switch between writes
613 assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B",
614 new String(bout.toByteArray(), "ISO8859_1"));
615 } catch (UnsupportedEncodingException e) {
616 // Can't test missing converter
617 System.out.println(e);
622 * @tests java.io.OutputStreamWriter#flush()
624 public void test_flush() throws IOException {
625 char[] buf = new char[testString.length()];
626 osw.write(testString, 0, testString.length());
629 isr.read(buf, 0, buf.length);
630 assertTrue("Chars not flushed", new String(buf, 0, buf.length)
631 .equals(testString));
635 * @tests java.io.OutputStreamWriter#getEncoding()
637 public void test_getEncoding() throws IOException {
639 osw = new OutputStreamWriter(fos, "8859_1");
640 } catch (UnsupportedEncodingException e) {
641 assertEquals("Returned incorrect encoding", "8859_1", osw
645 OutputStreamWriter out = new OutputStreamWriter(
646 new ByteArrayOutputStream(), "UTF-16BE");
649 String result = out.getEncoding();
654 out = new OutputStreamWriter(new ByteArrayOutputStream(),
656 } catch (UnsupportedEncodingException e) {
659 result = out.getEncoding();
660 assertEquals("UnicodeBigUnmarked", result);
664 * @tests java.io.OutputStreamWriter#write(char[], int, int)
666 public void test_write$CII() throws IOException {
667 char[] buf = new char[testString.length()];
668 osw.write(testString, 0, testString.length());
671 isr.read(buf, 0, buf.length);
672 assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
673 .equals(testString));
677 * @tests java.io.OutputStreamWriter#write(int)
679 public void test_writeI() throws IOException {
684 assertEquals("Incorrect char returned", 'T', (char) c);
688 * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int)
690 public void test_writeLjava_lang_StringII() throws IOException {
691 char[] buf = new char[testString.length()];
692 osw.write(testString, 0, testString.length());
696 assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
697 .equals(testString));
700 private void openInputStream() {
701 isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()));