test/tools/jdeps/APIDeps.java

changeset 2139
defadd528513
child 2214
4a2ed1900428
equal deleted inserted replaced
2138:4d8af6fda907 2139:defadd528513
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 */
23
24 /*
25 * @test
26 * @bug 8015912
27 * @summary find API dependencies
28 * @build m.Bar m.Foo m.Gee b.B c.C c.I d.D e.E f.F g.G
29 * @run main APIDeps
30 */
31
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.*;
40
41 public class APIDeps {
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 }
62
63 public static void main(String... args) throws Exception {
64 int errors = 0;
65 errors += new APIDeps().run();
66 if (errors > 0)
67 throw new Exception(errors + " errors found");
68 }
69
70 int run() throws IOException {
71 File testDir = new File(System.getProperty("test.classes", "."));
72 String testDirBasename = testDir.toPath().getFileName().toString();
73 File mDir = new File(testDir, "m");
74 // all dependencies
75 test(new File(mDir, "Bar.class"),
76 new String[] {"java.lang.Object", "java.lang.String",
77 "java.util.Set", "java.util.HashSet",
78 "java.lang.management.ManagementFactory",
79 "java.lang.management.RuntimeMXBean",
80 "b.B", "c.C", "d.D", "f.F", "g.G"},
81 new String[] {"compact1", "compact3", testDirBasename},
82 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
83 test(new File(mDir, "Foo.class"),
84 new String[] {"c.I", "e.E", "f.F", "m.Bar"},
85 new String[] {testDirBasename},
86 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
87 test(new File(mDir, "Gee.class"),
88 new String[] {"g.G", "sun.misc.Lock"},
89 new String[] {testDirBasename, "JDK internal API"},
90 new String[] {"-classpath", testDir.getPath(), "-verbose"});
91 // parse only APIs
92 test(mDir,
93 new String[] {"java.lang.Object", "java.lang.String",
94 "java.util.Set",
95 "c.C", "d.D", "c.I", "e.E", "m.Bar"},
96 new String[] {"compact1", testDirBasename, mDir.getName()},
97 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"});
98 return errors;
99 }
100
101 void test(File file, String[] expect, String[] profiles) {
102 test(file, expect, profiles, new String[0]);
103 }
104
105 void test(File file, String[] expect, String[] profiles, String[] options) {
106 List<String> args = new ArrayList<>(Arrays.asList(options));
107 if (file != null) {
108 args.add(file.getPath());
109 }
110 checkResult("api-dependencies", expect, profiles,
111 jdeps(args.toArray(new String[0])));
112 }
113
114 Map<String,String> jdeps(String... args) {
115 StringWriter sw = new StringWriter();
116 PrintWriter pw = new PrintWriter(sw);
117 System.err.println("jdeps " + Arrays.toString(args));
118 int rc = com.sun.tools.jdeps.Main.run(args, pw);
119 pw.close();
120 String out = sw.toString();
121 if (!out.isEmpty())
122 System.err.println(out);
123 if (rc != 0)
124 throw new Error("jdeps failed: rc=" + rc);
125 return findDeps(out);
126 }
127
128 // Pattern used to parse lines
129 private static Pattern linePattern = Pattern.compile(".*\r?\n");
130 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
131
132 // Use the linePattern to break the given String into lines, applying
133 // the pattern to each line to see if we have a match
134 private static Map<String,String> findDeps(String out) {
135 Map<String,String> result = new HashMap<>();
136 Matcher lm = linePattern.matcher(out); // Line matcher
137 Matcher pm = null; // Pattern matcher
138 int lines = 0;
139 while (lm.find()) {
140 lines++;
141 CharSequence cs = lm.group(); // The current line
142 if (pm == null)
143 pm = pattern.matcher(cs);
144 else
145 pm.reset(cs);
146 if (pm.find())
147 result.put(pm.group(1), pm.group(2).trim());
148 if (lm.end() == out.length())
149 break;
150 }
151 return result;
152 }
153
154 void checkResult(String label, String[] expect, Collection<String> found) {
155 List<String> list = Arrays.asList(expect);
156 if (!isEqual(list, found))
157 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
158 }
159
160 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
161 // check the dependencies
162 checkResult(label, expect, result.keySet());
163 // check profile information
164 Set<String> values = new TreeSet<>();
165 String internal = "JDK internal API";
166 for (String s: result.values()) {
167 if (s.startsWith(internal)){
168 values.add(internal);
169 } else {
170 values.add(s);
171 }
172 }
173 checkResult(label, profiles, values);
174 }
175
176 boolean isEqual(List<String> expected, Collection<String> found) {
177 if (expected.size() != found.size())
178 return false;
179
180 List<String> list = new ArrayList<>(found);
181 list.removeAll(expected);
182 return list.isEmpty();
183 }
184
185 void error(String msg) {
186 System.err.println("Error: " + msg);
187 errors++;
188 }
189
190 int errors;
191 }

mercurial