Path: blob/master/test/langtools/tools/jdeps/modules/DotFileTest.java
41152 views
/*1* Copyright (c) 2017, 2018, 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 817337426* @summary Tests module dot graph27* @modules java.desktop28* java.sql29* jdk.jdeps/com.sun.tools.jdeps30* jdk.unsupported31* @library ../lib32* @build CompilerUtils33* @run testng DotFileTest34*/3536import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.HashSet;40import java.util.Set;4142import java.util.regex.Matcher;43import java.util.regex.Pattern;44import java.util.spi.ToolProvider;45import java.util.stream.Collectors;4647import 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.assertEquals;5354public class DotFileTest {55private static final ToolProvider JDEPS = ToolProvider.findFirst("jdeps")56.orElseThrow(() -> new RuntimeException("jdeps not found"));57private static final ToolProvider JAR = ToolProvider.findFirst("jar")58.orElseThrow(() -> new RuntimeException("jar not found"));5960private static final String TEST_SRC = System.getProperty("test.src");61private static final Path DOTS_DIR = Paths.get("dots");62private static final Path SPEC_DIR = Paths.get("spec");63private static final Path MODS = Paths.get("mods");646566@BeforeTest67public void setup() throws Exception {68assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "unsafe"), MODS));69}7071@DataProvider(name = "modules")72public Object[][] modules() {73return new Object[][]{74{"java.desktop", Set.of("java.datatransfer -> java.base",75"java.desktop -> java.datatransfer",76"java.desktop -> java.prefs",77"java.prefs -> java.xml",78"java.xml -> java.base" )79},80{ "java.sql", Set.of("java.logging -> java.base",81"java.transaction.xa -> java.base",82"java.sql -> java.logging",83"java.sql -> java.transaction.xa",84"java.sql -> java.xml",85"java.xml -> java.base" )86}87};88}89@DataProvider(name = "specVersion")90public Object[][] specVersion() {91return new Object[][]{92{"java.desktop", Set.of("java.datatransfer -> java.base",93"java.desktop -> java.datatransfer",94"java.desktop -> java.xml",95"java.xml -> java.base")96},97{ "java.sql", Set.of("java.logging -> java.base",98"java.transaction.xa -> java.base",99"java.sql -> java.logging",100"java.sql -> java.transaction.xa",101"java.sql -> java.xml",102"java.xml -> java.base" )103}104};105}106107@Test(dataProvider = "modules")108public void test(String name, Set<String> edges) throws Exception {109String[] options = new String[] {110"-dotoutput", DOTS_DIR.toString(),111"-s", "-m", name112};113assertTrue(JDEPS.run(System.out, System.out, options) == 0);114115Path path = DOTS_DIR.resolve(name + ".dot");116assertTrue(Files.exists(path));117Set<String> lines = Files.readAllLines(path).stream()118.filter(l -> l.contains(" -> "))119.map(this::split)120.collect(Collectors.toSet());121assertEquals(lines, edges);122}123124@Test(dataProvider = "specVersion")125public void testAPIOnly(String name, Set<String> edges) throws Exception {126String[] options = new String[]{127"-dotoutput", SPEC_DIR.toString(),128"-s", "-apionly",129"-m", name130};131assertTrue(JDEPS.run(System.out, System.out, options) == 0);132133Path path = SPEC_DIR.resolve(name + ".dot");134assertTrue(Files.exists(path));135Set<String> lines = Files.readAllLines(path).stream()136.filter(l -> l.contains(" -> "))137.map(this::split)138.collect(Collectors.toSet());139assertEquals(lines, edges);140}141142/*143* Test if the file name of the dot output file matches the input filename144*/145@Test146public void testModularJar() throws Exception {147String filename = "org.unsafe-v1.0.jar";148assertTrue(JAR.run(System.out, System.out, "cf", filename,149"-C", MODS.toString(), ".") == 0);150151// assertTrue(JDEPS.run(System.out, System.out,152// "--dot-output", DOTS_DIR.toString(), filename) == 0);153assertTrue(JDEPS.run(System.out, System.out,154"--dot-output", DOTS_DIR.toString(),155"--module-path", filename,156"-m", "unsafe") == 0);157158Path path = DOTS_DIR.resolve(filename + ".dot");159assertTrue(Files.exists(path));160161// package dependences162Set<String> expected = Set.of(163"org.indirect -> java.lang",164"org.indirect -> org.unsafe",165"org.safe -> java.io",166"org.safe -> java.lang",167"org.unsafe -> java.lang",168"org.unsafe -> sun.misc"169);170171Pattern pattern = Pattern.compile("(.*) -> +([^ ]*) (.*)");172Set<String> lines = new HashSet<>();173for (String line : Files.readAllLines(path)) {174line = line.replace('"', ' ').replace(';', ' ');175Matcher pm = pattern.matcher(line);176if (pm.find()) {177String origin = pm.group(1).trim();178String target = pm.group(2).trim();179lines.add(origin + " -> " + target);180}181}182assertEquals(lines, expected);183}184185/*186* Test module summary with -m option187*/188@Test189public void testModuleSummary() throws Exception {190String filename = "org.unsafe-v2.0.jar";191assertTrue(JAR.run(System.out, System.out, "cf", filename,192"-C", MODS.toString(), ".") == 0);193194assertTrue(JDEPS.run(System.out, System.out, "-s",195"--dot-output", DOTS_DIR.toString(),196"--module-path", filename,197"-m", "unsafe") == 0);198199Path path = DOTS_DIR.resolve(filename + ".dot");200assertTrue(Files.exists(path));201202// module dependences203Set<String> expected = Set.of(204"unsafe -> jdk.unsupported",205"jdk.unsupported -> java.base"206);207208Set<String> lines = Files.readAllLines(path).stream()209.filter(l -> l.contains(" -> "))210.map(this::split)211.collect(Collectors.toSet());212assertEquals(lines, expected);213}214215static Pattern PATTERN = Pattern.compile(" *\"(\\S+)\" -> \"(\\S+)\" .*");216String split(String line) {217Matcher pm = PATTERN.matcher(line);218assertTrue(pm.find());219return String.format("%s -> %s", pm.group(1), pm.group(2));220}221}222223224