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  * @summary Tests jdeps -m and --module-path options on named modules and unnamed modules
27  * @library ../lib
28  * @build CompilerUtils JdepsUtil
29  * @modules jdk.jdeps/com.sun.tools.jdeps
30  * @run testng ModuleTest
31  */
32 
33 import java.io.File;
34 import java.io.IOException;
35 
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.*;
40 import java.util.stream.Collectors;
41 
42 
43 import com.sun.tools.jdeps.DepsAnalyzer;
44 import com.sun.tools.jdeps.Graph;
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.BeforeTest;
47 import org.testng.annotations.Test;
48 
49 import static org.testng.Assert.assertTrue;
50 
51 public class ModuleTest {
52     private static final String TEST_SRC = System.getProperty("test.src");
53     private static final String TEST_CLASSES = System.getProperty("test.classes");
54 
55     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
56     private static final Path MODS_DIR = Paths.get("mods");
57     private static final Path UNNAMED_DIR = Paths.get("unnamed");
58 
59     // the names of the modules in this test
60     private static final String UNSUPPORTED = "unsupported";
61     private static String[] modules = new String[] {"mI", "mII", "mIII", "mIV", UNSUPPORTED};
62     /**
63      * Compiles all modules used by the test
64      */
65     @BeforeTest
compileAll()66     public void compileAll() throws Exception {
67         CompilerUtils.cleanDir(MODS_DIR);
68         CompilerUtils.cleanDir(UNNAMED_DIR);
69 
70         assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, UNSUPPORTED,
71                                                "--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));
72         // m4 is not referenced
73         Arrays.asList("mI", "mII", "mIII", "mIV")
74               .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));
75 
76         assertTrue(CompilerUtils.compile(SRC_DIR.resolve("mIII"), UNNAMED_DIR, "-p", MODS_DIR.toString()));
77         Files.delete(UNNAMED_DIR.resolve("module-info.class"));
78     }
79 
80     @DataProvider(name = "modules")
expected()81     public Object[][] expected() {
82         return new Object[][]{
83                 { "mIII", new ModuleMetaData("mIII").requiresTransitive("java.sql")
84                             .requiresTransitive("mII")
85                             .requires("java.logging")
86                             .requiresTransitive("mI")
87                             .reference("p3", "java.lang", "java.base")
88                             .reference("p3", "java.sql", "java.sql")
89                             .reference("p3", "java.util.logging", "java.logging")
90                             .reference("p3", "p1", "mI")
91                             .reference("p3", "p2", "mII")
92                             .qualified("p3", "p2.internal", "mII")
93                 },
94                 { "mII", new ModuleMetaData("mII").requiresTransitive("mI")
95                             .reference("p2", "java.lang", "java.base")
96                             .reference("p2", "p1", "mI")
97                             .reference("p2.internal", "java.lang", "java.base")
98                             .reference("p2.internal", "java.io", "java.base")
99                 },
100                 { "mI", new ModuleMetaData("mI").requires("unsupported")
101                             .reference("p1", "java.lang", "java.base")
102                             .reference("p1.internal", "java.lang", "java.base")
103                             .reference("p1.internal", "p1", "mI")
104                             .reference("p1.internal", "q", "unsupported")
105                 },
106                 { "unsupported", new ModuleMetaData("unsupported")
107                             .reference("q", "java.lang", "java.base")
108                             .jdkInternal("q", "jdk.internal.perf", "java.base")
109                 },
110         };
111     }
112 
113     @Test(dataProvider = "modules")
modularTest(String name, ModuleMetaData data)114     public void modularTest(String name, ModuleMetaData data) throws IOException {
115         // jdeps --module-path mods -m <name>
116         runTest(data, MODS_DIR.toString(), Set.of(name));
117 
118         // jdeps --module-path libs/mI.jar:.... -m <name>
119         String mp = Arrays.stream(modules)
120                 .filter(mn -> !mn.equals(name))
121                 .map(mn -> MODS_DIR.resolve(mn).toString())
122                 .collect(Collectors.joining(File.pathSeparator));
123         runTest(data, mp, Collections.emptySet(), MODS_DIR.resolve(name));
124     }
125 
126     @DataProvider(name = "unnamed")
unnamed()127     public Object[][] unnamed() {
128         return new Object[][]{
129                 { "unnamed", new ModuleMetaData("unnamed", false)
130                             .depends("java.sql")
131                             .depends("java.logging")
132                             .depends("mI")
133                             .depends("mII")
134                             .reference("p3", "java.lang", "java.base")
135                             .reference("p3", "java.sql", "java.sql")
136                             .reference("p3", "java.util.logging", "java.logging")
137                             .reference("p3", "p1", "mI")
138                             .reference("p3", "p2", "mII")
139                             .internal("p3", "p2.internal", "mII")
140                 },
141         };
142     }
143 
144     @Test(dataProvider = "unnamed")
unnamedTest(String name, ModuleMetaData data)145     public void unnamedTest(String name, ModuleMetaData data) throws IOException {
146         runTest(data, MODS_DIR.toString(), Set.of("mI", "mII"), UNNAMED_DIR);
147     }
148 
runTest(ModuleMetaData data, String modulepath, Set<String> roots, Path... paths)149     private void runTest(ModuleMetaData data, String modulepath,
150                          Set<String> roots, Path... paths)
151         throws IOException
152     {
153         // jdeps --module-path <modulepath> -m root paths
154         String cmd = String.format("jdeps --module-path %s --add-modules %s %s%n",
155             MODS_DIR, roots.stream().collect(Collectors.joining(",")), paths);
156 
157         try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
158             jdeps.appModulePath(modulepath)
159                 .addmods(roots);
160             Arrays.stream(paths).forEach(jdeps::addRoot);
161 
162             // run the analyzer
163             DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
164             assertTrue(analyzer.run());
165 
166             // analyze result
167             Graph<DepsAnalyzer.Node> g1 = analyzer.moduleGraph();
168             g1.nodes().stream()
169                 .filter(u -> u.name.equals(data.moduleName))
170                 .forEach(u -> data.checkRequires(u.name, g1.adjacentNodes(u)));
171 
172             Graph<DepsAnalyzer.Node> g2 = analyzer.dependenceGraph();
173             g2.nodes().stream()
174                 .filter(u -> u.name.equals(data.moduleName))
175                 .forEach(u -> data.checkDependences(u.name, g2.adjacentNodes(u)));
176 
177             jdeps.dumpOutput(System.err);
178         }
179     }
180 }
181