1 /*
  2  * Copyright (c) 2024, 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  * @requires vm.cds.write.archived.java.heap
 28  * @library /test/jdk/lib/testlibrary /test/lib
 29  *          /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 30  * @build ArchivedPackages
 31  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar ArchivedPackagesApp pkg3.Package3A pkg3.Package3B
 32  * @run driver ArchivedPackages STATIC
 33  */
 34 
 35 import jdk.test.lib.cds.CDSAppTester;
 36 import jdk.test.lib.helpers.ClassFileInstaller;
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 
 39 import pkg3.Package3A;
 40 import pkg3.Package3B;
 41 
 42 public class ArchivedPackages {
 43     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 44     static final String mainClass = "ArchivedPackagesApp";
 45 
 46     public static void main(String[] args) throws Exception {
 47         Tester t = new Tester();
 48         t.run(args);
 49     }
 50 
 51     static class Tester extends CDSAppTester {
 52         public Tester() {
 53             super(mainClass);
 54         }
 55 
 56         @Override
 57         public String classpath(RunMode runMode) {
 58             return appJar;
 59         }
 60 
 61         @Override
 62         public String[] vmArgs(RunMode runMode) {
 63             return new String[] {
 64                 "-Dcds.debug.archived.packages=true",
 65                 "-XX:+AOTClassLinking",
 66                 "-XX:+ArchivePackages"
 67             };
 68         }
 69 
 70         @Override
 71         public String[] appCommandLine(RunMode runMode) {
 72             return new String[] {
 73                 mainClass,
 74                 runMode.name()
 75             };
 76         }
 77 
 78         @Override
 79         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
 80             if (runMode.isStaticDump()) {
 81                 out.shouldContain("Archiving NamedPackage \"\" for jdk.internal.loader.ClassLoaders$AppClassLoader");
 82                 out.shouldContain("Archiving Package \"pkg3\" for jdk.internal.loader.ClassLoaders$AppClassLoader");
 83                 out.shouldContain("Archiving NamedPackage \"com.sun.tools.javac\" for jdk.internal.loader.ClassLoaders$AppClassLoader");
 84             } else if (runMode.isProductionRun()) {
 85                 out.shouldContain(ArchivedPackagesApp.msg);
 86             }
 87         }
 88     }
 89 }
 90 
 91 class ArchivedPackagesApp {
 92     public static String msg = "Package for archived class equals to that of non-archived class";
 93     public static void main(String args[]) throws Exception {
 94         Class.forName("com.sun.tools.javac.Main");
 95         Package x = Package3A.class.getPackage();
 96         if (args[0].equals("PRODUCTION")) {
 97             Package y = Package3B.class.getPackage();
 98             System.out.println("Package for archived class: " + x);
 99             System.out.println("Package for non-archived class: " + y);
100             if (x == y) {
101                 System.out.println(msg);
102             } else {
103                 throw new RuntimeException("Unexpected for archived package");
104             }
105         }
106     }
107 }