ModulesAndClassPathTest.java revision 3294:9adfb22ff08f
150477Speter/*
22343Scsgr * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
32343Scsgr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42343Scsgr *
5201386Sed * This code is free software; you can redistribute it and/or modify it
6201386Sed * under the terms of the GNU General Public License version 2 only, as
72343Scsgr * published by the Free Software Foundation.
82343Scsgr *
92343Scsgr * This code is distributed in the hope that it will be useful, but WITHOUT
102343Scsgr * 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 Ensure named modules cannot refer to classpath types.
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 ModulesAndClassPathTest
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.Set;
42
43import javax.annotation.processing.AbstractProcessor;
44import javax.annotation.processing.RoundEnvironment;
45import javax.annotation.processing.SupportedAnnotationTypes;
46import javax.lang.model.element.TypeElement;
47
48public class ModulesAndClassPathTest extends ModuleTestBase {
49
50    public static void main(String... args) throws Exception {
51        new ModulesAndClassPathTest().runTests();
52    }
53
54    @Test
55    void testModulesAndClassPath(Path base) throws Exception {
56        Path jar = prepareTestJar(base);
57
58        Path moduleSrc = base.resolve("module-src");
59        Path m1 = moduleSrc.resolve("m1");
60
61        Path classes = base.resolve("classes");
62
63        Files.createDirectories(classes);
64
65        tb.writeJavaFiles(m1,
66                          "module m1 { }",
67                          "package impl; public class Impl { api.Api api; }");
68
69        List<String> modLog = tb.new JavacTask()
70                                .options("-classpath", jar.toString(),
71                                         "-XDrawDiagnostics")
72                                .outdir(classes)
73                                .files(findJavaFiles(moduleSrc))
74                                .run(ToolBox.Expect.FAIL)
75                                .writeAll()
76                                .getOutputLines(ToolBox.OutputKind.DIRECT);
77
78        List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api",
79                                              "1 error");
80
81        if (!expected.equals(modLog)) {
82            throw new Exception("unexpected output: " + modLog);
83        }
84
85        tb.new JavacTask()
86          .options("-classpath", jar.toString(),
87                   "-XaddReads:m1=ALL-UNNAMED")
88          .outdir(classes)
89          .files(findJavaFiles(moduleSrc))
90          .run()
91          .writeAll()
92          .getOutputLines(ToolBox.OutputKind.DIRECT);
93
94        tb.new JavacTask()
95          .options("-classpath", jar.toString() + File.pathSeparator + System.getProperty("test.classes"),
96                   "-XaddReads:m1=ALL-UNNAMED",
97                   "-processor", ProcessorImpl.class.getName())
98          .outdir(classes)
99          .files(findJavaFiles(moduleSrc))
100          .run()
101          .writeAll()
102          .getOutputLines(ToolBox.OutputKind.DIRECT);
103    }
104
105    @Test
106    void testImplicitSourcePathModuleInfo(Path base) throws Exception {
107        Path jar = prepareTestJar(base);
108
109        Path moduleSrc = base.resolve("module-src");
110        Path m1 = moduleSrc.resolve("m1");
111
112        Path classes = base.resolve("classes");
113
114        Files.createDirectories(classes);
115
116        tb.writeJavaFiles(m1,
117                          "module m1 { }",
118                          "package impl; public class Impl { api.Api api; }");
119
120        List<String> modLog = tb.new JavacTask()
121                                .options("-classpath", jar.toString(),
122                                         "-sourcepath", m1.toString(),
123                                         "-XDrawDiagnostics")
124                                .outdir(classes)
125                                .files(m1.resolve("impl").resolve("Impl.java"))
126                                .run(ToolBox.Expect.FAIL)
127                                .writeAll()
128                                .getOutputLines(ToolBox.OutputKind.DIRECT);
129
130        List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api",
131                                              "1 error");
132
133        if (!expected.equals(modLog)) {
134            throw new Exception("unexpected output: " + modLog);
135        }
136    }
137
138    @Test
139    void testModuleInfoFromOutput(Path base) throws Exception {
140        Path jar = prepareTestJar(base);
141
142        Path moduleSrc = base.resolve("module-src");
143        Path m1 = moduleSrc.resolve("m1");
144
145        Path classes = base.resolve("classes");
146
147        Files.createDirectories(classes);
148
149        tb.writeJavaFiles(m1,
150                          "module m1 { }",
151                          "package impl; public class Impl { api.Api api; }");
152
153        tb.new JavacTask()
154          .options("-classpath", jar.toString(),
155                   "-XDrawDiagnostics")
156          .outdir(classes)
157          .files(m1.resolve("module-info.java"))
158          .run()
159          .writeAll()
160          .getOutputLines(ToolBox.OutputKind.DIRECT);
161
162        List<String> modLog = tb.new JavacTask()
163                                .options("-classpath", jar.toString(),
164                                         "-XDrawDiagnostics")
165                                .outdir(classes)
166                                .files(m1.resolve("impl").resolve("Impl.java"))
167                                .run(ToolBox.Expect.FAIL)
168                                .writeAll()
169                                .getOutputLines(ToolBox.OutputKind.DIRECT);
170
171        List<String> expected = Arrays.asList("Impl.java:1:38: compiler.err.doesnt.exist: api",
172                                              "1 error");
173
174        if (!expected.equals(modLog)) {
175            throw new Exception("unexpected output: " + modLog);
176        }
177    }
178
179    private Path prepareTestJar(Path base) throws Exception {
180        Path legacySrc = base.resolve("legacy-src");
181        tb.writeJavaFiles(legacySrc,
182                          "package api; public abstract class Api {}");
183        Path legacyClasses = base.resolve("legacy-classes");
184        Files.createDirectories(legacyClasses);
185
186        String log = tb.new JavacTask()
187                .options()
188                .outdir(legacyClasses)
189                .files(findJavaFiles(legacySrc))
190                .run()
191                .writeAll()
192                .getOutput(ToolBox.OutputKind.DIRECT);
193
194        if (!log.isEmpty()) {
195            throw new Exception("unexpected output: " + log);
196        }
197
198        Path lib = base.resolve("lib");
199
200        Files.createDirectories(lib);
201
202        Path jar = lib.resolve("test-api-1.0.jar");
203
204        tb.new JarTask(jar)
205          .baseDir(legacyClasses)
206          .files("api/Api.class")
207          .run();
208
209        return jar;
210    }
211
212    @SupportedAnnotationTypes("*")
213    public static class ProcessorImpl extends AbstractProcessor {
214        @Override
215        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
216            return false;
217        }
218    }
219
220    @Test
221    void testClassOutputVisibleForIncrementalCompilation(Path base) throws Exception {
222        Path moduleSrc = base.resolve("module-src");
223        Path m1 = moduleSrc.resolve("m1");
224
225        Path classes = base.resolve("classes");
226
227        Files.createDirectories(classes);
228
229        tb.writeJavaFiles(m1,
230                          "module m1 { exports impl; }",
231                          "package impl; public class Impl { }",
232                          "package src; public class Src { }",
233                          "package test; public class TestCP extends impl.Impl { }",
234                          "package test; public class TestSP extends src.Src { }");
235
236        tb.new JavacTask()
237          .outdir(classes)
238          .files(m1.resolve("impl").resolve("Impl.java"))
239          .run()
240          .writeAll()
241          .getOutputLines(ToolBox.OutputKind.DIRECT);
242
243        tb.new JavacTask()
244          .outdir(classes)
245          .files(m1.resolve("module-info.java"))
246          .run()
247          .writeAll()
248          .getOutputLines(ToolBox.OutputKind.DIRECT);
249
250        tb.new JavacTask()
251          .outdir(classes)
252          .files(m1.resolve("test").resolve("TestCP.java"))
253          .run()
254          .writeAll()
255          .getOutputLines(ToolBox.OutputKind.DIRECT);
256
257        tb.new JavacTask()
258          .options("-sourcepath", m1.toString())
259          .outdir(classes)
260          .files(m1.resolve("test").resolve("TestSP.java"))
261          .run()
262          .writeAll()
263          .getOutputLines(ToolBox.OutputKind.DIRECT);
264    }
265}
266