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