test/hotspot/jtreg/runtime/cds/appcds/jigsaw/modulepath/JvmtiAddPath.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54927 1512d88b24c6
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
       
     1 /*
       
     2  * Copyright (c) 2018, 2019, 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 /*
       
    26  * @test
       
    27  * @summary JvmtiEnv::AddToBootstrapClassLoaderSearch and JvmtiEnv::AddToSystemClassLoaderSearch should disable AppCDS
       
    28  * @requires vm.cds
       
    29  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds
       
    30  * @modules jdk.jartool/sun.tools.jar
       
    31  * @build sun.hotspot.WhiteBox
       
    32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
       
    33  * @compile ../../test-classes/JvmtiApp.java
       
    34  * @run driver/timeout=240 JvmtiAddPath
       
    35  */
       
    36 
       
    37 import java.io.File;
       
    38 import java.nio.file.Files;
       
    39 import java.nio.file.Path;
       
    40 import java.nio.file.Paths;
       
    41 import jdk.test.lib.process.OutputAnalyzer;
       
    42 import sun.hotspot.WhiteBox;
       
    43 
       
    44 public class JvmtiAddPath {
       
    45     static String use_whitebox_jar;
       
    46     static String[] no_extra_matches = {};
       
    47     static String[] check_appcds_enabled = {
       
    48         "[class,load] ExtraClass source: shared object"
       
    49     };
       
    50     static String[] check_appcds_disabled = {
       
    51         "[class,load] ExtraClass source: file:"
       
    52     };
       
    53 
       
    54     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
       
    55 
       
    56     private static final String TEST_SRC = System.getProperty("test.src");
       
    57 
       
    58     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
       
    59     private static final Path MODS_DIR = Paths.get("mods");
       
    60 
       
    61     // the module name of the test module
       
    62     private static final String TEST_MODULE1 = "com.simple";
       
    63 
       
    64     // the module main class
       
    65     private static final String MAIN_CLASS = "com.simple.Main";
       
    66 
       
    67     private static Path moduleDir = null;
       
    68     private static Path mainJar = null;
       
    69 
       
    70     public static void buildTestModule() throws Exception {
       
    71 
       
    72         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
       
    73         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
       
    74                                  MODS_DIR.resolve(TEST_MODULE1),
       
    75                                  MODS_DIR.toString());
       
    76 
       
    77         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
       
    78 
       
    79         mainJar = moduleDir.resolve(TEST_MODULE1 + ".jar");
       
    80         String classes = MODS_DIR.resolve(TEST_MODULE1).toString();
       
    81         JarBuilder.createModularJar(mainJar.toString(), classes, MAIN_CLASS);
       
    82     }
       
    83 
       
    84     static void run(String cp, String... args) throws Exception {
       
    85         run(no_extra_matches, cp, args);
       
    86     }
       
    87 
       
    88     static void run(String[] extra_matches, String cp, String... args) throws Exception {
       
    89         String[] opts = {"-cp", cp, "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", use_whitebox_jar};
       
    90         opts = TestCommon.concat(opts, args);
       
    91         TestCommon.run(opts).assertNormalExit(extra_matches);
       
    92     }
       
    93 
       
    94     public static void main(String[] args) throws Exception {
       
    95         buildTestModule();
       
    96         JarBuilder.build("jvmti_app", "JvmtiApp", "ExtraClass");
       
    97         JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
       
    98 
       
    99         // In all the test cases below, appJar does not contain Hello.class. Instead, we
       
   100         // append JAR file(s) that contain Hello.class to the boot classpath, the app
       
   101         // classpath, or both, and verify that Hello.class is loaded by the expected ClassLoader.
       
   102         String appJar = TestCommon.getTestJar("jvmti_app.jar");         // contains JvmtiApp.class
       
   103         String addappJar = mainJar.toString();  // contains Main.class
       
   104         String addbootJar = mainJar.toString(); // contains Main.class
       
   105         String twoAppJars = appJar + File.pathSeparator + addappJar;
       
   106         String modulePath = "--module-path=" + moduleDir.toString();
       
   107         String wbJar = TestCommon.getTestJar("WhiteBox.jar");
       
   108         use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
       
   109 
       
   110         OutputAnalyzer output = TestCommon.createArchive(
       
   111                                     appJar,
       
   112                                     TestCommon.list("JvmtiApp", "ExtraClass", MAIN_CLASS),
       
   113                                     use_whitebox_jar,
       
   114                                     modulePath);
       
   115         TestCommon.checkDump(output);
       
   116 
       
   117         System.out.println("Test case 1: not adding module path - Hello.class should not be found");
       
   118         run(check_appcds_enabled, appJar,
       
   119             "-Xlog:class+load", "JvmtiApp", "noadd", MAIN_CLASS); // appcds should be enabled
       
   120 
       
   121         System.out.println("Test case 2: add to boot classpath only - should find Hello.class in boot loader");
       
   122         String[] toCheck = TestCommon.isDynamicArchive() ? check_appcds_enabled :
       
   123                            check_appcds_disabled;
       
   124         run(toCheck, appJar,
       
   125             "-Xlog:class+load=trace",
       
   126             modulePath,
       
   127             "JvmtiApp", "bootonly", addbootJar, MAIN_CLASS); // appcds should be disabled
       
   128 
       
   129         System.out.println("Test case 3: add to app classpath only - should find Hello.class in app loader");
       
   130         run(appJar, modulePath,
       
   131             "JvmtiApp", "apponly", addappJar, MAIN_CLASS);
       
   132 
       
   133         System.out.println("Test case 4: add to boot and app paths - should find Hello.class in boot loader");
       
   134         run(appJar, modulePath,
       
   135             "JvmtiApp", "appandboot", addbootJar, addappJar, MAIN_CLASS);
       
   136 
       
   137         System.out.println("Test case 5: add to app using -cp, but add to boot using JVMTI - should find Hello.class in boot loader");
       
   138         run(appJar, modulePath,
       
   139             "JvmtiApp", "bootonly", addappJar, MAIN_CLASS);
       
   140 
       
   141         System.out.println("Test case 6: add to app using AppCDS, but add to boot using JVMTI - should find Hello.class in boot loader");
       
   142         output = TestCommon.createArchive(
       
   143                      appJar, TestCommon.list("JvmtiApp", "ExtraClass"),
       
   144                      use_whitebox_jar,
       
   145                      modulePath);
       
   146         TestCommon.checkDump(output);
       
   147         run(twoAppJars, modulePath,
       
   148             "JvmtiApp", "bootonly", addappJar, MAIN_CLASS);
       
   149 
       
   150         System.out.println("Test case 7: add to app using AppCDS, no JVMTI calls - should find Hello.class in app loader");
       
   151         run(twoAppJars, modulePath,
       
   152             "JvmtiApp", "noadd-appcds", MAIN_CLASS);
       
   153     }
       
   154 }