summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java/org/apache/harmony/tests/java/io/OutputStreamWriterTest.java
blob: 39b909ec108ffbc10d7bdeb7f78723cc64047699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.tests.java.io;

import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class OutputStreamWriterTest extends TestCase {

    private static final int UPPER = 0xd800;

    private static final int BUFFER_SIZE = 10000;

    private ByteArrayOutputStream out;

    private OutputStreamWriter writer;

    static private final String source = "This is a test message with Unicode character. \u4e2d\u56fd is China's name in Chinese";

    static private final String[] MINIMAL_CHARSETS = new String[] { "US-ASCII",
            "ISO-8859-1", "UTF-16BE", "UTF-16LE", "UTF-16", "UTF-8" };

    OutputStreamWriter osw;

    InputStreamReader isr;

    private ByteArrayOutputStream fos;

    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";

    /*
     * @see TestCase#setUp()
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        out = new ByteArrayOutputStream();
        writer = new OutputStreamWriter(out, "utf-8");

        fos = new ByteArrayOutputStream();
        osw = new OutputStreamWriter(fos);
    }

    /*
     * @see TestCase#tearDown()
     */
    @Override
    protected void tearDown() throws Exception {
        try {
            writer.close();

            if (isr != null) {
                isr.close();
            }
            osw.close();
        } catch (Exception e) {
            // Ignored
        }

        super.tearDown();
    }

    public void testClose() throws Exception {
        writer.flush();
        writer.close();
        try {
            writer.flush();
            fail();
        } catch (IOException e) {
            // Expected
        }
    }

    public void testFlush() throws Exception {
        writer.write(source);
        writer.flush();
        String result = out.toString("utf-8");
        assertEquals(source, result);
    }

    /*
     * Class under test for void write(char[], int, int)
     */
    public void testWritecharArrayintint() throws IOException {
        char[] chars = source.toCharArray();

        // Throws IndexOutOfBoundsException if offset is negative
        try {
            writer.write((char[]) null, -1, -1);
            fail();
        } catch (NullPointerException exception) {
        } catch (IndexOutOfBoundsException exception) {
        }

        // throws NullPointerException though count is negative
        try {
            writer.write((char[]) null, 1, -1);
            fail();
        } catch (NullPointerException exception) {
        } catch (IndexOutOfBoundsException exception) {
        }

        try {
            writer.write((char[]) null, 1, 1);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        try {
            writer.write(new char[0], 0, 1);
            fail();
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write(chars, -1, 1);
            fail();
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write(chars, 0, -1);
            fail();
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write(chars, 1, chars.length);
            fail();
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        writer.write(chars, 1, 2);
        writer.flush();
        assertEquals("hi", out.toString("utf-8"));
        writer.write(chars, 0, chars.length);
        writer.flush();
        assertEquals("hi" + source, out.toString("utf-8"));

        writer.close();
        // After the stream is closed, should throw IOException first
        try {
            writer.write((char[]) null, -1, -1);
            fail("should throw IOException");
        } catch (IOException e) {
            // Expected
        }
    }

    /*
     * Class under test for void write(int)
     */
    public void testWriteint() throws IOException {
        writer.write(1);
        writer.flush();
        String str = new String(out.toByteArray(), "utf-8");
        assertEquals("\u0001", str);

        writer.write(2);
        writer.flush();
        str = new String(out.toByteArray(), "utf-8");
        assertEquals("\u0001\u0002", str);

        writer.write(-1);
        writer.flush();
        str = new String(out.toByteArray(), "utf-8");
        assertEquals("\u0001\u0002\uffff", str);

        writer.write(0xfedcb);
        writer.flush();
        str = new String(out.toByteArray(), "utf-8");
        assertEquals("\u0001\u0002\uffff\uedcb", str);

        writer.close();
        // After the stream is closed, should throw IOException
        try {
            writer.write(1);
            fail("should throw IOException");
        } catch (IOException e) {
            // expected
        }
    }

    /*
     * Class under test for void write(String, int, int)
     */
    public void testWriteStringintint() throws IOException {
        try {
            writer.write((String) null, 1, 1);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        try {
            writer.write("", 0, 1);
            fail();
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write("abc", -1, 1);
            fail();
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write("abc", 0, -1);
            fail();
        } catch (IndexOutOfBoundsException e) {
            // Expected
        }
        try {
            writer.write("abc", 1, 3);
            fail();
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }

        try {
            writer.write((String) null, -1, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        }

        // Throws NullPointerException before StringIndexOutOfBoundsException
        try {
            writer.write((String) null, -1, 0);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        }

        writer.write("abc", 1, 2);
        writer.flush();
        assertEquals("bc", out.toString("utf-8"));
        writer.write(source, 0, source.length());
        writer.flush();
        assertEquals("bc" + source, out.toString("utf-8"));

        writer.close();
        // Throws IndexOutOfBoundsException first if count is negative
        try {
            writer.write((String) null, 0, -1);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        }

        try {
            writer.write((String) null, -1, 0);
            fail();
        } catch (NullPointerException expected) {
        } catch (IndexOutOfBoundsException expected) {
        }

        try {
            writer.write("abc", -1, 0);
            fail("should throw StringIndexOutOfBoundsException");
        } catch (StringIndexOutOfBoundsException e) {
            // Expected
        }

        // Throws IOException
        try {
            writer.write("abc", 0, 1);
            fail("should throw IOException");
        } catch (IOException e) {
            // expected
        }
    }

    /*
     * Class under test for void OutputStreamWriter(OutputStream)
     */
    public void testOutputStreamWriterOutputStream() throws IOException {
        try {
            writer = new OutputStreamWriter(null);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        OutputStreamWriter writer2 = new OutputStreamWriter(out);
        writer2.close();
    }

    /*
     * Class under test for void OutputStreamWriter(OutputStream, String)
     */
    public void testOutputStreamWriterOutputStreamString() throws IOException {
        try {
            writer = new OutputStreamWriter(null, "utf-8");
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        try {
            writer = new OutputStreamWriter(out, "");
            fail();
        } catch (UnsupportedEncodingException e) {
            // Expected
        }
        try {
            writer = new OutputStreamWriter(out, "badname");
            fail();
        } catch (UnsupportedEncodingException e) {
            // Expected
        }
        try {
            writer = new OutputStreamWriter(out, (String) null);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        OutputStreamWriter writer2 = new OutputStreamWriter(out, "ascii");
        assertEquals(Charset.forName("ascii"), Charset.forName(writer2
                .getEncoding()));
        writer2.close();
    }

    /*
     * Class under test for void OutputStreamWriter(OutputStream)
     */
    public void testOutputStreamWriterOutputStreamCharset() throws IOException {
        Charset cs = Charset.forName("ascii");
        try {
            writer = new OutputStreamWriter(null, cs);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        try {
            writer = new OutputStreamWriter(out, (Charset) null);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        OutputStreamWriter writer2 = new OutputStreamWriter(out, cs);
        assertEquals(cs, Charset.forName(writer2.getEncoding()));
        writer2.close();
    }

    /*
     * Class under test for void OutputStreamWriter(OutputStream, String)
     */
    public void testOutputStreamWriterOutputStreamCharsetEncoder()
            throws IOException {
        Charset cs = Charset.forName("ascii");
        CharsetEncoder enc = cs.newEncoder();
        try {
            writer = new OutputStreamWriter(null, enc);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        try {
            writer = new OutputStreamWriter(out, (CharsetEncoder) null);
            fail();
        } catch (NullPointerException e) {
            // Expected
        }
        OutputStreamWriter writer2 = new OutputStreamWriter(out, enc);
        assertEquals(cs, Charset.forName(writer2.getEncoding()));
        writer2.close();
    }

    public void testGetEncoding() {
        Charset cs = Charset.forName("utf-8");
        assertEquals(cs, Charset.forName(writer.getEncoding()));
    }

    public void testHandleEarlyEOFChar_1() throws IOException {
        String str = "All work and no play makes Jack a dull boy\n"; //$NON-NLS-1$
        int NUMBER = 2048;
        int j = 0;
        int len = str.length() * NUMBER;
        char[] strChars = new char[len];
        for (int i = 0; i < NUMBER; ++i) {
            for (int k = 0; k < str.length(); ++k) {
                strChars[j++] = str.charAt(k);
            }
        }

        File f = File.createTempFile("one", "by_one");
        f.deleteOnExit();
        FileWriter fw = new FileWriter(f);
        fw.write(strChars);
        fw.close();
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader in = new InputStreamReader(fis);
        for (int offset = 0; offset < strChars.length; ++offset) {
            int b = in.read();
            assertFalse("Early EOF at offset", -1 == b);
        }
    }

    public void testHandleEarlyEOFChar_2() throws IOException {
        int capacity = 65536;
        byte[] bytes = new byte[capacity];
        byte[] bs = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = bs[i / 8192];
        }
        String inputStr = new String(bytes);
        int len = inputStr.length();
        File f = File.createTempFile("FileWriterBugTest ", null); //$NON-NLS-1$
        f.deleteOnExit();
        FileWriter writer = new FileWriter(f);
        writer.write(inputStr);
        writer.close();
        long flen = f.length();

        FileReader reader = new FileReader(f);
        char[] outChars = new char[capacity];
        int outCount = reader.read(outChars);
        String outStr = new String(outChars, 0, outCount);

        assertEquals(len, flen);
        assertEquals(inputStr, outStr);
    }

    public void testSingleCharIO() throws Exception {
        InputStreamReader isr = null;
        for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
            try {
                out = new ByteArrayOutputStream();
                writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);

                int upper = UPPER;
                switch (i) {
                    case 0:
                        upper = 128;
                        break;
                    case 1:
                        upper = 256;
                        break;
                }

                for (int c = 0; c < upper; ++c) {
                    writer.write(c);
                }
                writer.flush();
                byte[] result = out.toByteArray();

                isr = new InputStreamReader(new ByteArrayInputStream(result),
                        MINIMAL_CHARSETS[i]);
                for (int expected = 0; expected < upper; ++expected) {
                    assertEquals("Error when reading bytes in "
                            + MINIMAL_CHARSETS[i], expected, isr.read());
                }
            } finally {
                try {
                    isr.close();
                } catch (Exception e) {
                }
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public void testBlockIO() throws Exception {
        InputStreamReader isr = null;
        char[] largeBuffer = new char[BUFFER_SIZE];
        for (int i = 0; i < MINIMAL_CHARSETS.length; ++i) {
            try {
                out = new ByteArrayOutputStream();
                writer = new OutputStreamWriter(out, MINIMAL_CHARSETS[i]);

                int upper = UPPER;
                switch (i) {
                    case 0:
                        upper = 128;
                        break;
                    case 1:
                        upper = 256;
                        break;
                }

                int m = 0;
                for (int c = 0; c < upper; ++c) {
                    largeBuffer[m++] = (char) c;
                    if (m == BUFFER_SIZE) {
                        writer.write(largeBuffer);
                        m = 0;
                    }
                }
                writer.write(largeBuffer, 0, m);
                writer.flush();
                byte[] result = out.toByteArray();

                isr = new InputStreamReader(new ByteArrayInputStream(result),
                        MINIMAL_CHARSETS[i]);
                int expected = 0, read = 0, j = 0;
                while (expected < upper) {
                    if (j == read) {
                        read = isr.read(largeBuffer);
                        j = 0;
                    }
                    assertEquals("Error when reading bytes in "
                            + MINIMAL_CHARSETS[i], expected++, largeBuffer[j++]);
                }
            } finally {
                try {
                    isr.close();
                } catch (Exception e) {
                }
                try {
                    writer.close();
                } catch (Exception e) {
                }
            }
        }
    }

    /**
     * java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
     */
    public void test_ConstructorLjava_io_OutputStream() {
        assertTrue("Used in tests", true);
    }

    /**
     * java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream,
     *java.lang.String)
     */
    public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
            throws UnsupportedEncodingException {
        osw = new OutputStreamWriter(fos, "8859_1");
        try {
            osw = new OutputStreamWriter(fos, "Bogus");
            fail("Failed to throw Unsupported Encoding exception");
        } catch (UnsupportedEncodingException e) {
            // Expected
        }
    }

    /**
     * java.io.OutputStreamWriter#close()
     */
    public void test_close() throws IOException {
        osw.close();

        try {
            osw.write(testString, 0, testString.length());
            fail("Chars written after close");
        } catch (IOException e) {
            // Expected
        }

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        try {
            OutputStreamWriter writer = new OutputStreamWriter(bout,
                    "ISO2022JP");
            writer.write(new char[] { 'a' });
            writer.close();
            // the default is ASCII, there should not be any mode changes
            String converted = new String(bout.toByteArray(), "ISO8859_1");
            assertTrue("invalid conversion 1: " + converted, converted
                    .equals("a"));

            bout.reset();
            writer = new OutputStreamWriter(bout, "ISO2022JP");
            writer.write(new char[] { '\u3048' });
            writer.flush();
            // the byte sequence should not switch to ASCII mode until the
            // stream is closed
            converted = new String(bout.toByteArray(), "ISO8859_1");
            assertTrue("invalid conversion 2: " + converted, converted
                    .equals("\u001b$B$("));
            writer.close();
            converted = new String(bout.toByteArray(), "ISO8859_1");
            assertTrue("invalid conversion 3: " + converted, converted
                    .equals("\u001b$B$(\u001b(B"));

            bout.reset();
            writer = new OutputStreamWriter(bout, "ISO2022JP");
            writer.write(new char[] { '\u3048' });
            writer.write(new char[] { '\u3048' });
            writer.close();
            // there should not be a mode switch between writes
            assertEquals("invalid conversion 4", "\u001b$B$($(\u001b(B",
                    new String(bout.toByteArray(), "ISO8859_1"));
        } catch (UnsupportedEncodingException e) {
            // Can't test missing converter
            System.out.println(e);
        }
    }

    /**
     * java.io.OutputStreamWriter#flush()
     */
    public void test_flush() throws IOException {
        char[] buf = new char[testString.length()];
        osw.write(testString, 0, testString.length());
        osw.flush();
        openInputStream();
        isr.read(buf, 0, buf.length);
        assertTrue("Chars not flushed", new String(buf, 0, buf.length)
                .equals(testString));
    }

    /**
     * Unlike the RI, we return the canonical encoding name and not something
     * java specific.
     */
    public void test_getEncoding() throws IOException {
        try {
            osw = new OutputStreamWriter(fos, "8859_1");
        } catch (UnsupportedEncodingException e) {
            assertEquals("Returned incorrect encoding", "8859_1", osw
                    .getEncoding());
        }

        OutputStreamWriter out = new OutputStreamWriter(
                new ByteArrayOutputStream(), "UTF-16BE");
        out.close();

        String result = out.getEncoding();
        assertNull(result);

        out = null;
        try {
            out = new OutputStreamWriter(new ByteArrayOutputStream(),
                    "UTF-16BE");
        } catch (UnsupportedEncodingException e) {
            // ok
        }
        result = out.getEncoding();
        assertEquals("UTF-16BE", result);
    }

    /**
     * java.io.OutputStreamWriter#write(char[], int, int)
     */
    public void test_write$CII() throws IOException {
        char[] buf = new char[testString.length()];
        osw.write(testString, 0, testString.length());
        osw.close();
        openInputStream();
        isr.read(buf, 0, buf.length);
        assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
                .equals(testString));
    }

    /**
     * java.io.OutputStreamWriter#write(int)
     */
    public void test_writeI() throws IOException {
        osw.write('T');
        osw.close();
        openInputStream();
        int c = isr.read();
        assertEquals("Incorrect char returned", 'T', (char) c);
    }

    /**
     * java.io.OutputStreamWriter#write(java.lang.String, int, int)
     */
    public void test_writeLjava_lang_StringII() throws IOException {
        char[] buf = new char[testString.length()];
        osw.write(testString, 0, testString.length());
        osw.close();
        openInputStream();
        isr.read(buf);
        assertTrue("Incorrect chars returned", new String(buf, 0, buf.length)
                .equals(testString));
    }

    private void openInputStream() {
        isr = new InputStreamReader(new ByteArrayInputStream(fos.toByteArray()));
    }
}