CheckEBCDICLocaleTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2015, 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/*
25 * @test
26 * @bug 4846262
27 * @summary check that javac operates correctly in EBCDIC locale
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.file
31 *          jdk.compiler/com.sun.tools.javac.main
32 *          jdk.jdeps/com.sun.tools.javap
33 * @build ToolBox
34 * @run main CheckEBCDICLocaleTest
35 */
36
37import java.io.File;
38import java.io.FileOutputStream;
39import java.io.OutputStreamWriter;
40import java.io.PrintStream;
41import java.io.PrintWriter;
42import java.nio.charset.Charset;
43import java.nio.file.Files;
44import java.nio.file.Paths;
45import java.util.Arrays;
46import java.util.List;
47
48public class CheckEBCDICLocaleTest {
49
50    private static final String TestSrc =
51        "public class Test {\n" +
52        "    public void test() {\n" +
53        "        abcdefg\n" +
54        "    }\n" +
55        "}";
56
57    private static final String TestOutTemplate =
58        "output%1$sTest.java:3: error: not a statement\n" +
59        "        abcdefg\n" +
60        "        ^\n" +
61        "output%1$sTest.java:3: error: ';' expected\n" +
62        "        abcdefg\n" +
63        "               ^\n" +
64        "2 errors\n";
65
66    public static void main(String[] args) throws Exception {
67        new CheckEBCDICLocaleTest().test();
68    }
69
70    public void test() throws Exception {
71        ToolBox tb = new ToolBox();
72        tb.writeFile("Test.java", TestSrc);
73        tb.createDirectories("output");
74
75        Charset ebcdic = Charset.forName("IBM1047");
76        Native2Ascii n2a = new Native2Ascii(ebcdic);
77        n2a.asciiToNative(Paths.get("Test.java"), Paths.get("output", "Test.java"));
78
79        // Use -encoding to specify the encoding with which to read source files
80        // Use a suitable configured output stream for javac diagnostics
81        int rc;
82        try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("Test.tmp"), ebcdic))) {
83            String[] args = { "-encoding", ebcdic.name(), "output/Test.java" };
84            rc = com.sun.tools.javac.Main.compile(args, out);
85            if (rc != 1)
86                throw new Exception("unexpected exit from javac: " + rc);
87        }
88
89        n2a.nativeToAscii(Paths.get("Test.tmp"), Paths.get("Test.out"));
90
91        List<String> expectLines = Arrays.asList(
92                String.format(TestOutTemplate, File.separator).split("\n"));
93        List<String> actualLines = Files.readAllLines(Paths.get("Test.out"));
94        try {
95            tb.checkEqual(expectLines, actualLines);
96        } catch (Throwable tt) {
97            PrintStream out = tb.out;
98            out.println("Output mismatch:");
99
100            out.println("Expected output:");
101            for (String s: expectLines) {
102                out.println(s);
103            }
104            out.println();
105
106            out.println("Actual output:");
107            for (String s : actualLines) {
108                out.println(s);
109            }
110            out.println();
111
112            throw tt;
113        }
114    }
115}
116