test/langtools/tools/jdeps/unsupported/JDKUnsupportedTest.java
author egahlin
Mon, 25 Nov 2019 14:06:13 +0100
changeset 59246 fcad92f425c5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234703: JFR TestOutOfProcessMigration.java should clean up files Reviewed-by: mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary jdeps should flag jdk.unsupported exported API as internal
37008
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    27
 * @modules java.base/jdk.internal.misc
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    28
 *          jdk.jdeps/com.sun.tools.jdeps
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    29
 *          jdk.unsupported
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    30
 * @build Foo Bar
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run testng JDKUnsupportedTest
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.PrintWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.io.StringWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Paths;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.regex.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import org.testng.annotations.DataProvider;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import org.testng.annotations.Test;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import static org.testng.Assert.assertTrue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public class JDKUnsupportedTest {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    private static final String TEST_CLASSES = System.getProperty("test.classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    @DataProvider(name = "data")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    public Object[][] expected() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        return new Object[][]{
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
            { "Foo.class", new String[][] {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
                               new String[] { "java.lang", "java.base" },
37008
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    52
                               new String[] { "jdk.internal.misc", "JDK internal API (java.base)" }
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    53
                           } },
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    54
            { "Bar.class", new String[][] {
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    55
                               new String[] { "java.lang", "java.base" },
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    56
                               new String[] { "sun.misc", "JDK internal API (jdk.unsupported)" }
55c73d04e57c 8153737: Unsupported Module
chegar
parents: 36526
diff changeset
    57
                           } }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    @Test(dataProvider = "data")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    public void test(String filename, String[][] expected) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        Path path = Paths.get(TEST_CLASSES, filename);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 37008
diff changeset
    65
        Map<String, String> result = jdeps(path.toString());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        for (String[] e : expected) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            String pn = e[0];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            String module = e[1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            assertTrue(module.equals(result.get(pn)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    private static Map<String,String> jdeps(String... args) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        StringWriter sw = new StringWriter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        PrintWriter pw = new PrintWriter(sw);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        System.err.println("jdeps " + Arrays.toString(args));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        pw.close();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        String out = sw.toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        if (!out.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            System.err.println(out);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        if (rc != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            throw new Error("jdeps failed: rc=" + rc);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        return findDeps(out);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    // Pattern used to parse lines
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    // Use the linePattern to break the given String into lines, applying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    // the pattern to each line to see if we have a match
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    private static Map<String,String> findDeps(String out) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        Map<String,String> result = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        Matcher lm = linePattern.matcher(out);  // Line matcher
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Matcher pm = null;                      // Pattern matcher
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        int lines = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        while (lm.find()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            lines++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            CharSequence cs = lm.group();       // The current line
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            if (pm == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                pm = pattern.matcher(cs);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            else
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                pm.reset(cs);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            if (pm.find())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                result.put(pm.group(1), pm.group(2).trim());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            if (lm.end() == out.length())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
}