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         // Dump without --module-path
  58         OutputAnalyzer output = TestCommon.dump(appJar,
  59                                     TestCommon.list("CheckArchivedModuleApp"),
  60                                     use_whitebox_jar);
  61         TestCommon.checkDump(output);
  62 
  63         // Test case 1)
  64         // - Dump without --module-path
  65         // - Run from -cp only, archived boot layer module ModuleDescriptors
  66         //   should be used.
  67         System.out.println("----------------------- Test case 1 ----------------------");
  68         output = TestCommon.exec(appJar, use_whitebox_jar,
  69                                  "-XX:+UnlockDiagnosticVMOptions",
  70                                  "-XX:+WhiteBoxAPI",
  71                                  "CheckArchivedModuleApp",
  72                                  "yes");
  73         TestCommon.checkExec(output);
  74 
  75         // Test case 2)
  76         // - Dump without --module-path
  77         // - Run from -cp only, archived boot layer module ModuleDescriptors
  78         //   should be used with --show-module-resolution (requires resolution).
  79         System.out.println("----------------------- Test case 2 ----------------------");
  80         output = TestCommon.exec(appJar, use_whitebox_jar,
  81                                  "--show-module-resolution",
  82                                  "-XX:+UnlockDiagnosticVMOptions",
  83                                  "-XX:+WhiteBoxAPI",
  84                                  "CheckArchivedModuleApp",
  85                                  "yes");
  86         TestCommon.checkExec(output);
  87 
  88         // Test case 3)
  89         // - Dump without --module-path
  90         // - Run with --module-path, archived boot layer module ModuleDescriptors
  91         //   should be disabled.
  92         System.out.println("----------------------- Test case 3 ----------------------");
  93         output = TestCommon.exec(appJar, use_whitebox_jar,
  94                                  "--module-path",
  95                                  moduleDir.toString(),
  96                                  "-XX:+UnlockDiagnosticVMOptions",
  97                                  "-XX:+WhiteBoxAPI",
  98                                  "CheckArchivedModuleApp",
  99                                  "no");
 100         TestCommon.checkExec(output);
 101 
 102         // Dump with --module-path specified (test case 4, 5)
 103         output = TestCommon.dump(appJar,
 104                                  TestCommon.list("CheckArchivedModuleApp"),
 105                                  "--module-path",
 106                                  moduleDir.toString(),
 107                                  use_whitebox_jar);
 108         TestCommon.checkDump(output);
 109         
 110         // Test case 4)
 111         // - Dump with --module-path
 112         // - Run from -cp only, no archived boot layer module ModuleDescriptors
 113         //   should be found.
 114         System.out.println("----------------------- Test case 4 ----------------------");
 115         output = TestCommon.exec(appJar, use_whitebox_jar,
 116                                  "-XX:+UnlockDiagnosticVMOptions",
 117                                  "-XX:+WhiteBoxAPI",
 118                                  "CheckArchivedModuleApp",
 119                                  "no");
 120         TestCommon.checkExec(output);
 121 
 122         // Test case 5)
 123         // - Dump with --module-path
 124         // - Run with --module-path, no archived boot layer module ModuleDescriptors
 125         //   should be found.
 126         System.out.println("----------------------- Test case 5 ----------------------");
 127         output = TestCommon.exec(appJar, use_whitebox_jar,
 128                                  "--module-path",
 129                                  moduleDir.toString(),
 130                                  "-XX:+UnlockDiagnosticVMOptions",
 131                                  "-XX:+WhiteBoxAPI",
 132                                  "CheckArchivedModuleApp",
 133                                  "no");
 134         TestCommon.checkExec(output);
 135     }
 136 }