1 /*
  2  * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2024, Red Hat, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /**
 26  * @test id=nocoops_nocoh
 27  * @summary Test Loading of default archives in all configurations
 28  * @requires vm.cds
 29  * @requires vm.cds.default.archive.available
 30  * @requires vm.cds.write.archived.java.heap
 31  * @requires vm.bits == 64
 32  * @library /test/lib
 33  * @modules java.base/jdk.internal.misc
 34  *          java.management
 35  * @run driver TestDefaultArchiveLoading nocoops_nocoh
 36  */
 37 
 38 /**
 39  * @test id=nocoops_coh
 40  * @summary Test Loading of default archives in all configurations (requires --enable-cds-archive-coh)
 41  * @requires vm.cds
 42  * @requires vm.cds.default.archive.available
 43  * @requires vm.cds.write.archived.java.heap
 44  * @requires vm.bits == 64
 45  * @library /test/lib
 46  * @modules java.base/jdk.internal.misc
 47  *          java.management
 48  * @run driver TestDefaultArchiveLoading nocoops_coh
 49  */
 50 
 51 /**
 52  * @test id=coops_nocoh
 53  * @summary Test Loading of default archives in all configurations
 54  * @requires vm.cds
 55  * @requires vm.cds.default.archive.available
 56  * @requires vm.cds.write.archived.java.heap
 57  * @requires vm.bits == 64
 58  * @requires vm.gc != "Z"
 59  * @library /test/lib
 60  * @modules java.base/jdk.internal.misc
 61  *          java.management
 62  * @run driver TestDefaultArchiveLoading coops_nocoh
 63  */
 64 
 65 /**
 66  * @test id=coops_coh
 67  * @summary Test Loading of default archives in all configurations (requires --enable-cds-archive-coh)
 68  * @requires vm.cds
 69  * @requires vm.cds.default.archive.available
 70  * @requires vm.cds.write.archived.java.heap
 71  * @requires vm.bits == 64
 72  * @requires vm.gc != "Z"
 73  * @library /test/lib
 74  * @modules java.base/jdk.internal.misc
 75  *          java.management
 76  * @run driver TestDefaultArchiveLoading coops_coh
 77  */
 78 
 79 import java.nio.file.Files;
 80 import java.nio.file.Path;
 81 import java.nio.file.Paths;
 82 import jdk.test.lib.Platform;
 83 import jdk.test.lib.process.OutputAnalyzer;
 84 import jdk.test.lib.process.ProcessTools;
 85 
 86 import jtreg.SkippedException;
 87 
 88 public class TestDefaultArchiveLoading {
 89 
 90     static String archivePreviewSuffix = "";
 91 
 92     private static String archiveName(String archiveSuffix) {
 93         return "classes" + archiveSuffix + archivePreviewSuffix + ".jsa";
 94     }
 95 
 96     private static Path archivePath(String archiveSuffix) {
 97         return Paths.get(System.getProperty("java.home"), "lib",
 98                          "server", archiveName(archiveSuffix));
 99     }
100 
101     private static boolean isArchiveAvailable(char coops, char coh,
102                                               String archiveSuffix) throws Exception {
103         Path archive= archivePath(archiveSuffix);
104         return Files.exists(archive);
105     }
106 
107     public static void main(String[] args) throws Exception {
108 
109         if (args.length != 1) {
110             throw new RuntimeException("Expected argument");
111         }
112 
113         String archiveSuffix;
114         char coh, coops;
115         String preview = System.getProperty("test.java.opts", "");
116         if (preview.contains("--enable-preview")) {
117             archivePreviewSuffix = "_preview";
118         }
119 
120         switch (args[0]) {
121             case "nocoops_nocoh":
122                 coh = coops = '-';
123                 archiveSuffix = "_nocoops";
124                 if (!isArchiveAvailable(coops, coh, archiveSuffix)) {
125                     throw new SkippedException("Skipping test due to " +
126                                                archivePath(archiveSuffix).toString() + " not available");
127                 }
128                 break;
129             case "nocoops_coh":
130                 coops = '-';
131                 coh = '+';
132                 archiveSuffix = "_nocoops_coh";
133                 if (!isArchiveAvailable(coops, coh, archiveSuffix)) {
134                     throw new SkippedException("Skipping test due to " +
135                                                archivePath(archiveSuffix).toString() + " not available");
136                 }
137                 break;
138             case "coops_nocoh":
139                 coops = '+';
140                 coh = '-';
141                 archiveSuffix = "";
142                 if (!isArchiveAvailable(coops, coh, archiveSuffix)) {
143                     throw new SkippedException("Skipping test due to " +
144                                                archivePath(archiveSuffix).toString() + " not available");
145                 }
146                 break;
147             case "coops_coh":
148                 coh = coops = '+';
149                 archiveSuffix = "_coh";
150                 if (!isArchiveAvailable(coops, coh, archiveSuffix)) {
151                     throw new SkippedException("Skipping test due to " +
152                                                archivePath(archiveSuffix).toString() + " not available");
153                 }
154                 break;
155             default: throw new RuntimeException("Invalid argument " + args[0]);
156         }
157 
158         ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
159                 "-XX:" + coh + "UseCompactObjectHeaders",
160                 "-XX:" + coops + "UseCompressedOops",
161                 "-Xlog:cds",
162                 "-Xshare:on", // fail if we cannot load archive
163                 "-version");
164 
165         OutputAnalyzer output = new OutputAnalyzer(pb.start());
166         output.shouldHaveExitValue(0);
167 
168         output.shouldContain(archiveName(archiveSuffix));
169     }
170 }