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 * @bug 8153042 27 * @summary Tests JDK internal APIs that have been removed. 28 * @library ../lib 29 * @build CompilerUtils JdepsRunner JdepsUtil ModuleMetaData 30 * @modules jdk.jdeps/com.sun.tools.jdeps 31 * @run testng RemovedJDKInternals 32 */ 33 34import java.nio.file.Path; 35import java.nio.file.Paths; 36import java.util.Map; 37 38import com.sun.tools.jdeps.DepsAnalyzer; 39import com.sun.tools.jdeps.Graph; 40import org.testng.annotations.BeforeTest; 41import org.testng.annotations.DataProvider; 42import org.testng.annotations.Test; 43 44import static org.testng.Assert.assertEquals; 45import static org.testng.Assert.assertTrue; 46 47public class RemovedJDKInternals { 48 private static final String TEST_SRC = System.getProperty("test.src"); 49 50 private static final Path CLASSES_DIR = Paths.get("classes"); 51 private static final Path PATCHES_DIR = Paths.get("patches"); 52 53 private static final String JDK_UNSUPPORTED = "jdk.unsupported"; 54 /** 55 * Compiles classes used by the test 56 */ 57 @BeforeTest 58 public void compileAll() throws Exception { 59 CompilerUtils.cleanDir(PATCHES_DIR); 60 CompilerUtils.cleanDir(CLASSES_DIR); 61 62 // compile sun.misc types 63 Path sunMiscSrc = Paths.get(TEST_SRC, "patches", JDK_UNSUPPORTED); 64 Path patchDir = PATCHES_DIR.resolve(JDK_UNSUPPORTED); 65 assertTrue(CompilerUtils.compile(sunMiscSrc, patchDir, 66 "--patch-module", JDK_UNSUPPORTED + "=" + sunMiscSrc.toString())); 67 68 // compile com.sun.image.codec.jpeg types 69 Path codecSrc = Paths.get(TEST_SRC, "patches", "java.desktop"); 70 Path codecDest = PATCHES_DIR; 71 assertTrue(CompilerUtils.compile(codecSrc, codecDest)); 72 73 // patch jdk.unsupported and set -cp to codec types 74 assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "p"), 75 CLASSES_DIR, 76 "--patch-module", "jdk.unsupported=" + patchDir, 77 "-cp", codecDest.toString())); 78 } 79 80 @DataProvider(name = "deps") 81 public Object[][] deps() { 82 return new Object[][] { 83 { "classes", new ModuleMetaData("classes", false) 84 .reference("p.Main", "java.lang.Class", "java.base") 85 .reference("p.Main", "java.lang.Object", "java.base") 86 .reference("p.Main", "java.util.Iterator", "java.base") 87 .reference("p.S", "java.lang.Object", "java.base") 88 .jdkInternal("p.Main", "sun.reflect.Reflection", "jdk.unsupported") 89 .removedJdkInternal("p.Main", "com.sun.image.codec.jpeg.JPEGCodec") 90 .removedJdkInternal("p.Main", "sun.misc.Service") 91 .removedJdkInternal("p.Main", "sun.misc.SoftCache") 92 }, 93 }; 94 } 95 96 @Test(dataProvider = "deps") 97 public void runTest(String name, ModuleMetaData data) throws Exception { 98 String cmd = String.format("jdeps -verbose:class %s%n", CLASSES_DIR); 99 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 100 jdeps.verbose("-verbose:class") 101 .addRoot(CLASSES_DIR); 102 103 DepsAnalyzer analyzer = jdeps.getDepsAnalyzer(); 104 assertTrue(analyzer.run()); 105 jdeps.dumpOutput(System.err); 106 107 Graph<DepsAnalyzer.Node> g = analyzer.dependenceGraph(); 108 // there are two node with p.Main as origin 109 // one for exported API and one for removed JDK internal 110 g.nodes().stream() 111 .filter(u -> u.source.equals(data.moduleName)) 112 .forEach(u -> g.adjacentNodes(u).stream() 113 .forEach(v -> data.checkDependence(u.name, v.name, v.source, v.info))); 114 } 115 } 116 117 private static final Map<String, String> REPLACEMENTS = Map.of( 118 "com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4", 119 "sun.misc.Service", "Use java.util.ServiceLoader @since 1.6", 120 "sun.misc.SoftCache", "Removed. See http://openjdk.java.net/jeps/260", 121 "sun.reflect.Reflection", "Use java.lang.StackWalker @since 9" 122 ); 123 124 @Test 125 public void checkReplacement() { 126 JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.toString()); 127 String[] output = jdeps.output(); 128 int i = 0; 129 while (!output[i].contains("Suggested Replacement")) { 130 i++; 131 } 132 133 // must match the number of JDK internal APIs 134 int count = output.length-i-2; 135 assertEquals(count, REPLACEMENTS.size()); 136 137 for (int j=i+2; j < output.length; j++) { 138 String line = output[j]; 139 int pos = line.indexOf("Use "); 140 if (pos < 0) 141 pos = line.indexOf("Removed. "); 142 143 assertTrue(pos > 0); 144 String name = line.substring(0, pos).trim(); 145 String repl = line.substring(pos, line.length()).trim(); 146 assertEquals(REPLACEMENTS.get(name), repl); 147 } 148 } 149} 150