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 8003562 8005428 8015912 8027481 8048063 8068937
  27  * @summary Basic tests for jdeps tool
  28  * @modules java.management
  29  *          jdk.jdeps/com.sun.tools.jdeps
  30  * @build Test p.Foo p.Bar p.C p.SubClass q.Gee
  31  * @run main Basic
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.PrintWriter;
  37 import java.io.StringWriter;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.*;
  42 import java.util.regex.*;
  43 import java.util.stream.Collectors;
  44 
  45 import static java.nio.file.StandardCopyOption.*;
  46 
  47 public class Basic {
  48     public static void main(String... args) throws Exception {
  49         int errors = 0;
  50         errors += new Basic().run();
  51         if (errors > 0)
  52             throw new Exception(errors + " errors found");
  53     }
  54 
  55     int run() throws IOException {
  56         File testDir = new File(System.getProperty("test.classes", "."));
  57         // test a .class file
  58         test(new File(testDir, "Test.class"),
  59              new String[] {"java.lang", "p"},
  60              new String[] {"compact1", "not found"});
  61         // test a directory
  62         test(new File(testDir, "p"),
  63              new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},
  64              new String[] {"compact1", "compact1", "compact3", "compact1"},
  65              new String[] {"-classpath", testDir.getPath()});
  66         // test class-level dependency output
  67         test(new File(testDir, "Test.class"),
  68              new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
  69              new String[] {"compact1", "compact1", "not found", "not found"},
  70              new String[] {"-verbose:class"});
  71         // test -filter:none option
  72         test(new File(testDir, "p"),
  73              new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto", "p"},
  74              new String[] {"compact1", "compact1", "compact3", "compact1", "p"},
  75              new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});
  76         // test -filter:archive option
  77         test(new File(testDir, "p"),
  78              new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},
  79              new String[] {"compact1", "compact1", "compact3", "compact1"},
  80              new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});
  81         // test -p option
  82         test(new File(testDir, "Test.class"),
  83              new String[] {"p.Foo", "p.Bar"},
  84              new String[] {"not found", "not found"},
  85              new String[] {"-verbose:class", "-p", "p"});
  86         // test -e option
  87         test(new File(testDir, "Test.class"),
  88              new String[] {"p.Foo", "p.Bar"},
  89              new String[] {"not found", "not found"},
  90              new String[] {"-verbose:class", "-e", "p\\..*"});
  91         test(new File(testDir, "Test.class"),
  92              new String[] {"java.lang"},
  93              new String[] {"compact1"},
  94              new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});
  95 
  96         // parse p.C, p.SubClass and q.*
  97         // p.SubClass have no dependency other than p.C
  98         // q.Gee depends on p.SubClass that should be found
  99         test(testDir,
 100              new String[] {"java.lang", "p"},
 101              new String[] {"compact1", testDir.getName()},
 102              new String[] {"-include", "p.C|p.SubClass|q\\..*"});
 103         test(testDir,
 104              new String[] {"java.lang", "p"},
 105              new String[] {"compact1", testDir.getName()},
 106              new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});
 107 
 108         // test -classpath and -include options
 109         test(null,
 110              new String[] {"java.lang", "java.util", "java.lang.management",
 111                            "javax.crypto"},
 112              new String[] {"compact1", "compact1", "compact3", "compact1"},
 113              new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});
 114         test(new File(testDir, "Test.class"),
 115              new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
 116              new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},
 117              new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});
 118 
 119         // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2
 120         Path testClassPath = testDir.toPath();
 121         Path dirP = testClassPath.resolve("p");
 122         Path dir1 = testClassPath.resolve("dir1");
 123         Path subdir1P = dir1.resolve("p");
 124         Path dir2 = testClassPath.resolve("dir2");
 125         Path subdir2P = dir2.resolve("p");
 126         if (!Files.exists(subdir1P))
 127             Files.createDirectories(subdir1P);
 128         if (!Files.exists(subdir2P))
 129             Files.createDirectories(subdir2P);
 130         Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);
 131         Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);
 132         StringBuilder cpath = new StringBuilder(testDir.toString());
 133         cpath.append(File.pathSeparator).append(dir1.toString());
 134         cpath.append(File.pathSeparator).append(dir2.toString());
 135         test(new File(testDir, "Test.class"),
 136              new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
 137              new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},
 138              new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});
 139 
 140         // tests --missing-deps option
 141         test(new File(testDir, "Test.class"),
 142              new String[] {"p.Foo", "p.Bar"},
 143              new String[] {"not found", "not found"},
 144              new String[] {"--missing-deps"});
 145 
 146         // no missing dependence
 147         test(new File(testDir, "Test.class"),
 148              new String[0],
 149              new String[0],
 150              new String[] {"--missing-deps", "-classpath", cpath.toString()});
 151 
 152         return errors;
 153     }
 154 
 155     void test(File file, String[] expect, String[] profiles) {
 156         test(file, expect, profiles, new String[0]);
 157     }
 158 
 159     void test(File file, String[] expect, String[] profiles, String[] options) {
 160         List<String> args = new ArrayList<>(Arrays.asList(options));
 161         if (file != null) {
 162             args.add(file.getPath());
 163         }
 164         List<String> argsWithDashP = new ArrayList<>();
 165         argsWithDashP.add("-P");
 166         argsWithDashP.addAll(args);
 167         // test without -P
 168         checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
 169         // test with -P
 170         checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
 171     }
 172 
 173     Map<String,String> jdeps(String... args) {
 174         StringWriter sw = new StringWriter();
 175         PrintWriter pw = new PrintWriter(sw);
 176         System.err.println("jdeps " + Arrays.stream(args).collect(Collectors.joining(" ")));
 177         int rc = com.sun.tools.jdeps.Main.run(args, pw);
 178         pw.close();
 179         String out = sw.toString();
 180         if (!out.isEmpty())
 181             System.err.println(out);
 182         if (rc != 0)
 183             throw new Error("jdeps failed: rc=" + rc);
 184         return findDeps(out);
 185     }
 186 
 187     // Pattern used to parse lines
 188     private static Pattern linePattern = Pattern.compile(".*\r?\n");
 189     private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
 190 
 191     // Use the linePattern to break the given String into lines, applying
 192     // the pattern to each line to see if we have a match
 193     private static Map<String,String> findDeps(String out) {
 194         Map<String,String> result = new LinkedHashMap<>();
 195         Matcher lm = linePattern.matcher(out);  // Line matcher
 196         Matcher pm = null;                      // Pattern matcher
 197         int lines = 0;
 198         while (lm.find()) {
 199             lines++;
 200             CharSequence cs = lm.group();       // The current line
 201             if (pm == null)
 202                 pm = pattern.matcher(cs);
 203             else
 204                 pm.reset(cs);
 205             if (pm.find())
 206                 result.put(pm.group(1), pm.group(2).trim());
 207             if (lm.end() == out.length())
 208                 break;
 209         }
 210         return result;
 211     }
 212 
 213     void checkResult(String label, String[] expect, Collection<String> found) {
 214         List<String> list = Arrays.asList(expect);
 215         if (!isEqual(list, found))
 216             error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
 217     }
 218 
 219     void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
 220         if (expect.length != profiles.length)
 221             error("Invalid expected names and profiles");
 222 
 223         // check the dependencies
 224         checkResult(label, expect, result.keySet());
 225         // check profile information
 226         checkResult(label, profiles, result.values());
 227         for (int i=0; i < expect.length; i++) {
 228             String profile = result.get(expect[i]);
 229             if (!profile.equals(profiles[i]))
 230                 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
 231         }
 232     }
 233 
 234     boolean isEqual(List<String> expected, Collection<String> found) {
 235         if (expected.size() != found.size())
 236             return false;
 237 
 238         List<String> list = new ArrayList<>(found);
 239         list.removeAll(expected);
 240         return list.isEmpty();
 241     }
 242 
 243     void error(String msg) {
 244         System.err.println("Error: " + msg);
 245         errors++;
 246     }
 247 
 248     int errors;
 249 }