Test.java revision 3014:a3dd196e5341
1/*
2 * Copyright (c) 2008, 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 6668794 6668796
27 * @summary javac puts localized text in raw diagnostics
28 *      bad diagnostic "bad class file" given for source files
29 * @modules jdk.compiler
30 */
31
32import java.io.*;
33import java.util.*;
34import javax.tools.*;
35
36public class Test {
37    public static void main(String[] args) throws Exception {
38        new Test().run();
39    }
40
41    void run() throws Exception {
42
43        // compile q.A then move it to p.A
44        compile("A.java");
45
46        File p = new File("p");
47        p.mkdirs();
48        new File("q/A.class").renameTo(new File("p/A.class"));
49
50        // compile B against p.A
51        String[] out = compile("B.java");
52        if (out.length == 0)
53            throw new Error("no diagnostics generated");
54
55        String expected = "B.java:6:6: compiler.err.cant.access: p.A, " +
56            "(compiler.misc.bad.class.file.header: A.class, " +
57            "(compiler.misc.class.file.wrong.class: q.A))";
58
59        if (!out[0].equals(expected)) {
60            System.err.println("expected: " + expected);
61            System.err.println("   found: " + out[0]);
62            throw new Error("test failed");
63        }
64    }
65
66    String[] compile(String file) {
67        String[] options = {
68            "-XDrawDiagnostics",
69            "-d", ".",
70            "-classpath", ".",
71            new File(testSrc, file).getPath()
72        };
73
74        System.err.println("compile: " + Arrays.asList(options));
75        StringWriter sw = new StringWriter();
76        PrintWriter out = new PrintWriter(sw);
77        int rc = com.sun.tools.javac.Main.compile(options, out);
78        out.close();
79
80        String outText = sw.toString();
81        System.err.println(outText);
82
83        return sw.toString().split("[\\r\\n]+");
84    }
85
86    File testSrc = new File(System.getProperty("test.src", "."));
87}
88