1 /*
   2  * Copyright (c) 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 Hello World test for dynamic archive
  28  * @requires vm.cds
  29  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
  30  * @build Hello
  31  * @build sun.hotspot.WhiteBox
  32  * @run driver ClassFileInstaller -jar hello.jar Hello
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. HelloDynamic
  35  */
  36 
  37 public class HelloDynamic extends DynamicArchiveTestBase {
  38     public static void main(String[] args) throws Exception {
  39         runTest(HelloDynamic::testDefaultBase);
  40         runTest(HelloDynamic::testCustomBase);
  41     }
  42 
  43     // (1) Test with default base archive + top archive
  44     static void testDefaultBase() throws Exception {
  45         String topArchiveName = getNewArchiveName("top");
  46         doTest(null, topArchiveName);
  47     }
  48 
  49     // (2) Test with custom base archive + top archive
  50     static void testCustomBase() throws Exception {
  51         String topArchiveName = getNewArchiveName("top2");
  52         String baseArchiveName = getNewArchiveName("base");
  53         TestCommon.dumpBaseArchive(baseArchiveName);
  54         doTest(baseArchiveName, topArchiveName);
  55     }
  56 
  57     private static final String JDWP_OPTION =
  58         "-Xrunjdwp:transport=dt_socket,server=y,suspend=n";
  59 
  60     private static void doTest(String baseArchiveName, String topArchiveName) throws Exception {
  61         String appJar = ClassFileInstaller.getJarPath("hello.jar");
  62         String mainClass = "Hello";
  63         dump2(baseArchiveName, topArchiveName,
  64              "-Xlog:cds",
  65              "-Xlog:cds+dynamic=debug",
  66              "-cp", appJar, mainClass)
  67             .assertNormalExit(output -> {
  68                     output.shouldContain("Buffer-space to target-space delta")
  69                            .shouldContain("Written dynamic archive 0x");
  70                 });
  71         run2(baseArchiveName, topArchiveName,
  72             "-Xlog:class+load",
  73             "-Xlog:cds+dynamic=debug,cds=debug",
  74             "-cp", appJar, mainClass)
  75             .assertNormalExit(output -> {
  76                     output.shouldContain("Hello source: shared objects file")
  77                           .shouldHaveExitValue(0);
  78                 });
  79 
  80         // Sanity test with JDWP options.
  81         // Test with the default base archive should be sufficient.
  82         if (baseArchiveName == null) {
  83             run2(baseArchiveName, topArchiveName,
  84                 JDWP_OPTION,
  85                 "-Xlog:class+load",
  86                 "-Xlog:cds+dynamic=debug,cds=debug",
  87                 "-cp", appJar, mainClass)
  88                 .assertNormalExit(output -> {
  89                     output.shouldContain("Hello source: shared objects file")
  90                           .shouldHaveExitValue(0);
  91                     });
  92         }
  93     }
  94 }