39
import java.nio.file.Path;
40
import java.nio.file.Paths;
41
import java.util.Arrays;
43
import java.util.stream.Collectors;
45
import org.testng.annotations.BeforeTest;
46
import org.testng.annotations.DataProvider;
47
import org.testng.annotations.Test;
49
import static org.testng.Assert.assertEquals;
50
import static org.testng.Assert.assertTrue;
52
public class ListModuleDeps {
53
private static final String TEST_SRC = System.getProperty("test.src");
55
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
56
private static final Path CLASSES_DIR = Paths.get("classes");
57
private static final Path LIB_DIR = Paths.get("lib");
58
private static final Path LIB2_DIR = Paths.get("lib2");
60
private static final Path HI_CLASS =
61
CLASSES_DIR.resolve("hi").resolve("Hi.class");
62
private static final Path FOO_CLASS =
63
CLASSES_DIR.resolve("z").resolve("Foo.class");
64
private static final Path BAR_CLASS =
65
CLASSES_DIR.resolve("z").resolve("Bar.class");
66
private static final Path UNSAFE_CLASS =
67
CLASSES_DIR.resolve("z").resolve("UseUnsafe.class");
73
public void compileAll() throws Exception {
75
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib2"), LIB2_DIR));
76
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib"), LIB_DIR, "-cp", LIB2_DIR.toString()));
79
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "hi"), CLASSES_DIR));
82
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "z"),
84
"-cp", LIB_DIR.toString(),
85
"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
86
"--add-exports=java.base/sun.security.util=ALL-UNNAMED",
87
"--add-exports=java.xml/jdk.xml.internal=ALL-UNNAMED"
91
@DataProvider(name = "jdkModules")
92
public Object[][] jdkModules() {
93
return new Object[][]{
94
{"jdk.compiler", new String[]{
95
"java.base/jdk.internal.javac",
96
"java.base/jdk.internal.jmod",
97
"java.base/jdk.internal.misc",
98
"java.base/jdk.internal.module",
99
"java.base/sun.reflect.annotation",
101
"jdk.internal.opt/jdk.internal.opt",
107
@Test(dataProvider = "jdkModules")
108
public void testJDKModule(String moduleName, String[] expected) {
109
JdepsRunner jdeps = JdepsRunner.run(
110
"--list-deps", "-m", moduleName
112
String[] output = Arrays.stream(jdeps.output())
114
.toArray(String[]::new);
115
assertEquals(output, expected);
118
@Test(dataProvider = "listdeps")
119
public void testListDeps(Path classes, String[] expected) {
120
JdepsRunner jdeps = JdepsRunner.run(
121
"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
122
"--list-deps", classes.toString()
124
String[] output = Arrays.stream(jdeps.output())
126
.toArray(String[]::new);
127
assertEquals(output, expected);
130
@Test(dataProvider = "reduceddeps")
131
public void testListReducedDeps(Path classes, String[] expected) {
132
JdepsRunner jdeps = JdepsRunner.run(
133
"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
134
"--list-reduced-deps", classes.toString()
136
String[] output = Arrays.stream(jdeps.output())
138
.toArray(String[]::new);
139
assertEquals(output, expected);
143
@DataProvider(name = "listdeps")
144
public Object[][] listdeps() {
145
return new Object[][] {
146
{ CLASSES_DIR, new String[] {
147
"java.base/jdk.internal.misc",
148
"java.base/sun.security.util",
152
"java.xml/jdk.xml.internal",
157
{ HI_CLASS, new String[] {
162
{ FOO_CLASS, new String[] {
171
{ BAR_CLASS, new String[] {
172
"java.base/sun.security.util",
173
"java.xml/jdk.xml.internal",
177
{ UNSAFE_CLASS, new String[] {
178
"java.base/jdk.internal.misc",
185
@DataProvider(name = "reduceddeps")
186
public Object[][] reduceddeps() {
187
Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
189
return new Object[][] {
190
{ CLASSES_DIR, new String[] {
191
"java.base/jdk.internal.misc",
192
"java.base/sun.security.util",
195
"java.xml/jdk.xml.internal",
200
{ HI_CLASS, new String[] {
205
{ FOO_CLASS, new String[] {
212
{ BAR_CLASS, new String[] {
213
"java.base/sun.security.util",
214
"java.xml/jdk.xml.internal",
218
{ UNSAFE_CLASS, new String[] {
219
"java.base/jdk.internal.misc",
226
@Test(dataProvider = "moduledeps")
227
public void testPrintModuleDeps(Path classes, String expected) {
228
JdepsRunner jdeps = JdepsRunner.run(
229
"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
230
"--print-module-deps", classes.toString()
232
String output = Arrays.stream(jdeps.output())
234
.collect(Collectors.joining(","));
235
assertEquals(output, expected);
239
@DataProvider(name = "moduledeps")
240
public Object[][] moduledeps() {
241
Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
243
return new Object[][] {
245
{ CLASSES_DIR, "java.base,java.management,java.sql,jdk.unsupported"},
246
{ HI_CLASS, "java.base"},
247
{ FOO_CLASS, "java.base,java.management,java.sql"},
248
{ BAR_CLASS, "java.base,java.xml"},
249
{ UNSAFE_CLASS, "java.base,jdk.unsupported"},
253
@Test(dataProvider = "noRecursiveModuledeps")
254
public void testNoRecursiveModuleDeps(Path classes, String expected) {
255
JdepsRunner jdeps = JdepsRunner.run(
256
"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),
257
"--print-module-deps", "--no-recursive", classes.toString()
259
String output = Arrays.stream(jdeps.output())
261
.collect(Collectors.joining(","));
262
assertEquals(output, expected);
265
@DataProvider(name = "noRecursiveModuledeps")
266
public Object[][] noRecursiveModuledeps() {
267
Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
269
return new Object[][] {
271
{ CLASSES_DIR, "java.base,java.sql,jdk.unsupported"},
272
{ HI_CLASS, "java.base"},
273
{ FOO_CLASS, "java.base,java.sql"},
274
{ BAR_CLASS, "java.base,java.xml"},
275
{ UNSAFE_CLASS, "java.base,jdk.unsupported"},
279
@DataProvider(name = "recursiveDeps")
280
public Object[][] recursiveDeps() {
281
return new Object[][] {
284
List.of("--list-deps", "--class-path", LIB2_DIR.toString(),
285
"--ignore-missing-deps", CLASSES_DIR.toString()),
287
"java.base/jdk.internal.misc",
288
"java.base/sun.security.util",
291
"java.xml/jdk.xml.internal",
297
List.of("--print-module-deps", "--class-path", LIB2_DIR.toString(),
298
"--ignore-missing-deps", CLASSES_DIR.toString()),
300
"java.base,java.sql,jdk.unsupported"
304
List.of("--print-module-deps",
305
"--ignore-missing-deps", FOO_CLASS.toString()),
313
@Test(dataProvider = "recursiveDeps")
314
public void testRecursiveDeps(List<String> options, String[] expected) {
315
JdepsRunner jdeps = JdepsRunner.run(options.toArray(new String[0]));
316
String[] output = Arrays.stream(jdeps.output())
318
.toArray(String[]::new);
319
assertEquals(output, expected);