test/tools/jdeps/APIDeps.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 2214
4a2ed1900428
child 2525
2eb010b6cb22
child 2538
1e39ae45d8ac
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

mchung@2139 1 /*
ksrini@2227 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
mchung@2139 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mchung@2139 4 *
mchung@2139 5 * This code is free software; you can redistribute it and/or modify it
mchung@2139 6 * under the terms of the GNU General Public License version 2 only, as
mchung@2139 7 * published by the Free Software Foundation.
mchung@2139 8 *
mchung@2139 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mchung@2139 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mchung@2139 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mchung@2139 12 * version 2 for more details (a copy is included in the LICENSE file that
mchung@2139 13 * accompanied this code).
mchung@2139 14 *
mchung@2139 15 * You should have received a copy of the GNU General Public License version
mchung@2139 16 * 2 along with this work; if not, write to the Free Software Foundation,
mchung@2139 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mchung@2139 18 *
mchung@2139 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mchung@2139 20 * or visit www.oracle.com if you need additional information or have any
mchung@2139 21 * questions.
mchung@2139 22 */
mchung@2139 23
mchung@2139 24 /*
mchung@2139 25 * @test
mchung@2214 26 * @bug 8015912 8029216
mchung@2214 27 * @summary Test -apionly and -jdkinternals options
mchung@2139 28 * @build m.Bar m.Foo m.Gee b.B c.C c.I d.D e.E f.F g.G
mchung@2139 29 * @run main APIDeps
mchung@2139 30 */
mchung@2139 31
mchung@2139 32 import java.io.File;
mchung@2139 33 import java.io.IOException;
mchung@2139 34 import java.io.PrintWriter;
mchung@2139 35 import java.io.StringWriter;
mchung@2139 36 import java.nio.file.Path;
mchung@2139 37 import java.nio.file.Paths;
mchung@2139 38 import java.util.*;
mchung@2139 39 import java.util.regex.*;
mchung@2139 40
mchung@2139 41 public class APIDeps {
mchung@2139 42 private static boolean symbolFileExist = initProfiles();
mchung@2139 43 private static boolean initProfiles() {
mchung@2139 44 // check if ct.sym exists; if not use the profiles.properties file
mchung@2139 45 Path home = Paths.get(System.getProperty("java.home"));
mchung@2139 46 if (home.endsWith("jre")) {
mchung@2139 47 home = home.getParent();
mchung@2139 48 }
mchung@2139 49 Path ctsym = home.resolve("lib").resolve("ct.sym");
mchung@2139 50 boolean symbolExists = ctsym.toFile().exists();
mchung@2139 51 if (!symbolExists) {
mchung@2139 52 Path testSrcProfiles =
mchung@2139 53 Paths.get(System.getProperty("test.src", "."), "profiles.properties");
mchung@2139 54 if (!testSrcProfiles.toFile().exists())
mchung@2139 55 throw new Error(testSrcProfiles + " does not exist");
mchung@2139 56 System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",
mchung@2139 57 ctsym, testSrcProfiles);
mchung@2139 58 System.setProperty("jdeps.profiles", testSrcProfiles.toString());
mchung@2139 59 }
mchung@2139 60 return symbolExists;
mchung@2139 61 }
mchung@2139 62
mchung@2139 63 public static void main(String... args) throws Exception {
mchung@2139 64 int errors = 0;
mchung@2139 65 errors += new APIDeps().run();
mchung@2139 66 if (errors > 0)
mchung@2139 67 throw new Exception(errors + " errors found");
mchung@2139 68 }
mchung@2139 69
mchung@2139 70 int run() throws IOException {
mchung@2139 71 File testDir = new File(System.getProperty("test.classes", "."));
mchung@2139 72 String testDirBasename = testDir.toPath().getFileName().toString();
mchung@2139 73 File mDir = new File(testDir, "m");
mchung@2139 74 // all dependencies
mchung@2139 75 test(new File(mDir, "Bar.class"),
mchung@2139 76 new String[] {"java.lang.Object", "java.lang.String",
mchung@2139 77 "java.util.Set", "java.util.HashSet",
mchung@2139 78 "java.lang.management.ManagementFactory",
mchung@2139 79 "java.lang.management.RuntimeMXBean",
mchung@2139 80 "b.B", "c.C", "d.D", "f.F", "g.G"},
mchung@2139 81 new String[] {"compact1", "compact3", testDirBasename},
mchung@2139 82 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
mchung@2139 83 test(new File(mDir, "Foo.class"),
mchung@2139 84 new String[] {"c.I", "e.E", "f.F", "m.Bar"},
mchung@2139 85 new String[] {testDirBasename},
mchung@2139 86 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
mchung@2139 87 test(new File(mDir, "Gee.class"),
mchung@2139 88 new String[] {"g.G", "sun.misc.Lock"},
mchung@2139 89 new String[] {testDirBasename, "JDK internal API"},
mchung@2139 90 new String[] {"-classpath", testDir.getPath(), "-verbose"});
mchung@2214 91
mchung@2214 92 // -jdkinternals
mchung@2214 93 test(new File(mDir, "Gee.class"),
mchung@2214 94 new String[] {"sun.misc.Lock"},
mchung@2214 95 new String[] {"JDK internal API"},
mchung@2214 96 new String[] {"-jdkinternals"});
mchung@2214 97 // -jdkinternals parses all classes on -classpath and the input arguments
mchung@2214 98 test(new File(mDir, "Gee.class"),
mchung@2214 99 new String[] {"sun.misc.Lock", "sun.misc.Unsafe"},
mchung@2214 100 new String[] {"JDK internal API"},
mchung@2214 101 new String[] {"-classpath", testDir.getPath(), "-jdkinternals"});
mchung@2214 102
mchung@2214 103 // parse only APIs
mchung@2139 104 // parse only APIs
mchung@2139 105 test(mDir,
mchung@2139 106 new String[] {"java.lang.Object", "java.lang.String",
mchung@2139 107 "java.util.Set",
mchung@2139 108 "c.C", "d.D", "c.I", "e.E", "m.Bar"},
mchung@2139 109 new String[] {"compact1", testDirBasename, mDir.getName()},
mchung@2139 110 new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"});
mchung@2139 111 return errors;
mchung@2139 112 }
mchung@2139 113
mchung@2139 114 void test(File file, String[] expect, String[] profiles) {
mchung@2139 115 test(file, expect, profiles, new String[0]);
mchung@2139 116 }
mchung@2139 117
mchung@2139 118 void test(File file, String[] expect, String[] profiles, String[] options) {
mchung@2139 119 List<String> args = new ArrayList<>(Arrays.asList(options));
mchung@2139 120 if (file != null) {
mchung@2139 121 args.add(file.getPath());
mchung@2139 122 }
mchung@2139 123 checkResult("api-dependencies", expect, profiles,
mchung@2139 124 jdeps(args.toArray(new String[0])));
mchung@2139 125 }
mchung@2139 126
mchung@2139 127 Map<String,String> jdeps(String... args) {
mchung@2139 128 StringWriter sw = new StringWriter();
mchung@2139 129 PrintWriter pw = new PrintWriter(sw);
mchung@2139 130 System.err.println("jdeps " + Arrays.toString(args));
mchung@2139 131 int rc = com.sun.tools.jdeps.Main.run(args, pw);
mchung@2139 132 pw.close();
mchung@2139 133 String out = sw.toString();
mchung@2139 134 if (!out.isEmpty())
mchung@2139 135 System.err.println(out);
mchung@2139 136 if (rc != 0)
mchung@2139 137 throw new Error("jdeps failed: rc=" + rc);
mchung@2139 138 return findDeps(out);
mchung@2139 139 }
mchung@2139 140
mchung@2139 141 // Pattern used to parse lines
mchung@2139 142 private static Pattern linePattern = Pattern.compile(".*\r?\n");
mchung@2139 143 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
mchung@2139 144
mchung@2139 145 // Use the linePattern to break the given String into lines, applying
mchung@2139 146 // the pattern to each line to see if we have a match
mchung@2139 147 private static Map<String,String> findDeps(String out) {
mchung@2139 148 Map<String,String> result = new HashMap<>();
mchung@2139 149 Matcher lm = linePattern.matcher(out); // Line matcher
mchung@2139 150 Matcher pm = null; // Pattern matcher
mchung@2139 151 int lines = 0;
mchung@2139 152 while (lm.find()) {
mchung@2139 153 lines++;
mchung@2139 154 CharSequence cs = lm.group(); // The current line
mchung@2139 155 if (pm == null)
mchung@2139 156 pm = pattern.matcher(cs);
mchung@2139 157 else
mchung@2139 158 pm.reset(cs);
mchung@2139 159 if (pm.find())
mchung@2139 160 result.put(pm.group(1), pm.group(2).trim());
mchung@2139 161 if (lm.end() == out.length())
mchung@2139 162 break;
mchung@2139 163 }
mchung@2139 164 return result;
mchung@2139 165 }
mchung@2139 166
mchung@2139 167 void checkResult(String label, String[] expect, Collection<String> found) {
mchung@2139 168 List<String> list = Arrays.asList(expect);
mchung@2139 169 if (!isEqual(list, found))
mchung@2139 170 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
mchung@2139 171 }
mchung@2139 172
mchung@2139 173 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
mchung@2139 174 // check the dependencies
mchung@2139 175 checkResult(label, expect, result.keySet());
mchung@2139 176 // check profile information
mchung@2139 177 Set<String> values = new TreeSet<>();
mchung@2139 178 String internal = "JDK internal API";
mchung@2139 179 for (String s: result.values()) {
mchung@2139 180 if (s.startsWith(internal)){
mchung@2139 181 values.add(internal);
mchung@2139 182 } else {
mchung@2139 183 values.add(s);
mchung@2139 184 }
mchung@2139 185 }
mchung@2139 186 checkResult(label, profiles, values);
mchung@2139 187 }
mchung@2139 188
mchung@2139 189 boolean isEqual(List<String> expected, Collection<String> found) {
mchung@2139 190 if (expected.size() != found.size())
mchung@2139 191 return false;
mchung@2139 192
mchung@2139 193 List<String> list = new ArrayList<>(found);
mchung@2139 194 list.removeAll(expected);
mchung@2139 195 return list.isEmpty();
mchung@2139 196 }
mchung@2139 197
mchung@2139 198 void error(String msg) {
mchung@2139 199 System.err.println("Error: " + msg);
mchung@2139 200 errors++;
mchung@2139 201 }
mchung@2139 202
mchung@2139 203 int errors;
mchung@2139 204 }

mercurial