Path: blob/master/test/langtools/tools/jdeps/listdeps/ListModuleDeps.java
64478 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.javac",95"java.base/jdk.internal.jmod",96"java.base/jdk.internal.misc",97"java.base/sun.reflect.annotation",98"java.compiler",99}100},101};102}103104@Test(dataProvider = "jdkModules")105public void testJDKModule(String moduleName, String[] expected) {106JdepsRunner jdeps = JdepsRunner.run(107"--list-deps", "-m", moduleName108);109String[] output = Arrays.stream(jdeps.output())110.map(s -> s.trim())111.toArray(String[]::new);112assertEquals(output, expected);113}114115@Test(dataProvider = "listdeps")116public void testListDeps(Path classes, String[] expected) {117JdepsRunner jdeps = JdepsRunner.run(118"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),119"--list-deps", classes.toString()120);121String[] output = Arrays.stream(jdeps.output())122.map(s -> s.trim())123.toArray(String[]::new);124assertEquals(output, expected);125}126127@Test(dataProvider = "reduceddeps")128public void testListReducedDeps(Path classes, String[] expected) {129JdepsRunner jdeps = JdepsRunner.run(130"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),131"--list-reduced-deps", classes.toString()132);133String[] output = Arrays.stream(jdeps.output())134.map(s -> s.trim())135.toArray(String[]::new);136assertEquals(output, expected);137}138139140@DataProvider(name = "listdeps")141public Object[][] listdeps() {142return new Object[][] {143{ CLASSES_DIR, new String[] {144"java.base/jdk.internal.misc",145"java.base/sun.security.util",146"java.logging",147"java.management",148"java.sql",149"java.xml/jdk.xml.internal",150"jdk.unsupported"151}152},153154{ HI_CLASS, new String[] {155"java.base"156}157},158159{ FOO_CLASS, new String[] {160"java.base",161"java.logging",162"java.management",163"java.sql",164"java.xml"165}166},167168{ BAR_CLASS, new String[] {169"java.base/sun.security.util",170"java.xml/jdk.xml.internal",171}172},173174{ UNSAFE_CLASS, new String[] {175"java.base/jdk.internal.misc",176"jdk.unsupported"177}178},179};180}181182@DataProvider(name = "reduceddeps")183public Object[][] reduceddeps() {184Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");185186return new Object[][] {187{ CLASSES_DIR, new String[] {188"java.base/jdk.internal.misc",189"java.base/sun.security.util",190"java.management",191"java.sql",192"java.xml/jdk.xml.internal",193"jdk.unsupported"194}195},196197{ HI_CLASS, new String[] {198"java.base"199}200},201202{ FOO_CLASS, new String[] {203"java.base",204"java.management",205"java.sql"206}207},208209{ BAR_CLASS, new String[] {210"java.base/sun.security.util",211"java.xml/jdk.xml.internal",212}213},214215{ UNSAFE_CLASS, new String[] {216"java.base/jdk.internal.misc",217"jdk.unsupported"218}219},220};221}222223@Test(dataProvider = "moduledeps")224public void testPrintModuleDeps(Path classes, String expected) {225JdepsRunner jdeps = JdepsRunner.run(226"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),227"--print-module-deps", classes.toString()228);229String output = Arrays.stream(jdeps.output())230.map(s -> s.trim())231.collect(Collectors.joining(","));232assertEquals(output, expected);233}234235236@DataProvider(name = "moduledeps")237public Object[][] moduledeps() {238Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");239240return new Object[][] {241// java.xml is an implied reads edge from java.sql242{ CLASSES_DIR, "java.base,java.management,java.sql,jdk.unsupported"},243{ HI_CLASS, "java.base"},244{ FOO_CLASS, "java.base,java.management,java.sql"},245{ BAR_CLASS, "java.base,java.xml"},246{ UNSAFE_CLASS, "java.base,jdk.unsupported"},247};248}249250@Test(dataProvider = "noRecursiveModuledeps")251public void testNoRecursiveModuleDeps(Path classes, String expected) {252JdepsRunner jdeps = JdepsRunner.run(253"--class-path", LIB_DIR.toString() + File.pathSeparator + LIB2_DIR.toString(),254"--print-module-deps", "--no-recursive", classes.toString()255);256String output = Arrays.stream(jdeps.output())257.map(s -> s.trim())258.collect(Collectors.joining(","));259assertEquals(output, expected);260}261262@DataProvider(name = "noRecursiveModuledeps")263public Object[][] noRecursiveModuledeps() {264Path barClass = CLASSES_DIR.resolve("z").resolve("Bar.class");265266return new Object[][] {267// java.xml is an implied reads edge from java.sql268{ CLASSES_DIR, "java.base,java.sql,jdk.unsupported"},269{ HI_CLASS, "java.base"},270{ FOO_CLASS, "java.base,java.sql"},271{ BAR_CLASS, "java.base,java.xml"},272{ UNSAFE_CLASS, "java.base,jdk.unsupported"},273};274}275276@DataProvider(name = "recursiveDeps")277public Object[][] recursiveDeps() {278return new Object[][] {279{ // lib2 is classpath but not analyzed because lib.Lib is not present280// but it is the only class depending on lib2.Lib2281List.of("--list-deps", "--class-path", LIB2_DIR.toString(),282"--ignore-missing-deps", CLASSES_DIR.toString()),283new String[] {284"java.base/jdk.internal.misc",285"java.base/sun.security.util",286"java.logging",287"java.sql",288"java.xml/jdk.xml.internal",289"jdk.unsupported"290}291},292{ // lib2 is classpath but not analyzed because lib.Lib is not present293// but it is the only class depending on lib2.Lib2294List.of("--print-module-deps", "--class-path", LIB2_DIR.toString(),295"--ignore-missing-deps", CLASSES_DIR.toString()),296new String[] {297"java.base,java.sql,jdk.unsupported"298}299},300{ // Foo depends on lib.Lib which depends on lib2.Libs301List.of("--print-module-deps",302"--ignore-missing-deps", FOO_CLASS.toString()),303new String[] {304"java.base,java.sql"305}306},307};308}309310@Test(dataProvider = "recursiveDeps")311public void testRecursiveDeps(List<String> options, String[] expected) {312JdepsRunner jdeps = JdepsRunner.run(options.toArray(new String[0]));313String[] output = Arrays.stream(jdeps.output())314.map(s -> s.trim())315.toArray(String[]::new);316assertEquals(output, expected);317}318}319320321