Path: blob/master/test/langtools/tools/jdeps/DotFileTest.java
41144 views
/*1* Copyright (c) 2014, 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 800356226* @summary Basic tests for jdeps -dotoutput option27* @modules java.management28* jdk.jdeps/com.sun.tools.jdeps29* @library /tools/lib30* @build toolbox.ToolBox Test p.Foo p.Bar31* @run main DotFileTest32*/3334import java.io.File;35import java.io.IOException;36import java.io.PrintWriter;37import java.io.StringWriter;38import java.nio.file.DirectoryStream;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.util.*;43import java.util.regex.*;44import java.util.stream.Collectors;4546import toolbox.ToolBox;4748public class DotFileTest {49public static void main(String... args) throws Exception {50int errors = 0;51errors += new DotFileTest().run();52if (errors > 0)53throw new Exception(errors + " errors found");54}5556final ToolBox toolBox;57final Path dir;58final Path dotoutput;59DotFileTest() {60this.toolBox = new ToolBox();61this.dir = Paths.get(System.getProperty("test.classes", "."));62this.dotoutput = dir.resolve("dots");63}6465int run() throws IOException {66File testDir = dir.toFile();67// test a .class file68test(new File(testDir, "Test.class"),69new String[] {"java.lang", "p"},70new String[] {"compact1", "not found"});71// test a directory72test(new File(testDir, "p"),73new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},74new String[] {"compact1", "compact1", "compact3", "compact1"},75new String[] {"-classpath", testDir.getPath()});76// test class-level dependency output77test(new File(testDir, "Test.class"),78new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},79new String[] {"compact1", "compact1", "not found", "not found"},80new String[] {"-verbose:class"});81// test -filter:none option82test(new File(testDir, "p"),83new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto", "p"},84new String[] {"compact1", "compact1", "compact3", "compact1", "p"},85new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});86// test -filter:archive option87test(new File(testDir, "p"),88new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},89new String[] {"compact1", "compact1", "compact3", "compact1"},90new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});91// test -p option92test(new File(testDir, "Test.class"),93new String[] {"p.Foo", "p.Bar"},94new String[] {"not found", "not found"},95new String[] {"-verbose:class", "-p", "p"});96// test -e option97test(new File(testDir, "Test.class"),98new String[] {"p.Foo", "p.Bar"},99new String[] {"not found", "not found"},100new String[] {"-verbose:class", "-e", "p\\..*"});101test(new File(testDir, "Test.class"),102new String[] {"java.lang"},103new String[] {"compact1"},104new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});105// test -classpath options106test(new File(testDir, "Test.class"),107new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},108new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},109new String[] {"-v", "-classpath", testDir.getPath()});110111testSummary(new File(testDir, "Test.class"),112new String[] {"java.base", testDir.getName()},113new String[] {"compact1", ""},114new String[] {"-classpath", testDir.getPath()});115testSummary(new File(testDir, "Test.class"),116new String[] {"java.lang", "p"},117new String[] {"compact1", testDir.getName()},118new String[] {"-v", "-classpath", testDir.getPath()});119return errors;120}121122void test(File file, String[] expect, String[] profiles) throws IOException {123test(file, expect, profiles, new String[0]);124}125126void test(File file, String[] expect, String[] profiles, String[] options)127throws IOException128{129Path dotfile = dotoutput.resolve(file.toPath().getFileName().toString() + ".dot");130131List<String> args = new ArrayList<>(Arrays.asList(options));132args.add("-dotoutput");133args.add(dotoutput.toString());134if (file != null) {135args.add(file.getPath());136}137138Map<String,String> result = jdeps(args, dotfile);139checkResult("dependencies", expect, result.keySet());140141// with -P option142List<String> argsWithDashP = new ArrayList<>();143argsWithDashP.add("-P");144argsWithDashP.addAll(args);145146result = jdeps(argsWithDashP, dotfile);147checkResult("profiles", expect, profiles, result);148}149150void testSummary(File file, String[] expect, String[] profiles, String[] options)151throws IOException152{153Path dotfile = dotoutput.resolve("summary.dot");154155List<String> args = new ArrayList<>(Arrays.asList(options));156args.add("-dotoutput");157args.add(dotoutput.toString());158if (file != null) {159args.add(file.getPath());160}161162Map<String,String> result = jdeps(args, dotfile);163checkResult("dependencies", expect, result.keySet());164165// with -P option166List<String> argsWithDashP = new ArrayList<>();167argsWithDashP.add("-P");168argsWithDashP.addAll(args);169170result = jdeps(argsWithDashP, dotfile);171checkResult("profiles", expect, profiles, result);172}173174Map<String,String> jdeps(List<String> args, Path dotfile) throws IOException {175if (Files.exists(dotoutput)) {176// delete contents of directory, then directory,177// waiting for confirmation on Windows178toolBox.cleanDirectory(dotoutput);179toolBox.deleteFiles(dotoutput);180}181// invoke jdeps182StringWriter sw = new StringWriter();183PrintWriter pw = new PrintWriter(sw);184System.err.println("jdeps " + args.stream().collect(Collectors.joining(" ")));185int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[0]), pw);186pw.close();187String out = sw.toString();188if (!out.isEmpty())189System.err.println(out);190if (rc != 0)191throw new Error("jdeps failed: rc=" + rc);192193// check output files194if (Files.notExists(dotfile)) {195throw new RuntimeException(dotfile + " doesn't exist");196}197return parse(dotfile);198}199private static Pattern pattern = Pattern.compile("(.*) -> +([^ ]*) (.*)");200private Map<String,String> parse(Path outfile) throws IOException {201Map<String,String> result = new LinkedHashMap<>();202for (String line : Files.readAllLines(outfile)) {203line = line.replace('"', ' ').replace(';', ' ');204Matcher pm = pattern.matcher(line);205if (pm.find()) {206String origin = pm.group(1).trim();207String target = pm.group(2).trim();208String module = pm.group(3).replace('(', ' ').replace(')', ' ').trim();209result.put(target, module);210}211}212return result;213}214215void checkResult(String label, String[] expect, Collection<String> found) {216List<String> list = Arrays.asList(expect);217if (!isEqual(list, found))218error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");219}220221void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {222if (expect.length != profiles.length)223error("Invalid expected names and profiles");224225// check the dependencies226checkResult(label, expect, result.keySet());227// check profile information228checkResult(label, profiles, result.values());229for (int i=0; i < expect.length; i++) {230String profile = result.get(expect[i]);231if (!profile.equals(profiles[i]))232error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");233}234}235236boolean isEqual(List<String> expected, Collection<String> found) {237if (expected.size() != found.size())238return false;239240List<String> list = new ArrayList<>(found);241list.removeAll(expected);242return list.isEmpty();243}244245void error(String msg) {246System.err.println("Error: " + msg);247errors++;248}249250int errors;251}252253254