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 -mp 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[] {"m1", "m2", "m3", "m4", UNSUPPORTED};
  62     /**
  63      * Compiles all modules used by the test
  64      */
  65     @BeforeTest
  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                                                "-XaddExports:java.base/jdk.internal.perf=" + UNSUPPORTED));
  72         // m4 is not referenced
  73         Arrays.asList("m1", "m2", "m3", "m4")
  74               .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));
  75 
  76         assertTrue(CompilerUtils.compile(SRC_DIR.resolve("m3"), UNNAMED_DIR, "-mp", MODS_DIR.toString()));
  77         Files.delete(UNNAMED_DIR.resolve("module-info.class"));
  78     }
  79 
  80     @DataProvider(name = "modules")
  81     public Object[][] expected() {
  82         return new Object[][]{
  83                 { "m3", new ModuleMetaData("m3").requiresPublic("java.sql")
  84                             .requiresPublic("m2")
  85                             .requires("java.logging")
  86                             .requiresPublic("m1")
  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", "m1")
  91                             .reference("p3", "p2", "m2")
  92                             .qualified("p3", "p2.internal", "m2")
  93                 },
  94                 { "m2", new ModuleMetaData("m2").requiresPublic("m1")
  95                             .reference("p2", "java.lang", "java.base")
  96                             .reference("p2", "p1", "m1")
  97                             .reference("p2.internal", "java.lang", "java.base")
  98                             .reference("p2.internal", "java.io", "java.base")
  99                 },
 100                 { "m1", new ModuleMetaData("m1").requires("unsupported")
 101                             .reference("p1", "java.lang", "java.base")
 102                             .reference("p1.internal", "java.lang", "java.base")
 103                             .reference("p1.internal", "p1", "m1")
 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")
 114     public void modularTest(String name, ModuleMetaData data) throws IOException {
 115         // jdeps -modulepath mods -m <name>
 116         runTest(data, MODS_DIR.toString(), Set.of(name));
 117 
 118         // jdeps -modulepath libs/m1.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")
 127     public Object[][] unnamed() {
 128         return new Object[][]{
 129                 { "unnamed", new ModuleMetaData("unnamed", false)
 130                             .depends("java.sql")
 131                             .depends("java.logging")
 132                             .depends("m1")
 133                             .depends("m2")
 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", "m1")
 138                             .reference("p3", "p2", "m2")
 139                             .internal("p3", "p2.internal", "m2")
 140                 },
 141         };
 142     }
 143 
 144     @Test(dataProvider = "unnamed")
 145     public void unnamedTest(String name, ModuleMetaData data) throws IOException {
 146         runTest(data, MODS_DIR.toString(), Set.of("m1", "m2"), UNNAMED_DIR);
 147     }
 148 
 149     private void runTest(ModuleMetaData data, String modulepath,
 150                          Set<String> roots, Path... paths)
 151         throws IOException
 152     {
 153         // jdeps -modulepath <modulepath> -m root paths
 154         String cmd = String.format("jdeps -modulepath %s -addmods %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 }