langtools/test/tools/jdeps/jdk.unsupported/JDKUnsupportedTest.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
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
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @modules jdk.jdeps/com.sun.tools.jdeps
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @build Foo
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @run testng JDKUnsupportedTest
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.PrintWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.StringWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Paths;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.regex.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import org.testng.annotations.DataProvider;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import org.testng.annotations.Test;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import static org.testng.Assert.assertTrue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
public class JDKUnsupportedTest {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    private static final String TEST_CLASSES = System.getProperty("test.classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
    @DataProvider(name = "data")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    public Object[][] expected() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        return new Object[][]{
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
            { "Foo.class", new String[][] {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
                               new String[] { "java.lang", "java.base" },
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
                               new String[] { "sun.misc", "JDK internal API (java.base)" }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
                           }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    @Test(dataProvider = "data")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    public void test(String filename, String[][] expected) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        Path path = Paths.get(TEST_CLASSES, filename);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        Map<String, String> result = jdeps("-M", path.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        for (String[] e : expected) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            String pn = e[0];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            String module = e[1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            assertTrue(module.equals(result.get(pn)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    private static Map<String,String> jdeps(String... args) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        StringWriter sw = new StringWriter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        PrintWriter pw = new PrintWriter(sw);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        System.err.println("jdeps " + Arrays.toString(args));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        int rc = com.sun.tools.jdeps.Main.run(args, pw);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        pw.close();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        String out = sw.toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        if (!out.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            System.err.println(out);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        if (rc != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            throw new Error("jdeps failed: rc=" + rc);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        return findDeps(out);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    // Pattern used to parse lines
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private static Pattern linePattern = Pattern.compile(".*\r?\n");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    // Use the linePattern to break the given String into lines, applying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    // the pattern to each line to see if we have a match
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    private static Map<String,String> findDeps(String out) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        Map<String,String> result = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        Matcher lm = linePattern.matcher(out);  // Line matcher
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        Matcher pm = null;                      // Pattern matcher
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        int lines = 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        while (lm.find()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            lines++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            CharSequence cs = lm.group();       // The current line
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            if (pm == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                pm = pattern.matcher(cs);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            else
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                pm.reset(cs);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            if (pm.find())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                result.put(pm.group(1), pm.group(2).trim());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            if (lm.end() == out.length())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
}