Path: blob/master/test/langtools/tools/jdeps/modules/ModuleTest.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Tests jdeps -m and --module-path options on named modules and unnamed modules26* @library ../lib27* @build CompilerUtils JdepsUtil28* @modules jdk.jdeps/com.sun.tools.jdeps29* @run testng ModuleTest30*/3132import java.io.File;33import java.io.IOException;3435import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.*;39import java.util.stream.Collectors;404142import com.sun.tools.jdeps.DepsAnalyzer;43import com.sun.tools.jdeps.Graph;44import org.testng.annotations.DataProvider;45import org.testng.annotations.BeforeTest;46import org.testng.annotations.Test;4748import static org.testng.Assert.assertTrue;4950public class ModuleTest {51private static final String TEST_SRC = System.getProperty("test.src");52private static final String TEST_CLASSES = System.getProperty("test.classes");5354private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");55private static final Path MODS_DIR = Paths.get("mods");56private static final Path UNNAMED_DIR = Paths.get("unnamed");5758// the names of the modules in this test59private static final String UNSUPPORTED = "unsupported";60private static String[] modules = new String[] {"mI", "mII", "mIII", "mIV", UNSUPPORTED};61/**62* Compiles all modules used by the test63*/64@BeforeTest65public void compileAll() throws Exception {66CompilerUtils.cleanDir(MODS_DIR);67CompilerUtils.cleanDir(UNNAMED_DIR);6869assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, UNSUPPORTED,70"--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));71// m4 is not referenced72Arrays.asList("mI", "mII", "mIII", "mIV")73.forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));7475assertTrue(CompilerUtils.compile(SRC_DIR.resolve("mIII"), UNNAMED_DIR, "-p", MODS_DIR.toString()));76Files.delete(UNNAMED_DIR.resolve("module-info.class"));77}7879@DataProvider(name = "modules")80public Object[][] expected() {81return new Object[][]{82{ "mIII", new ModuleMetaData("mIII").requiresTransitive("java.sql")83.requiresTransitive("mII")84.requires("java.logging")85.requiresTransitive("mI")86.reference("p3", "java.lang", "java.base")87.reference("p3", "java.sql", "java.sql")88.reference("p3", "java.util.logging", "java.logging")89.reference("p3", "p1", "mI")90.reference("p3", "p2", "mII")91.qualified("p3", "p2.internal", "mII")92},93{ "mII", new ModuleMetaData("mII").requiresTransitive("mI")94.reference("p2", "java.lang", "java.base")95.reference("p2", "p1", "mI")96.reference("p2.internal", "java.lang", "java.base")97.reference("p2.internal", "java.io", "java.base")98},99{ "mI", new ModuleMetaData("mI").requires("unsupported")100.reference("p1", "java.lang", "java.base")101.reference("p1.internal", "java.lang", "java.base")102.reference("p1.internal", "p1", "mI")103.reference("p1.internal", "q", "unsupported")104},105{ "unsupported", new ModuleMetaData("unsupported")106.reference("q", "java.lang", "java.base")107.jdkInternal("q", "jdk.internal.perf", "java.base")108},109};110}111112@Test(dataProvider = "modules")113public void modularTest(String name, ModuleMetaData data) throws IOException {114// jdeps --module-path mods -m <name>115runTest(data, MODS_DIR.toString(), Set.of(name));116117// jdeps --module-path libs/mI.jar:.... -m <name>118String mp = Arrays.stream(modules)119.filter(mn -> !mn.equals(name))120.map(mn -> MODS_DIR.resolve(mn).toString())121.collect(Collectors.joining(File.pathSeparator));122runTest(data, mp, Collections.emptySet(), MODS_DIR.resolve(name));123}124125@DataProvider(name = "unnamed")126public Object[][] unnamed() {127return new Object[][]{128{ "unnamed", new ModuleMetaData("unnamed", false)129.depends("java.sql")130.depends("java.logging")131.depends("mI")132.depends("mII")133.reference("p3", "java.lang", "java.base")134.reference("p3", "java.sql", "java.sql")135.reference("p3", "java.util.logging", "java.logging")136.reference("p3", "p1", "mI")137.reference("p3", "p2", "mII")138.internal("p3", "p2.internal", "mII")139},140};141}142143@Test(dataProvider = "unnamed")144public void unnamedTest(String name, ModuleMetaData data) throws IOException {145runTest(data, MODS_DIR.toString(), Set.of("mI", "mII"), UNNAMED_DIR);146}147148private void runTest(ModuleMetaData data, String modulepath,149Set<String> roots, Path... paths)150throws IOException151{152// jdeps --module-path <modulepath> -m root paths153String cmd = String.format("jdeps --module-path %s --add-modules %s %s%n",154MODS_DIR, roots.stream().collect(Collectors.joining(",")), paths);155156try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {157jdeps.appModulePath(modulepath)158.addmods(roots);159Arrays.stream(paths).forEach(jdeps::addRoot);160161// run the analyzer162DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();163assertTrue(analyzer.run());164165// analyze result166Graph<DepsAnalyzer.Node> g1 = analyzer.moduleGraph();167g1.nodes().stream()168.filter(u -> u.name.equals(data.moduleName))169.forEach(u -> data.checkRequires(u.name, g1.adjacentNodes(u)));170171Graph<DepsAnalyzer.Node> g2 = analyzer.dependenceGraph();172g2.nodes().stream()173.filter(u -> u.name.equals(data.moduleName))174.forEach(u -> data.checkDependences(u.name, g2.adjacentNodes(u)));175176jdeps.dumpOutput(System.err);177}178}179}180181182