Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/jdeps/DotFileTest.java
32285 views
/*1* Copyright (c) 2014, 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 800356226* @summary Basic tests for jdeps -dotoutput option27* @build Test p.Foo p.Bar javax.activity.NotCompactProfile28* @run main DotFileTest29*/3031import java.io.File;32import java.io.IOException;33import java.io.PrintWriter;34import java.io.StringWriter;35import java.nio.file.DirectoryStream;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.*;40import java.util.regex.*;4142public class DotFileTest {43private static boolean symbolFileExist = initProfiles();44private static boolean initProfiles() {45// check if ct.sym exists; if not use the profiles.properties file46Path home = Paths.get(System.getProperty("java.home"));47if (home.endsWith("jre")) {48home = home.getParent();49}50Path ctsym = home.resolve("lib").resolve("ct.sym");51boolean symbolExists = ctsym.toFile().exists();52if (!symbolExists) {53Path testSrcProfiles =54Paths.get(System.getProperty("test.src", "."), "profiles.properties");55if (!testSrcProfiles.toFile().exists())56throw new Error(testSrcProfiles + " does not exist");57System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",58ctsym, testSrcProfiles);59System.setProperty("jdeps.profiles", testSrcProfiles.toString());60}61return symbolExists;62}6364public static void main(String... args) throws Exception {65int errors = 0;66errors += new DotFileTest().run();67if (errors > 0)68throw new Exception(errors + " errors found");69}7071final Path dir;72final Path dotoutput;73DotFileTest() {74this.dir = Paths.get(System.getProperty("test.classes", "."));75this.dotoutput = dir.resolve("dots");76}7778int run() throws IOException {79File testDir = dir.toFile();80// test a .class file81test(new File(testDir, "Test.class"),82new String[] {"java.lang", "p"},83new String[] {"compact1", "not found"});84// test a directory85// also test non-SE javax.activity class dependency86test(new File(testDir, "p"),87new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},88new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},89new String[] {"-classpath", testDir.getPath()});90// test class-level dependency output91test(new File(testDir, "Test.class"),92new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},93new String[] {"compact1", "compact1", "not found", "not found"},94new String[] {"-verbose:class"});95// test -filter:none option96test(new File(testDir, "p"),97new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto", "p"},98new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1", "p"},99new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});100// test -filter:archive option101test(new File(testDir, "p"),102new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},103new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},104new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});105// test -p option106test(new File(testDir, "Test.class"),107new String[] {"p.Foo", "p.Bar"},108new String[] {"not found", "not found"},109new String[] {"-verbose:class", "-p", "p"});110// test -e option111test(new File(testDir, "Test.class"),112new String[] {"p.Foo", "p.Bar"},113new String[] {"not found", "not found"},114new String[] {"-verbose:class", "-e", "p\\..*"});115test(new File(testDir, "Test.class"),116new String[] {"java.lang"},117new String[] {"compact1"},118new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});119// test -classpath options120test(new File(testDir, "Test.class"),121new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},122new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},123new String[] {"-v", "-classpath", testDir.getPath()});124125testSummary(new File(testDir, "Test.class"),126new String[] {"rt.jar", testDir.getName()},127new String[] {"compact1", ""},128new String[] {"-classpath", testDir.getPath()});129testSummary(new File(testDir, "Test.class"),130new String[] {"java.lang", "p"},131new String[] {"compact1", testDir.getName()},132new String[] {"-v", "-classpath", testDir.getPath()});133return errors;134}135136void test(File file, String[] expect, String[] profiles) throws IOException {137test(file, expect, profiles, new String[0]);138}139140void test(File file, String[] expect, String[] profiles, String[] options)141throws IOException142{143Path dotfile = dotoutput.resolve(file.toPath().getFileName().toString() + ".dot");144145List<String> args = new ArrayList<>(Arrays.asList(options));146args.add("-dotoutput");147args.add(dotoutput.toString());148if (file != null) {149args.add(file.getPath());150}151152Map<String,String> result = jdeps(args, dotfile);153checkResult("dependencies", expect, result.keySet());154155// with -P option156List<String> argsWithDashP = new ArrayList<>();157argsWithDashP.add("-dotoutput");158argsWithDashP.add(dotoutput.toString());159argsWithDashP.add("-P");160argsWithDashP.addAll(args);161162result = jdeps(argsWithDashP, dotfile);163checkResult("profiles", expect, profiles, result);164}165166void testSummary(File file, String[] expect, String[] profiles, String[] options)167throws IOException168{169Path dotfile = dotoutput.resolve("summary.dot");170171List<String> args = new ArrayList<>(Arrays.asList(options));172args.add("-dotoutput");173args.add(dotoutput.toString());174if (file != null) {175args.add(file.getPath());176}177178Map<String,String> result = jdeps(args, dotfile);179checkResult("dependencies", expect, result.keySet());180181// with -P option182List<String> argsWithDashP = new ArrayList<>();183argsWithDashP.add("-dotoutput");184argsWithDashP.add(dotoutput.toString());185argsWithDashP.add("-P");186argsWithDashP.addAll(args);187188result = jdeps(argsWithDashP, dotfile);189checkResult("profiles", expect, profiles, result);190}191192Map<String,String> jdeps(List<String> args, Path dotfile) throws IOException {193if (Files.exists(dotoutput)) {194try (DirectoryStream<Path> stream = Files.newDirectoryStream(dotoutput)) {195for (Path p : stream) {196Files.delete(p);197}198}199Files.delete(dotoutput);200}201// invoke jdeps202StringWriter sw = new StringWriter();203PrintWriter pw = new PrintWriter(sw);204System.err.println("jdeps " + args);205int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[0]), pw);206pw.close();207String out = sw.toString();208if (!out.isEmpty())209System.err.println(out);210if (rc != 0)211throw new Error("jdeps failed: rc=" + rc);212213// check output files214if (Files.notExists(dotfile)) {215throw new RuntimeException(dotfile + " doesn't exist");216}217return parse(dotfile);218}219private static Pattern pattern = Pattern.compile("(.*) -> +([^ ]*) (.*)");220private Map<String,String> parse(Path outfile) throws IOException {221Map<String,String> result = new LinkedHashMap<>();222for (String line : Files.readAllLines(outfile)) {223line = line.replace('"', ' ').replace(';', ' ');224Matcher pm = pattern.matcher(line);225if (pm.find()) {226String origin = pm.group(1).trim();227String target = pm.group(2).trim();228String module = pm.group(3).replace('(', ' ').replace(')', ' ').trim();229result.put(target, module);230}231}232return result;233}234235void checkResult(String label, String[] expect, Collection<String> found) {236List<String> list = Arrays.asList(expect);237if (!isEqual(list, found))238error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");239}240241void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {242if (expect.length != profiles.length)243error("Invalid expected names and profiles");244245// check the dependencies246checkResult(label, expect, result.keySet());247// check profile information248checkResult(label, profiles, result.values());249for (int i=0; i < expect.length; i++) {250String profile = result.get(expect[i]);251if (!profile.equals(profiles[i]))252error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");253}254}255256boolean isEqual(List<String> expected, Collection<String> found) {257if (expected.size() != found.size())258return false;259260List<String> list = new ArrayList<>(found);261list.removeAll(expected);262return list.isEmpty();263}264265void error(String msg) {266System.err.println("Error: " + msg);267errors++;268}269270int errors;271}272273274