langtools/test/tools/jdeps/jdk.unsupported/JDKUnsupportedTest.java
changeset 37008 55c73d04e57c
parent 37007 6023a9a9d58a
child 37009 476d8d615222
equal deleted inserted replaced
37007:6023a9a9d58a 37008:55c73d04e57c
     1 /*
       
     2  * Copyright (c) 2016, 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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @summary jdeps should flag jdk.unsupported exported API as internal
       
    27  * @modules jdk.jdeps/com.sun.tools.jdeps
       
    28  * @build Foo
       
    29  * @run testng JDKUnsupportedTest
       
    30  */
       
    31 
       
    32 import java.io.PrintWriter;
       
    33 import java.io.StringWriter;
       
    34 import java.nio.file.Path;
       
    35 import java.nio.file.Paths;
       
    36 import java.util.*;
       
    37 import java.util.regex.*;
       
    38 
       
    39 import org.testng.annotations.DataProvider;
       
    40 import org.testng.annotations.Test;
       
    41 import static org.testng.Assert.assertTrue;
       
    42 
       
    43 public class JDKUnsupportedTest {
       
    44     private static final String TEST_CLASSES = System.getProperty("test.classes");
       
    45     @DataProvider(name = "data")
       
    46     public Object[][] expected() {
       
    47         return new Object[][]{
       
    48             { "Foo.class", new String[][] {
       
    49                                new String[] { "java.lang", "java.base" },
       
    50                                new String[] { "sun.misc", "JDK internal API (java.base)" }
       
    51                            }
       
    52             }
       
    53         };
       
    54     }
       
    55 
       
    56     @Test(dataProvider = "data")
       
    57     public void test(String filename, String[][] expected) {
       
    58         Path path = Paths.get(TEST_CLASSES, filename);
       
    59 
       
    60         Map<String, String> result = jdeps("-M", path.toString());
       
    61         for (String[] e : expected) {
       
    62             String pn = e[0];
       
    63             String module = e[1];
       
    64             assertTrue(module.equals(result.get(pn)));
       
    65         }
       
    66     }
       
    67 
       
    68     private static Map<String,String> jdeps(String... args) {
       
    69         StringWriter sw = new StringWriter();
       
    70         PrintWriter pw = new PrintWriter(sw);
       
    71         System.err.println("jdeps " + Arrays.toString(args));
       
    72         int rc = com.sun.tools.jdeps.Main.run(args, pw);
       
    73         pw.close();
       
    74         String out = sw.toString();
       
    75         if (!out.isEmpty())
       
    76             System.err.println(out);
       
    77         if (rc != 0)
       
    78             throw new Error("jdeps failed: rc=" + rc);
       
    79         return findDeps(out);
       
    80     }
       
    81 
       
    82     // Pattern used to parse lines
       
    83     private static Pattern linePattern = Pattern.compile(".*\r?\n");
       
    84     private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
       
    85 
       
    86     // Use the linePattern to break the given String into lines, applying
       
    87     // the pattern to each line to see if we have a match
       
    88     private static Map<String,String> findDeps(String out) {
       
    89         Map<String,String> result = new LinkedHashMap<>();
       
    90         Matcher lm = linePattern.matcher(out);  // Line matcher
       
    91         Matcher pm = null;                      // Pattern matcher
       
    92         int lines = 0;
       
    93         while (lm.find()) {
       
    94             lines++;
       
    95             CharSequence cs = lm.group();       // The current line
       
    96             if (pm == null)
       
    97                 pm = pattern.matcher(cs);
       
    98             else
       
    99                 pm.reset(cs);
       
   100             if (pm.find())
       
   101                 result.put(pm.group(1), pm.group(2).trim());
       
   102             if (lm.end() == out.length())
       
   103                 break;
       
   104         }
       
   105         return result;
       
   106     }
       
   107 }