Thu, 17 Oct 2013 13:19:48 -0700
8015912: jdeps support to output in dot file format
8026255: Switch jdeps to follow traditional Java option style
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 8005428 8015912
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.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.*;
39 import java.util.regex.*;
41 public class Basic {
42 private static boolean symbolFileExist = initProfiles();
43 private static boolean initProfiles() {
44 // check if ct.sym exists; if not use the profiles.properties file
45 Path home = Paths.get(System.getProperty("java.home"));
46 if (home.endsWith("jre")) {
47 home = home.getParent();
48 }
49 Path ctsym = home.resolve("lib").resolve("ct.sym");
50 boolean symbolExists = ctsym.toFile().exists();
51 if (!symbolExists) {
52 Path testSrcProfiles =
53 Paths.get(System.getProperty("test.src", "."), "profiles.properties");
54 if (!testSrcProfiles.toFile().exists())
55 throw new Error(testSrcProfiles + " does not exist");
56 System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",
57 ctsym, testSrcProfiles);
58 System.setProperty("jdeps.profiles", testSrcProfiles.toString());
59 }
60 return symbolExists;
61 }
63 public static void main(String... args) throws Exception {
64 int errors = 0;
65 errors += new Basic().run();
66 if (errors > 0)
67 throw new Exception(errors + " errors found");
68 }
70 int run() throws IOException {
71 File testDir = new File(System.getProperty("test.classes", "."));
72 // test a .class file
73 test(new File(testDir, "Test.class"),
74 new String[] {"java.lang", "p"},
75 new String[] {"compact1", "not found"});
76 // test a directory
77 test(new File(testDir, "p"),
78 new String[] {"java.lang", "java.util", "java.lang.management"},
79 new String[] {"compact1", "compact1", "compact3"});
80 // test class-level dependency output
81 test(new File(testDir, "Test.class"),
82 new String[] {"java.lang.Object", "java.lang.String", "p.Foo"},
83 new String[] {"compact1", "compact1", "not found"},
84 new String[] {"-verbose:class"});
85 // test -p option
86 test(new File(testDir, "Test.class"),
87 new String[] {"p.Foo"},
88 new String[] {"not found"},
89 new String[] {"-verbose:class", "-p", "p"});
90 // test -e option
91 test(new File(testDir, "Test.class"),
92 new String[] {"p.Foo"},
93 new String[] {"not found"},
94 new String[] {"-verbose:class", "-e", "p\\..*"});
95 test(new File(testDir, "Test.class"),
96 new String[] {"java.lang"},
97 new String[] {"compact1"},
98 new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});
99 // test -classpath and -include options
100 test(null,
101 new String[] {"java.lang", "java.util",
102 "java.lang.management"},
103 new String[] {"compact1", "compact1", "compact3"},
104 new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});
105 test(new File(testDir, "Test.class"),
106 new String[] {"java.lang.Object", "java.lang.String", "p.Foo"},
107 new String[] {"compact1", "compact1", testDir.getName()},
108 new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});
109 return errors;
110 }
112 void test(File file, String[] expect, String[] profiles) {
113 test(file, expect, profiles, new String[0]);
114 }
116 void test(File file, String[] expect, String[] profiles, String[] options) {
117 List<String> args = new ArrayList<>(Arrays.asList(options));
118 if (file != null) {
119 args.add(file.getPath());
120 }
121 List<String> argsWithDashP = new ArrayList<>();
122 argsWithDashP.add("-P");
123 argsWithDashP.addAll(args);
124 // test without -P
125 checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
126 // test with -P
127 checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
128 }
130 Map<String,String> jdeps(String... args) {
131 StringWriter sw = new StringWriter();
132 PrintWriter pw = new PrintWriter(sw);
133 System.err.println("jdeps " + Arrays.toString(args));
134 int rc = com.sun.tools.jdeps.Main.run(args, pw);
135 pw.close();
136 String out = sw.toString();
137 if (!out.isEmpty())
138 System.err.println(out);
139 if (rc != 0)
140 throw new Error("jdeps failed: rc=" + rc);
141 return findDeps(out);
142 }
144 // Pattern used to parse lines
145 private static Pattern linePattern = Pattern.compile(".*\r?\n");
146 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
148 // Use the linePattern to break the given String into lines, applying
149 // the pattern to each line to see if we have a match
150 private static Map<String,String> findDeps(String out) {
151 Map<String,String> result = new HashMap<>();
152 Matcher lm = linePattern.matcher(out); // Line matcher
153 Matcher pm = null; // Pattern matcher
154 int lines = 0;
155 while (lm.find()) {
156 lines++;
157 CharSequence cs = lm.group(); // The current line
158 if (pm == null)
159 pm = pattern.matcher(cs);
160 else
161 pm.reset(cs);
162 if (pm.find())
163 result.put(pm.group(1), pm.group(2).trim());
164 if (lm.end() == out.length())
165 break;
166 }
167 return result;
168 }
170 void checkResult(String label, String[] expect, Collection<String> found) {
171 List<String> list = Arrays.asList(expect);
172 if (!isEqual(list, found))
173 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
174 }
176 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
177 if (expect.length != profiles.length)
178 error("Invalid expected names and profiles");
180 // check the dependencies
181 checkResult(label, expect, result.keySet());
182 // check profile information
183 checkResult(label, profiles, result.values());
184 for (int i=0; i < expect.length; i++) {
185 String profile = result.get(expect[i]);
186 if (!profile.equals(profiles[i]))
187 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
188 }
189 }
191 boolean isEqual(List<String> expected, Collection<String> found) {
192 if (expected.size() != found.size())
193 return false;
195 List<String> list = new ArrayList<>(found);
196 list.removeAll(expected);
197 return list.isEmpty();
198 }
200 void error(String msg) {
201 System.err.println("Error: " + msg);
202 errors++;
203 }
205 int errors;
206 }