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.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileReader;
025: import java.io.FileWriter;
026: import java.io.IOException;
027: import java.io.InputStreamReader;
028: import java.io.OutputStreamWriter;
029: import java.io.UnsupportedEncodingException;
030: import java.nio.charset.Charset;
031: import java.nio.charset.CharsetEncoder;
032:
033: import junit.framework.TestCase;
034:
035: public class OutputStreamWriterTest extends TestCase {
036:
037: private static final int UPPER = 0xd800;
038:
039: private static final int BUFFER_SIZE = 10000;
040:
041: private ByteArrayOutputStream out;
042:
043: private OutputStreamWriter writer;
044:
045: static private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";
046:
047: static private final String[] MINIMAL_CHARSETS = new String[] {
048: "US-ASCII", "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16",
049: "UTF-8" };
050:
051: OutputStreamWriter osw;
052:
053: InputStreamReader isr;
054:
055: private ByteArrayOutputStream fos;
056:
057: 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";
058:
059: /*
060: * @see TestCase#setUp()
061: */
062: @Override
063: protected void setUp() throws Exception {
064: super .setUp();
065: out = new ByteArrayOutputStream();
066: writer = new OutputStreamWriter(out, "utf-8");
067:
068: fos = new ByteArrayOutputStream();
069: osw = new OutputStreamWriter(fos);
070: }
071:
072: /*
073: * @see TestCase#tearDown()
074: */
075: @Override
076: protected void tearDown() throws Exception {
077: try {
078: writer.close();
079:
080: if (isr != null) {
081: isr.close();
082: }
083: osw.close();
084: } catch (Exception e) {
085: // Ignored
086: }
087:
088: super .tearDown();
089: }
090:
091: public void testClose() throws Exception {
092: writer.flush();
093: writer.close();
094: try {
095: writer.flush();
096: fail();
097: } catch (IOException e) {
098: // Expected
099: }
100: }
101:
102: public void testFlush() throws Exception {
103: writer.write(source);
104: writer.flush();
105: String result = out.toString("utf-8");
106: assertEquals(source, result);
107: }
108:
109: /*
110: * Class under test for void write(char[], int, int)
111: */
112: public void testWritecharArrayintint() throws IOException {
113: char[] chars = source.toCharArray();
114:
115: // Throws IndexOutOfBoundsException if offset is negative
116: try {
117: writer.write((char[]) null, -1, -1);
118: fail("should throw IndexOutOfBoundsException");
119: } catch (IndexOutOfBoundsException e) {
120: // Expected
121: }
122:
123: // throws NullPointerException though count is negative
124: try {
125: writer.write((char[]) null, 1, -1);
126: fail("should throw NullPointerException");
127: } catch (NullPointerException e) {
128: // Expected
129: }
130:
131: try {
132: writer.write((char[]) null, 1, 1);
133: fail();
134: } catch (NullPointerException e) {
135: // Expected
136: }
137: try {
138: writer.write(new char[0], 0, 1);
139: fail();
140: } catch (IndexOutOfBoundsException e) {
141: // Expected
142: }
143: try {
144: writer.write(chars, -1, 1);
145: fail();
146: } catch (IndexOutOfBoundsException e) {
147: // Expected
148: }
149: try {
150: writer.write(chars, 0, -1);
151: fail();
152: } catch (IndexOutOfBoundsException e) {
153: // Expected
154: }
155: try {
156: writer.write(chars, 1, chars.length);
157: fail();
158: } catch (IndexOutOfBoundsException e) {
159: // Expected
160: }
161: writer.write(chars, 1, 2);
162: writer.flush();
163: assertEquals("hi", out.toString("utf-8"));
164: writer.write(chars, 0, chars.length);
165: writer.flush();
166: assertEquals("hi" + source, out.toString("utf-8"));
167:
168: writer.close();
169: // After the stream is closed, should throw IOException first
170: try {
171: writer.write((char[]) null, -1, -1);
172: fail("should throw IOException");
173: } catch (IOException e) {
174: // Expected
175: }
176: }
177:
178: /*
179: * Class under test for void write(int)
180: */
181: public void testWriteint() throws IOException {
182: writer.write(1);
183: writer.flush();
184: String str = new String(out.toByteArray(), "utf-8");
185: assertEquals("\u0001", str);
186:
187: writer.write(2);
188: writer.flush();
189: str = new String(out.toByteArray(), "utf-8");
190: assertEquals("\u0001\u0002", str);
191:
192: writer.write(-1);
193: writer.flush();
194: str = new String(out.toByteArray(), "utf-8");
195: assertEquals("\u0001\u0002\uffff", str);
196:
197: writer.write(0xfedcb);
198: writer.flush();
199: str = new String(out.toByteArray(), "utf-8");
200: assertEquals("\u0001\u0002\uffff\uedcb", str);
201:
202: writer.close();
203: // After the stream is closed, should throw IOException
204: try {
205: writer.write(1);
206: fail("should throw IOException");
207: } catch (IOException e) {
208: // expected
209: }
210: }
211:
212: /*
213: * Class under test for void write(String, int, int)
214: */
215: public void testWriteStringintint() throws IOException {
216: try {
217: writer.write((String) null, 1, 1);
218: fail();
219: } catch (NullPointerException e) {
220: // Expected
221: }
222: try {
223: writer.write("", 0, 1);
224: fail();
225: } catch (StringIndexOutOfBoundsException e) {
226: // Expected
227: }
228: try {
229: writer.write("abc", -1, 1);
230: fail();
231: } catch (StringIndexOutOfBoundsException e) {
232: // Expected
233: }
234: try {
235: writer.write("abc", 0, -1);
236: fail();
237: } catch (IndexOutOfBoundsException e) {
238: // Expected
239: }
240: try {
241: writer.write("abc", 1, 3);
242: fail();
243: } catch (StringIndexOutOfBoundsException e) {
244: // Expected
245: }
246:
247: // Throws IndexOutOfBoundsException before NullPointerException if count
248: // is negative
249: try {
250: writer.write((String) null, -1, -1);
251: fail("should throw IndexOutOfBoundsException");
252: } catch (IndexOutOfBoundsException e) {
253: // Expected
254: }
255:
256: // Throws NullPointerException before StringIndexOutOfBoundsException
257: try {
258: writer.write((String) null, -1, 0);
259: fail("should throw NullPointerException");
260: } catch (NullPointerException e) {
261: // expected
262: }
263:
264: writer.write("abc", 1, 2);
265: writer.flush();
266: assertEquals("bc", out.toString("utf-8"));
267: writer.write(source, 0, source.length());
268: writer.flush();
269: assertEquals("bc" + source, out.toString("utf-8"));
270:
271: writer.close();
272: // Throws IndexOutOfBoundsException first if count is negative
273: try {
274: writer.write((String) null, 0, -1);
275: fail("should throw IndexOutOfBoundsException");
276: } catch (IndexOutOfBoundsException e) {
277: // Expected
278: }
279:
280: try {
281: writer.write((String) null, -1, 0);
282: fail("should throw NullPointerException");
283: } catch (NullPointerException e) {
284: // Expected
285: }
286:
287: try {
288: writer.write("abc", -1, 0);
289: fail("should throw StringIndexOutOfBoundsException");
290: } catch (StringIndexOutOfBoundsException e) {
291: // Expected
292: }
293:
294: // Throws IOException
295: try {
296: writer.write("abc", 0, 1);
297: fail("should throw IOException");
298: } catch (IOException e) {
299: // expected
300: }
301: }
302:
303: /*
304: * Class under test for void OutputStreamWriter(OutputStream)
305: */
306: public void testOutputStreamWriterOutputStream() throws IOException {
307: try {
308: writer = new OutputStreamWriter(null);
309: fail();
310: } catch (NullPointerException e) {
311: // Expected
312: }
313: OutputStreamWriter writer2 = new OutputStreamWriter(out);
314: writer2.close();
315: }
316:
317: /*
318: * Class under test for void OutputStreamWriter(OutputStream, String)
319: */
320: public void testOutputStreamWriterOutputStreamString()
321: throws IOException {
322: try {
323: writer = new OutputStreamWriter(null, "utf-8");
324: fail();
325: } catch (NullPointerException e) {
326: // Expected
327: }
328: try {
329: writer = new OutputStreamWriter(out, "");
330: fail();
331: } catch (UnsupportedEncodingException e) {
332: // Expected
333: }
334: try {
335: writer = new OutputStreamWriter(out, "badname");
336: fail();
337: } catch (UnsupportedEncodingException e) {
338: // Expected
339: }
340: try {
341: writer = new OutputStreamWriter(out, (String) null);
342: fail();
343: } catch (NullPointerException e) {
344: // Expected
345: }
346: OutputStreamWriter writer2 = new OutputStreamWriter(out,
347: "ascii");
348: assertEquals(Charset.forName("ascii"), Charset.forName(writer2
349: .getEncoding()));
350: writer2.close();
351: }
352:
353: /*
354: * Class under test for void OutputStreamWriter(OutputStream)
355: */
356: public void testOutputStreamWriterOutputStreamCharset()
357: throws IOException {
358: Charset cs = Charset.forName("ascii");
359: try {
360: writer = new OutputStreamWriter(null, cs);
361: fail();
362: } catch (NullPointerException e) {
363: // Expected
364: }
365: try {
366: writer = new OutputStreamWriter(out, (Charset) null);
367: fail();
368: } catch (NullPointerException e) {
369: // Expected
370: }
371: OutputStreamWriter writer2 = new OutputStreamWriter(out, cs);
372: assertEquals(cs, Charset.forName(writer2.getEncoding()));
373: writer2.close();
374: }
375:
376: /*
377: * Class under test for void OutputStreamWriter(OutputStream, String)
378: */
379: public void testOutputStreamWriterOutputStreamCharsetEncoder()
380: throws IOException {
381: Charset cs = Charset.forName("ascii");
382: CharsetEncoder enc = cs.newEncoder();
383: try {
384: writer = new OutputStreamWriter(null, enc);
385: fail();
386: } catch (NullPointerException e) {
387: // Expected
388: }
389: try {
390: writer = new OutputStreamWriter(out, (CharsetEncoder) null);
391: fail();
392: } catch (NullPointerException e) {
393: // Expected
394: }
395: OutputStreamWriter writer2 = new OutputStreamWriter(out, enc);
396: assertEquals(cs, Charset.forName(writer2.getEncoding()));
397: writer2.close();
398: }
399:
400: public void testGetEncoding() {
401: Charset cs = Charset.forName("utf-8");
402: assertEquals(cs, Charset.forName(writer.getEncoding()));
403: }
404:
405: public void testHandleEarlyEOFChar_1() throws IOException {
406: String str = "All work and no play makes Jack a dull boy\n"; //$NON-NLS-1$
407: int NUMBER = 2048;
408: int j = 0;
409: int len = str.length() * NUMBER;
410: char[] strChars = new char[len];
411: for (int i = 0; i < NUMBER; ++i) {
412: for (int k = 0; k < str.length(); ++k) {
413: strChars[j++] = str.charAt(k);
414: }
415: }
416:
417: File f = File.createTempFile("one", "by_one");
418: FileWriter fw = new FileWriter(f);
419: fw.write(strChars);
420: fw.close();
421: FileInputStream fis = new FileInputStream(f);
422: InputStreamReader in = new InputStreamReader(fis);
423: for (int offset = 0; offset < strChars.length; ++offset) {
424: int b = in.read();
425: assertFalse("Early EOF at offset", -1 == b);
426: }
427: }
428:
429: public void testHandleEarlyEOFChar_2() throws IOException {
430: int capacity = 65536;
431: byte[] bytes = new byte[capacity];
432: byte[] bs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
433: for (int i = 0; i < bytes.length; i++) {
434: bytes[i] = bs[i / 8192];
435: }
436: String inputStr = new String(bytes);
437: int len = inputStr.length();
438: File f = File.createTempFile("FileWriterBugTest ", null); //$NON-NLS-1$
439: FileWriter writer = new FileWriter(f);
440: writer.write(inputStr);
441: writer.close();
442: long flen = f.length();
443:
444: FileReader reader = new FileReader(f);
445: char[] outChars = new char[capacity];
446: int outCount = reader.read(outChars);
447: String outStr = new String(outChars, 0, outCount);
448:
449: f.deleteOnExit();
450: assertEquals(len, flen);
451: assertEquals(inputStr, outStr);
452: }
453:
454: public void testSingleCharIO() throws Exception {
455: InputStreamReader isr = null;
456: for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
457: try {
458: out = new ByteArrayOutputStream();
459: writer = new OutputStreamWriter(out,
460: MINIMAL_CHARSETS[i]);
461:
462: int upper = UPPER;
463: switch (i) {
464: case 0:
465: upper = 128;
466: break;
467: case 1:
468: upper = 256;
469: break;
470: }
471:
472: for (int c = 0; c < upper; ++c) {
473: writer.write(c);
474: }
475: writer.flush();
476: byte[] result = out.toByteArray();
477:
478: isr = new InputStreamReader(new ByteArrayInputStream(
479: result), MINIMAL_CHARSETS[i]);
480: for (int expected = 0; expected < upper; ++expected) {
481: assertEquals("Error when reading bytes in "
482: + MINIMAL_CHARSETS[i], expected, isr.read());
483: }
484: } finally {
485: try {
486: isr.close();
487: } catch (Exception e) {
488: }
489: try {
490: writer.close();
491: } catch (Exception e) {
492: }
493: }
494: }
495: }
496:
497: public void testBlockIO() throws Exception {
498: InputStreamReader isr = null;
499: char[] largeBuffer = new char[BUFFER_SIZE];
500: for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
501: try {
502: out = new ByteArrayOutputStream();
503: writer = new OutputStreamWriter(out,
504: MINIMAL_CHARSETS[i]);
505:
506: int upper = UPPER;
507: switch (i) {
508: case 0:
509: upper = 128;
510: break;
511: case 1:
512: upper = 256;
513: break;
514: }
515:
516: int m = 0;
517: for (int c = 0; c < upper; ++c) {
518: largeBuffer[m++] = (char) c;
519: if (m == BUFFER_SIZE) {
520: writer.write(largeBuffer);
521: m = 0;
522: }
523: }
524: writer.write(largeBuffer, 0, m);
525: writer.flush();
526: byte[] result = out.toByteArray();
527:
528: isr = new InputStreamReader(new ByteArrayInputStream(
529: result), MINIMAL_CHARSETS[i]);
530: int expected = 0, read = 0, j = 0;
531: while (expected < upper) {
532: if (j == read) {
533: read = isr.read(largeBuffer);
534: j = 0;
535: }
536: assertEquals("Error when reading bytes in "
537: + MINIMAL_CHARSETS[i], expected++,
538: largeBuffer[j++]);
539: }
540: } finally {
541: try {
542: isr.close();
543: } catch (Exception e) {
544: }
545: try {
546: writer.close();
547: } catch (Exception e) {
548: }
549: }
550: }
551: }
552:
553: /**
554: * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
555: */
556: public void test_ConstructorLjava_io_OutputStream() {
557: assertTrue("Used in tests", true);
558: }
559:
560: /**
561: * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
562: * java.lang.String)
563: */
564: public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
565: throws UnsupportedEncodingException {
566: osw = new OutputStreamWriter(fos, "8859_1");
567: try {
568: osw = new OutputStreamWriter(fos, "Bogus");
569: fail("Failed to throw Unsupported Encoding exception");
570: } catch (UnsupportedEncodingException e) {
571: // Expected
572: }
573: }
574:
575: /**
576: * @tests java.io.OutputStreamWriter#close()
577: */
578: public void test_close() throws IOException {
579: osw.close();
580:
581: try {
582: osw.write(testString, 0, testString.length());
583: fail("Chars written after close");
584: } catch (IOException e) {
585: // Expected
586: }
587:
588: ByteArrayOutputStream bout = new ByteArrayOutputStream();
589: try {
590: OutputStreamWriter writer = new OutputStreamWriter(bout,
591: "ISO2022JP");
592: writer.write(new char[] { 'a' });
593: writer.close();
594: // the default is ASCII, there should not be any mode changes
595: String converted = new String(bout.toByteArray(),
596: "ISO8859_1");
597: assertTrue("invalid conversion 1: " + converted, converted
598: .equals("a"));
599:
600: bout.reset();
601: writer = new OutputStreamWriter(bout, "ISO2022JP");
602: writer.write(new char[] { '\u3048' });
603: writer.flush();
604: // the byte sequence should not switch to ASCII mode until the
605: // stream is closed
606: converted = new String(bout.toByteArray(), "ISO8859_1");
607: assertTrue("invalid conversion 2: " + converted, converted
608: .equals("\u001b$B$("));
609: writer.close();
610: converted = new String(bout.toByteArray(), "ISO8859_1");
611: assertTrue("invalid conversion 3: " + converted, converted
612: .equals("\u001b$B$(\u001b(B"));
613:
614: bout.reset();
615: writer = new OutputStreamWriter(bout, "ISO2022JP");
616: writer.write(new char[] { '\u3048' });
617: writer.write(new char[] { '\u3048' });
618: writer.close();
619: // there should not be a mode switch between writes
620: assertEquals("invalid conversion 4",
621: "\u001b$B$($(\u001b(B", new String(bout
622: .toByteArray(), "ISO8859_1"));
623: } catch (UnsupportedEncodingException e) {
624: // Can't test missing converter
625: System.out.println(e);
626: }
627: }
628:
629: /**
630: * @tests java.io.OutputStreamWriter#flush()
631: */
632: public void test_flush() throws IOException {
633: char[] buf = new char[testString.length()];
634: osw.write(testString, 0, testString.length());
635: osw.flush();
636: openInputStream();
637: isr.read(buf, 0, buf.length);
638: assertTrue("Chars not flushed", new String(buf, 0, buf.length)
639: .equals(testString));
640: }
641:
642: /**
643: * @tests java.io.OutputStreamWriter#getEncoding()
644: */
645: public void test_getEncoding() throws IOException {
646: try {
647: osw = new OutputStreamWriter(fos, "8859_1");
648: } catch (UnsupportedEncodingException e) {
649: assertEquals("Returned incorrect encoding", "8859_1", osw
650: .getEncoding());
651: }
652:
653: OutputStreamWriter out = new OutputStreamWriter(
654: new ByteArrayOutputStream(), "UTF-16BE");
655: out.close();
656:
657: String result = out.getEncoding();
658: assertNull(result);
659:
660: out = null;
661: try {
662: out = new OutputStreamWriter(new ByteArrayOutputStream(),
663: "UTF-16BE");
664: } catch (UnsupportedEncodingException e) {
665: // ok
666: }
667: result = out.getEncoding();
668: assertEquals("UnicodeBigUnmarked", result);
669: }
670:
671: /**
672: * @tests java.io.OutputStreamWriter#write(char[], int, int)
673: */
674: public void test_write$CII() throws IOException {
675: char[] buf = new char[testString.length()];
676: osw.write(testString, 0, testString.length());
677: osw.close();
678: openInputStream();
679: isr.read(buf, 0, buf.length);
680: assertTrue("Incorrect chars returned", new String(buf, 0,
681: buf.length).equals(testString));
682: }
683:
684: /**
685: * @tests java.io.OutputStreamWriter#write(int)
686: */
687: public void test_writeI() throws IOException {
688: osw.write('T');
689: osw.close();
690: openInputStream();
691: int c = isr.read();
692: assertEquals("Incorrect char returned", 'T', (char) c);
693: }
694:
695: /**
696: * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int)
697: */
698: public void test_writeLjava_lang_StringII() throws IOException {
699: char[] buf = new char[testString.length()];
700: osw.write(testString, 0, testString.length());
701: osw.close();
702: openInputStream();
703: isr.read(buf);
704: assertTrue("Incorrect chars returned", new String(buf, 0,
705: buf.length).equals(testString));
706: }
707:
708: private void openInputStream() {
709: isr = new InputStreamReader(new ByteArrayInputStream(fos
710: .toByteArray()));
711: }
712: }
|