1 /*
   2  * Copyright (c) 2018, 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 Test archived system module sub-graph and verify objects are archived.
  28  * @requires vm.cds.archived.java.heap
  29  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/appcds
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @build sun.hotspot.WhiteBox
  34  * @compile CheckArchivedModuleApp.java
  35  * @run driver ClassFileInstaller -jar app.jar CheckArchivedModuleApp
  36  * @run driver ClassFileInstaller -jar WhiteBox.jar sun.hotspot.WhiteBox
  37  * @run main ArchivedModuleComboTest
  38  */
  39 
  40 import java.io.File;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import jdk.test.lib.process.OutputAnalyzer;
  45 import jdk.test.lib.process.ProcessTools;
  46 import sun.hotspot.WhiteBox;
  47 
  48 public class ArchivedModuleComboTest {
  49     public static void main(String[] args) throws Exception {
  50         String wbJar = ClassFileInstaller.getJarPath("WhiteBox.jar");
  51         String use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
  52         String appJar = ClassFileInstaller.getJarPath("app.jar");
  53 
  54         Path userDir = Paths.get(System.getProperty("user.dir"));
  55         Path moduleDir = Files.createTempDirectory(userDir, "mods");
  56 
  57         //
  58         // Dump without --module-path, without --show-module-resolution
  59         //
  60         OutputAnalyzer output = TestCommon.dump(appJar,
  61                                     TestCommon.list("CheckArchivedModuleApp"),
  62                                     use_whitebox_jar);
  63         TestCommon.checkDump(output);
  64 
  65         // Test case 1)
  66         // - Dump without --module-path, without --show-module-resolution
  67         // - Run from -cp only and without --show-module-resolution
  68         //     + archived boot layer module ModuleDescriptors should be used
  69         //     + archived boot layer configuration should be used
  70         System.out.println("----------------------- Test case 1 ----------------------");
  71         output = TestCommon.exec(appJar, use_whitebox_jar,
  72                                  "-XX:+UnlockDiagnosticVMOptions",
  73                                  "-XX:+WhiteBoxAPI",
  74                                  "CheckArchivedModuleApp",
  75                                  "yes",
  76                                  "yes");
  77         TestCommon.checkExec(output);
  78 
  79         // Test case 2)
  80         // - Dump without --module-path, without --show-module-resolution
  81         // - Run from -cp only and with --show-module-resolution
  82         //     + archived boot layer module ModuleDescriptors should be used with
  83         //       --show-module-resolution (requires resolution)
  84         //     + archived boot layer Configuration should not be disabled 
  85         System.out.println("----------------------- Test case 2 ----------------------");
  86         output = TestCommon.exec(appJar, use_whitebox_jar,
  87                                  "--show-module-resolution",
  88                                  "-XX:+UnlockDiagnosticVMOptions",
  89                                  "-XX:+WhiteBoxAPI",
  90                                  "CheckArchivedModuleApp",
  91                                  "yes",
  92                                  "no");
  93         TestCommon.checkExec(output, "root java.base jrt:/java.base");
  94 
  95         // Test case 3)
  96         // - Dump without --module-path, without --show-module-resolution
  97         // - Run with --module-path
  98         //    + archived boot layer module ModuleDescriptors should be disabled
  99         //    + archived boot layer Configuration should be disabled
 100         System.out.println("----------------------- Test case 3 ----------------------");
 101         output = TestCommon.exec(appJar, use_whitebox_jar,
 102                                  "--module-path",
 103                                  moduleDir.toString(),
 104                                  "-XX:+UnlockDiagnosticVMOptions",
 105                                  "-XX:+WhiteBoxAPI",
 106                                  "CheckArchivedModuleApp",
 107                                  "no",
 108                                  "no");
 109         TestCommon.checkExec(output);
 110 
 111         //
 112         // Dump with --module-path specified (test case 4, 5). Use an
 113         // empty directory as it's simple and still triggers the case
 114         // where system module objects are not archived.
 115         //
 116         output = TestCommon.dump(appJar,
 117                                  TestCommon.list("CheckArchivedModuleApp"),
 118                                  "--module-path",
 119                                  moduleDir.toString(),
 120                                  use_whitebox_jar);
 121         TestCommon.checkDump(output);
 122 
 123         // Test case 4)
 124         // - Dump with --module-path
 125         // - Run from -cp only, no archived boot layer module ModuleDescriptors
 126         //   and Configuration should be found.
 127         System.out.println("----------------------- Test case 4 ----------------------");
 128         output = TestCommon.exec(appJar, use_whitebox_jar,
 129                                  "-XX:+UnlockDiagnosticVMOptions",
 130                                  "-XX:+WhiteBoxAPI",
 131                                  "CheckArchivedModuleApp",
 132                                  "no",
 133                                  "no");
 134         TestCommon.checkExec(output);
 135 
 136         // Test case 5)
 137         // - Dump with --module-path
 138         // - Run with --module-path, no archived boot layer module ModuleDescriptors
 139         //   and Configuration should be found.
 140         System.out.println("----------------------- Test case 5 ----------------------");
 141         output = TestCommon.exec(appJar, use_whitebox_jar,
 142                                  "--module-path",
 143                                  moduleDir.toString(),
 144                                  "-XX:+UnlockDiagnosticVMOptions",
 145                                  "-XX:+WhiteBoxAPI",
 146                                  "CheckArchivedModuleApp",
 147                                  "no",
 148                                  "no");
 149         TestCommon.checkExec(output);
 150 
 151         //
 152         // Dump without --module-path, with --show-module-resolution
 153         //
 154         output = TestCommon.dump(appJar,
 155                                  TestCommon.list("CheckArchivedModuleApp"),
 156                                  "--show-module-resolution",
 157                                  use_whitebox_jar);
 158         TestCommon.checkDump(output, "root java.base jrt:/java.base");
 159 
 160         // Test case 6)
 161         // - Dump without --module-path, with --show-module-resolution
 162         // - Run from -cp only and without --show-module-resolution
 163         //     + archived boot layer module ModuleDescriptors should be used
 164         //     + archived boot layer Configuration should be used
 165         System.out.println("----------------------- Test case 6 ----------------------");
 166         output = TestCommon.exec(appJar, use_whitebox_jar,
 167                                  "-XX:+UnlockDiagnosticVMOptions",
 168                                  "-XX:+WhiteBoxAPI",
 169                                  "CheckArchivedModuleApp",
 170                                  "yes",
 171                                  "yes");
 172         TestCommon.checkExec(output);
 173 
 174         // Test case 7)
 175         // - Dump without --module-path, with --show-module-resolution
 176         // - Run from -cp only and with --show-module-resolution
 177         //     + archived boot layer module ModuleDescriptors should be used with
 178         //       --show-module-resolution (requires resolution)
 179         //     + archived boot layer Configuration should be disabled
 180         System.out.println("----------------------- Test case 7 ----------------------");
 181         output = TestCommon.exec(appJar, use_whitebox_jar,
 182                                  "--show-module-resolution",
 183                                  "-XX:+UnlockDiagnosticVMOptions",
 184                                  "-XX:+WhiteBoxAPI",
 185                                  "CheckArchivedModuleApp",
 186                                  "yes",
 187                                  "no");
 188         TestCommon.checkExec(output, "root java.base jrt:/java.base");
 189     }
 190 }