1 /*
 2  * Copyright (c) 2021, 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 8276184
28  * @summary Archive an old interface in the base archive and an app class which
29  *          uses the old interface via a lambda expression in the dynamic archive.
30  *          The lambda proxy class of the app class should be in the dynamic archive.
31  * @requires vm.cds
32  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
33  * @build LambdaContainsOldInfApp jdk.test.whitebox.WhiteBox OldProvider
34  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar old-inf-base-archive.jar LambdaContainsOldInfApp OldProvider
35  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
36  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. LambdaForOldInfInBaseArchive
37  */
38 
39 import java.io.File;
40 import jdk.test.lib.cds.CDSOptions;
41 import jdk.test.lib.cds.CDSTestUtils;
42 import jdk.test.lib.process.OutputAnalyzer;
43 import jdk.test.lib.helpers.ClassFileInstaller;
44 
45 public class LambdaForOldInfInBaseArchive extends DynamicArchiveTestBase {
46     static final String classList = CDSTestUtils.getOutputFileName("classlist");
47     static final String appClass = "LambdaContainsOldInfApp";
48     static final String baseArchiveClass = "OldProvider";
49 
50     public static void main(String[] args) throws Exception {
51         runTest(LambdaForOldInfInBaseArchive::testCustomBase);
52     }
53 
54     static void testCustomBase() throws Exception {
55         String topArchiveName = getNewArchiveName("top");
56         doTestCustomBase(topArchiveName);
57     }
58 
59     private static void doTestCustomBase(String topArchiveName) throws Exception {
60         String appJar = ClassFileInstaller.getJarPath("old-inf-base-archive.jar");
61 
62         // create a custom base archive containing and old interface
63         OutputAnalyzer output = TestCommon.dump(appJar,
64             TestCommon.list("OldProvider"), "-Xlog:class+load,cds+class=debug",
65             "-XX:-PreloadSharedClasses");
66         TestCommon.checkDump(output);
67         // Check that the OldProvider is being dumped into the base archive.
68         output.shouldMatch(".cds,class.*klass.*0x.*app.*OldProvider.*unlinked");
69 
70         String baseArchiveName = TestCommon.getCurrentArchiveName();
71 
72         // create a dynamic archive with the custom base archive.
73         // The old interface is in the base archive and will be
74         // accessed using a lambda expression of LambdaContainsOldInfApp.
75         // The lambda proxy class and the app class will be archived in the dynamic archive.
76         dump2(baseArchiveName, topArchiveName,
77               "-Xlog:cds,cds+dynamic,class+load,cds+class=debug",
78               "-cp", appJar,
79               "-XX:-PreloadSharedClasses",
80               appClass)
81             .assertNormalExit(out -> {
82                     out.shouldContain("OldProvider source: shared objects file")
83                        .shouldMatch("Archiving hidden LambdaContainsOldInfApp[$][$]Lambda*");
84                 });
85 
86         // Run with both base and dynamic archives. The OldProvider class
87         // should be loaded from the base archive. The LambdaContainsOldInfApp
88         // and its lambda proxy class should be loaded from the dynamic archive.
89         run2(baseArchiveName, topArchiveName,
90               "-Xlog:cds,cds+dynamic,class+load",
91               "-cp", appJar,
92               appClass)
93             .assertNormalExit(out -> {
94                     out.shouldContain("OldProvider source: shared objects file")
95                        .shouldContain("LambdaContainsOldInfApp source: shared objects file (top)")
96                        .shouldMatch(".class.load. LambdaContainsOldInfApp[$][$]Lambda.*/0x.*source:.*shared.*objects.*file.*(top)");
97                 });
98     }
99 }