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

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

mercurial