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