1 /*
   2  * Copyright (c) 2012, 2015, 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  * @modules java.base/sun.misc
  29  *          java.management
  30  *          jdk.compiler/com.sun.tools.classfile
  31  *          jdk.compiler/com.sun.tools.jdeps
  32  * @build m.Bar m.Foo m.Gee b.B c.C c.I d.D e.E f.F g.G
  33  * @run main APIDeps
  34  */
  35 
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.io.PrintWriter;
  39 import java.io.StringWriter;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.*;
  43 import java.util.regex.*;
  44 
  45 public class APIDeps {
  46     public static void main(String... args) throws Exception {
  47         int errors = 0;
  48         errors += new APIDeps().run();
  49         if (errors > 0)
  50             throw new Exception(errors + " errors found");
  51     }
  52 
  53     int run() throws IOException {
  54         File testDir = new File(System.getProperty("test.classes", "."));
  55         String testDirBasename = testDir.toPath().getFileName().toString();
  56         File mDir = new File(testDir, "m");
  57         // all dependencies
  58         test(new File(mDir, "Bar.class"),
  59              new String[] {"java.lang.Object", "java.lang.String",
  60                            "java.util.Set", "java.util.HashSet",
  61                            "java.lang.management.ManagementFactory",
  62                            "java.lang.management.RuntimeMXBean",
  63                            "b.B", "c.C", "d.D", "f.F", "g.G"},
  64              new String[] {"compact1", "compact3", testDirBasename},
  65              new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
  66         test(new File(mDir, "Foo.class"),
  67              new String[] {"c.I", "e.E", "f.F"},
  68              new String[] {testDirBasename},
  69              new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P"});
  70         test(new File(mDir, "Foo.class"),
  71              new String[] {"c.I", "e.E", "f.F", "m.Bar"},
  72              new String[] {testDirBasename},
  73              new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"});
  74         test(new File(mDir, "Gee.class"),
  75              new String[] {"g.G", "sun.misc.Lock", "com.sun.tools.classfile.ClassFile",
  76                            "com.sun.management.ThreadMXBean", "com.sun.source.tree.BinaryTree"},
  77              new String[] {testDirBasename, "JDK internal API", "compact3", ""},
  78              new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"});
  79 
  80         // -jdkinternals
  81         test(new File(mDir, "Gee.class"),
  82              new String[] {"sun.misc.Lock", "com.sun.tools.classfile.ClassFile"},
  83              new String[] {"JDK internal API"},
  84              new String[] {"-jdkinternals"});
  85         // -jdkinternals parses all classes on -classpath and the input arguments
  86         test(new File(mDir, "Gee.class"),
  87              new String[] {"com.sun.tools.jdeps.Main", "com.sun.tools.classfile.ClassFile",
  88                            "sun.misc.Lock", "sun.misc.Unsafe"},
  89              new String[] {"JDK internal API"},
  90              new String[] {"-classpath", testDir.getPath(), "-jdkinternals"});
  91 
  92         // parse only APIs
  93         test(mDir,
  94              new String[] {"java.lang.Object", "java.lang.String",
  95                            "java.util.Set",
  96                            "c.C", "d.D", "c.I", "e.E"},
  97              new String[] {"compact1", testDirBasename},
  98              new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P", "-apionly"});
  99 
 100         test(mDir,
 101              new String[] {"java.lang.Object", "java.lang.String",
 102                            "java.util.Set",
 103                            "c.C", "d.D", "c.I", "e.E", "m.Bar"},
 104              new String[] {"compact1", testDirBasename, mDir.getName()},
 105              new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"});
 106         return errors;
 107     }
 108 
 109     void test(File file, String[] expect, String[] profiles) {
 110         test(file, expect, profiles, new String[0]);
 111     }
 112 
 113     void test(File file, String[] expect, String[] profiles, String[] options) {
 114         List<String> args = new ArrayList<>(Arrays.asList(options));
 115         if (file != null) {
 116             args.add(file.getPath());
 117         }
 118         checkResult("api-dependencies", expect, profiles,
 119                     jdeps(args.toArray(new String[0])));
 120     }
 121 
 122     Map<String,String> jdeps(String... args) {
 123         StringWriter sw = new StringWriter();
 124         PrintWriter pw = new PrintWriter(sw);
 125         System.err.println("jdeps " + Arrays.toString(args));
 126         int rc = com.sun.tools.jdeps.Main.run(args, pw);
 127         pw.close();
 128         String out = sw.toString();
 129         if (!out.isEmpty())
 130             System.err.println(out);
 131         if (rc != 0)
 132             throw new Error("jdeps failed: rc=" + rc);
 133         return findDeps(out);
 134     }
 135 
 136     // Pattern used to parse lines
 137     private static Pattern linePattern = Pattern.compile(".*\r?\n");
 138     private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
 139 
 140     // Use the linePattern to break the given String into lines, applying
 141     // the pattern to each line to see if we have a match
 142     private static Map<String,String> findDeps(String out) {
 143         Map<String,String> result = new HashMap<>();
 144         Matcher lm = linePattern.matcher(out);  // Line matcher
 145         Matcher pm = null;                      // Pattern matcher
 146         int lines = 0;
 147         while (lm.find()) {
 148             lines++;
 149             CharSequence cs = lm.group();       // The current line
 150             if (pm == null)
 151                 pm = pattern.matcher(cs);
 152             else
 153                 pm.reset(cs);
 154             if (pm.find())
 155                 result.put(pm.group(1), pm.group(2).trim());
 156             if (lm.end() == out.length())
 157                 break;
 158         }
 159         return result;
 160     }
 161 
 162     void checkResult(String label, String[] expect, Collection<String> found) {
 163         List<String> list = Arrays.asList(expect);
 164         if (!isEqual(list, found))
 165             error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
 166     }
 167 
 168     void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
 169         // check the dependencies
 170         checkResult(label, expect, result.keySet());
 171         // check profile information
 172         Set<String> values = new TreeSet<>();
 173         String internal = "JDK internal API";
 174         for (String s: result.values()) {
 175             if (s.startsWith(internal)){
 176                 values.add(internal);
 177             } else {
 178                 values.add(s);
 179             }
 180         }
 181         checkResult(label, profiles, values);
 182     }
 183 
 184     boolean isEqual(List<String> expected, Collection<String> found) {
 185         if (expected.size() != found.size())
 186             return false;
 187 
 188         List<String> list = new ArrayList<>(found);
 189         list.removeAll(expected);
 190         return list.isEmpty();
 191     }
 192 
 193     void error(String msg) {
 194         System.err.println("Error: " + msg);
 195         errors++;
 196     }
 197 
 198     int errors;
 199 }