1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/jdeps/Basic.java Fri Dec 28 22:25:21 2012 -0800 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8003562 1.30 + * @summary Basic tests for jdeps tool 1.31 + * @build Test p.Foo 1.32 + * @run main Basic 1.33 + */ 1.34 + 1.35 +import java.io.File; 1.36 +import java.io.IOException; 1.37 +import java.io.PrintWriter; 1.38 +import java.io.StringWriter; 1.39 +import java.util.*; 1.40 +import java.util.regex.*; 1.41 + 1.42 +public class Basic { 1.43 + public static void main(String... args) throws Exception { 1.44 + int errors = 0; 1.45 + 1.46 + errors += new Basic().run(); 1.47 + if (errors > 0) 1.48 + throw new Exception(errors + " errors found"); 1.49 + } 1.50 + 1.51 + int run() throws IOException { 1.52 + File testDir = new File(System.getProperty("test.classes", ".")); 1.53 + // test a .class file 1.54 + test(new File(testDir, "Test.class"), 1.55 + new String[] {"java.lang", "p"}); 1.56 + // test a directory 1.57 + test(new File(testDir, "p"), 1.58 + new String[] {"java.lang", "java.util"}); 1.59 + // test class-level dependency output 1.60 + test(new File(testDir, "Test.class"), 1.61 + new String[] {"java.lang.Object", "p.Foo"}, 1.62 + new String[] {"-V", "class"}); 1.63 + // test -p option 1.64 + test(new File(testDir, "Test.class"), 1.65 + new String[] {"p.Foo"}, 1.66 + new String[] {"--verbose-level=class", "-p", "p"}); 1.67 + // test -e option 1.68 + test(new File(testDir, "Test.class"), 1.69 + new String[] {"p.Foo"}, 1.70 + new String[] {"-V", "class", "-e", "p\\..*"}); 1.71 + test(new File(testDir, "Test.class"), 1.72 + new String[] {"java.lang"}, 1.73 + new String[] {"-V", "package", "-e", "java\\.lang\\..*"}); 1.74 + // test -classpath and -all options 1.75 + test(null, 1.76 + new String[] {"com.sun.tools.jdeps", "java.lang", "java.util", 1.77 + "java.util.regex", "java.io", "p"}, 1.78 + new String[] {"--classpath", testDir.getPath(), "*"}); 1.79 + return errors; 1.80 + } 1.81 + 1.82 + void test(File file, String[] expect) { 1.83 + test(file, expect, new String[0]); 1.84 + } 1.85 + 1.86 + void test(File file, String[] expect, String[] options) { 1.87 + String[] args; 1.88 + if (file != null) { 1.89 + args = Arrays.copyOf(options, options.length+1); 1.90 + args[options.length] = file.getPath(); 1.91 + } else { 1.92 + args = options; 1.93 + } 1.94 + String[] deps = jdeps(args); 1.95 + checkEqual("dependencies", expect, deps); 1.96 + } 1.97 + 1.98 + String[] jdeps(String... args) { 1.99 + StringWriter sw = new StringWriter(); 1.100 + PrintWriter pw = new PrintWriter(sw); 1.101 + System.err.println("jdeps " + Arrays.toString(args)); 1.102 + int rc = com.sun.tools.jdeps.Main.run(args, pw); 1.103 + pw.close(); 1.104 + String out = sw.toString(); 1.105 + if (!out.isEmpty()) 1.106 + System.err.println(out); 1.107 + if (rc != 0) 1.108 + throw new Error("jdeps failed: rc=" + rc); 1.109 + return findDeps(out); 1.110 + } 1.111 + 1.112 + // Pattern used to parse lines 1.113 + private static Pattern linePattern = Pattern.compile(".*\r?\n"); 1.114 + private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +.*"); 1.115 + 1.116 + // Use the linePattern to break the given String into lines, applying 1.117 + // the pattern to each line to see if we have a match 1.118 + private static String[] findDeps(String out) { 1.119 + List<String> result = new ArrayList<>(); 1.120 + Matcher lm = linePattern.matcher(out); // Line matcher 1.121 + Matcher pm = null; // Pattern matcher 1.122 + int lines = 0; 1.123 + while (lm.find()) { 1.124 + lines++; 1.125 + CharSequence cs = lm.group(); // The current line 1.126 + if (pm == null) 1.127 + pm = pattern.matcher(cs); 1.128 + else 1.129 + pm.reset(cs); 1.130 + if (pm.find()) 1.131 + result.add(pm.group(1)); 1.132 + if (lm.end() == out.length()) 1.133 + break; 1.134 + } 1.135 + return result.toArray(new String[0]); 1.136 + } 1.137 + 1.138 + void checkEqual(String label, String[] expect, String[] found) { 1.139 + Set<String> s1 = new HashSet<>(Arrays.asList(expect)); 1.140 + Set<String> s2 = new HashSet<>(Arrays.asList(found)); 1.141 + 1.142 + if (!s1.equals(s2)) 1.143 + error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'"); 1.144 + } 1.145 + 1.146 + void error(String msg) { 1.147 + System.err.println("Error: " + msg); 1.148 + errors++; 1.149 + } 1.150 + 1.151 + int errors; 1.152 +}