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