1 /*
  2  * Copyright (c) 2020, 2023, 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  * @bug 8257241
 28  * @summary Run the LambdaEagerInitTest.java test in static CDS archive mode.
 29  *          Create a custom base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property.
 30  *          Run with the custom base archive with and without specifying the property.
 31  *          With the disableEagerInit set to true during dump time, lambda proxy classes
 32  *          will not be archived. During runtime, lambda proxy classes will not be loaded
 33  *          from the archive.
 34  *          Run with the default CDS archive, lambda proxy classes will be loaded
 35  *          from the archive if the property is not set.
 36  * @requires vm.cds
 37  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds test-classes
 38  * @compile ../../../../../lib/jdk/test/lib/Asserts.java
 39  * @run main/othervm LambdaEagerInit
 40  */
 41 
 42 import java.io.File;
 43 
 44 import jdk.test.lib.cds.CDSOptions;
 45 import jdk.test.lib.cds.CDSTestUtils;
 46 import jdk.test.lib.process.OutputAnalyzer;
 47 
 48 public class LambdaEagerInit {
 49     public static void main(String[] args) throws Exception {
 50         createArchiveWithEagerInitializationEnabled();
 51         testWithEagerInitializationEnabled();
 52         testWithEagerInitializationDisabled();
 53         // Skip testing with default CDS archive on aarch64 platform because
 54         // default archive isn't being generated on that platform.
 55         if (!("aarch64".equals(System.getProperty("os.arch")))) {
 56             testDefaultArchiveWithEagerInitializationEnabled();
 57             testDefaultArchiveWithEagerInitializationDisabled();
 58         }
 59     }
 60 
 61     private static final String classDir = System.getProperty("test.classes");
 62     private static final String mainClass = LambdaEagerInitTest.class.getName();
 63     private static final String testProperty = "-Djdk.internal.lambda.disableEagerInitialization=true";
 64     private static final String lambdaNotLoadedFromArchive =
 65         ".class.load. java.util.stream.Collectors[$][$]Lambda.*/0x.*source:.*java.*util.*stream.*Collectors";
 66     private static final String lambdaLoadedFromArchive =
 67         ".class.load. java.util.stream.Collectors[$][$]Lambda.*/0x.*source:.*shared.*objects.*file";
 68     private static final String cdsLoadedLambdaProxy = ".cds.*Loaded.*lambda.*proxy";
 69     private static final String archiveName = mainClass + ".jsa";
 70     private static String appJar;
 71 
 72     static void createArchiveWithEagerInitializationEnabled() throws Exception {
 73         appJar = JarBuilder.build("lambda_eager", new File(classDir), null);
 74 
 75         // create base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
 76         CDSOptions opts = (new CDSOptions())
 77             .addPrefix(testProperty,
 78                        "-Xlog:class+load,cds")
 79             .setArchiveName(archiveName);
 80         CDSTestUtils.createArchiveAndCheck(opts);
 81     }
 82 
 83     static void testWithEagerInitializationEnabled() throws Exception {
 84         // run with custom base archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
 85         CDSOptions runOpts = (new CDSOptions())
 86             .addPrefix("-cp", appJar, testProperty,  "-Xlog:class+load,cds=debug")
 87             .setArchiveName(archiveName)
 88             .setUseVersion(false)
 89             .addSuffix(mainClass);
 90         OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
 91         output.shouldMatch(lambdaNotLoadedFromArchive)
 92               .shouldNotMatch(cdsLoadedLambdaProxy)
 93               .shouldHaveExitValue(0);
 94     }
 95 
 96     static void testWithEagerInitializationDisabled() throws Exception {
 97         // run with custom base archive without the -Djdk.internal.lambda.disableEagerInitialization=true property
 98         CDSOptions runOpts = (new CDSOptions())
 99             .addPrefix("-cp", appJar, "-Xlog:class+load,cds=debug")
100             .setArchiveName(archiveName)
101             .setUseVersion(false)
102             .addSuffix(mainClass);
103         OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
104         output.shouldMatch(lambdaNotLoadedFromArchive)
105               .shouldNotMatch(cdsLoadedLambdaProxy)
106               .shouldHaveExitValue(0);
107     }
108 
109     static void testDefaultArchiveWithEagerInitializationEnabled() throws Exception {
110         // run with default CDS archive with the -Djdk.internal.lambda.disableEagerInitialization=true property
111         CDSOptions runOpts = (new CDSOptions())
112             .setXShareMode("auto")
113             .addPrefix("-cp", appJar, testProperty,  "-Xlog:class+load,cds=debug")
114             .setUseSystemArchive(true)
115             .setUseVersion(false)
116             .addSuffix(mainClass);
117         OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
118         output.shouldMatch(lambdaNotLoadedFromArchive)
119               .shouldNotMatch(cdsLoadedLambdaProxy)
120               .shouldHaveExitValue(0);
121     }
122 
123     static void testDefaultArchiveWithEagerInitializationDisabled() throws Exception {
124         // run with default CDS archive without the -Djdk.internal.lambda.disableEagerInitialization=true property
125         CDSOptions runOpts = (new CDSOptions())
126             .setXShareMode("auto")
127             .addPrefix("-cp", appJar, "-Xlog:class+load,cds=debug")
128             .setUseSystemArchive(true)
129             .setUseVersion(false)
130             .addSuffix("-showversion", mainClass);
131         OutputAnalyzer output = CDSTestUtils.runWithArchive(runOpts);
132         if (output.getStderr().contains("sharing")) {
133             output.shouldMatch(lambdaLoadedFromArchive)
134                   .shouldMatch(cdsLoadedLambdaProxy);
135         }
136         output.shouldHaveExitValue(0);
137     }
138 }