1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8153042
* @summary Tests JDK internal APIs that have been removed.
* @library ../lib
* @build CompilerUtils JdepsRunner JdepsUtil ModuleMetaData
* @modules jdk.jdeps/com.sun.tools.jdeps
* @run testng RemovedJDKInternals
*/
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import com.sun.tools.jdeps.DepsAnalyzer;
import com.sun.tools.jdeps.Graph;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class RemovedJDKInternals {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path CLASSES_DIR = Paths.get("classes");
private static final Path PATCHES_DIR = Paths.get("patches");
private static final String JDK_UNSUPPORTED = "jdk.unsupported";
/**
* Compiles classes used by the test
*/
@BeforeTest
public void compileAll() throws Exception {
CompilerUtils.cleanDir(PATCHES_DIR);
CompilerUtils.cleanDir(CLASSES_DIR);
// compile sun.misc types
Path sunMiscSrc = Paths.get(TEST_SRC, "patches", JDK_UNSUPPORTED);
Path patchDir = PATCHES_DIR.resolve(JDK_UNSUPPORTED);
assertTrue(CompilerUtils.compile(sunMiscSrc, patchDir,
"--patch-module", JDK_UNSUPPORTED + "=" + sunMiscSrc.toString()));
// compile com.sun.image.codec.jpeg types
Path codecSrc = Paths.get(TEST_SRC, "patches", "java.desktop");
Path codecDest = PATCHES_DIR;
assertTrue(CompilerUtils.compile(codecSrc, codecDest));
// patch jdk.unsupported and set -cp to codec types
assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "p"),
CLASSES_DIR,
"--patch-module", "jdk.unsupported=" + patchDir,
"-cp", codecDest.toString()));
}
@DataProvider(name = "deps")
public Object[][] deps() {
return new Object[][] {
{ "classes", new ModuleMetaData("classes", false)
.reference("p.Main", "java.lang.Class", "java.base")
.reference("p.Main", "java.lang.Object", "java.base")
.reference("p.Main", "java.util.Iterator", "java.base")
.reference("p.S", "java.lang.Object", "java.base")
.jdkInternal("p.Main", "sun.reflect.Reflection", "jdk.unsupported")
.removedJdkInternal("p.Main", "com.sun.image.codec.jpeg.JPEGCodec")
.removedJdkInternal("p.Main", "sun.misc.Service")
.removedJdkInternal("p.Main", "sun.misc.SoftCache")
},
};
}
@Test(dataProvider = "deps")
public void runTest(String name, ModuleMetaData data) throws Exception {
String cmd = String.format("jdeps -verbose:class %s%n", CLASSES_DIR);
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
jdeps.verbose("-verbose:class")
.addRoot(CLASSES_DIR);
DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
assertTrue(analyzer.run());
jdeps.dumpOutput(System.err);
Graph<DepsAnalyzer.Node> g = analyzer.dependenceGraph();
// there are two node with p.Main as origin
// one for exported API and one for removed JDK internal
g.nodes().stream()
.filter(u -> u.source.equals(data.moduleName))
.forEach(u -> g.adjacentNodes(u).stream()
.forEach(v -> data.checkDependence(u.name, v.name, v.source, v.info)));
}
}
private static final Map<String, String> REPLACEMENTS = Map.of(
"com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4",
"sun.misc.Service", "Use java.util.ServiceLoader @since 1.6",
"sun.misc.SoftCache", "Removed. See http://openjdk.java.net/jeps/260",
"sun.reflect.Reflection", "Use java.lang.StackWalker @since 9"
);
@Test
public void checkReplacement() {
JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.toString());
String[] output = jdeps.output();
int i = 0;
while (!output[i].contains("Suggested Replacement")) {
i++;
}
// must match the number of JDK internal APIs
int count = output.length-i-2;
assertEquals(count, REPLACEMENTS.size());
for (int j=i+2; j < output.length; j++) {
String line = output[j];
int pos = line.indexOf("Use ");
if (pos < 0)
pos = line.indexOf("Removed. ");
assertTrue(pos > 0);
String name = line.substring(0, pos).trim();
String repl = line.substring(pos, line.length()).trim();
assertEquals(REPLACEMENTS.get(name), repl);
}
}
}
|