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 
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.Map;
  37 
  38 import org.testng.annotations.BeforeTest;
  39 import org.testng.annotations.Test;
  40 
  41 import static org.testng.Assert.assertEquals;
  42 import static org.testng.Assert.assertTrue;
  43 
  44 public 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         String[] output = JdepsUtil.jdeps("-jdkinternals", CLASSES_DIR.resolve(file).toString());
  74         int i = 0;
  75         while (!output[i].contains("Suggested Replacement")) {
  76             i++;
  77         }
  78 
  79         // must match the number of JDK internal APIs
  80         int count = output.length-i-2;
  81         assertEquals(count, REPLACEMENTS.size());
  82 
  83         for (int j=i+2; j < output.length; j++) {
  84             String line = output[j];
  85             int pos = line.indexOf("Use ");
  86             if (pos < 0)
  87                 pos = line.indexOf("or");
  88 
  89             assertTrue(pos > 0);
  90             String name = line.substring(0, pos).trim();
  91             String repl = line.substring(pos, line.length()).trim();
  92             assertEquals(REPLACEMENTS.get(name), repl);
  93         }
  94     }
  95 
  96     /*
  97      * A JDK internal class has been removed while its package still exists.
  98      */
  99     @Test
 100     public void noReplacement() {
 101         Path file = Paths.get("q", "NoRepl.class");
 102         String[] output = JdepsUtil.jdeps("-jdkinternals", CLASSES_DIR.resolve(file).toString());
 103         int i = 0;
 104         // expect no replacement
 105         while (i < output.length && !output[i].contains("Suggested Replacement")) {
 106             i++;
 107         }
 108 
 109         // no replacement
 110         assertEquals(output.length-i, 0);
 111     }
 112 
 113     /*
 114      * A JDK internal package has been removed.
 115      */
 116     @Test
 117     public void removedPackage() {
 118         Path file = Paths.get("q", "RemovedPackage.class");
 119         String[] output = JdepsUtil.jdeps("-jdkinternals", CLASSES_DIR.resolve(file).toString());
 120         int i = 0;
 121         // expect no replacement
 122         while (i < output.length && !output[i].contains("Suggested Replacement")) {
 123             i++;
 124         }
 125 
 126         // no replacement
 127         assertEquals(output.length-i, 0);
 128     }
 129 }