test/tools/jdeps/Basic.java

changeset 1638
fd3fdaff0257
parent 1577
88286a36bb34
child 2139
defadd528513
     1.1 --- a/test/tools/jdeps/Basic.java	Thu Mar 14 08:30:16 2013 +0000
     1.2 +++ b/test/tools/jdeps/Basic.java	Thu Mar 14 10:33:31 2013 -0700
     1.3 @@ -23,7 +23,7 @@
     1.4  
     1.5  /*
     1.6   * @test
     1.7 - * @bug 8003562
     1.8 + * @bug 8003562 8005428
     1.9   * @summary Basic tests for jdeps tool
    1.10   * @build Test p.Foo
    1.11   * @run main Basic
    1.12 @@ -33,13 +33,35 @@
    1.13  import java.io.IOException;
    1.14  import java.io.PrintWriter;
    1.15  import java.io.StringWriter;
    1.16 +import java.nio.file.Path;
    1.17 +import java.nio.file.Paths;
    1.18  import java.util.*;
    1.19  import java.util.regex.*;
    1.20  
    1.21  public class Basic {
    1.22 +    private static boolean symbolFileExist = initProfiles();
    1.23 +    private static boolean initProfiles() {
    1.24 +        // check if ct.sym exists; if not use the profiles.properties file
    1.25 +        Path home = Paths.get(System.getProperty("java.home"));
    1.26 +        if (home.endsWith("jre")) {
    1.27 +            home = home.getParent();
    1.28 +        }
    1.29 +        Path ctsym = home.resolve("lib").resolve("ct.sym");
    1.30 +        boolean symbolExists = ctsym.toFile().exists();
    1.31 +        if (!symbolExists) {
    1.32 +            Path testSrcProfiles =
    1.33 +                Paths.get(System.getProperty("test.src", "."), "profiles.properties");
    1.34 +            if (!testSrcProfiles.toFile().exists())
    1.35 +                throw new Error(testSrcProfiles + " does not exist");
    1.36 +            System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",
    1.37 +                ctsym, testSrcProfiles);
    1.38 +            System.setProperty("jdeps.profiles", testSrcProfiles.toString());
    1.39 +        }
    1.40 +        return symbolExists;
    1.41 +    }
    1.42 +
    1.43      public static void main(String... args) throws Exception {
    1.44          int errors = 0;
    1.45 -
    1.46          errors += new Basic().run();
    1.47          if (errors > 0)
    1.48              throw new Exception(errors + " errors found");
    1.49 @@ -49,54 +71,70 @@
    1.50          File testDir = new File(System.getProperty("test.classes", "."));
    1.51          // test a .class file
    1.52          test(new File(testDir, "Test.class"),
    1.53 -             new String[] {"java.lang", "p"});
    1.54 +             new String[] {"java.lang", "p"},
    1.55 +             new String[] {"compact1", "not found"});
    1.56          // test a directory
    1.57          test(new File(testDir, "p"),
    1.58 -             new String[] {"java.lang", "java.util"});
    1.59 +             new String[] {"java.lang", "java.util", "java.lang.management"},
    1.60 +             new String[] {"compact1", "compact1", "compact3"});
    1.61          // test class-level dependency output
    1.62          test(new File(testDir, "Test.class"),
    1.63               new String[] {"java.lang.Object", "p.Foo"},
    1.64 +             new String[] {"compact1", "not found"},
    1.65               new String[] {"-V", "class"});
    1.66          // test -p option
    1.67          test(new File(testDir, "Test.class"),
    1.68               new String[] {"p.Foo"},
    1.69 +             new String[] {"not found"},
    1.70               new String[] {"--verbose-level=class", "-p", "p"});
    1.71          // test -e option
    1.72          test(new File(testDir, "Test.class"),
    1.73               new String[] {"p.Foo"},
    1.74 +             new String[] {"not found"},
    1.75               new String[] {"-V", "class", "-e", "p\\..*"});
    1.76          test(new File(testDir, "Test.class"),
    1.77               new String[] {"java.lang"},
    1.78 +             new String[] {"compact1"},
    1.79               new String[] {"-V", "package", "-e", "java\\.lang\\..*"});
    1.80          // test -classpath and wildcard options
    1.81          test(null,
    1.82               new String[] {"com.sun.tools.jdeps", "java.lang", "java.util",
    1.83 -                           "java.util.regex", "java.io"},
    1.84 +                           "java.util.regex", "java.io", "java.nio.file",
    1.85 +                           "java.lang.management"},
    1.86 +             new String[] {(symbolFileExist? "not found" : "JDK internal API (classes)"),
    1.87 +                           "compact1", "compact1", "compact1",
    1.88 +                           "compact1", "compact1", "compact3"},
    1.89               new String[] {"--classpath", testDir.getPath(), "*"});
    1.90 -        // -v shows intra-dependency
    1.91 -        test(new File(testDir, "Test.class"),
    1.92 -             new String[] {"java.lang.Object", "p.Foo"},
    1.93 -             new String[] {"-v", "--classpath", testDir.getPath(), "Test.class"});
    1.94 +        /* Temporary disable this test case.  Test.class has a dependency
    1.95 +         * on java.lang.String on certain windows machine (8008479).
    1.96 +         // -v shows intra-dependency
    1.97 +         test(new File(testDir, "Test.class"),
    1.98 +              new String[] {"java.lang.Object", "p.Foo"},
    1.99 +              new String[] {"compact1", testDir.getName()},
   1.100 +              new String[] {"-v", "--classpath", testDir.getPath(), "Test.class"});
   1.101 +        */
   1.102          return errors;
   1.103      }
   1.104  
   1.105 -    void test(File file, String[] expect) {
   1.106 -        test(file, expect, new String[0]);
   1.107 +    void test(File file, String[] expect, String[] profiles) {
   1.108 +        test(file, expect, profiles, new String[0]);
   1.109      }
   1.110  
   1.111 -    void test(File file, String[] expect, String[] options) {
   1.112 -        String[] args;
   1.113 +    void test(File file, String[] expect, String[] profiles, String[] options) {
   1.114 +        List<String> args = new ArrayList<>(Arrays.asList(options));
   1.115          if (file != null) {
   1.116 -            args = Arrays.copyOf(options, options.length+1);
   1.117 -            args[options.length] = file.getPath();
   1.118 -        } else {
   1.119 -            args = options;
   1.120 +            args.add(file.getPath());
   1.121          }
   1.122 -        String[] deps = jdeps(args);
   1.123 -        checkEqual("dependencies", expect, deps);
   1.124 +        List<String> argsWithDashP = new ArrayList<>();
   1.125 +        argsWithDashP.add("-P");
   1.126 +        argsWithDashP.addAll(args);
   1.127 +        // test without -P
   1.128 +        checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
   1.129 +        // test with -P
   1.130 +        checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
   1.131      }
   1.132  
   1.133 -    String[] jdeps(String... args) {
   1.134 +    Map<String,String> jdeps(String... args) {
   1.135          StringWriter sw = new StringWriter();
   1.136          PrintWriter pw = new PrintWriter(sw);
   1.137          System.err.println("jdeps " + Arrays.toString(args));
   1.138 @@ -112,12 +150,12 @@
   1.139  
   1.140      // Pattern used to parse lines
   1.141      private static Pattern linePattern = Pattern.compile(".*\r?\n");
   1.142 -    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +.*");
   1.143 +    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
   1.144  
   1.145      // Use the linePattern to break the given String into lines, applying
   1.146      // the pattern to each line to see if we have a match
   1.147 -    private static String[] findDeps(String out) {
   1.148 -        List<String> result = new ArrayList<>();
   1.149 +    private static Map<String,String> findDeps(String out) {
   1.150 +        Map<String,String> result = new HashMap<>();
   1.151          Matcher lm = linePattern.matcher(out);  // Line matcher
   1.152          Matcher pm = null;                      // Pattern matcher
   1.153          int lines = 0;
   1.154 @@ -129,19 +167,41 @@
   1.155              else
   1.156                  pm.reset(cs);
   1.157              if (pm.find())
   1.158 -                result.add(pm.group(1));
   1.159 +                result.put(pm.group(1), pm.group(2).trim());
   1.160              if (lm.end() == out.length())
   1.161                  break;
   1.162          }
   1.163 -        return result.toArray(new String[0]);
   1.164 +        return result;
   1.165      }
   1.166  
   1.167 -    void checkEqual(String label, String[] expect, String[] found) {
   1.168 -        Set<String> s1 = new HashSet<>(Arrays.asList(expect));
   1.169 -        Set<String> s2 = new HashSet<>(Arrays.asList(found));
   1.170 +    void checkResult(String label, String[] expect, Collection<String> found) {
   1.171 +        List<String> list = Arrays.asList(expect);
   1.172 +        if (!isEqual(list, found))
   1.173 +            error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
   1.174 +    }
   1.175  
   1.176 -        if (!s1.equals(s2))
   1.177 -            error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'");
   1.178 +    void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
   1.179 +        if (expect.length != profiles.length)
   1.180 +            error("Invalid expected names and profiles");
   1.181 +
   1.182 +        // check the dependencies
   1.183 +        checkResult(label, expect, result.keySet());
   1.184 +        // check profile information
   1.185 +        checkResult(label, profiles, result.values());
   1.186 +        for (int i=0; i < expect.length; i++) {
   1.187 +            String profile = result.get(expect[i]);
   1.188 +            if (!profile.equals(profiles[i]))
   1.189 +                error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
   1.190 +        }
   1.191 +    }
   1.192 +
   1.193 +    boolean isEqual(List<String> expected, Collection<String> found) {
   1.194 +        if (expected.size() != found.size())
   1.195 +            return false;
   1.196 +
   1.197 +        List<String> list = new ArrayList<>(found);
   1.198 +        list.removeAll(expected);
   1.199 +        return list.isEmpty();
   1.200      }
   1.201  
   1.202      void error(String msg) {

mercurial