Path: blob/master/test/langtools/tools/jdeps/Basic.java
41144 views
/*1* Copyright (c) 2012, 2015, 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 8003562 8005428 8015912 8027481 8048063 806893726* @summary Basic tests for jdeps tool27* @modules java.management28* jdk.jdeps/com.sun.tools.jdeps29* @build Test p.Foo p.Bar p.C p.SubClass q.Gee30* @run main Basic31*/3233import java.io.File;34import java.io.IOException;35import java.io.PrintWriter;36import java.io.StringWriter;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.util.*;41import java.util.regex.*;42import java.util.stream.Collectors;4344import static java.nio.file.StandardCopyOption.*;4546public class Basic {47public static void main(String... args) throws Exception {48int errors = 0;49errors += new Basic().run();50if (errors > 0)51throw new Exception(errors + " errors found");52}5354int run() throws IOException {55File testDir = new File(System.getProperty("test.classes", "."));56// test a .class file57test(new File(testDir, "Test.class"),58new String[] {"java.lang", "p"},59new String[] {"compact1", "not found"});60// test a directory61test(new File(testDir, "p"),62new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},63new String[] {"compact1", "compact1", "compact3", "compact1"},64new String[] {"-classpath", testDir.getPath()});65// test class-level dependency output66test(new File(testDir, "Test.class"),67new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},68new String[] {"compact1", "compact1", "not found", "not found"},69new String[] {"-verbose:class"});70// test -filter:none option71test(new File(testDir, "p"),72new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto", "p"},73new String[] {"compact1", "compact1", "compact3", "compact1", "p"},74new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});75// test -filter:archive option76test(new File(testDir, "p"),77new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},78new String[] {"compact1", "compact1", "compact3", "compact1"},79new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});80// test -p option81test(new File(testDir, "Test.class"),82new String[] {"p.Foo", "p.Bar"},83new String[] {"not found", "not found"},84new String[] {"-verbose:class", "-p", "p"});85// test -e option86test(new File(testDir, "Test.class"),87new String[] {"p.Foo", "p.Bar"},88new String[] {"not found", "not found"},89new String[] {"-verbose:class", "-e", "p\\..*"});90test(new File(testDir, "Test.class"),91new String[] {"java.lang"},92new String[] {"compact1"},93new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});9495// parse p.C, p.SubClass and q.*96// p.SubClass have no dependency other than p.C97// q.Gee depends on p.SubClass that should be found98test(testDir,99new String[] {"java.lang", "p"},100new String[] {"compact1", testDir.getName()},101new String[] {"-include", "p.C|p.SubClass|q\\..*"});102test(testDir,103new String[] {"java.lang", "p"},104new String[] {"compact1", testDir.getName()},105new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});106107// test -classpath and -include options108test(null,109new String[] {"java.lang", "java.util", "java.lang.management",110"javax.crypto"},111new String[] {"compact1", "compact1", "compact3", "compact1"},112new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});113test(new File(testDir, "Test.class"),114new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},115new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},116new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});117118// split package p - move p/Foo.class to dir1 and p/Bar.class to dir2119Path testClassPath = testDir.toPath();120Path dirP = testClassPath.resolve("p");121Path dir1 = testClassPath.resolve("dir1");122Path subdir1P = dir1.resolve("p");123Path dir2 = testClassPath.resolve("dir2");124Path subdir2P = dir2.resolve("p");125if (!Files.exists(subdir1P))126Files.createDirectories(subdir1P);127if (!Files.exists(subdir2P))128Files.createDirectories(subdir2P);129Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);130Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);131StringBuilder cpath = new StringBuilder(testDir.toString());132cpath.append(File.pathSeparator).append(dir1.toString());133cpath.append(File.pathSeparator).append(dir2.toString());134test(new File(testDir, "Test.class"),135new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},136new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},137new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});138139// tests --missing-deps option140test(new File(testDir, "Test.class"),141new String[] {"p.Foo", "p.Bar"},142new String[] {"not found", "not found"},143new String[] {"--missing-deps"});144145// no missing dependence146test(new File(testDir, "Test.class"),147new String[0],148new String[0],149new String[] {"--missing-deps", "-classpath", cpath.toString()});150151return errors;152}153154void test(File file, String[] expect, String[] profiles) {155test(file, expect, profiles, new String[0]);156}157158void test(File file, String[] expect, String[] profiles, String[] options) {159List<String> args = new ArrayList<>(Arrays.asList(options));160if (file != null) {161args.add(file.getPath());162}163List<String> argsWithDashP = new ArrayList<>();164argsWithDashP.add("-P");165argsWithDashP.addAll(args);166// test without -P167checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());168// test with -P169checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));170}171172Map<String,String> jdeps(String... args) {173StringWriter sw = new StringWriter();174PrintWriter pw = new PrintWriter(sw);175System.err.println("jdeps " + Arrays.stream(args).collect(Collectors.joining(" ")));176int rc = com.sun.tools.jdeps.Main.run(args, pw);177pw.close();178String out = sw.toString();179if (!out.isEmpty())180System.err.println(out);181if (rc != 0)182throw new Error("jdeps failed: rc=" + rc);183return findDeps(out);184}185186// Pattern used to parse lines187private static Pattern linePattern = Pattern.compile(".*\r?\n");188private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");189190// Use the linePattern to break the given String into lines, applying191// the pattern to each line to see if we have a match192private static Map<String,String> findDeps(String out) {193Map<String,String> result = new LinkedHashMap<>();194Matcher lm = linePattern.matcher(out); // Line matcher195Matcher pm = null; // Pattern matcher196int lines = 0;197while (lm.find()) {198lines++;199CharSequence cs = lm.group(); // The current line200if (pm == null)201pm = pattern.matcher(cs);202else203pm.reset(cs);204if (pm.find())205result.put(pm.group(1), pm.group(2).trim());206if (lm.end() == out.length())207break;208}209return result;210}211212void checkResult(String label, String[] expect, Collection<String> found) {213List<String> list = Arrays.asList(expect);214if (!isEqual(list, found))215error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");216}217218void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {219if (expect.length != profiles.length)220error("Invalid expected names and profiles");221222// check the dependencies223checkResult(label, expect, result.keySet());224// check profile information225checkResult(label, profiles, result.values());226for (int i=0; i < expect.length; i++) {227String profile = result.get(expect[i]);228if (!profile.equals(profiles[i]))229error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");230}231}232233boolean isEqual(List<String> expected, Collection<String> found) {234if (expected.size() != found.size())235return false;236237List<String> list = new ArrayList<>(found);238list.removeAll(expected);239return list.isEmpty();240}241242void error(String msg) {243System.err.println("Error: " + msg);244errors++;245}246247int errors;248}249250251