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