Path: blob/master/test/langtools/tools/jdeps/listdeps/ListModuleDeps.java
41152 views
/*1* Copyright (c) 2016, 2019, 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* @bug 816705726* @summary Tests --list-deps, --list-reduced-deps, --print-module-deps options27* @modules java.logging28* java.xml29* jdk.compiler30* jdk.jdeps31* jdk.unsupported32* @library ../lib33* @build CompilerUtils JdepsRunner34* @run testng ListModuleDeps35*/3637import java.io.File;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.Arrays;41import java.util.List;42import java.util.stream.Collectors;4344import org.testng.annotations.BeforeTest;45import org.testng.annotations.DataProvider;46import org.testng.annotations.Test;4748import static org.testng.Assert.assertEquals;49import static org.testng.Assert.assertTrue;5051public class ListModuleDeps {52private static final String TEST_SRC = System.getProperty("test.src");5354private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");55private static final Path CLASSES_DIR = Paths.get("classes");56private static final Path LIB_DIR = Paths.get("lib");57private static final Path LIB2_DIR = Paths.get("lib2");5859private static final Path HI_CLASS =60CLASSES_DIR.resolve("hi").resolve("Hi.class");61private static final Path FOO_CLASS =62CLASSES_DIR.resolve("z").resolve("Foo.class");63private static final Path BAR_CLASS =64CLASSES_DIR.resolve("z").resolve("Bar.class");65private static final Path UNSAFE_CLASS =66CLASSES_DIR.resolve("z").resolve("UseUnsafe.class");6768/**69* Compiles classes used by the test70*/71@BeforeTest72public void compileAll() throws Exception {73// compile library74assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib2"), LIB2_DIR));75assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "lib"), LIB_DIR, "-cp", LIB2_DIR.toString()));7677// simple program depends only on java.base78assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "hi"), CLASSES_DIR));7980// compile classes in unnamed module81assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "z"),82CLASSES_DIR,83"-cp", LIB_DIR.toString(),84"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",85"--add-exports=java.base/sun.security.util=ALL-UNNAMED",86"--add-exports=java.xml/jdk.xml.internal=ALL-UNNAMED"87));88}8990@DataProvider(name = "jdkModules")91public Object[][] jdkModules() {92return new Object[][]{93{"jdk.compiler", new String[]{94"java.base/jdk.internal.jmod",95"java.base/jdk.internal.misc",96"java.base/sun.reflect.annotation",97"java.compiler",98}99},100};101}102103@Test(dataProvider = "jdkModules")104public void testJDKModule(String moduleName, String[] expected) {105JdepsRunner jdeps = JdepsRunner.run(106"--list-deps", "-m", moduleName107);108String[] output = Arrays.stream(jdeps.output())109.map(s -> s.trim())110.toArray(String[]::new);111assertEquals(output, expected);112}113114@Test(dataProvider = "listdeps")115public void testListDeps(Path classes, String[] expected) {116JdepsRunner jdeps = JdepsRunner.run(117"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),118"--list-deps", classes.toString()119);120String[] output = Arrays.stream(jdeps.output())121.map(s -> s.trim())122.toArray(String[]::new);123assertEquals(output, expected);124}125126@Test(dataProvider = "reduceddeps")127public void testListReducedDeps(Path classes, String[] expected) {128JdepsRunner jdeps = JdepsRunner.run(129"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),130"--list-reduced-deps", classes.toString()131);132String[] output = Arrays.stream(jdeps.output())133.map(s -> s.trim())134.toArray(String[]::new);135assertEquals(output, expected);136}137138139@DataProvider(name = "listdeps")140public Object[][] listdeps() {141return new Object[][] {142{ CLASSES_DIR, new String[] {143"java.base/jdk.internal.misc",144"java.base/sun.security.util",145"java.logging",146"java.management",147"java.sql",148"java.xml/jdk.xml.internal",149"jdk.unsupported"150}151},152153{ HI_CLASS, new String[] {154"java.base"155}156},157158{ FOO_CLASS, new String[] {159"java.base",160"java.logging",161"java.management",162"java.sql",163"java.xml"164}165},166167{ BAR_CLASS, new String[] {168"java.base/sun.security.util",169"java.xml/jdk.xml.internal",170}171},172173{ UNSAFE_CLASS, new String[] {174"java.base/jdk.internal.misc",175"jdk.unsupported"176}177},178};179}180181@DataProvider(name = "reduceddeps")182public Object[][] reduceddeps() {183Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");184185return new Object[][] {186{ CLASSES_DIR, new String[] {187"java.base/jdk.internal.misc",188"java.base/sun.security.util",189"java.management",190"java.sql",191"java.xml/jdk.xml.internal",192"jdk.unsupported"193}194},195196{ HI_CLASS, new String[] {197"java.base"198}199},200201{ FOO_CLASS, new String[] {202"java.base",203"java.management",204"java.sql"205}206},207208{ BAR_CLASS, new String[] {209"java.base/sun.security.util",210"java.xml/jdk.xml.internal",211}212},213214{ UNSAFE_CLASS, new String[] {215"java.base/jdk.internal.misc",216"jdk.unsupported"217}218},219};220}221222@Test(dataProvider = "moduledeps")223public void testPrintModuleDeps(Path classes, String expected) {224JdepsRunner jdeps = JdepsRunner.run(225"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),226"--print-module-deps", classes.toString()227);228String output = Arrays.stream(jdeps.output())229.map(s -> s.trim())230.collect(Collectors.joining(","));231assertEquals(output, expected);232}233234235@DataProvider(name = "moduledeps")236public Object[][] moduledeps() {237Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");238239return new Object[][] {240// java.xml is an implied reads edge from java.sql241{ CLASSES_DIR, "java.base,java.management,java.sql,jdk.unsupported"},242{ HI_CLASS, "java.base"},243{ FOO_CLASS, "java.base,java.management,java.sql"},244{ BAR_CLASS, "java.base,java.xml"},245{ UNSAFE_CLASS, "java.base,jdk.unsupported"},246};247}248249@Test(dataProvider = "noRecursiveModuledeps")250public void testNoRecursiveModuleDeps(Path classes, String expected) {251JdepsRunner jdeps = JdepsRunner.run(252"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),253"--print-module-deps", "--no-recursive", classes.toString()254);255String output = Arrays.stream(jdeps.output())256.map(s -> s.trim())257.collect(Collectors.joining(","));258assertEquals(output, expected);259}260261@DataProvider(name = "noRecursiveModuledeps")262public Object[][] noRecursiveModuledeps() {263Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");264265return new Object[][] {266// java.xml is an implied reads edge from java.sql267{ CLASSES_DIR, "java.base,java.sql,jdk.unsupported"},268{ HI_CLASS, "java.base"},269{ FOO_CLASS, "java.base,java.sql"},270{ BAR_CLASS, "java.base,java.xml"},271{ UNSAFE_CLASS, "java.base,jdk.unsupported"},272};273}274275@DataProvider(name = "recursiveDeps")276public Object[][] recursiveDeps() {277return new Object[][] {278{ // lib2 is classpath but not analyzed because lib.Lib is not present279// but it is the only class depending on lib2.Lib2280List.of("--list-deps", "--class-path", LIB2_DIR.toString(),281"--ignore-missing-deps", CLASSES_DIR.toString()),282new String[] {283"java.base/jdk.internal.misc",284"java.base/sun.security.util",285"java.logging",286"java.sql",287"java.xml/jdk.xml.internal",288"jdk.unsupported"289}290},291{ // lib2 is classpath but not analyzed because lib.Lib is not present292// but it is the only class depending on lib2.Lib2293List.of("--print-module-deps", "--class-path", LIB2_DIR.toString(),294"--ignore-missing-deps", CLASSES_DIR.toString()),295new String[] {296"java.base,java.sql,jdk.unsupported"297}298},299{ // Foo depends on lib.Lib which depends on lib2.Libs300List.of("--print-module-deps",301"--ignore-missing-deps", FOO_CLASS.toString()),302new String[] {303"java.base,java.sql"304}305},306};307}308309@Test(dataProvider = "recursiveDeps")310public void testRecursiveDeps(List<String> options, String[] expected) {311JdepsRunner jdeps = JdepsRunner.run(options.toArray(new String[0]));312String[] output = Arrays.stream(jdeps.output())313.map(s -> s.trim())314.toArray(String[]::new);315assertEquals(output, expected);316}317}318319320