|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import org.testng.annotations.DataProvider; |
| 25 | +import org.testng.annotations.Test; |
| 26 | +import static org.testng.Assert.assertEquals; |
| 27 | + |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.OutputStreamWriter; |
| 31 | +import java.io.PrintStream; |
| 32 | +import java.io.PrintWriter; |
| 33 | +import java.nio.charset.Charset; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | + |
| 36 | +/** |
| 37 | + * @test |
| 38 | + * @bug 8276970 |
| 39 | + * @summary Test to verify the charset in PrintStream is inherited |
| 40 | + * in the OutputStreamWriter/PrintWriter |
| 41 | + * @run testng InheritEncodingTest |
| 42 | + */ |
| 43 | +@Test |
| 44 | +public class InheritEncodingTest { |
| 45 | + |
| 46 | + private static final String testString = "\u00e9\u3042"; // "éあ" |
| 47 | + |
| 48 | + @DataProvider |
| 49 | + public Object[][] encodings() { |
| 50 | + return new Object[][]{ |
| 51 | + {StandardCharsets.ISO_8859_1}, |
| 52 | + {StandardCharsets.US_ASCII}, |
| 53 | + {StandardCharsets.UTF_8}, |
| 54 | + {StandardCharsets.UTF_16}, |
| 55 | + {StandardCharsets.UTF_16BE}, |
| 56 | + {StandardCharsets.UTF_16LE}, |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + @Test (dataProvider = "encodings") |
| 61 | + public void testOutputStreamWriter(Charset stdCharset) throws IOException { |
| 62 | + var ba = new ByteArrayOutputStream(); |
| 63 | + var ps = new PrintStream(ba, true, stdCharset); |
| 64 | + var expected = new String(testString.getBytes(stdCharset), stdCharset); |
| 65 | + |
| 66 | + // tests OutputStreamWriter's encoding explicitly |
| 67 | + var osw = new OutputStreamWriter(ps); |
| 68 | + assertEquals(Charset.forName(osw.getEncoding()), stdCharset); |
| 69 | + |
| 70 | + // tests roundtrip result |
| 71 | + osw.write(testString); |
| 72 | + osw.flush(); |
| 73 | + var result = ba.toString(stdCharset); |
| 74 | + assertEquals(result, expected); |
| 75 | + } |
| 76 | + |
| 77 | + @Test (dataProvider = "encodings") |
| 78 | + public void testPrintWriter(Charset stdCharset) throws IOException { |
| 79 | + var ba = new ByteArrayOutputStream(); |
| 80 | + var ps = new PrintStream(ba, true, stdCharset); |
| 81 | + var expected = new String(testString.getBytes(stdCharset), stdCharset); |
| 82 | + |
| 83 | + // tests roundtrip result |
| 84 | + var pw = new PrintWriter(ps); |
| 85 | + pw.write(testString); |
| 86 | + pw.flush(); |
| 87 | + var result = ba.toString(stdCharset); |
| 88 | + assertEquals(result, expected); |
| 89 | + } |
| 90 | + |
| 91 | + @Test (dataProvider = "encodings") |
| 92 | + public void testPrintStream(Charset stdCharset) throws IOException { |
| 93 | + var ba = new ByteArrayOutputStream(); |
| 94 | + var ps = new PrintStream(ba, true, stdCharset); |
| 95 | + var expected = new String(testString.getBytes(stdCharset), stdCharset); |
| 96 | + |
| 97 | + // tests PrintStream's charset explicitly |
| 98 | + var psWrapper = new PrintStream(ps); |
| 99 | + assertEquals(psWrapper.charset(), stdCharset); |
| 100 | + |
| 101 | + // tests roundtrip result |
| 102 | + psWrapper.print(testString); |
| 103 | + psWrapper.flush(); |
| 104 | + var result = ba.toString(stdCharset); |
| 105 | + assertEquals(result, expected); |
| 106 | + } |
| 107 | +} |
0 commit comments