36526
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
* @summary Test automatic modules
|
|
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 AutomaticModules
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.Path;
|
|
38 |
|
|
39 |
public class AutomaticModules extends ModuleTestBase {
|
|
40 |
|
|
41 |
public static void main(String... args) throws Exception {
|
|
42 |
AutomaticModules t = new AutomaticModules();
|
|
43 |
t.runTests();
|
|
44 |
}
|
|
45 |
|
|
46 |
@Test
|
|
47 |
void testSimple(Path base) throws Exception {
|
|
48 |
Path legacySrc = base.resolve("legacy-src");
|
|
49 |
tb.writeJavaFiles(legacySrc,
|
|
50 |
"package api; import java.awt.event.ActionListener; public abstract class Api implements ActionListener {}");
|
|
51 |
Path legacyClasses = base.resolve("legacy-classes");
|
|
52 |
Files.createDirectories(legacyClasses);
|
|
53 |
|
|
54 |
String log = tb.new JavacTask()
|
|
55 |
.options()
|
|
56 |
.outdir(legacyClasses)
|
|
57 |
.files(findJavaFiles(legacySrc))
|
|
58 |
.run()
|
|
59 |
.writeAll()
|
|
60 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
61 |
|
|
62 |
if (!log.isEmpty()) {
|
|
63 |
throw new Exception("unexpected output: " + log);
|
|
64 |
}
|
|
65 |
|
|
66 |
Path modulePath = base.resolve("module-path");
|
|
67 |
|
|
68 |
Files.createDirectories(modulePath);
|
|
69 |
|
|
70 |
Path jar = modulePath.resolve("test-api-1.0.jar");
|
|
71 |
|
|
72 |
tb.new JarTask(jar)
|
|
73 |
.baseDir(legacyClasses)
|
|
74 |
.files("api/Api.class")
|
|
75 |
.run();
|
|
76 |
|
|
77 |
Path moduleSrc = base.resolve("module-src");
|
|
78 |
Path m1 = moduleSrc.resolve("m1");
|
|
79 |
|
|
80 |
Path classes = base.resolve("classes");
|
|
81 |
|
|
82 |
Files.createDirectories(classes);
|
|
83 |
|
|
84 |
tb.writeJavaFiles(m1,
|
|
85 |
"module m1 { requires test.api; }",
|
|
86 |
"package impl; public class Impl { public void e(api.Api api) { api.actionPerformed(null); } }");
|
|
87 |
|
|
88 |
tb.new JavacTask()
|
|
89 |
.options("-modulesourcepath", moduleSrc.toString(), "-modulepath", modulePath.toString(), "-addmods", "java.desktop")
|
|
90 |
.outdir(classes)
|
|
91 |
.files(findJavaFiles(moduleSrc))
|
|
92 |
.run()
|
|
93 |
.writeAll();
|
|
94 |
}
|
|
95 |
|
|
96 |
@Test
|
|
97 |
void testUnnamedModule(Path base) throws Exception {
|
|
98 |
Path legacySrc = base.resolve("legacy-src");
|
|
99 |
tb.writeJavaFiles(legacySrc,
|
|
100 |
"package api; public abstract class Api { public void run(CharSequence str) { } private void run(base.Base base) { } }",
|
|
101 |
"package base; public interface Base { public void run(); }");
|
|
102 |
Path legacyClasses = base.resolve("legacy-classes");
|
|
103 |
Files.createDirectories(legacyClasses);
|
|
104 |
|
|
105 |
String log = tb.new JavacTask()
|
|
106 |
.options()
|
|
107 |
.outdir(legacyClasses)
|
|
108 |
.files(findJavaFiles(legacySrc))
|
|
109 |
.run()
|
|
110 |
.writeAll()
|
|
111 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
112 |
|
|
113 |
if (!log.isEmpty()) {
|
|
114 |
throw new Exception("unexpected output: " + log);
|
|
115 |
}
|
|
116 |
|
|
117 |
Path modulePath = base.resolve("module-path");
|
|
118 |
|
|
119 |
Files.createDirectories(modulePath);
|
|
120 |
|
|
121 |
Path apiJar = modulePath.resolve("test-api-1.0.jar");
|
|
122 |
|
|
123 |
tb.new JarTask(apiJar)
|
|
124 |
.baseDir(legacyClasses)
|
|
125 |
.files("api/Api.class")
|
|
126 |
.run();
|
|
127 |
|
|
128 |
Path baseJar = base.resolve("base.jar");
|
|
129 |
|
|
130 |
tb.new JarTask(baseJar)
|
|
131 |
.baseDir(legacyClasses)
|
|
132 |
.files("base/Base.class")
|
|
133 |
.run();
|
|
134 |
|
|
135 |
Path moduleSrc = base.resolve("module-src");
|
|
136 |
Path m1 = moduleSrc.resolve("m1");
|
|
137 |
|
|
138 |
Path classes = base.resolve("classes");
|
|
139 |
|
|
140 |
Files.createDirectories(classes);
|
|
141 |
|
|
142 |
tb.writeJavaFiles(m1,
|
|
143 |
"module m1 { requires test.api; }",
|
|
144 |
"package impl; public class Impl { public void e(api.Api api) { api.run(\"\"); } }");
|
|
145 |
|
|
146 |
tb.new JavacTask()
|
|
147 |
.options("-modulesourcepath", moduleSrc.toString(), "-modulepath", modulePath.toString(), "-classpath", baseJar.toString())
|
|
148 |
.outdir(classes)
|
|
149 |
.files(findJavaFiles(moduleSrc))
|
|
150 |
.run()
|
|
151 |
.writeAll();
|
|
152 |
}
|
|
153 |
|
|
154 |
@Test
|
|
155 |
void testModuleInfoFromClassFileDependsOnAutomatic(Path base) throws Exception {
|
|
156 |
Path automaticSrc = base.resolve("automaticSrc");
|
|
157 |
tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
|
|
158 |
Path automaticClasses = base.resolve("automaticClasses");
|
|
159 |
tb.createDirectories(automaticClasses);
|
|
160 |
|
|
161 |
String automaticLog = tb.new JavacTask()
|
|
162 |
.outdir(automaticClasses)
|
|
163 |
.files(findJavaFiles(automaticSrc))
|
|
164 |
.run()
|
|
165 |
.writeAll()
|
|
166 |
.getOutput(ToolBox.OutputKind.DIRECT);
|
|
167 |
|
|
168 |
if (!automaticLog.isEmpty())
|
|
169 |
throw new Exception("expected output not found: " + automaticLog);
|
|
170 |
|
|
171 |
Path modulePath = base.resolve("module-path");
|
|
172 |
|
|
173 |
Files.createDirectories(modulePath);
|
|
174 |
|
|
175 |
Path automaticJar = modulePath.resolve("automatic-1.0.jar");
|
|
176 |
|
|
177 |
tb.new JarTask(automaticJar)
|
|
178 |
.baseDir(automaticClasses)
|
|
179 |
.files("api/Api.class")
|
|
180 |
.run();
|
|
181 |
|
|
182 |
Path depSrc = base.resolve("depSrc");
|
|
183 |
Path depClasses = base.resolve("depClasses");
|
|
184 |
|
|
185 |
Files.createDirectories(depSrc);
|
|
186 |
Files.createDirectories(depClasses);
|
|
187 |
|
|
188 |
tb.writeJavaFiles(depSrc,
|
|
189 |
"module m1 { requires public automatic; }",
|
|
190 |
"package dep; public class Dep { api.Api api; }");
|
|
191 |
|
|
192 |
tb.new JavacTask()
|
|
193 |
.options("-modulepath", modulePath.toString())
|
|
194 |
.outdir(depClasses)
|
|
195 |
.files(findJavaFiles(depSrc))
|
|
196 |
.run()
|
|
197 |
.writeAll();
|
|
198 |
|
|
199 |
Path moduleJar = modulePath.resolve("m1.jar");
|
|
200 |
|
|
201 |
tb.new JarTask(moduleJar)
|
|
202 |
.baseDir(depClasses)
|
|
203 |
.files("module-info.class", "dep/Dep.class")
|
|
204 |
.run();
|
|
205 |
|
|
206 |
Path testSrc = base.resolve("testSrc");
|
|
207 |
Path testClasses = base.resolve("testClasses");
|
|
208 |
|
|
209 |
Files.createDirectories(testSrc);
|
|
210 |
Files.createDirectories(testClasses);
|
|
211 |
|
|
212 |
tb.writeJavaFiles(testSrc,
|
|
213 |
"module m2 { requires automatic; }",
|
|
214 |
"package test; public class Test { }");
|
|
215 |
|
|
216 |
tb.new JavacTask()
|
|
217 |
.options("-modulepath", modulePath.toString())
|
|
218 |
.outdir(testClasses)
|
|
219 |
.files(findJavaFiles(testSrc))
|
|
220 |
.run()
|
|
221 |
.writeAll();
|
|
222 |
}
|
|
223 |
}
|