test/tools/jdeps/Basic.java

Mon, 23 Sep 2013 17:27:38 +0400

author
kizune
date
Mon, 23 Sep 2013 17:27:38 +0400
changeset 2048
809a50f24d6f
parent 1638
fd3fdaff0257
child 2139
defadd528513
permissions
-rw-r--r--

7154966: CRs found to be in Fixed state with no test and no noreg- keyword.
Reviewed-by: ksrini

     1 /*
     2  * Copyright (c) 2012, 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
    27  * @summary Basic tests for jdeps tool
    28  * @build Test p.Foo
    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.Path;
    37 import java.nio.file.Paths;
    38 import java.util.*;
    39 import java.util.regex.*;
    41 public class Basic {
    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 Basic().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         // test a .class file
    73         test(new File(testDir, "Test.class"),
    74              new String[] {"java.lang", "p"},
    75              new String[] {"compact1", "not found"});
    76         // test a directory
    77         test(new File(testDir, "p"),
    78              new String[] {"java.lang", "java.util", "java.lang.management"},
    79              new String[] {"compact1", "compact1", "compact3"});
    80         // test class-level dependency output
    81         test(new File(testDir, "Test.class"),
    82              new String[] {"java.lang.Object", "p.Foo"},
    83              new String[] {"compact1", "not found"},
    84              new String[] {"-V", "class"});
    85         // test -p option
    86         test(new File(testDir, "Test.class"),
    87              new String[] {"p.Foo"},
    88              new String[] {"not found"},
    89              new String[] {"--verbose-level=class", "-p", "p"});
    90         // test -e option
    91         test(new File(testDir, "Test.class"),
    92              new String[] {"p.Foo"},
    93              new String[] {"not found"},
    94              new String[] {"-V", "class", "-e", "p\\..*"});
    95         test(new File(testDir, "Test.class"),
    96              new String[] {"java.lang"},
    97              new String[] {"compact1"},
    98              new String[] {"-V", "package", "-e", "java\\.lang\\..*"});
    99         // test -classpath and wildcard options
   100         test(null,
   101              new String[] {"com.sun.tools.jdeps", "java.lang", "java.util",
   102                            "java.util.regex", "java.io", "java.nio.file",
   103                            "java.lang.management"},
   104              new String[] {(symbolFileExist? "not found" : "JDK internal API (classes)"),
   105                            "compact1", "compact1", "compact1",
   106                            "compact1", "compact1", "compact3"},
   107              new String[] {"--classpath", testDir.getPath(), "*"});
   108         /* Temporary disable this test case.  Test.class has a dependency
   109          * on java.lang.String on certain windows machine (8008479).
   110          // -v shows intra-dependency
   111          test(new File(testDir, "Test.class"),
   112               new String[] {"java.lang.Object", "p.Foo"},
   113               new String[] {"compact1", testDir.getName()},
   114               new String[] {"-v", "--classpath", testDir.getPath(), "Test.class"});
   115         */
   116         return errors;
   117     }
   119     void test(File file, String[] expect, String[] profiles) {
   120         test(file, expect, profiles, new String[0]);
   121     }
   123     void test(File file, String[] expect, String[] profiles, String[] options) {
   124         List<String> args = new ArrayList<>(Arrays.asList(options));
   125         if (file != null) {
   126             args.add(file.getPath());
   127         }
   128         List<String> argsWithDashP = new ArrayList<>();
   129         argsWithDashP.add("-P");
   130         argsWithDashP.addAll(args);
   131         // test without -P
   132         checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
   133         // test with -P
   134         checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
   135     }
   137     Map<String,String> jdeps(String... args) {
   138         StringWriter sw = new StringWriter();
   139         PrintWriter pw = new PrintWriter(sw);
   140         System.err.println("jdeps " + Arrays.toString(args));
   141         int rc = com.sun.tools.jdeps.Main.run(args, pw);
   142         pw.close();
   143         String out = sw.toString();
   144         if (!out.isEmpty())
   145             System.err.println(out);
   146         if (rc != 0)
   147             throw new Error("jdeps failed: rc=" + rc);
   148         return findDeps(out);
   149     }
   151     // Pattern used to parse lines
   152     private static Pattern linePattern = Pattern.compile(".*\r?\n");
   153     private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
   155     // Use the linePattern to break the given String into lines, applying
   156     // the pattern to each line to see if we have a match
   157     private static Map<String,String> findDeps(String out) {
   158         Map<String,String> result = new HashMap<>();
   159         Matcher lm = linePattern.matcher(out);  // Line matcher
   160         Matcher pm = null;                      // Pattern matcher
   161         int lines = 0;
   162         while (lm.find()) {
   163             lines++;
   164             CharSequence cs = lm.group();       // The current line
   165             if (pm == null)
   166                 pm = pattern.matcher(cs);
   167             else
   168                 pm.reset(cs);
   169             if (pm.find())
   170                 result.put(pm.group(1), pm.group(2).trim());
   171             if (lm.end() == out.length())
   172                 break;
   173         }
   174         return result;
   175     }
   177     void checkResult(String label, String[] expect, Collection<String> found) {
   178         List<String> list = Arrays.asList(expect);
   179         if (!isEqual(list, found))
   180             error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
   181     }
   183     void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
   184         if (expect.length != profiles.length)
   185             error("Invalid expected names and profiles");
   187         // check the dependencies
   188         checkResult(label, expect, result.keySet());
   189         // check profile information
   190         checkResult(label, profiles, result.values());
   191         for (int i=0; i < expect.length; i++) {
   192             String profile = result.get(expect[i]);
   193             if (!profile.equals(profiles[i]))
   194                 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
   195         }
   196     }
   198     boolean isEqual(List<String> expected, Collection<String> found) {
   199         if (expected.size() != found.size())
   200             return false;
   202         List<String> list = new ArrayList<>(found);
   203         list.removeAll(expected);
   204         return list.isEmpty();
   205     }
   207     void error(String msg) {
   208         System.err.println("Error: " + msg);
   209         errors++;
   210     }
   212     int errors;
   213 }

mercurial