1/*
2 * Copyright (c) 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 * @bug 8167057
27 * @summary Tests --list-deps and --list-reduced-deps options
28 * @modules java.logging
29 *          java.xml
30 *          jdk.compiler
31 *          jdk.jdeps
32 *          jdk.unsupported
33 * @library ../lib
34 * @build CompilerUtils JdepsRunner
35 * @run testng ListModuleDeps
36 */
37
38import java.nio.file.Path;
39import java.nio.file.Paths;
40import java.util.Arrays;
41
42import org.testng.annotations.BeforeTest;
43import org.testng.annotations.DataProvider;
44import org.testng.annotations.Test;
45
46import static org.testng.Assert.assertEquals;
47import static org.testng.Assert.assertTrue;
48
49public class ListModuleDeps {
50    private static final String TEST_SRC = System.getProperty("test.src");
51
52    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
53    private static final Path CLASSES_DIR = Paths.get("classes");
54    private static final Path LIB_DIR = Paths.get("lib");
55
56    private static final Path HI_CLASS =
57        CLASSES_DIR.resolve("hi").resolve("Hi.class");
58    private static final Path FOO_CLASS =
59        CLASSES_DIR.resolve("z").resolve("Foo.class");
60    private static final Path BAR_CLASS =
61        CLASSES_DIR.resolve("z").resolve("Bar.class");
62    private static final Path UNSAFE_CLASS =
63        CLASSES_DIR.resolve("z").resolve("UseUnsafe.class");
64
65    /**
66     * Compiles classes used by the test
67     */
68    @BeforeTest
69    public void compileAll() throws Exception {
70        // compile library
71        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib"), LIB_DIR));
72
73        // simple program depends only on java.base
74        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "hi"), CLASSES_DIR));
75
76        // compile classes in unnamed module
77        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "z"),
78            CLASSES_DIR,
79            "-cp", LIB_DIR.toString(),
80            "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
81            "--add-exports=java.base/sun.security.util=ALL-UNNAMED",
82            "--add-exports=java.xml/jdk.xml.internal=ALL-UNNAMED"
83        ));
84    }
85
86    @DataProvider(name = "jdkModules")
87    public Object[][] jdkModules() {
88        return new Object[][]{
89            {"jdk.compiler", new String[]{
90                                "java.base/sun.reflect.annotation",
91                                "java.compiler",
92                             }
93            },
94        };
95    }
96
97    @Test(dataProvider = "jdkModules")
98    public void testJDKModule(String moduleName, String[] expected) {
99        JdepsRunner jdeps = JdepsRunner.run(
100            "--list-deps", "-m", moduleName
101        );
102        String[] output = Arrays.stream(jdeps.output())
103                                .map(s -> s.trim())
104                                .toArray(String[]::new);
105        assertEquals(output, expected);
106    }
107
108    @Test(dataProvider = "listdeps")
109    public void testListDeps(Path classes, String[] expected) {
110        JdepsRunner jdeps = JdepsRunner.run(
111            "--class-path", LIB_DIR.toString(),
112            "--list-deps", classes.toString()
113        );
114        String[] output = Arrays.stream(jdeps.output())
115                                .map(s -> s.trim())
116                                .toArray(String[]::new);
117        assertEquals(output, expected);
118    }
119
120    @Test(dataProvider = "reduceddeps")
121    public void testListReducedDeps(Path classes, String[]  expected) {
122        JdepsRunner jdeps = JdepsRunner.run(
123            "--class-path", LIB_DIR.toString(),
124            "--list-reduced-deps", classes.toString()
125        );
126        String[] output = Arrays.stream(jdeps.output())
127                                .map(s -> s.trim())
128                                .toArray(String[]::new);
129        assertEquals(output, expected);
130    }
131
132
133    @DataProvider(name = "listdeps")
134    public Object[][] listdeps() {
135        return new Object[][] {
136            { CLASSES_DIR,  new String[] {
137                                "java.base/jdk.internal.misc",
138                                "java.base/sun.security.util",
139                                "java.logging",
140                                "java.sql",
141                                "java.xml/jdk.xml.internal",
142                                "jdk.unsupported",
143                                "unnamed module: lib"
144                            }
145            },
146
147            { HI_CLASS,     new String[] {
148                                "java.base"
149                            }
150            },
151
152            { FOO_CLASS,    new String[] {
153                                "java.base",
154                                "java.logging",
155                                "java.sql",
156                                "java.xml",
157                                "unnamed module: lib"
158                            }
159            },
160
161            { BAR_CLASS,    new String[] {
162                                "java.base/sun.security.util",
163                                "java.xml/jdk.xml.internal",
164                            }
165            },
166
167            { UNSAFE_CLASS, new String[] {
168                                "java.base/jdk.internal.misc",
169                                "jdk.unsupported",
170                            }
171            },
172        };
173    }
174
175    @DataProvider(name = "reduceddeps")
176    public Object[][] reduceddeps() {
177        Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");
178
179        return new Object[][] {
180            { CLASSES_DIR,  new String[] {
181                                "java.base/jdk.internal.misc",
182                                "java.base/sun.security.util",
183                                "java.sql",
184                                "java.xml/jdk.xml.internal",
185                                "jdk.unsupported",
186                                "unnamed module: lib"
187                            }
188            },
189
190            { HI_CLASS,     new String[] {
191                                "java.base"
192                            }
193            },
194
195            { FOO_CLASS,    new String[] {
196                                "java.sql",
197                                "unnamed module: lib"
198                            }
199            },
200
201            { BAR_CLASS,    new String[] {
202                                "java.base/sun.security.util",
203                                "java.xml/jdk.xml.internal",
204                            }
205            },
206
207            { UNSAFE_CLASS, new String[] {
208                                "java.base/jdk.internal.misc",
209                                "jdk.unsupported",
210                            }
211            },
212        };
213    }
214
215}
216