Path: blob/master/test/langtools/tools/jdeps/modules/InverseDeps.java
41149 views
/*1* Copyright (c) 2016, 2020, 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 split packages26* @library ../lib27* @build CompilerUtils JdepsUtil28* @modules java.logging29* jdk.jdeps/com.sun.tools.jdeps30* jdk.unsupported31* @run testng InverseDeps32*/3334import java.io.File;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.Arrays;39import java.util.LinkedHashSet;40import java.util.List;41import java.util.Set;42import java.util.stream.Collectors;43import java.util.stream.Stream;4445import com.sun.tools.jdeps.Archive;46import com.sun.tools.jdeps.InverseDepsAnalyzer;47import org.testng.annotations.BeforeTest;48import org.testng.annotations.DataProvider;49import org.testng.annotations.Test;5051import static org.testng.Assert.assertTrue;52import static org.testng.Assert.assertFalse;53import static org.testng.Assert.assertEquals;545556public class InverseDeps {57private static final String TEST_SRC = System.getProperty("test.src");58private static final String TEST_CLASSES = System.getProperty("test.classes");5960private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");61private static final Path MODS_DIR = Paths.get("mods");62private static final Path LIBS_DIR = Paths.get("libs");6364private static final Set<String> modules = new LinkedHashSet(65List.of("unsafe", "mIV", "mV", "mVI", "mVII")66);6768/**69* Compiles classes used by the test70*/71@BeforeTest72public void compileAll() throws Exception {73CompilerUtils.cleanDir(MODS_DIR);7475for (String mn : modules) {76// compile a module77assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn));7879// create JAR files with no module-info.class80Path root = MODS_DIR.resolve(mn);8182try (Stream<Path> stream = Files.walk(root, Integer.MAX_VALUE)) {83Stream<Path> entries = stream.filter(f -> {84String fn = f.getFileName().toString();85return fn.endsWith(".class") && !fn.equals("module-info.class");86});87JdepsUtil.createJar(LIBS_DIR.resolve(mn + ".jar"), root, entries);88}89}90}91@DataProvider(name = "jdkModules")92public Object[][] jdkModules() {93return new Object[][]{94// --require and a subset of dependencies95{ "jdk.compiler", new String[][] {96new String[] {"jdk.compiler", "jdk.jshell"},97new String[] {"jdk.compiler", "jdk.javadoc"},98}99},100{ "java.compiler", new String[][] {101new String[] {"java.compiler", "jdk.jshell"},102new String[] {"java.compiler", "jdk.compiler", "jdk.jshell"},103new String[] {"java.compiler", "jdk.compiler"},104new String[] {"java.compiler", "jdk.compiler", "jdk.javadoc"},105new String[] {"java.compiler", "java.se"},106}107},108};109}110111@Test(dataProvider = "jdkModules")112public void testJDKModule(String moduleName, String[][] expected) throws Exception {113// this invokes the jdeps launcher so that all system modules are observable114JdepsRunner jdeps = JdepsRunner.run(115"--inverse", "--require", moduleName116);117List<String> output = Arrays.stream(jdeps.output())118.map(s -> s.trim())119.collect(Collectors.toList());120121// verify the dependences122assertTrue(Arrays.stream(expected)123.map(path -> Arrays.stream(path)124.collect(Collectors.joining(" <- ")))125.anyMatch(output::contains));126}127128129@DataProvider(name = "testrequires")130public Object[][] expected1() {131return new Object[][] {132// --require and result133{ "java.sql", new String[][] {134new String[] { "java.sql", "mV" },135}136},137{ "java.compiler", new String[][] {138new String[] { "java.compiler", "mV" },139new String[] { "java.compiler", "mIV", "mV" },140}141},142{ "java.logging", new String[][]{143new String[] {"java.logging", "mV"},144new String[] {"java.logging", "mIV", "mV"},145new String[] {"java.logging", "java.sql", "mV"},146}147},148{ "jdk.unsupported", new String[][] {149new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"},150new String[] {"jdk.unsupported", "unsafe", "mVII"}151}152},153};154}155156@Test(dataProvider = "testrequires")157public void testrequires(String name, String[][] expected) throws Exception {158String cmd1 = String.format("jdeps --inverse --module-path %s --require %s --add-modules %s%n",159MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));160161try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) {162jdeps.appModulePath(MODS_DIR.toString())163.addmods(modules)164.requires(Set.of(name));165166runJdeps(jdeps, expected);167}168169String cmd2 = String.format("jdeps --inverse --module-path %s --require %s" +170" --add-modules ALL-MODULE-PATH%n", LIBS_DIR, name);171172// automatic module173try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) {174jdeps.appModulePath(MODS_DIR.toString())175.addmods(Set.of("ALL-MODULE-PATH"))176.requires(Set.of(name));177178runJdeps(jdeps, expected);179}180}181182@DataProvider(name = "testpackage")183public Object[][] expected2() {184return new Object[][] {185// -package and result186{ "p4", new String[][] {187new String[] { "mIV", "mV"},188}189},190{ "javax.tools", new String[][] {191new String[] {"java.compiler", "mV"},192new String[] {"java.compiler", "mIV", "mV"},193}194},195{ "sun.misc", new String[][] {196new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"},197new String[] {"jdk.unsupported", "unsafe", "mVII"}198}199}200};201}202203@Test(dataProvider = "testpackage")204public void testpackage(String name, String[][] expected) throws Exception {205String cmd = String.format("jdeps --inverse --module-path %s -package %s --add-modules %s%n",206MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));207try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {208jdeps.appModulePath(MODS_DIR.toString())209.addmods(modules)210.matchPackages(Set.of(name));211212runJdeps(jdeps, expected);213}214}215216@DataProvider(name = "testregex")217public Object[][] expected3() {218return new Object[][] {219// -regex and result220{ "org.safe.Lib", new String[][] {221new String[] { "unsafe", "mVII"},222new String[] { "unsafe", "mVI", "mVII"},223}224},225{ "java.util.logging.*|org.safe.Lib", new String[][] {226new String[] { "unsafe", "mVII"},227new String[] { "unsafe", "mVI", "mVII"},228new String[] { "java.logging", "mV"},229}230}231};232}233234@Test(dataProvider = "testregex")235public void testregex(String name, String[][] expected) throws Exception {236String cmd = String.format("jdeps --inverse --module-path %s -regex %s --add-modules %s%n",237MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));238239try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {240jdeps.appModulePath(MODS_DIR.toString())241.addmods(modules)242.regex(name);243244runJdeps(jdeps, expected);245}246}247248@DataProvider(name = "classpath")249public Object[][] expected4() {250return new Object[][] {251// -regex and result252{ "sun.misc.Unsafe", new String[][] {253new String[] {"jdk.unsupported", "unsafe.jar", "mVI.jar", "mVII.jar"},254new String[] {"jdk.unsupported", "unsafe.jar", "mVII.jar"}255}256},257{ "org.safe.Lib", new String[][] {258new String[] { "unsafe.jar", "mVII.jar"},259new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"},260}261},262{ "java.util.logging.*|org.safe.Lib", new String[][] {263new String[] { "unsafe.jar", "mVII.jar"},264new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"},265new String[] { "java.logging", "mV.jar"},266}267}268};269}270271@Test(dataProvider = "classpath")272public void testClassPath(String name, String[][] expected) throws Exception {273// -classpath274String cpath = modules.stream()275.filter(mn -> !mn.equals("mVII"))276.map(mn -> LIBS_DIR.resolve(mn + ".jar").toString())277.collect(Collectors.joining(File.pathSeparator));278279Path jarfile = LIBS_DIR.resolve("mVII.jar");280281String cmd1 = String.format("jdeps --inverse -classpath %s -regex %s %s%n",282cpath, name, jarfile);283try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) {284jdeps.verbose("-verbose:class")285.addClassPath(cpath)286.regex(name).addRoot(jarfile);287runJdeps(jdeps, expected);288}289290// all JAR files on the command-line arguments291Set<Path> paths = modules.stream()292.map(mn -> LIBS_DIR.resolve(mn + ".jar"))293.collect(Collectors.toSet());294String cmd2 = String.format("jdeps --inverse -regex %s %s%n", name, paths);295try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) {296jdeps.verbose("-verbose:class").regex(name);297paths.forEach(jdeps::addRoot);298runJdeps(jdeps, expected);299}300}301302private void runJdeps(JdepsUtil.Command jdeps, String[][] expected) throws Exception {303InverseDepsAnalyzer analyzer = jdeps.getInverseDepsAnalyzer();304305assertTrue(analyzer.run());306307// get the inverse transitive dependences308List<String[]> paths = analyzer.inverseDependences().stream()309.map(deque -> deque.stream()310.map(Archive::getName)311.collect(Collectors.toList()).toArray(new String[0]))312.collect(Collectors.toList());313314jdeps.dumpOutput(System.err);315paths.forEach(path -> System.err.println(Arrays.stream(path)316.collect(Collectors.joining(" <- "))));317318// verify the dependences319assertEquals(paths.size(), expected.length);320321for (int i=0; i < paths.size(); i++) {322String[] path = paths.get(i);323boolean noneMatched = Arrays.stream(expected)324.filter(array -> array.length == path.length)325.noneMatch(array -> Arrays.equals(array, path));326if (noneMatched)327System.err.format("Expected: %s found: %s%n",328Arrays.stream(expected)329.map(Arrays::toString)330.collect(Collectors.joining(", ")),331Arrays.toString(path));332333assertFalse(noneMatched);334}335}336337}338339340