Path: blob/master/test/langtools/tools/jdeps/jdkinternals/RemovedJDKInternals.java
41152 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* @bug 815304226* @summary Tests JDK internal APIs that have been removed.27* @library ../lib28* @build CompilerUtils JdepsRunner JdepsUtil ModuleMetaData29* @modules jdk.jdeps/com.sun.tools.jdeps30* @run testng RemovedJDKInternals31*/3233import java.io.IOException;34import java.nio.file.Path;35import java.nio.file.Paths;36import java.util.Arrays;37import java.util.LinkedHashMap;38import java.util.List;39import java.util.Map;40import java.util.regex.Matcher;41import java.util.regex.Pattern;4243import com.sun.tools.jdeps.DepsAnalyzer;44import com.sun.tools.jdeps.Graph;45import org.testng.annotations.BeforeTest;46import org.testng.annotations.DataProvider;47import org.testng.annotations.Test;4849import static org.testng.Assert.assertEquals;50import static org.testng.Assert.assertFalse;51import static org.testng.Assert.assertTrue;5253public class RemovedJDKInternals {54private static final String TEST_SRC = System.getProperty("test.src");5556private static final Path CLASSES_DIR = Paths.get("classes");57private static final Path PATCHES_DIR = Paths.get("patches");5859private static final String JDK_UNSUPPORTED = "jdk.unsupported";60/**61* Compiles classes used by the test62*/63@BeforeTest64public void compileAll() throws Exception {65CompilerUtils.cleanDir(PATCHES_DIR);66CompilerUtils.cleanDir(CLASSES_DIR);6768// compile sun.misc types69Path sunMiscSrc = Paths.get(TEST_SRC, "patches", JDK_UNSUPPORTED);70Path patchDir = PATCHES_DIR.resolve(JDK_UNSUPPORTED);71assertTrue(CompilerUtils.compile(sunMiscSrc, patchDir,72"--patch-module", JDK_UNSUPPORTED + "=" + sunMiscSrc.toString()));7374// compile com.sun.image.codec.jpeg types75Path codecSrc = Paths.get(TEST_SRC, "patches", "java.desktop");76Path codecDest = PATCHES_DIR;77assertTrue(CompilerUtils.compile(codecSrc, codecDest));7879// patch jdk.unsupported and set -cp to codec types80assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "p"),81CLASSES_DIR,82"--patch-module", "jdk.unsupported=" + patchDir,83"-cp", codecDest.toString()));84}8586@DataProvider(name = "deps")87public Object[][] deps() {88return new Object[][] {89{ "classes", new ModuleMetaData("classes", false)90.reference("p.Main", "java.lang.Class", "java.base")91.reference("p.Main", "java.lang.Object", "java.base")92.reference("p.Main", "java.util.Iterator", "java.base")93.reference("p.S", "java.lang.Object", "java.base")94.jdkInternal("p.Main", "sun.reflect.ReflectionFactory", "jdk.unsupported")95.removedJdkInternal("p.Main", "sun.reflect.Reflection")96.removedJdkInternal("p.Main", "com.sun.image.codec.jpeg.JPEGCodec")97.removedJdkInternal("p.Main", "sun.misc.Service")98.removedJdkInternal("p.Main", "sun.misc.SoftCache")99},100};101}102103@Test(dataProvider = "deps")104public void runTest(String name, ModuleMetaData data) throws Exception {105String cmd = String.format("jdeps -verbose:class %s%n", CLASSES_DIR);106try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {107jdeps.verbose("-verbose:class")108.addRoot(CLASSES_DIR);109110DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();111assertTrue(analyzer.run());112jdeps.dumpOutput(System.err);113114Graph<DepsAnalyzer.Node> g = analyzer.dependenceGraph();115// there are two node with p.Main as origin116// one for exported API and one for removed JDK internal117g.nodes().stream()118.filter(u -> u.source.equals(data.moduleName))119.forEach(u -> g.adjacentNodes(u).stream()120.forEach(v -> data.checkDependence(u.name, v.name, v.source, v.info)));121}122}123124private static final List<String> REMOVED_APIS = List.of(125"com.sun.image.codec.jpeg.JPEGCodec",126"sun.misc.Service",127"sun.misc.SoftCache",128"sun.reflect.Reflection"129);130private static final String REMOVED_INTERNAL_API = "JDK removed internal API";131132133@Test134public void removedInternalJDKs() throws IOException {135// verify the JDK removed internal API136JdepsRunner summary = JdepsRunner.run("-summary", CLASSES_DIR.toString());137Arrays.stream(summary.output()).map(l -> l.split(" -> "))138.map(a -> a[1]).filter(n -> n.equals(REMOVED_INTERNAL_API))139.findFirst().orElseThrow();140141JdepsRunner jdeps = JdepsRunner.run("-verbose:class", CLASSES_DIR.toString());142String output = jdeps.stdout.toString();143Map<String, String> result = findDeps(output);144for (String cn : result.keySet()) {145String name = result.get(cn);146if (REMOVED_APIS.contains(cn)) {147assertEquals(name, REMOVED_INTERNAL_API);148} else if (cn.startsWith("sun.reflect")){149assertEquals(name, "JDK internal API (jdk.unsupported)");150} else {151assertEquals(name, "java.base");152}153}154REMOVED_APIS.stream().map(result::containsKey).allMatch(b -> b);155}156157// Pattern used to parse lines158private static final Pattern linePattern = Pattern.compile(".*\r?\n");159private static final Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");160161// Use the linePattern to break the given String into lines, applying162// the pattern to each line to see if we have a match163private static Map<String, String> findDeps(String out) {164Map<String, String> result = new LinkedHashMap<>();165Matcher lm = linePattern.matcher(out); // Line matcher166Matcher pm = null; // Pattern matcher167int lines = 0;168while (lm.find()) {169lines++;170CharSequence cs = lm.group(); // The current line171if (pm == null)172pm = pattern.matcher(cs);173else174pm.reset(cs);175if (pm.find())176result.put(pm.group(1), pm.group(2).trim());177if (lm.end() == out.length())178break;179}180return result;181}182183private static final Map<String, String> REPLACEMENTS = Map.of(184"com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4",185"sun.misc.Service", "Use java.util.ServiceLoader @since 1.6",186"sun.misc.SoftCache", "Removed. See http://openjdk.java.net/jeps/260",187"sun.reflect.Reflection", "Use java.lang.StackWalker @since 9",188"sun.reflect.ReflectionFactory", "See http://openjdk.java.net/jeps/260"189);190191@Test192public void checkReplacement() {193JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.toString());194String[] output = jdeps.output();195int i = 0;196while (!output[i].contains("Suggested Replacement")) {197i++;198}199200// must match the number of JDK internal APIs201int count = output.length-i-2;202assertEquals(count, REPLACEMENTS.size());203204for (int j=i+2; j < output.length; j++) {205String line = output[j];206int pos = line.indexOf("Use ");207if (pos < 0)208pos = line.indexOf("Removed. ");209if (pos < 0)210pos = line.indexOf("See ");211212assertTrue(pos > 0);213String name = line.substring(0, pos).trim();214String repl = line.substring(pos, line.length()).trim();215assertEquals(REPLACEMENTS.get(name), repl);216}217}218}219220221