test/tools/jdeps/APIDeps.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial