1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/jdeps/APIDeps.java Thu Oct 17 13:19:48 2013 -0700 1.3 @@ -0,0 +1,191 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8015912 1.30 + * @summary find API dependencies 1.31 + * @build m.Bar m.Foo m.Gee b.B c.C c.I d.D e.E f.F g.G 1.32 + * @run main APIDeps 1.33 + */ 1.34 + 1.35 +import java.io.File; 1.36 +import java.io.IOException; 1.37 +import java.io.PrintWriter; 1.38 +import java.io.StringWriter; 1.39 +import java.nio.file.Path; 1.40 +import java.nio.file.Paths; 1.41 +import java.util.*; 1.42 +import java.util.regex.*; 1.43 + 1.44 +public class APIDeps { 1.45 + private static boolean symbolFileExist = initProfiles(); 1.46 + private static boolean initProfiles() { 1.47 + // check if ct.sym exists; if not use the profiles.properties file 1.48 + Path home = Paths.get(System.getProperty("java.home")); 1.49 + if (home.endsWith("jre")) { 1.50 + home = home.getParent(); 1.51 + } 1.52 + Path ctsym = home.resolve("lib").resolve("ct.sym"); 1.53 + boolean symbolExists = ctsym.toFile().exists(); 1.54 + if (!symbolExists) { 1.55 + Path testSrcProfiles = 1.56 + Paths.get(System.getProperty("test.src", "."), "profiles.properties"); 1.57 + if (!testSrcProfiles.toFile().exists()) 1.58 + throw new Error(testSrcProfiles + " does not exist"); 1.59 + System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n", 1.60 + ctsym, testSrcProfiles); 1.61 + System.setProperty("jdeps.profiles", testSrcProfiles.toString()); 1.62 + } 1.63 + return symbolExists; 1.64 + } 1.65 + 1.66 + public static void main(String... args) throws Exception { 1.67 + int errors = 0; 1.68 + errors += new APIDeps().run(); 1.69 + if (errors > 0) 1.70 + throw new Exception(errors + " errors found"); 1.71 + } 1.72 + 1.73 + int run() throws IOException { 1.74 + File testDir = new File(System.getProperty("test.classes", ".")); 1.75 + String testDirBasename = testDir.toPath().getFileName().toString(); 1.76 + File mDir = new File(testDir, "m"); 1.77 + // all dependencies 1.78 + test(new File(mDir, "Bar.class"), 1.79 + new String[] {"java.lang.Object", "java.lang.String", 1.80 + "java.util.Set", "java.util.HashSet", 1.81 + "java.lang.management.ManagementFactory", 1.82 + "java.lang.management.RuntimeMXBean", 1.83 + "b.B", "c.C", "d.D", "f.F", "g.G"}, 1.84 + new String[] {"compact1", "compact3", testDirBasename}, 1.85 + new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); 1.86 + test(new File(mDir, "Foo.class"), 1.87 + new String[] {"c.I", "e.E", "f.F", "m.Bar"}, 1.88 + new String[] {testDirBasename}, 1.89 + new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); 1.90 + test(new File(mDir, "Gee.class"), 1.91 + new String[] {"g.G", "sun.misc.Lock"}, 1.92 + new String[] {testDirBasename, "JDK internal API"}, 1.93 + new String[] {"-classpath", testDir.getPath(), "-verbose"}); 1.94 + // parse only APIs 1.95 + test(mDir, 1.96 + new String[] {"java.lang.Object", "java.lang.String", 1.97 + "java.util.Set", 1.98 + "c.C", "d.D", "c.I", "e.E", "m.Bar"}, 1.99 + new String[] {"compact1", testDirBasename, mDir.getName()}, 1.100 + new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"}); 1.101 + return errors; 1.102 + } 1.103 + 1.104 + void test(File file, String[] expect, String[] profiles) { 1.105 + test(file, expect, profiles, new String[0]); 1.106 + } 1.107 + 1.108 + void test(File file, String[] expect, String[] profiles, String[] options) { 1.109 + List<String> args = new ArrayList<>(Arrays.asList(options)); 1.110 + if (file != null) { 1.111 + args.add(file.getPath()); 1.112 + } 1.113 + checkResult("api-dependencies", expect, profiles, 1.114 + jdeps(args.toArray(new String[0]))); 1.115 + } 1.116 + 1.117 + Map<String,String> jdeps(String... args) { 1.118 + StringWriter sw = new StringWriter(); 1.119 + PrintWriter pw = new PrintWriter(sw); 1.120 + System.err.println("jdeps " + Arrays.toString(args)); 1.121 + int rc = com.sun.tools.jdeps.Main.run(args, pw); 1.122 + pw.close(); 1.123 + String out = sw.toString(); 1.124 + if (!out.isEmpty()) 1.125 + System.err.println(out); 1.126 + if (rc != 0) 1.127 + throw new Error("jdeps failed: rc=" + rc); 1.128 + return findDeps(out); 1.129 + } 1.130 + 1.131 + // Pattern used to parse lines 1.132 + private static Pattern linePattern = Pattern.compile(".*\r?\n"); 1.133 + private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); 1.134 + 1.135 + // Use the linePattern to break the given String into lines, applying 1.136 + // the pattern to each line to see if we have a match 1.137 + private static Map<String,String> findDeps(String out) { 1.138 + Map<String,String> result = new HashMap<>(); 1.139 + Matcher lm = linePattern.matcher(out); // Line matcher 1.140 + Matcher pm = null; // Pattern matcher 1.141 + int lines = 0; 1.142 + while (lm.find()) { 1.143 + lines++; 1.144 + CharSequence cs = lm.group(); // The current line 1.145 + if (pm == null) 1.146 + pm = pattern.matcher(cs); 1.147 + else 1.148 + pm.reset(cs); 1.149 + if (pm.find()) 1.150 + result.put(pm.group(1), pm.group(2).trim()); 1.151 + if (lm.end() == out.length()) 1.152 + break; 1.153 + } 1.154 + return result; 1.155 + } 1.156 + 1.157 + void checkResult(String label, String[] expect, Collection<String> found) { 1.158 + List<String> list = Arrays.asList(expect); 1.159 + if (!isEqual(list, found)) 1.160 + error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); 1.161 + } 1.162 + 1.163 + void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { 1.164 + // check the dependencies 1.165 + checkResult(label, expect, result.keySet()); 1.166 + // check profile information 1.167 + Set<String> values = new TreeSet<>(); 1.168 + String internal = "JDK internal API"; 1.169 + for (String s: result.values()) { 1.170 + if (s.startsWith(internal)){ 1.171 + values.add(internal); 1.172 + } else { 1.173 + values.add(s); 1.174 + } 1.175 + } 1.176 + checkResult(label, profiles, values); 1.177 + } 1.178 + 1.179 + boolean isEqual(List<String> expected, Collection<String> found) { 1.180 + if (expected.size() != found.size()) 1.181 + return false; 1.182 + 1.183 + List<String> list = new ArrayList<>(found); 1.184 + list.removeAll(expected); 1.185 + return list.isEmpty(); 1.186 + } 1.187 + 1.188 + void error(String msg) { 1.189 + System.err.println("Error: " + msg); 1.190 + errors++; 1.191 + } 1.192 + 1.193 + int errors; 1.194 +}