56 private static final String TEST_SRC = System.getProperty("test.src"); |
57 private static final String TEST_SRC = System.getProperty("test.src"); |
57 |
58 |
58 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); |
59 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); |
59 private static final Path MODS_DIR = Paths.get("mods"); |
60 private static final Path MODS_DIR = Paths.get("mods"); |
60 private static final Path LIBS_DIR = Paths.get("libs"); |
61 private static final Path LIBS_DIR = Paths.get("libs"); |
|
62 private static final Path MLIBS_DIR = Paths.get("mlibs"); |
61 private static final Path DEST_DIR = Paths.get("moduleinfosrc"); |
63 private static final Path DEST_DIR = Paths.get("moduleinfosrc"); |
62 private static final Path NEW_MODS_DIR = Paths.get("new_mods"); |
64 private static final Path NEW_MODS_DIR = Paths.get("new_mods"); |
63 |
65 |
64 // the names of the modules in this test |
66 // the names of the modules in this test |
65 public static final String UNSUPPORTED = "unsupported"; |
67 public static final String UNSUPPORTED = "unsupported"; |
66 public static final Set<String> MODULES = Set.of( |
68 public static final Set<String> MODULES = Set.of( |
67 "mI", "mII", "mIII", "provider", UNSUPPORTED |
69 "mI", "mII", "mIII", "provider", "test", UNSUPPORTED |
68 ); |
70 ); |
|
71 |
|
72 @BeforeTest |
|
73 public void setup() throws Exception { |
|
74 compileAndCreateJars(); |
|
75 } |
69 |
76 |
70 /** |
77 /** |
71 * Compile modules |
78 * Compile modules |
72 */ |
79 */ |
73 public static void compileModules(Path dest) { |
80 public static void compileModules(Path dest) { |
74 assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED, |
81 assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED, |
75 "--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED)); |
82 "--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED)); |
76 MODULES.stream() |
83 MODULES.stream() |
77 .filter(mn -> !mn.equals(UNSUPPORTED)) |
84 .filter(mn -> !mn.equals(UNSUPPORTED)) |
78 .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn))); |
85 .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn))); |
|
86 } |
|
87 |
|
88 /** |
|
89 * Create JAR files with no module-info.class |
|
90 */ |
|
91 public static void createModularJARs(Path mods, Path dest, String... modules) throws IOException { |
|
92 Files.createDirectory(dest); |
|
93 // create modular JAR |
|
94 for (String mn : modules) { |
|
95 Path root = mods.resolve(mn); |
|
96 try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, |
|
97 (p, attr) -> { return attr.isRegularFile(); })) { |
|
98 JdepsUtil.createJar(dest.resolve(mn + ".jar"), root, stream); |
|
99 } |
|
100 } |
79 } |
101 } |
80 |
102 |
81 /** |
103 /** |
82 * Create JAR files with no module-info.class |
104 * Create JAR files with no module-info.class |
83 */ |
105 */ |
139 mn, "-p", dest.toString())) |
161 mn, "-p", dest.toString())) |
140 ); |
162 ); |
141 |
163 |
142 } |
164 } |
143 |
165 |
144 /** |
166 public static void compileAndCreateJars() throws Exception { |
145 * Compiles all modules used by the test |
|
146 */ |
|
147 @BeforeTest |
|
148 public void compileAll() throws Exception { |
|
149 CompilerUtils.cleanDir(MODS_DIR); |
167 CompilerUtils.cleanDir(MODS_DIR); |
150 CompilerUtils.cleanDir(LIBS_DIR); |
168 CompilerUtils.cleanDir(LIBS_DIR); |
151 CompilerUtils.cleanDir(DEST_DIR); |
|
152 CompilerUtils.cleanDir(NEW_MODS_DIR); |
|
153 |
169 |
154 compileModules(MODS_DIR); |
170 compileModules(MODS_DIR); |
155 |
171 |
|
172 // create modular JARs except test |
|
173 createModularJARs(MODS_DIR, MLIBS_DIR, MODULES.stream().filter(mn -> !mn.equals("test")) |
|
174 .toArray(String[]::new)); |
|
175 // create non-modular JARs |
156 createJARFiles(MODS_DIR, LIBS_DIR); |
176 createJARFiles(MODS_DIR, LIBS_DIR); |
157 } |
177 } |
158 |
178 |
159 @Test |
179 @Test |
160 public void automaticModules() throws IOException { |
180 public void automaticModules() throws IOException { |
164 JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new)); |
184 JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new)); |
165 } |
185 } |
166 |
186 |
167 @Test |
187 @Test |
168 public void test() throws IOException { |
188 public void test() throws IOException { |
169 Files.createDirectory(DEST_DIR); |
189 Path dest = DEST_DIR.resolve("case1"); |
|
190 Path classes = NEW_MODS_DIR.resolve("case1"); |
|
191 Files.createDirectories(dest); |
|
192 Files.createDirectories(classes); |
170 |
193 |
171 Stream<String> files = MODULES.stream() |
194 Stream<String> files = MODULES.stream() |
172 .map(mn -> LIBS_DIR.resolve(mn + ".jar")) |
195 .map(mn -> LIBS_DIR.resolve(mn + ".jar")) |
173 .map(Path::toString); |
196 .map(Path::toString); |
174 |
197 |
175 Stream<String> options = Stream.concat( |
198 Stream<String> options = Stream.concat( |
176 Stream.of("--generate-module-info", DEST_DIR.toString()), files); |
199 Stream.of("--generate-module-info", dest.toString()), files); |
177 JdepsRunner.run(options.toArray(String[]::new)); |
200 JdepsRunner.run(options.toArray(String[]::new)); |
178 |
201 |
179 // check file exists |
202 // check file exists |
180 MODULES.stream() |
203 MODULES.stream() |
181 .map(mn -> DEST_DIR.resolve(mn).resolve("module-info.java")) |
204 .map(mn -> dest.resolve(mn).resolve("module-info.java")) |
182 .forEach(f -> assertTrue(Files.exists(f))); |
205 .forEach(f -> assertTrue(Files.exists(f))); |
183 |
206 |
184 // copy classes to a temporary directory |
207 // copy classes to a temporary directory |
185 // and then compile new module-info.java |
208 // and then compile new module-info.java |
186 copyClasses(MODS_DIR, NEW_MODS_DIR); |
209 copyClasses(MODS_DIR, classes); |
187 compileNewGenModuleInfo(DEST_DIR, NEW_MODS_DIR); |
210 compileNewGenModuleInfo(dest, classes); |
188 |
211 |
189 for (String mn : MODULES) { |
212 for (String mn : MODULES) { |
190 Path p1 = NEW_MODS_DIR.resolve(mn).resolve(MODULE_INFO); |
213 verify(mn, classes, MODS_DIR); |
191 Path p2 = MODS_DIR.resolve(mn).resolve(MODULE_INFO); |
214 } |
192 |
215 } |
193 try (InputStream in1 = Files.newInputStream(p1); |
216 |
194 InputStream in2 = Files.newInputStream(p2)) { |
217 @Test |
195 verify(ModuleDescriptor.read(in1), |
218 public void withModulePath() throws IOException { |
196 ModuleDescriptor.read(in2, () -> packages(MODS_DIR.resolve(mn)))); |
219 Path dest = DEST_DIR.resolve("case2"); |
197 } |
220 Path classes = NEW_MODS_DIR.resolve("case2"); |
|
221 Files.createDirectories(dest); |
|
222 Files.createDirectories(classes); |
|
223 |
|
224 JdepsRunner.run("--module-path", MLIBS_DIR.toString(), |
|
225 "--generate-module-info", dest.toString(), |
|
226 LIBS_DIR.resolve("test.jar").toString()); |
|
227 |
|
228 String name = "test"; |
|
229 Path gensrc = dest.resolve(name).resolve("module-info.java"); |
|
230 assertTrue(Files.exists(gensrc)); |
|
231 |
|
232 // copy classes to a temporary directory |
|
233 // and then compile new module-info.java |
|
234 copyClasses(MODS_DIR.resolve(name), classes.resolve(name)); |
|
235 assertTrue(CompilerUtils.compileModule(dest, classes, name, "-p", MLIBS_DIR.toString())); |
|
236 |
|
237 verify(name, classes, MODS_DIR); |
|
238 } |
|
239 |
|
240 /** |
|
241 * Verify the dependences from the given module-info.class files |
|
242 */ |
|
243 public void verify(String mn, Path mdir1, Path mdir2) throws IOException { |
|
244 Path p1 = mdir1.resolve(mn).resolve(MODULE_INFO); |
|
245 Path p2 = mdir2.resolve(mn).resolve(MODULE_INFO); |
|
246 try (InputStream in1 = Files.newInputStream(p1); |
|
247 InputStream in2 = Files.newInputStream(p2)) { |
|
248 verify(ModuleDescriptor.read(in1), |
|
249 ModuleDescriptor.read(in2, () -> packages(mdir2.resolve(mn)))); |
198 } |
250 } |
199 } |
251 } |
200 |
252 |
201 /** |
253 /** |
202 * Copy classes except the module-info.class to the destination directory |
254 * Copy classes except the module-info.class to the destination directory |