1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package tests.api.java.io;
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.InputStreamReader;
29import java.io.OutputStreamWriter;
30import java.io.UnsupportedEncodingException;
31import java.nio.charset.Charset;
32import java.nio.charset.CharsetEncoder;
33
34import tests.support.Support_OutputStream;
35
36import junit.framework.TestCase;
37
38/**
39 *
40 */
41@TestTargetClass(OutputStreamWriter.class)
42public class OutputStreamWriterTest extends TestCase {
43
44    private static final int UPPER = 0xd800;
45
46    private static final int BUFFER_SIZE = 10000;
47
48    static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII",
49            "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" };
50
51    OutputStreamWriter osw;
52
53    InputStreamReader isr;
54
55    private Support_OutputStream fos;
56
57    public String testString = "This is a test message with Unicode characters. \u4e2d\u56fd is China's name in Chinese";
58
59    /*
60     * @see TestCase#setUp()
61     */
62    protected void setUp() throws Exception {
63        super.setUp();
64        fos = new Support_OutputStream(500);
65        osw = new OutputStreamWriter(fos, "UTF-8");
66    }
67
68    /*
69     * @see TestCase#tearDown()
70     */
71    protected void tearDown() throws Exception {
72        try {
73            if (isr != null) isr.close();
74            osw.close();
75        } catch (Exception e) {
76        }
77
78        super.tearDown();
79    }
80
81    /**
82     * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
83     */
84    @TestTargetNew(
85        level = TestLevel.COMPLETE,
86        notes = "",
87        method = "OutputStreamWriter",
88        args = {java.io.OutputStream.class}
89    )
90    public void test_ConstructorLjava_io_OutputStream() throws IOException {
91        OutputStreamWriter writer = null;
92
93        try {
94            writer = new OutputStreamWriter(null);
95            fail("Test 1: NullPointerException expected.");
96        } catch (NullPointerException e) {
97            // Expected
98        }
99
100        try {
101            writer = new OutputStreamWriter(new Support_OutputStream());
102        } catch (Exception e) {
103            fail("Test 2: Unexpected exception: " + e.getMessage());
104        }
105
106        // Test that the default encoding has been used.
107        assertEquals("Test 3: Incorrect default encoding used.",
108                     Charset.defaultCharset(),
109                     Charset.forName(writer.getEncoding()));
110
111        if (writer != null) writer.close();
112    }
113
114    /**
115     * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
116     *        java.lang.String)
117     */
118    @TestTargetNew(
119        level = TestLevel.COMPLETE,
120        notes = "",
121        method = "OutputStreamWriter",
122        args = {java.io.OutputStream.class, java.lang.String.class}
123    )
124    public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
125            throws UnsupportedEncodingException {
126
127        try {
128            osw = new OutputStreamWriter(null, "utf-8");
129            fail("Test 1: NullPointerException expected.");
130        } catch (NullPointerException e) {
131            // Expected
132        }
133
134        try {
135            osw = new OutputStreamWriter(fos, (String) null);
136            fail("Test 2: NullPointerException expected.");
137        } catch (NullPointerException e) {
138            // Expected
139        }
140
141        try {
142            osw = new OutputStreamWriter(fos, "");
143            fail("Test 3: UnsupportedEncodingException expected.");
144        } catch (UnsupportedEncodingException e) {
145            // Expected
146        }
147
148        try {
149            osw = new OutputStreamWriter(fos, "Bogus");
150            fail("Test 4: UnsupportedEncodingException expected.");
151        } catch (UnsupportedEncodingException e) {
152            // Expected
153        }
154
155        try {
156            osw = new OutputStreamWriter(fos, "8859_1");
157        } catch (UnsupportedEncodingException e) {
158            fail("Test 5: Unexpected UnsupportedEncodingException.");
159        }
160
161        assertEquals("Test 6: Encoding not set correctly. ",
162                     Charset.forName("8859_1"),
163                     Charset.forName(osw.getEncoding()));
164    }
165
166    /**
167     * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
168     *        java.nio.charset.Charset)
169     */
170    @TestTargetNew(
171        level = TestLevel.COMPLETE,
172        notes = "",
173        method = "OutputStreamWriter",
174        args = {java.io.OutputStream.class, java.nio.charset.Charset.class}
175    )
176    public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_Charset()
177            throws IOException {
178        OutputStreamWriter writer;
179        Support_OutputStream out = new Support_OutputStream();
180        Charset cs = Charset.forName("ascii");
181
182        try {
183            writer = new OutputStreamWriter(null, cs);
184            fail("Test 1: NullPointerException expected.");
185        } catch (NullPointerException e) {
186            // Expected
187        }
188
189        try {
190            writer = new OutputStreamWriter(out, (Charset) null);
191            fail("Test 2: NullPointerException expected.");
192        } catch (NullPointerException e) {
193            // Expected
194        }
195
196        writer = new OutputStreamWriter(out, cs);
197        assertEquals("Test 3: Encoding not set correctly. ",
198                     cs, Charset.forName(writer.getEncoding()));
199        writer.close();
200    }
201
202    /**
203     * @tests java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
204     *        java.nio.charset.CharsetEncoder)
205     */
206    @TestTargetNew(
207        level = TestLevel.COMPLETE,
208        notes = "",
209        method = "OutputStreamWriter",
210        args = {java.io.OutputStream.class, java.nio.charset.CharsetEncoder.class}
211    )
212    public void test_ConstructorLjava_io_OutputStreamLjava_nio_charset_CharsetEncoder()
213            throws IOException {
214        OutputStreamWriter writer;
215        Support_OutputStream out = new Support_OutputStream();
216        Charset cs = Charset.forName("ascii");
217        CharsetEncoder enc = cs.newEncoder();
218
219        try {
220            writer = new OutputStreamWriter(null, enc);
221            fail("Test 1: NullPointerException expected.");
222        } catch (NullPointerException e) {
223            // Expected
224        }
225
226        try {
227            writer = new OutputStreamWriter(out, (CharsetEncoder) null);
228            fail("Test 2: NullPointerException expected.");
229        } catch (NullPointerException e) {
230            // Expected
231        }
232
233        writer = new OutputStreamWriter(out, cs);
234        assertEquals("Test 3: CharacterEncoder not set correctly. ",
235                     cs, Charset.forName(writer.getEncoding()));
236        writer.close();
237    }
238
239    /**
240     * @tests java.io.OutputStreamWriter#close()
241     */
242    @TestTargetNew(
243        level = TestLevel.COMPLETE,
244        notes = "An issue in the API code has been identified (ticket #87). This test must be updated when the ticket is closed.",
245        method = "close",
246        args = {}
247    )
248    public void test_close() {
249
250        fos.setThrowsException(true);
251        try {
252            osw.close();
253            fail("Test 1: IOException expected.");
254        } catch (IOException e) {
255            // Expected.
256        }
257
258/* Test 2 does not work and has therefore been disabled (see Ticket #87).
259        // Test 2: Write should not fail since the closing
260        // in test 1 has not been successful.
261        try {
262            osw.write("Lorem ipsum...");
263        } catch (IOException e) {
264            fail("Test 2: Unexpected IOException.");
265        }
266
267        // Test 3: Close should succeed.
268        fos.setThrowsException(false);
269        try {
270            osw.close();
271        } catch (IOException e) {
272            fail("Test 3: Unexpected IOException.");
273        }
274*/
275
276        ByteArrayOutputStream bout = new ByteArrayOutputStream();
277        try {
278            OutputStreamWriter writer = new OutputStreamWriter(bout,
279                    "ISO2022JP");
280            writer.write(new char[] { 'a' });
281            writer.close();
282            // The default is ASCII, there should not be any mode changes.
283            String converted = new String(bout.toByteArray(), "ISO8859_1");
284            assertTrue("Test 4: Invalid conversion: " + converted,
285                       converted.equals("a"));
286
287            bout.reset();
288            writer = new OutputStreamWriter(bout, "ISO2022JP");
289            writer.write(new char[] { '\u3048' });
290            writer.flush();
291            // The byte sequence should not switch to ASCII mode until the
292            // stream is closed.
293            converted = new String(bout.toByteArray(), "ISO8859_1");
294            assertTrue("Test 5: Invalid conversion: " + converted,
295                       converted.equals("\u001b$B$("));
296            writer.close();
297            converted = new String(bout.toByteArray(), "ISO8859_1");
298            assertTrue("Test 6: Invalid conversion: " + converted,
299                       converted.equals("\u001b$B$(\u001b(B"));
300
301            bout.reset();
302            writer = new OutputStreamWriter(bout, "ISO2022JP");
303            writer.write(new char[] { '\u3048' });
304            writer.write(new char[] { '\u3048' });
305            writer.close();
306            // There should not be a mode switch between writes.
307            assertEquals("Test 7: Invalid conversion. ",
308                         "\u001b$B$($(\u001b(B",
309                         new String(bout.toByteArray(), "ISO8859_1"));
310        } catch (UnsupportedEncodingException e) {
311            // Can't test missing converter.
312            System.out.println(e);
313        } catch (IOException e) {
314            fail("Unexpected: " + e);
315        }
316    }
317
318    /**
319     * @tests java.io.OutputStreamWriter#flush()
320     */
321    @TestTargetNew(
322        level = TestLevel.COMPLETE,
323        notes = "",
324        method = "flush",
325        args = {}
326    )
327    public void test_flush() {
328        // Test for method void java.io.OutputStreamWriter.flush()
329        try {
330            char[] buf = new char[testString.length()];
331            osw.write(testString, 0, testString.length());
332            osw.flush();
333            openInputStream();
334            isr.read(buf, 0, buf.length);
335            assertTrue("Test 1: Characters have not been flushed.",
336                       new String(buf, 0, buf.length).equals(testString));
337        } catch (Exception e) {
338            fail("Test 1: Unexpected exception: " + e.getMessage());
339        }
340
341        fos.setThrowsException(true);
342        try {
343            osw.flush();
344            fail("Test 2: IOException expected.");
345        } catch (IOException e) {
346            // Expected
347        }
348        fos.setThrowsException(false);
349    }
350
351    @TestTargets({
352        @TestTargetNew(
353            level = TestLevel.PARTIAL_COMPLETE,
354            notes = "",
355            method = "write",
356            args = {int.class}
357        ),
358        @TestTargetNew(
359            level = TestLevel.PARTIAL_COMPLETE,
360            notes = "",
361            method = "read",
362            args = {},
363            clazz = InputStreamReader.class
364        )
365    })
366    public void test_singleCharIO() throws Exception {
367        int upper;
368        OutputStreamWriter writer = null;
369        ByteArrayOutputStream out;
370        InputStreamReader isr = null;
371
372        for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
373            try {
374                out = new ByteArrayOutputStream();
375                writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);
376
377                switch (i) {
378                case 0:
379                    upper = 128;
380                    break;
381                case 1:
382                    upper = 256;
383                    break;
384                default:
385                    upper = UPPER;
386                }
387
388                for (int c = 0; c < upper; ++c) {
389                    writer.write(c);
390                }
391                writer.flush();
392                byte[] result = out.toByteArray();
393
394                isr = new InputStreamReader(new ByteArrayInputStream(result),
395                        MINIMAL_CHARSETS[i]);
396                for (int expected = 0; expected < upper; ++expected) {
397                    assertEquals("Error when reading bytes in "
398                            + MINIMAL_CHARSETS[i], expected, isr.read());
399                }
400            } finally {
401                try {
402                    if (isr != null) isr.close();
403                } catch (Exception e) {
404                }
405                try {
406                    if (writer != null) writer.close();
407                } catch (Exception e) {
408                }
409            }
410        }
411    }
412
413    @TestTargets({
414        @TestTargetNew(
415                level = TestLevel.PARTIAL_COMPLETE,
416                method = "write",
417                args = {char[].class}
418        ),
419        @TestTargetNew(
420                level = TestLevel.PARTIAL_COMPLETE,
421                notes = "",
422                method = "read",
423                args = {},
424                clazz = InputStreamReader.class
425        )
426    })
427    public void test_write$C() throws Exception {
428        int upper;
429        InputStreamReader isr = null;
430        ByteArrayOutputStream baos = new ByteArrayOutputStream();
431        OutputStreamWriter writer = null;
432
433        char[] largeBuffer = new char[BUFFER_SIZE];
434        for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
435            try {
436                baos = new ByteArrayOutputStream();
437                writer = new OutputStreamWriter(baos, MINIMAL_CHARSETS[i]);
438
439                switch (i) {
440                case 0:
441                    upper = 128;
442                    break;
443                case 1:
444                    upper = 256;
445                    break;
446                default:
447                    upper = UPPER;
448                }
449
450                int m = 0;
451                for (int c = 0; c < upper; ++c) {
452                    largeBuffer[m++] = (char) c;
453                    if (m == BUFFER_SIZE) {
454                        writer.write(largeBuffer);
455                        m = 0;
456                    }
457                }
458                writer.write(largeBuffer, 0, m);
459                writer.flush();
460                byte[] result = baos.toByteArray();
461
462                isr = new InputStreamReader(new ByteArrayInputStream(result),
463                        MINIMAL_CHARSETS[i]);
464                int expected = 0, read = 0, j = 0;
465                while (expected < upper) {
466                    if (j == read) {
467                        read = isr.read(largeBuffer);
468                        j = 0;
469                    }
470                    assertEquals("Error when reading bytes in "
471                            + MINIMAL_CHARSETS[i] + " at " + j, expected++, largeBuffer[j++]);
472                }
473            } finally {
474                try {
475                    if (isr != null) isr.close();
476                } catch (Exception e) {
477                }
478                try {
479                    if (writer != null) writer.close();
480                } catch (Exception e) {
481                }
482            }
483        }
484    }
485
486    @TestTargetNew(
487            level = TestLevel.PARTIAL_COMPLETE,
488            notes = "",
489            method = "write",
490            args = {char[].class}
491    )
492    public void test_write_US_ASCII() throws Exception {
493        testEncodeCharset("US-ASCII", 128);
494    }
495
496    @TestTargetNew(
497            level = TestLevel.PARTIAL_COMPLETE,
498            notes = "",
499            method = "write",
500            args = {char[].class}
501    )
502    public void test_write_ISO_8859_1() throws Exception {
503        testEncodeCharset("ISO-8859-1", 256);
504    }
505
506    @TestTargetNew(
507            level = TestLevel.PARTIAL_COMPLETE,
508            notes = "",
509            method = "write",
510            args = {char[].class}
511    )
512    public void test_write_UTF_16BE() throws Exception {
513        testEncodeCharset("UTF-16BE", 0xd800);
514    }
515
516    @TestTargetNew(
517            level = TestLevel.PARTIAL_COMPLETE,
518            notes = "",
519            method = "write",
520            args = {char[].class}
521    )
522    public void test_write_UTF_16LE() throws Exception {
523        testEncodeCharset("UTF-16LE", 0xd800);
524    }
525
526    @TestTargetNew(
527            level = TestLevel.PARTIAL_COMPLETE,
528            notes = "",
529            method = "write",
530            args = {char[].class}
531    )
532    public void test_write_UTF_16() throws Exception {
533        testEncodeCharset("UTF-16", 0xd800);
534    }
535
536    @TestTargetNew(
537            level = TestLevel.PARTIAL_COMPLETE,
538            notes = "",
539            method = "write",
540            args = {char[].class}
541    )
542    public void test_write_UTF_8() throws Exception {
543        testEncodeCharset("UTF-8", 0xd800);
544    }
545
546    private void testEncodeCharset(String charset, int maxChar) throws Exception {
547        char[] chars = new char[maxChar];
548        for (int i = 0; i < maxChar; i++) {
549            chars[i] = (char) i;
550        }
551
552        // to byte array
553        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
554        OutputStreamWriter charsOut = new OutputStreamWriter(bytesOut, charset);
555        charsOut.write(chars);
556        charsOut.flush();
557
558        // decode from byte array, one character at a time
559        ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesOut.toByteArray());
560        InputStreamReader charsIn = new InputStreamReader(bytesIn, charset);
561        for (int i = 0; i < maxChar; i++) {
562            assertEquals(i, charsIn.read());
563        }
564        assertEquals(-1, charsIn.read());
565
566        // decode from byte array, using byte buffers
567        bytesIn = new ByteArrayInputStream(bytesOut.toByteArray());
568        charsIn = new InputStreamReader(bytesIn, charset);
569        char[] decoded = new char[maxChar];
570        for (int r = 0; r < maxChar; ) {
571            r += charsIn.read(decoded, r, maxChar - r);
572        }
573        assertEquals(-1, charsIn.read());
574        for (int i = 0; i < maxChar; i++) {
575            assertEquals(i, decoded[i]);
576        }
577    }
578
579    /**
580     * @tests java.io.OutputStreamWriter#getEncoding()
581     */
582    @TestTargetNew(
583        level = TestLevel.COMPLETE,
584        notes = "",
585        method = "getEncoding",
586        args = {}
587    )
588    public void test_getEncoding() throws IOException {
589        OutputStreamWriter writer;
590        writer = new OutputStreamWriter(new Support_OutputStream(), "utf-8");
591        assertEquals("Test 1: Incorrect encoding returned.",
592                     Charset.forName("utf-8"),
593                     Charset.forName(writer.getEncoding()));
594
595        writer.close();
596        assertNull("Test 2: getEncoding() did not return null for a closed writer.",
597                   writer.getEncoding());
598    }
599
600    /**
601     * @tests java.io.OutputStreamWriter#write(char[], int, int)
602     */
603    @TestTargetNew(
604        level = TestLevel.PARTIAL_COMPLETE,
605        notes = "",
606        method = "write",
607        args = {char[].class, int.class, int.class}
608    )
609    public void test_write$CII() throws IOException {
610        char[] chars = testString.toCharArray();
611        ByteArrayOutputStream baos = new ByteArrayOutputStream();
612        Support_OutputStream out = new Support_OutputStream(500);
613        OutputStreamWriter writer;
614
615        writer = new OutputStreamWriter(out, "utf-8");
616
617        try {
618            writer.write(chars, -1, 1);
619            fail("Test 1: IndexOutOfBoundsException expected.");
620        } catch (IndexOutOfBoundsException e) {
621            // Expected
622        }
623
624        try {
625            writer.write(chars, 0, -1);
626            fail("Test 2: IndexOutOfBoundsException expected.");
627        } catch (IndexOutOfBoundsException e) {
628            // Expected
629        }
630
631        try {
632            writer.write(new char[0], 0, 1);
633            fail("Test 3: IndexOutOfBoundsException expected.");
634        } catch (IndexOutOfBoundsException e) {
635            // Expected
636        }
637
638        try {
639            writer.write((char[]) null, 0, 1);
640            fail("Test 4: NullPointerException expected.");
641        } catch (NullPointerException e) {
642            // Expected
643        }
644
645        try {
646            writer.write(chars, 1, chars.length);
647            fail("Test 5a: IndexOutOfBoundsException expected.");
648        } catch (IndexOutOfBoundsException e) {
649            // Expected
650        }
651        try {
652            writer.write(chars, 0, chars.length + 1);
653            fail("Test 5b: IndexOutOfBoundsException expected.");
654        } catch (IndexOutOfBoundsException e) {
655            // Expected
656        }
657        try {
658            writer.write(chars, chars.length, 1);
659            fail("Test 5c: IndexOutOfBoundsException expected.");
660        } catch (IndexOutOfBoundsException e) {
661            // Expected
662        }
663        try {
664            writer.write(chars, chars.length + 1, 0);
665            fail("Test 5d: IndexOutOfBoundsException expected.");
666        } catch (IndexOutOfBoundsException e) {
667            // Expected
668        }
669
670        out.setThrowsException(true);
671        try {
672            for (int i = 0; i < 200; i++) {
673                writer.write(chars, 0, chars.length);
674            }
675            fail("Test 6: IOException expected.");
676        } catch (IOException e) {
677            // Expected
678        }
679        out.setThrowsException(false);
680
681        writer.close();
682        writer = new OutputStreamWriter(baos, "utf-8");
683        writer.write(chars, 1, 2);
684        writer.flush();
685        assertEquals("Test 7: write(char[], int, int) has not produced the " +
686                     "expected content in the output stream.",
687                     "hi", baos.toString("utf-8"));
688
689        writer.write(chars, 0, chars.length);
690        writer.flush();
691        assertEquals("Test 8: write(char[], int, int) has not produced the " +
692                "expected content in the output stream.",
693                "hi" + testString, baos.toString("utf-8"));
694
695        writer.close();
696        try {
697            writer.write((char[]) null, -1, -1);
698            fail("Test 9: IOException expected.");
699        } catch (IOException e) {
700            // Expected
701        }
702    }
703
704    /**
705     * @tests java.io.OutputStreamWriter#write(int)
706     */
707    @TestTargetNew(
708        level = TestLevel.PARTIAL_COMPLETE,
709        notes = "",
710        method = "write",
711        args = {int.class}
712    )
713    public void test_writeI() throws IOException {
714        Support_OutputStream out = new Support_OutputStream(500);
715        OutputStreamWriter writer;
716
717        out.setThrowsException(true);
718        writer = new OutputStreamWriter(out, "utf-8");
719        try {
720            // Since there is an internal buffer in the encoder, more than
721            // one character needs to be written.
722            for (int i = 0; i < 200; i++) {
723                for (int j = 0; j < testString.length(); j++) {
724                    writer.write(testString.charAt(j));
725                }
726            }
727            fail("Test 1: IOException expected.");
728        } catch (IOException e) {
729            // Expected
730        }
731        out.setThrowsException(false);
732        writer.close();
733
734        writer = new OutputStreamWriter(out, "utf-8");
735        writer.write(1);
736        writer.flush();
737        String str = new String(out.toByteArray(), "utf-8");
738        assertEquals("Test 2: ", "\u0001", str);
739
740        writer.write(2);
741        writer.flush();
742        str = new String(out.toByteArray(), "utf-8");
743        assertEquals("Test 3: ", "\u0001\u0002", str);
744
745        writer.write(-1);
746        writer.flush();
747        str = new String(out.toByteArray(), "utf-8");
748        assertEquals("Test 4: ", "\u0001\u0002\uffff", str);
749
750        writer.write(0xfedcb);
751        writer.flush();
752        str = new String(out.toByteArray(), "utf-8");
753        assertEquals("Test 5: ", "\u0001\u0002\uffff\uedcb", str);
754
755        writer.close();
756        try {
757            writer.write(1);
758            fail("Test 6: IOException expected.");
759        } catch (IOException e) {
760            // Expected
761        }
762    }
763
764    /**
765     * @tests java.io.OutputStreamWriter#write(java.lang.String, int, int)
766     */
767    @TestTargetNew(
768        level = TestLevel.COMPLETE,
769        notes = "",
770        method = "write",
771        args = {java.lang.String.class, int.class, int.class}
772    )
773    public void test_writeLjava_lang_StringII() throws IOException {
774        ByteArrayOutputStream baos = new ByteArrayOutputStream();
775        Support_OutputStream out = new Support_OutputStream(500);
776        OutputStreamWriter writer;
777
778        writer = new OutputStreamWriter(out, "utf-8");
779
780        try {
781            writer.write("Lorem", -1, 0);
782            fail("Test 1: IndexOutOfBoundsException expected.");
783        } catch (IndexOutOfBoundsException e) {
784            // Expected
785        }
786
787        try {
788            writer.write("Lorem", 0, -1);
789            fail("Test 2: IndexOutOfBoundsException expected.");
790        } catch (IndexOutOfBoundsException e) {
791            // Expected
792        }
793
794        try {
795            writer.write("", 0, 1);
796            fail("Test 3: IndexOutOfBoundsException expected.");
797        } catch (IndexOutOfBoundsException e) {
798            // Expected
799        }
800
801        try {
802            writer.write(testString, 1, testString.length());
803            fail("Test 4a: IndexOutOfBoundsException expected.");
804        } catch (IndexOutOfBoundsException e) {
805            // Expected
806        }
807
808        try {
809            writer.write(testString, 0, testString.length() + 1);
810            fail("Test 4b: IndexOutOfBoundsException expected.");
811        } catch (IndexOutOfBoundsException e) {
812            // Expected
813        }
814
815        try {
816            writer.write(testString, testString.length(), 1);
817            fail("Test 4c: IndexOutOfBoundsException expected.");
818        } catch (IndexOutOfBoundsException e) {
819            // Expected
820        }
821
822        try {
823            writer.write(testString, testString.length() + 1, 0);
824            fail("Test 4d: IndexOutOfBoundsException expected.");
825        } catch (IndexOutOfBoundsException e) {
826            // Expected
827        }
828
829        try {
830            writer.write((String) null, 0, 1);
831            fail("Test 5: NullPointerException expected.");
832        } catch (NullPointerException e) {
833            // Expected
834        }
835
836        out.setThrowsException(true);
837        try {
838            for (int i = 0; i < 200; i++) {
839                writer.write(testString, 0, testString.length());
840            }
841            fail("Test 6: IOException expected.");
842        } catch (IOException e) {
843            // Expected
844        }
845        out.setThrowsException(false);
846
847        writer.close();
848        writer = new OutputStreamWriter(baos, "utf-8");
849
850        writer.write("abc", 1, 2);
851        writer.flush();
852        assertEquals("Test 7: write(String, int, int) has not produced the " +
853                     "expected content in the output stream.",
854                     "bc", baos.toString("utf-8"));
855
856        writer.write(testString, 0, testString.length());
857        writer.flush();
858        assertEquals("Test 7: write(String, int, int) has not produced the " +
859                     "expected content in the output stream.",
860                     "bc" + testString, baos.toString("utf-8"));
861
862        writer.close();
863        try {
864            writer.write("abc", 0, 1);
865            fail("Test 8: IOException expected.");
866        } catch (IOException e) {
867            // Expected
868        }
869    }
870
871    private void openInputStream() {
872        try {
873            isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()), "UTF-8");
874        } catch (UnsupportedEncodingException e) {
875            fail("UTF-8 not supported");
876        }
877    }
878}
879