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 8159524
27 * @summary Tests JDK internal APIs with and without replacements
28 * @library ../lib
29 * @modules jdk.jdeps/com.sun.tools.jdeps
30 * @build CompilerUtils JdepsUtil
31 * @run testng ShowReplacement
32 */
33
34import java.nio.file.Path;
35import java.nio.file.Paths;
36import java.util.Map;
37
38import org.testng.annotations.BeforeTest;
39import org.testng.annotations.Test;
40
41import static org.testng.Assert.assertEquals;
42import static org.testng.Assert.assertTrue;
43
44public class ShowReplacement {
45    private static final String TEST_SRC = System.getProperty("test.src");
46
47    private static final Path CLASSES_DIR = Paths.get("classes");
48
49    private static final Map<String, String> REPLACEMENTS = Map.of(
50        "sun.security.util.HostnameChecker",
51        "Use javax.net.ssl.SSLParameters.setEndpointIdentificationAlgorithm(\"HTTPS\") @since 1.7",
52        "",
53        "or javax.net.ssl.HttpsURLConnection.setHostnameVerifier() @since 1.4");
54
55    /**
56     * Compiles classes used by the test
57     */
58    @BeforeTest
59    public void compileAll() throws Exception {
60        CompilerUtils.cleanDir(CLASSES_DIR);
61
62        Path tmp = Paths.get("tmp");
63        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "apple"), tmp));
64        assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "q"),
65                                         CLASSES_DIR,
66                                         "-cp", tmp.toString(),
67                                         "--add-exports=java.base/sun.security.util=ALL-UNNAMED"));
68    }
69
70    @Test
71    public void withReplacement() {
72        Path file = Paths.get("q", "WithRepl.class");
73        JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.resolve(file).toString());
74        String[] output = jdeps.output();
75        int i = 0;
76        while (!output[i].contains("Suggested Replacement")) {
77            i++;
78        }
79
80        // must match the number of JDK internal APIs
81        int count = output.length-i-2;
82        assertEquals(count, REPLACEMENTS.size());
83
84        for (int j=i+2; j < output.length; j++) {
85            String line = output[j];
86            int pos = line.indexOf("Use ");
87            if (pos < 0)
88                pos = line.indexOf("or");
89
90            assertTrue(pos > 0);
91            String name = line.substring(0, pos).trim();
92            String repl = line.substring(pos, line.length()).trim();
93            assertEquals(REPLACEMENTS.get(name), repl);
94        }
95    }
96
97    /*
98     * A JDK internal class has been removed while its package still exists.
99     */
100    @Test
101    public void noReplacement() {
102        Path file = Paths.get("q", "NoRepl.class");
103        JdepsRunner jdeps = JdepsRunner.run("-jdkinternals", CLASSES_DIR.resolve(file).toString());
104        String[] output = jdeps.output();
105        int i = 0;
106        // expect no replacement
107        while (i < output.length && !output[i].contains("Suggested Replacement")) {
108            i++;
109        }
110
111        // no replacement
112        assertEquals(output.length-i, 0);
113    }
114
115    /*
116     * A JDK internal package has been removed.
117     */
118    @Test
119    public void removedPackage() {
120        Path file = Paths.get("q", "RemovedPackage.class");
121        JdepsRunner jdeps = JdepsRunner.run("--jdk-internals", CLASSES_DIR.resolve(file).toString());
122        String[] output = jdeps.output();
123        int i = 0;
124        // expect no replacement
125        while (i < output.length && !output[i].contains("Suggested Replacement")) {
126            i++;
127        }
128
129        // no replacement
130        assertEquals(output.length-i, 0);
131    }
132}
133