AnachronisticModuleInfoTest.java revision 3589:b27ce9a1374e
1/*
2 * Copyright (c) 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 8157512
27 * @summary AssertionError in javac when module-info < v53.0
28 * @library /tools/lib
29 * @modules
30 *      jdk.compiler/com.sun.tools.javac.api
31 *      jdk.compiler/com.sun.tools.javac.main
32 * @build toolbox.ToolBox toolbox.JavacTask module-info
33 * @run main AnachronisticModuleInfoTest
34 */
35
36import toolbox.JavacTask;
37import toolbox.Task;
38import toolbox.TestRunner;
39import toolbox.ToolBox;
40
41import java.io.IOException;
42import java.nio.file.Path;
43import java.nio.file.Paths;
44
45public class AnachronisticModuleInfoTest extends TestRunner {
46
47    protected ToolBox tb;
48
49    AnachronisticModuleInfoTest() {
50        super(System.err);
51        tb = new ToolBox();
52    }
53
54    public static void main(String... args) throws Exception {
55        AnachronisticModuleInfoTest t = new AnachronisticModuleInfoTest();
56        t.runTests();
57    }
58
59    /**
60     * Run all methods annotated with @Test, and throw an exception if any
61     * errors are reported..
62     *
63     * @throws Exception if any errors occurred
64     */
65    protected void runTests() throws Exception {
66        runTests(m -> new Object[] { Paths.get(m.getName()) });
67    }
68
69    Path[] findJavaFiles(Path... paths) throws IOException {
70        return tb.findJavaFiles(paths);
71    }
72
73    @Test
74    public void anachronisticModuleInfoTest(Path base) throws Exception {
75        Path src = base.resolve("src");
76        tb.writeJavaFiles(src, "class C { }");
77        String modulePath = System.getProperty("test.classes");
78
79        String log = new JavacTask(tb, Task.Mode.CMDLINE)
80                .options("-XDrawDiagnostics",
81                        "-upgrademodulepath", modulePath)
82                .files(findJavaFiles(src))
83                .run(Task.Expect.FAIL)
84                .writeAll()
85                .getOutput(Task.OutputKind.DIRECT);
86        String expected = "- compiler.err.cant.access: foo.module-info, (compiler.misc.bad.class.file.header: module-info.class, (compiler.misc.anachronistic.module.info: 52, 0))";
87        if (!log.contains(expected))
88            throw new Exception("expected output not found" + log);
89    }
90}
91