1 /*
   2  * Copyright (c) 2020, 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 Archive an base app class in a base archive and its lambda proxy
  28  *          class in a dynamic archive. During runtime, the base app class
  29  *          should be loaded from the base archive and the lambda proxy class
  30  *          should be loaded from the dynamic archive.
  31  * @requires vm.cds
  32  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes
  33  * @build SimpleApp sun.hotspot.WhiteBox
  34  * @run driver ClassFileInstaller -jar simpleApp.jar SimpleApp MyClass MyInterface
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. LambdaForClassInBaseArchive
  37  */
  38 
  39 import java.io.File;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 
  43 public class LambdaForClassInBaseArchive extends DynamicArchiveTestBase {
  44     static final String classList = System.getProperty("test.classes") +
  45         File.separator + "LambdaForClassInBaseArchive.list";
  46     static final String appClass = "SimpleApp";
  47 
  48     public static void main(String[] args) throws Exception {
  49         runTest(LambdaForClassInBaseArchive::testCustomBase);
  50     }
  51 
  52     static void testCustomBase() throws Exception {
  53         String topArchiveName = getNewArchiveName("top");
  54         String baseArchiveName = getNewArchiveName("base");
  55         doTestCustomBase(baseArchiveName, topArchiveName);
  56     }
  57 
  58     private static void doTestCustomBase(String baseArchiveName, String topArchiveName) throws Exception {
  59         String appJar = ClassFileInstaller.getJarPath("simpleApp.jar");
  60         // dump class list by running the SimpleApp
  61         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  62             "-XX:DumpLoadedClassList=" + classList,
  63             "-cp",
  64             appJar,
  65             appClass);
  66         OutputAnalyzer output = TestCommon.executeAndLog(pb, "dumpClassList");
  67         TestCommon.checkExecReturn(output, 0, true);
  68 
  69         // create a custom base archive based on the class list
  70         TestCommon.dumpBaseArchive(baseArchiveName,
  71                         "-XX:SharedClassListFile=" + classList,
  72                         "-cp", appJar, appClass);
  73 
  74         // create a dynamic archive with the custom base archive.
  75         // The SimpleApp class is in the base archive. Its interface
  76         // will be accessed using a lambda expression and the lambda
  77         // proxy class will be archived in the dynamic archive.
  78         dump2(baseArchiveName, topArchiveName,
  79               "-Xlog:cds,cds+dynamic",
  80               "-cp", appJar,
  81               appClass, "lambda")
  82             .assertNormalExit(out -> {
  83                     out.shouldHaveExitValue(0)
  84                        .shouldContain("Archiving hidden SimpleApp$$Lambda$1");
  85                 });
  86 
  87         // Run with both base and dynamic archives. The SimpleApp class
  88         // should be loaded from the base archive. Its lambda proxy class
  89         // should be loaded from the dynamic archive.
  90         run2(baseArchiveName, topArchiveName,
  91               "-Xlog:cds,cds+dynamic",
  92               "-Xlog:class+load=trace",
  93               "-cp", appJar,
  94               appClass, "lambda")
  95             .assertNormalExit(out -> {
  96                     out.shouldHaveExitValue(0)
  97                        .shouldContain("SimpleApp source: shared objects file")
  98                        .shouldMatch(".class.load. SimpleApp[$][$]Lambda[$]1/0x.*source:.*shared.*objects.*file.*(top)");
  99                 });
 100     }
 101 }