test/tools/jdeps/Basic.java

Thu, 14 Feb 2013 09:43:00 -0800

author
mchung
date
Thu, 14 Feb 2013 09:43:00 -0800
changeset 1577
88286a36bb34
parent 1472
0c244701188e
child 1638
fd3fdaff0257
permissions
-rw-r--r--

8006225: tools/jdeps/Basic.java failes with AssertionError
Reviewed-by: alanb

mchung@1472 1 /*
mchung@1472 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
mchung@1472 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mchung@1472 4 *
mchung@1472 5 * This code is free software; you can redistribute it and/or modify it
mchung@1472 6 * under the terms of the GNU General Public License version 2 only, as
mchung@1472 7 * published by the Free Software Foundation.
mchung@1472 8 *
mchung@1472 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mchung@1472 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mchung@1472 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mchung@1472 12 * version 2 for more details (a copy is included in the LICENSE file that
mchung@1472 13 * accompanied this code).
mchung@1472 14 *
mchung@1472 15 * You should have received a copy of the GNU General Public License version
mchung@1472 16 * 2 along with this work; if not, write to the Free Software Foundation,
mchung@1472 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mchung@1472 18 *
mchung@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mchung@1472 20 * or visit www.oracle.com if you need additional information or have any
mchung@1472 21 * questions.
mchung@1472 22 */
mchung@1472 23
mchung@1472 24 /*
mchung@1472 25 * @test
mchung@1472 26 * @bug 8003562
mchung@1472 27 * @summary Basic tests for jdeps tool
mchung@1472 28 * @build Test p.Foo
mchung@1472 29 * @run main Basic
mchung@1472 30 */
mchung@1472 31
mchung@1472 32 import java.io.File;
mchung@1472 33 import java.io.IOException;
mchung@1472 34 import java.io.PrintWriter;
mchung@1472 35 import java.io.StringWriter;
mchung@1472 36 import java.util.*;
mchung@1472 37 import java.util.regex.*;
mchung@1472 38
mchung@1472 39 public class Basic {
mchung@1472 40 public static void main(String... args) throws Exception {
mchung@1472 41 int errors = 0;
mchung@1472 42
mchung@1472 43 errors += new Basic().run();
mchung@1472 44 if (errors > 0)
mchung@1472 45 throw new Exception(errors + " errors found");
mchung@1472 46 }
mchung@1472 47
mchung@1472 48 int run() throws IOException {
mchung@1472 49 File testDir = new File(System.getProperty("test.classes", "."));
mchung@1472 50 // test a .class file
mchung@1472 51 test(new File(testDir, "Test.class"),
mchung@1472 52 new String[] {"java.lang", "p"});
mchung@1472 53 // test a directory
mchung@1472 54 test(new File(testDir, "p"),
mchung@1472 55 new String[] {"java.lang", "java.util"});
mchung@1472 56 // test class-level dependency output
mchung@1472 57 test(new File(testDir, "Test.class"),
mchung@1472 58 new String[] {"java.lang.Object", "p.Foo"},
mchung@1472 59 new String[] {"-V", "class"});
mchung@1472 60 // test -p option
mchung@1472 61 test(new File(testDir, "Test.class"),
mchung@1472 62 new String[] {"p.Foo"},
mchung@1472 63 new String[] {"--verbose-level=class", "-p", "p"});
mchung@1472 64 // test -e option
mchung@1472 65 test(new File(testDir, "Test.class"),
mchung@1472 66 new String[] {"p.Foo"},
mchung@1472 67 new String[] {"-V", "class", "-e", "p\\..*"});
mchung@1472 68 test(new File(testDir, "Test.class"),
mchung@1472 69 new String[] {"java.lang"},
mchung@1472 70 new String[] {"-V", "package", "-e", "java\\.lang\\..*"});
mchung@1577 71 // test -classpath and wildcard options
mchung@1472 72 test(null,
mchung@1472 73 new String[] {"com.sun.tools.jdeps", "java.lang", "java.util",
mchung@1577 74 "java.util.regex", "java.io"},
mchung@1472 75 new String[] {"--classpath", testDir.getPath(), "*"});
mchung@1577 76 // -v shows intra-dependency
mchung@1577 77 test(new File(testDir, "Test.class"),
mchung@1577 78 new String[] {"java.lang.Object", "p.Foo"},
mchung@1577 79 new String[] {"-v", "--classpath", testDir.getPath(), "Test.class"});
mchung@1472 80 return errors;
mchung@1472 81 }
mchung@1472 82
mchung@1472 83 void test(File file, String[] expect) {
mchung@1472 84 test(file, expect, new String[0]);
mchung@1472 85 }
mchung@1472 86
mchung@1472 87 void test(File file, String[] expect, String[] options) {
mchung@1472 88 String[] args;
mchung@1472 89 if (file != null) {
mchung@1472 90 args = Arrays.copyOf(options, options.length+1);
mchung@1472 91 args[options.length] = file.getPath();
mchung@1472 92 } else {
mchung@1472 93 args = options;
mchung@1472 94 }
mchung@1472 95 String[] deps = jdeps(args);
mchung@1472 96 checkEqual("dependencies", expect, deps);
mchung@1472 97 }
mchung@1472 98
mchung@1472 99 String[] jdeps(String... args) {
mchung@1472 100 StringWriter sw = new StringWriter();
mchung@1472 101 PrintWriter pw = new PrintWriter(sw);
mchung@1472 102 System.err.println("jdeps " + Arrays.toString(args));
mchung@1472 103 int rc = com.sun.tools.jdeps.Main.run(args, pw);
mchung@1472 104 pw.close();
mchung@1472 105 String out = sw.toString();
mchung@1472 106 if (!out.isEmpty())
mchung@1472 107 System.err.println(out);
mchung@1472 108 if (rc != 0)
mchung@1472 109 throw new Error("jdeps failed: rc=" + rc);
mchung@1472 110 return findDeps(out);
mchung@1472 111 }
mchung@1472 112
mchung@1472 113 // Pattern used to parse lines
mchung@1472 114 private static Pattern linePattern = Pattern.compile(".*\r?\n");
mchung@1472 115 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +.*");
mchung@1472 116
mchung@1472 117 // Use the linePattern to break the given String into lines, applying
mchung@1472 118 // the pattern to each line to see if we have a match
mchung@1472 119 private static String[] findDeps(String out) {
mchung@1472 120 List<String> result = new ArrayList<>();
mchung@1472 121 Matcher lm = linePattern.matcher(out); // Line matcher
mchung@1472 122 Matcher pm = null; // Pattern matcher
mchung@1472 123 int lines = 0;
mchung@1472 124 while (lm.find()) {
mchung@1472 125 lines++;
mchung@1472 126 CharSequence cs = lm.group(); // The current line
mchung@1472 127 if (pm == null)
mchung@1472 128 pm = pattern.matcher(cs);
mchung@1472 129 else
mchung@1472 130 pm.reset(cs);
mchung@1472 131 if (pm.find())
mchung@1472 132 result.add(pm.group(1));
mchung@1472 133 if (lm.end() == out.length())
mchung@1472 134 break;
mchung@1472 135 }
mchung@1472 136 return result.toArray(new String[0]);
mchung@1472 137 }
mchung@1472 138
mchung@1472 139 void checkEqual(String label, String[] expect, String[] found) {
mchung@1472 140 Set<String> s1 = new HashSet<>(Arrays.asList(expect));
mchung@1472 141 Set<String> s2 = new HashSet<>(Arrays.asList(found));
mchung@1472 142
mchung@1472 143 if (!s1.equals(s2))
mchung@1472 144 error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'");
mchung@1472 145 }
mchung@1472 146
mchung@1472 147 void error(String msg) {
mchung@1472 148 System.err.println("Error: " + msg);
mchung@1472 149 errors++;
mchung@1472 150 }
mchung@1472 151
mchung@1472 152 int errors;
mchung@1472 153 }

mercurial