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