Path: blob/master/test/langtools/tools/jdeps/unsupported/JDKUnsupportedTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary jdeps should flag jdk.unsupported exported API as internal26* @modules java.base/jdk.internal.misc27* jdk.jdeps/com.sun.tools.jdeps28* jdk.unsupported29* @build Foo Bar30* @run testng JDKUnsupportedTest31*/3233import java.io.PrintWriter;34import java.io.StringWriter;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.*;38import java.util.regex.*;3940import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;42import static org.testng.Assert.assertTrue;4344public class JDKUnsupportedTest {45private static final String TEST_CLASSES = System.getProperty("test.classes");46@DataProvider(name = "data")47public Object[][] expected() {48return new Object[][]{49{ "Foo.class", new String[][] {50new String[] { "java.lang", "java.base" },51new String[] { "jdk.internal.misc", "JDK internal API (java.base)" }52} },53{ "Bar.class", new String[][] {54new String[] { "java.lang", "java.base" },55new String[] { "sun.misc", "JDK internal API (jdk.unsupported)" }56} }57};58}5960@Test(dataProvider = "data")61public void test(String filename, String[][] expected) {62Path path = Paths.get(TEST_CLASSES, filename);6364Map<String, String> result = jdeps(path.toString());65for (String[] e : expected) {66String pn = e[0];67String module = e[1];68assertTrue(module.equals(result.get(pn)));69}70}7172private static Map<String,String> jdeps(String... args) {73StringWriter sw = new StringWriter();74PrintWriter pw = new PrintWriter(sw);75System.err.println("jdeps " + Arrays.toString(args));76int rc = com.sun.tools.jdeps.Main.run(args, pw);77pw.close();78String out = sw.toString();79if (!out.isEmpty())80System.err.println(out);81if (rc != 0)82throw new Error("jdeps failed: rc=" + rc);83return findDeps(out);84}8586// Pattern used to parse lines87private static Pattern linePattern = Pattern.compile(".*\r?\n");88private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");8990// Use the linePattern to break the given String into lines, applying91// the pattern to each line to see if we have a match92private static Map<String,String> findDeps(String out) {93Map<String,String> result = new LinkedHashMap<>();94Matcher lm = linePattern.matcher(out); // Line matcher95Matcher pm = null; // Pattern matcher96int lines = 0;97while (lm.find()) {98lines++;99CharSequence cs = lm.group(); // The current line100if (pm == null)101pm = pattern.matcher(cs);102else103pm.reset(cs);104if (pm.find())105result.put(pm.group(1), pm.group(2).trim());106if (lm.end() == out.length())107break;108}109return result;110}111}112113114