Tue, 08 Jan 2019 16:17:56 -0800
Added tag jdk8u202-b26 for changeset c0f526208190
1 /*
2 * Copyright (c) 2012, 2014, 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 8015912 8029216 8048063 8050804
27 * @summary Test -apionly and -jdkinternals options
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 */
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 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 }
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 }
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"},
85 new String[] {testDirBasename},
86 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P"});
87 test(new File(mDir, "Foo.class"),
88 new String[] {"c.I", "e.E", "f.F", "m.Bar"},
89 new String[] {testDirBasename},
90 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"});
91 test(new File(mDir, "Gee.class"),
92 new String[] {"g.G", "sun.misc.Lock", "com.sun.tools.classfile.ClassFile",
93 "com.sun.management.ThreadMXBean", "com.sun.source.tree.BinaryTree",
94 "org.w3c.dom.css.CSSValue"},
95 new String[] {testDirBasename, "JDK internal API", "compact2", "compact3", ""},
96 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
98 // -jdkinternals
99 test(new File(mDir, "Gee.class"),
100 new String[] {"sun.misc.Lock", "com.sun.tools.classfile.ClassFile"},
101 new String[] {"JDK internal API"},
102 new String[] {"-jdkinternals"});
103 // -jdkinternals parses all classes on -classpath and the input arguments
104 test(new File(mDir, "Gee.class"),
105 new String[] {"com.sun.tools.jdeps.Main", "com.sun.tools.classfile.ClassFile",
106 "sun.misc.Lock", "sun.misc.Unsafe"},
107 new String[] {"JDK internal API"},
108 new String[] {"-classpath", testDir.getPath(), "-jdkinternals"});
110 // parse only APIs
111 test(mDir,
112 new String[] {"java.lang.Object", "java.lang.String",
113 "java.util.Set",
114 "c.C", "d.D", "c.I", "e.E"},
115 new String[] {"compact1", testDirBasename},
116 new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P", "-apionly"});
118 test(mDir,
119 new String[] {"java.lang.Object", "java.lang.String",
120 "java.util.Set",
121 "c.C", "d.D", "c.I", "e.E", "m.Bar"},
122 new String[] {"compact1", testDirBasename, mDir.getName()},
123 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"});
124 return errors;
125 }
127 void test(File file, String[] expect, String[] profiles) {
128 test(file, expect, profiles, new String[0]);
129 }
131 void test(File file, String[] expect, String[] profiles, String[] options) {
132 List<String> args = new ArrayList<>(Arrays.asList(options));
133 if (file != null) {
134 args.add(file.getPath());
135 }
136 checkResult("api-dependencies", expect, profiles,
137 jdeps(args.toArray(new String[0])));
138 }
140 Map<String,String> jdeps(String... args) {
141 StringWriter sw = new StringWriter();
142 PrintWriter pw = new PrintWriter(sw);
143 System.err.println("jdeps " + Arrays.toString(args));
144 int rc = com.sun.tools.jdeps.Main.run(args, pw);
145 pw.close();
146 String out = sw.toString();
147 if (!out.isEmpty())
148 System.err.println(out);
149 if (rc != 0)
150 throw new Error("jdeps failed: rc=" + rc);
151 return findDeps(out);
152 }
154 // Pattern used to parse lines
155 private static Pattern linePattern = Pattern.compile(".*\r?\n");
156 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
158 // Use the linePattern to break the given String into lines, applying
159 // the pattern to each line to see if we have a match
160 private static Map<String,String> findDeps(String out) {
161 Map<String,String> result = new HashMap<>();
162 Matcher lm = linePattern.matcher(out); // Line matcher
163 Matcher pm = null; // Pattern matcher
164 int lines = 0;
165 while (lm.find()) {
166 lines++;
167 CharSequence cs = lm.group(); // The current line
168 if (pm == null)
169 pm = pattern.matcher(cs);
170 else
171 pm.reset(cs);
172 if (pm.find())
173 result.put(pm.group(1), pm.group(2).trim());
174 if (lm.end() == out.length())
175 break;
176 }
177 return result;
178 }
180 void checkResult(String label, String[] expect, Collection<String> found) {
181 List<String> list = Arrays.asList(expect);
182 if (!isEqual(list, found))
183 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
184 }
186 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
187 // check the dependencies
188 checkResult(label, expect, result.keySet());
189 // check profile information
190 Set<String> values = new TreeSet<>();
191 String internal = "JDK internal API";
192 for (String s: result.values()) {
193 if (s.startsWith(internal)){
194 values.add(internal);
195 } else {
196 values.add(s);
197 }
198 }
199 checkResult(label, profiles, values);
200 }
202 boolean isEqual(List<String> expected, Collection<String> found) {
203 if (expected.size() != found.size())
204 return false;
206 List<String> list = new ArrayList<>(found);
207 list.removeAll(expected);
208 return list.isEmpty();
209 }
211 void error(String msg) {
212 System.err.println("Error: " + msg);
213 errors++;
214 }
216 int errors;
217 }