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         assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "p"),
  63                                          CLASSES_DIR,
  64                                          "-XaddExports:java.base/sun.security.util=ALL-UNNAMED"));
  65     }
  66 
  67     @Test
  68     public void withReplacement() {
  69         Path file = Paths.get("p", "WithRepl.class");
  70         String[] output = JdepsUtil.jdeps("-jdkinternals", CLASSES_DIR.resolve(file).toString());
  71         int i = 0;
  72         while (!output[i].contains("Suggested Replacement")) {
  73             i++;
  74         }
  75 
  76         // must match the number of JDK internal APIs
  77         int count = output.length-i-2;
  78         assertEquals(count, REPLACEMENTS.size());
  79 
  80         for (int j=i+2; j < output.length; j++) {
  81             String line = output[j];
  82             int pos = line.indexOf("Use ");
  83             if (pos < 0)
  84                 pos = line.indexOf("or");
  85 
  86             assertTrue(pos > 0);
  87             String name = line.substring(0, pos).trim();
  88             String repl = line.substring(pos, line.length()).trim();
  89             assertEquals(REPLACEMENTS.get(name), repl);
  90         }
  91     }
  92 
  93     @Test
  94     public void noReplacement() {
  95         Path file = Paths.get("p", "NoRepl.class");
  96         String[] output = JdepsUtil.jdeps("-jdkinternals", CLASSES_DIR.resolve(file).toString());
  97         int i = 0;
  98         // expect no replacement
  99         while (i < output.length && !output[i].contains("Suggested Replacement")) {
 100             i++;
 101         }
 102 
 103         // no replacement
 104         assertEquals(output.length-i, 0);
 105     }
 106 }