1/*
2 * Copyright (c) 2017, 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 8182450
27 * @summary Test model behavior when a completing a broken module-info.
28 * @library /tools/lib
29 * @modules
30 *      jdk.compiler/com.sun.tools.javac.api
31 *      jdk.compiler/com.sun.tools.javac.code
32 *      jdk.compiler/com.sun.tools.javac.main
33 *      jdk.compiler/com.sun.tools.javac.processing
34 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
35 * @run main BrokenModulesTest
36 */
37
38import java.nio.file.Path;
39import java.util.List;
40
41import javax.tools.JavaCompiler;
42import javax.tools.ToolProvider;
43
44import com.sun.tools.javac.api.JavacTaskImpl;
45
46public class BrokenModulesTest extends ModuleTestBase {
47
48    public static void main(String... args) throws Exception {
49        new BrokenModulesTest().runTests();
50    }
51
52    List<String> jlObjectList = List.of("java.lang.Object");
53    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
54
55    @Test
56    public void testBrokenModuleInfoModuleSourcePath(Path base) throws Exception {
57        Path msp = base.resolve("msp");
58        Path ma = msp.resolve("ma");
59        tb.writeJavaFiles(ma,
60                          "module ma { requires not.available; exports api1; }",
61                          "package api1; public interface Api { }");
62        Path out = base.resolve("out");
63        tb.createDirectories(out);
64
65        List<String> opts = List.of("--module-source-path", msp.toString(),
66                                    "--add-modules", "ma");
67        JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
68
69        task.enter();
70
71        task.getElements().getModuleElement("java.base");
72    }
73
74    @Test
75    public void testSystem(Path base) throws Exception {
76        Path jdkCompiler = base.resolve("jdk.compiler");
77        tb.writeJavaFiles(jdkCompiler,
78                          "module jdk.compiler { requires not.available; }");
79        Path out = base.resolve("out");
80        tb.createDirectories(out);
81
82        List<String> opts = List.of("--patch-module", "jdk.compiler=" + jdkCompiler.toString(),
83                                    "--add-modules", "jdk.compiler");
84        JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
85
86        task.enter();
87
88        task.getElements().getModuleElement("java.base");
89    }
90
91    @Test
92    public void testParseError(Path base) throws Exception {
93        Path msp = base.resolve("msp");
94        Path ma = msp.resolve("ma");
95        tb.writeJavaFiles(ma,
96                          "broken module ma { }",
97                          "package api1; public interface Api { }");
98        Path out = base.resolve("out");
99        tb.createDirectories(out);
100
101        List<String> opts = List.of("--module-source-path", msp.toString(),
102                                    "--add-modules", "ma");
103        JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(null, null, null, opts, jlObjectList, null);
104
105        task.analyze();
106
107        task.getElements().getModuleElement("java.base");
108    }
109
110}
111