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 
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.Map;
37 
38 import com.sun.tools.jdeps.DepsAnalyzer;
39 import com.sun.tools.jdeps.Graph;
40 import org.testng.annotations.BeforeTest;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 
44 import static org.testng.Assert.assertEquals;
45 import static org.testng.Assert.assertTrue;
46 
47 public 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
compileAll()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")
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.ReflectionFactory", "jdk.unsupported")
89                 .removedJdkInternal("p.Main", "sun.reflect.Reflection")
90                 .removedJdkInternal("p.Main", "com.sun.image.codec.jpeg.JPEGCodec")
91                 .removedJdkInternal("p.Main", "sun.misc.Service")
92                 .removedJdkInternal("p.Main", "sun.misc.SoftCache")
93             },
94         };
95     }
96 
97     @Test(dataProvider = "deps")
runTest(String name, ModuleMetaData data)98     public void runTest(String name, ModuleMetaData data) throws Exception {
99         String cmd = String.format("jdeps -verbose:class %s%n", CLASSES_DIR);
100         try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
101             jdeps.verbose("-verbose:class")
102                 .addRoot(CLASSES_DIR);
103 
104             DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
105             assertTrue(analyzer.run());
106             jdeps.dumpOutput(System.err);
107 
108             Graph<DepsAnalyzer.Node> g = analyzer.dependenceGraph();
109             // there are two node with p.Main as origin
110             // one for exported API and one for removed JDK internal
111             g.nodes().stream()
112                 .filter(u -> u.source.equals(data.moduleName))
113                 .forEach(u -> g.adjacentNodes(u).stream()
114                     .forEach(v -> data.checkDependence(u.name, v.name, v.source, v.info)));
115         }
116     }
117 
118     private static final Map<String, String> REPLACEMENTS = Map.of(
119         "com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4",
120         "sun.misc.Service", "Use java.util.ServiceLoader @since 1.6",
121         "sun.misc.SoftCache", "Removed. See http://openjdk.java.net/jeps/260",
122         "sun.reflect.Reflection", "Use java.lang.StackWalker @since 9",
123         "sun.reflect.ReflectionFactory", "See http://openjdk.java.net/jeps/260"
124     );
125 
126     @Test
checkReplacement()127     public void checkReplacement() {
128         JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.toString());
129         String[] output = jdeps.output();
130         int i = 0;
131         while (!output[i].contains("Suggested Replacement")) {
132             i++;
133         }
134 
135         // must match the number of JDK internal APIs
136         int count = output.length-i-2;
137         assertEquals(count, REPLACEMENTS.size());
138 
139         for (int j=i+2; j < output.length; j++) {
140             String line = output[j];
141             int pos = line.indexOf("Use ");
142             if (pos < 0)
143                 pos = line.indexOf("Removed. ");
144             if (pos < 0)
145                 pos = line.indexOf("See ");
146 
147             assertTrue(pos > 0);
148             String name = line.substring(0, pos).trim();
149             String repl = line.substring(pos, line.length()).trim();
150             assertEquals(REPLACEMENTS.get(name), repl);
151         }
152     }
153 }
154