test/langtools/tools/jdeps/Basic.java
author mchung
Wed, 21 Nov 2018 22:34:01 -0800
changeset 52650 c16b6cc93272
parent 47216 71c04702a3d5
permissions -rw-r--r--
8213909: jdeps --print-module-deps should report missing dependences 8168869: jdeps: localized messages don't use proper line breaks Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 28706
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     4
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     8
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    13
 * accompanied this code).
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    14
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    18
 *
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    21
 * questions.
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    22
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    23
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    24
/*
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    25
 * @test
28706
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
    26
 * @bug 8003562 8005428 8015912 8027481 8048063 8068937
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    27
 * @summary Basic tests for jdeps tool
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 28706
diff changeset
    28
 * @modules java.management
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    29
 *          jdk.jdeps/com.sun.tools.jdeps
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    30
 * @build Test p.Foo p.Bar p.C p.SubClass q.Gee
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    31
 * @run main Basic
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    32
 */
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    33
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    34
import java.io.File;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    35
import java.io.IOException;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    36
import java.io.PrintWriter;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    37
import java.io.StringWriter;
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    38
import java.nio.file.Files;
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    39
import java.nio.file.Path;
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
    40
import java.nio.file.Paths;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    41
import java.util.*;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    42
import java.util.regex.*;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    43
import java.util.stream.Collectors;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    44
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    45
import static java.nio.file.StandardCopyOption.*;
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    46
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    47
public class Basic {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    48
    public static void main(String... args) throws Exception {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    49
        int errors = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    50
        errors += new Basic().run();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    51
        if (errors > 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    52
            throw new Exception(errors + " errors found");
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    53
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    54
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    55
    int run() throws IOException {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    56
        File testDir = new File(System.getProperty("test.classes", "."));
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    57
        // test a .class file
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    58
        test(new File(testDir, "Test.class"),
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    59
             new String[] {"java.lang", "p"},
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    60
             new String[] {"compact1", "not found"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    61
        // test a directory
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    62
        test(new File(testDir, "p"),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    63
             new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    64
             new String[] {"compact1", "compact1", "compact3", "compact1"},
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    65
             new String[] {"-classpath", testDir.getPath()});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    66
        // test class-level dependency output
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    67
        test(new File(testDir, "Test.class"),
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    68
             new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    69
             new String[] {"compact1", "compact1", "not found", "not found"},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    70
             new String[] {"-verbose:class"});
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    71
        // test -filter:none option
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    72
        test(new File(testDir, "p"),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    73
             new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto", "p"},
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    74
             new String[] {"compact1", "compact1", "compact3", "compact1", "p"},
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    75
             new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    76
        // test -filter:archive option
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    77
        test(new File(testDir, "p"),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    78
             new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"},
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    79
             new String[] {"compact1", "compact1", "compact3", "compact1"},
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    80
             new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    81
        // test -p option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    82
        test(new File(testDir, "Test.class"),
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    83
             new String[] {"p.Foo", "p.Bar"},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    84
             new String[] {"not found", "not found"},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    85
             new String[] {"-verbose:class", "-p", "p"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    86
        // test -e option
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    87
        test(new File(testDir, "Test.class"),
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    88
             new String[] {"p.Foo", "p.Bar"},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
    89
             new String[] {"not found", "not found"},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    90
             new String[] {"-verbose:class", "-e", "p\\..*"});
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    91
        test(new File(testDir, "Test.class"),
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
    92
             new String[] {"java.lang"},
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
    93
             new String[] {"compact1"},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
    94
             new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
    95
28706
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
    96
        // parse p.C, p.SubClass and q.*
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
    97
        // p.SubClass have no dependency other than p.C
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
    98
        // q.Gee depends on p.SubClass that should be found
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
    99
        test(testDir,
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   100
             new String[] {"java.lang", "p"},
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   101
             new String[] {"compact1", testDir.getName()},
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   102
             new String[] {"-include", "p.C|p.SubClass|q\\..*"});
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   103
        test(testDir,
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   104
             new String[] {"java.lang", "p"},
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   105
             new String[] {"compact1", testDir.getName()},
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   106
             new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});
a724585645ce 8068937: jdeps shows "not found" if target class has no reference other than its own package
mchung
parents: 27579
diff changeset
   107
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   108
        // test -classpath and -include options
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   109
        test(null,
25442
755ff386d1ac 8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents: 21503
diff changeset
   110
             new String[] {"java.lang", "java.util", "java.lang.management",
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
   111
                           "javax.crypto"},
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
   112
             new String[] {"compact1", "compact1", "compact3", "compact1"},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   113
             new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   114
        test(new File(testDir, "Test.class"),
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   115
             new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   116
             new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},
21046
ebf16a1a6328 8015912: jdeps support to output in dot file format
mchung
parents: 16550
diff changeset
   117
             new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   118
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   119
        // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   120
        Path testClassPath = testDir.toPath();
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   121
        Path dirP = testClassPath.resolve("p");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   122
        Path dir1 = testClassPath.resolve("dir1");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   123
        Path subdir1P = dir1.resolve("p");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   124
        Path dir2 = testClassPath.resolve("dir2");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   125
        Path subdir2P = dir2.resolve("p");
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   126
        if (!Files.exists(subdir1P))
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   127
            Files.createDirectories(subdir1P);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   128
        if (!Files.exists(subdir2P))
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   129
            Files.createDirectories(subdir2P);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   130
        Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   131
        Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   132
        StringBuilder cpath = new StringBuilder(testDir.toString());
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   133
        cpath.append(File.pathSeparator).append(dir1.toString());
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   134
        cpath.append(File.pathSeparator).append(dir2.toString());
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   135
        test(new File(testDir, "Test.class"),
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   136
             new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   137
             new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   138
             new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});
52650
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   139
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   140
        // tests --missing-deps option
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   141
        test(new File(testDir, "Test.class"),
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   142
             new String[] {"p.Foo", "p.Bar"},
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   143
             new String[] {"not found", "not found"},
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   144
             new String[] {"--missing-deps"});
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   145
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   146
        // no missing dependence
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   147
        test(new File(testDir, "Test.class"),
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   148
             new String[0],
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   149
             new String[0],
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   150
             new String[] {"--missing-deps", "-classpath", cpath.toString()});
c16b6cc93272 8213909: jdeps --print-module-deps should report missing dependences
mchung
parents: 47216
diff changeset
   151
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   152
        return errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   153
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   154
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   155
    void test(File file, String[] expect, String[] profiles) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   156
        test(file, expect, profiles, new String[0]);
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   157
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   158
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   159
    void test(File file, String[] expect, String[] profiles, String[] options) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   160
        List<String> args = new ArrayList<>(Arrays.asList(options));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   161
        if (file != null) {
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   162
            args.add(file.getPath());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   163
        }
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   164
        List<String> argsWithDashP = new ArrayList<>();
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   165
        argsWithDashP.add("-P");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   166
        argsWithDashP.addAll(args);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   167
        // test without -P
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   168
        checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   169
        // test with -P
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   170
        checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   171
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   172
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   173
    Map<String,String> jdeps(String... args) {
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   174
        StringWriter sw = new StringWriter();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   175
        PrintWriter pw = new PrintWriter(sw);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   176
        System.err.println("jdeps " + Arrays.stream(args).collect(Collectors.joining(" ")));
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   177
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   178
        pw.close();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   179
        String out = sw.toString();
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   180
        if (!out.isEmpty())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   181
            System.err.println(out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   182
        if (rc != 0)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   183
            throw new Error("jdeps failed: rc=" + rc);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   184
        return findDeps(out);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   185
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   186
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   187
    // Pattern used to parse lines
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   188
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   189
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   190
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   191
    // Use the linePattern to break the given String into lines, applying
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   192
    // the pattern to each line to see if we have a match
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   193
    private static Map<String,String> findDeps(String out) {
21503
45fc62482cae 8027481: jdeps to handle classes with the same package name and correct profile for javax.crypto.*
mchung
parents: 21046
diff changeset
   194
        Map<String,String> result = new LinkedHashMap<>();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   195
        Matcher lm = linePattern.matcher(out);  // Line matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   196
        Matcher pm = null;                      // Pattern matcher
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   197
        int lines = 0;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   198
        while (lm.find()) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   199
            lines++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   200
            CharSequence cs = lm.group();       // The current line
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   201
            if (pm == null)
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   202
                pm = pattern.matcher(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   203
            else
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   204
                pm.reset(cs);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   205
            if (pm.find())
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   206
                result.put(pm.group(1), pm.group(2).trim());
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   207
            if (lm.end() == out.length())
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   208
                break;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   209
        }
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   210
        return result;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   211
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   212
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   213
    void checkResult(String label, String[] expect, Collection<String> found) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   214
        List<String> list = Arrays.asList(expect);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   215
        if (!isEqual(list, found))
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   216
            error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   217
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   218
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   219
    void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   220
        if (expect.length != profiles.length)
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   221
            error("Invalid expected names and profiles");
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   222
16550
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   223
        // check the dependencies
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   224
        checkResult(label, expect, result.keySet());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   225
        // check profile information
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   226
        checkResult(label, profiles, result.values());
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   227
        for (int i=0; i < expect.length; i++) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   228
            String profile = result.get(expect[i]);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   229
            if (!profile.equals(profiles[i]))
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   230
                error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   231
        }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   232
    }
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   233
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   234
    boolean isEqual(List<String> expected, Collection<String> found) {
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   235
        if (expected.size() != found.size())
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   236
            return false;
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   237
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   238
        List<String> list = new ArrayList<>(found);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   239
        list.removeAll(expected);
f20e2521f3df 8005428: Update jdeps to read the same profile information as by javac
mchung
parents: 16290
diff changeset
   240
        return list.isEmpty();
15030
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   241
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   242
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   243
    void error(String msg) {
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   244
        System.err.println("Error: " + msg);
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   245
        errors++;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   246
    }
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   247
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   248
    int errors;
2d8dec41f029 8003562: Provide a CLI tool to analyze class dependencies
mchung
parents:
diff changeset
   249
}