test/tools/jdeps/Basic.java

Wed, 18 Jun 2014 12:56:12 -0700

author
asaha
date
Wed, 18 Jun 2014 12:56:12 -0700
changeset 2469
c8b8cabfc922
parent 2172
aa91bc6e8480
child 2525
2eb010b6cb22
child 2538
1e39ae45d8ac
permissions
-rw-r--r--

Merge

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

mercurial