GraphsTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 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 * @summary tests for module graph resolution issues
27 * @library /tools/lib
28 * @modules
29 *      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 ModuleTestBase
33 * @run main GraphsTest
34 */
35
36import java.io.File;
37import java.nio.file.Files;
38import java.nio.file.Path;
39import java.util.Arrays;
40import java.util.List;
41import java.util.regex.Pattern;
42
43public class GraphsTest extends ModuleTestBase {
44
45    public static void main(String... args) throws Exception {
46        GraphsTest t = new GraphsTest();
47        t.runTests();
48    }
49
50    /**
51     * Tests diamond graph with an automatic module added in.
52     * +-------------+          +--------------------+         +------------------+
53     * | module M    |          | module N           |         | module O         |
54     * |             | ----->   |                    | --->    |                  |  --> J.jar
55     * | require N   |          | requires public  O |         |                  |
56     * | require L   |          |                    |         +------------------+
57     * +-------------+          +--------------------+                  ^
58     *       |                                                          |
59     *       |                  +--------------------+                  |
60     *       ------------------>| module L           |                  |
61     *                          |                    |------------------
62     *                          | requires public O  |
63     *                          |                    |
64     *                          +--------------------+
65     *
66     */
67    @Test
68    void diamond(Path base) throws Exception {
69
70        Path modules = Files.createDirectories(base.resolve("modules"));
71
72        new ModuleBuilder("J")
73                .exports("openJ")
74                .classes("package openJ; public class J { }")
75                .classes("package closedJ; public class J { }")
76                .build(base.resolve("jar"));
77
78        Path jarModules = Files.createDirectories(base.resolve("jarModules"));
79        Path jar = jarModules.resolve("J.jar");
80        tb.new JarTask(jar)
81                .baseDir(base.resolve("jar/J"))
82                .files(".")
83                .run()
84                .writeAll();
85
86        new ModuleBuilder("O")
87                .exports("openO")
88                .requiresPublic("J", jarModules)
89                .classes("package openO; public class O { openJ.J j; }")
90                .classes("package closedO; public class O { }")
91                .build(modules);
92        new ModuleBuilder("N")
93                .requiresPublic("O", modules, jarModules)
94                .exports("openN")
95                .classes("package openN; public class N { }")
96                .classes("package closedN; public class N { }")
97                .build(modules);
98        new ModuleBuilder("L")
99                .requiresPublic("O", modules, jarModules)
100                .exports("openL")
101                .classes("package openL; public class L { }")
102                .classes("package closedL; public class L { }")
103                .build(modules);
104        ModuleBuilder m = new ModuleBuilder("M");
105        //positive case
106        Path positiveSrc = m
107                .requires("N", modules)
108                .requires("L", modules)
109                .classes("package p; public class Positive { openO.O o; openN.N n; openL.L l; }")
110                .write(base.resolve("positiveSrc"));
111
112        tb.new JavacTask()
113                .options("-XDrawDiagnostics", "-mp", modules + File.pathSeparator + jarModules)
114                .outdir(Files.createDirectories(base.resolve("positive")))
115                .files(findJavaFiles(positiveSrc))
116                .run()
117                .writeAll();
118        //negative case
119        Path negativeSrc = m.classes("package p; public class Negative { closedO.O o; closedN.N n; closedL.L l; }")
120                .write(base.resolve("negativeSrc"));
121        List<String> log = tb.new JavacTask()
122                .options("-XDrawDiagnostics", "-mp", modules + File.pathSeparator + jarModules)
123                .outdir(Files.createDirectories(base.resolve("negative")))
124                .files(findJavaFiles(negativeSrc))
125                .run(ToolBox.Expect.FAIL)
126                .writeAll()
127                .getOutputLines(ToolBox.OutputKind.DIRECT);
128
129        List<String> expected = Arrays.asList(
130                "Negative.java:1:43: compiler.err.doesnt.exist: closedO",
131                "Negative.java:1:56: compiler.err.doesnt.exist: closedN",
132                "Negative.java:1:69: compiler.err.doesnt.exist: closedL");
133        if (!log.containsAll(expected)) {
134            throw new Exception("Expected output not found");
135        }
136        //multi module mode
137        m.write(modules);
138        List<String> out = tb.new JavacTask()
139                .options("-XDrawDiagnostics",
140                        "-modulesourcepath", modules + "/*/src",
141                        "-mp", jarModules.toString()
142                )
143                .outdir(Files.createDirectories(base.resolve("negative")))
144                .files(findJavaFiles(modules))
145                .run(ToolBox.Expect.FAIL)
146                .writeAll()
147                .getOutputLines(ToolBox.OutputKind.DIRECT);
148        expected = Arrays.asList(
149                "Negative.java:1:43: compiler.err.not.def.access.package.cant.access: closedO.O, closedO",
150                "Negative.java:1:56: compiler.err.not.def.access.package.cant.access: closedN.N, closedN",
151                "Negative.java:1:69: compiler.err.not.def.access.package.cant.access: closedL.L, closedL");
152        if (!out.containsAll(expected)) {
153            throw new Exception("Expected output not found");
154        }
155        //checks if the output does not contain messages about exported packages.
156        Pattern regex = Pattern.compile("compiler\\.err.*(openO\\.O|openN\\.N|openL\\.L)");
157        for (String s : out) {
158            if (regex.matcher(s).find()) {
159                throw new Exception("Unexpected output: " + s);
160            }
161        }
162    }
163
164    /**
165     * Tests graph where module M reexport package of N, but N export the package only to M.
166     *
167    +-------------+        +--------------------+        +---------------+
168    | module L    |        | module M           |        | module N      |
169    |             | -----> |                    | -----> |               |
170    |  requires M |        |  requires public N |        | exports P to M|
171    +-------------+        |                    |        +---------------+
172                           +--------------------+
173    */
174    @Test
175    public void reexportOfQualifiedExport(Path base) throws Exception {
176        Path modules = base.resolve("modules");
177        new ModuleBuilder("M")
178                .requiresPublic("N")
179                .write(modules);
180        new ModuleBuilder("N")
181                .exportsTo("pack", "M")
182                .classes("package pack; public class Clazz { }")
183                .write(modules);
184        new ModuleBuilder("L")
185                .requires("M")
186                .classes("package p; public class A { A(pack.Clazz cl){} } ")
187                .write(modules);
188        String log = tb.new JavacTask()
189                .options("-XDrawDiagnostics",
190                        "-modulesourcepath", modules + "/*/src")
191                .outdir(Files.createDirectories(base.resolve("negative")))
192                .files(findJavaFiles(modules))
193                .run(ToolBox.Expect.FAIL)
194                .writeAll()
195                .getOutput(ToolBox.OutputKind.DIRECT);
196
197        String expected = "A.java:1:35: compiler.err.not.def.access.package.cant.access: pack.Clazz, pack";
198        if (!log.contains(expected)) {
199            throw new Exception("Expected output not found");
200        }
201    }
202}
203