1 /*
  2  * Copyright (c) 2023, 2025, 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 /*
 27  * @test id=static
 28  * @key external-dep
 29  * @requires vm.cds
 30  * @summary run Spring Pet Clinic demo with the classic static archive workflow
 31  * @library /test/lib
 32  * @run driver/timeout=120 SpringPetClinic STATIC
 33  */
 34 
 35 /*
 36  * @test id=dynamic
 37  * @key external-dep
 38  * @requires vm.cds
 39  * @summary run Spring Pet Clinic demo with the classic dynamic archive workflow
 40  * @library /test/lib
 41  * @build jdk.test.whitebox.WhiteBox
 42  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 43  * @run main/othervm/timeout=120 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. SpringPetClinic DYNAMIC
 44  */
 45 
 46 /*
 47  * @test id=aot
 48  * @key external-dep
 49  * @requires vm.cds
 50  * @requires vm.cds.write.archived.java.heap
 51  * @summary run Spring Pet Clinic demo with JEP 483 workflow
 52  * @library /test/lib
 53  * @run driver/timeout=120 SpringPetClinic AOT
 54  */
 55 
 56 /*
 57  * @test id=leyden
 58  * @key external-dep
 59  * @requires vm.cds
 60  * @requires vm.cds.write.archived.java.heap
 61  * @summary run Spring Pet Clinic demo with leyden-premain "new workflow"
 62  * @library /test/lib
 63  * @run driver/timeout=120 SpringPetClinic LEYDEN
 64  */
 65 
 66 import java.io.BufferedInputStream;
 67 import java.io.BufferedOutputStream;
 68 import java.io.BufferedReader;
 69 import java.io.File;
 70 import java.io.FileOutputStream;
 71 import java.nio.file.Files;
 72 import java.nio.file.Path;
 73 import java.nio.file.Paths;
 74 import java.util.Enumeration;
 75 import java.util.Map;
 76 import java.util.zip.ZipEntry;
 77 import java.util.zip.ZipFile;
 78 import jdk.test.lib.StringArrayUtils;
 79 import jdk.test.lib.artifacts.Artifact;
 80 import jdk.test.lib.artifacts.ArtifactResolver;
 81 import jdk.test.lib.cds.CDSAppTester;
 82 import jdk.test.lib.process.OutputAnalyzer;
 83 
 84 // NOTE: if you have not set up an artifactory, you can create spring-petclinic-3.2.0.zip by:
 85 //
 86 // - Make a clone of https://github.com/openjdk/leyden/tree/premain
 87 // - Change to the directory test/hotspot/jtreg/premain/spring-petclinic
 88 // - Edit the Makefile
 89 // - Run the command "make unpack"
 90 //
 91 // Then, you can add the following to your jtreg command-line to run the test cases in this directory:
 92 // -vmoption:-Djdk.test.lib.artifacts.spring-petclinic=/repo/test/hotspot/jtreg/premain/spring-petclinic/petclinic-snapshot/target/spring-petclinic-3.2.0.zip
 93 
 94 @Artifact(organization = "org.springframework.samples", name = "spring-petclinic", revision = "3.2.0", extension = "zip", unpack = false)
 95 public class SpringPetClinic {
 96     public static void main(String args[]) throws Exception {
 97         String cp = getArtifact();
 98         SpringPetClinicTester tester = new SpringPetClinicTester(cp);
 99         tester.run(args);
100     }
101 
102     private static String getArtifact() throws Exception {
103         File cpFile = new File("petclinic-snapshot/target/unpacked/classpath");
104         if (!cpFile.exists()) {
105             long started = System.currentTimeMillis();
106             System.out.println("Download and extract spring-petclinic");
107 
108             Map<String, Path> artifacts = ArtifactResolver.resolve(SpringPetClinic.class);
109             System.out.println(artifacts);
110             Path zip = artifacts.get("org.springframework.samples.spring-petclinic-3.2.0");
111 
112             long elapsed = System.currentTimeMillis() - started;
113             System.out.println("Resolved artifacts in " + elapsed + " ms");
114 
115             extractZip(zip.toString(), new File("."));
116         }
117 
118         // Copy the classpath file and edit its path separator if necessary.
119         String cpFileCopy = "petclinic-classpath.txt";
120         String cp = Files.readString(cpFile.toPath());
121         if (File.pathSeparatorChar == ';') {
122             // This file was generated with ":" as path separator. Change it
123             // to ';' for Windows.
124             cp = cp.replace(':', ';');
125         }
126         System.out.println("\nClasspath = \"" + cp + "\"\n");
127         Files.writeString(Paths.get(cpFileCopy), cp);
128         return "@" + cpFileCopy;
129     }
130 
131     static void extractZip(String srcFile, File outDir) throws Exception {
132         long zipSize = (new File(srcFile)).length();
133         System.out.println("Extracting from " + srcFile + " (" + zipSize + " bytes) to " + outDir);
134 
135         long started = System.currentTimeMillis();
136         try (ZipFile zipFile = new ZipFile(srcFile)) {
137             int numFiles = 0;
138             long numBytes = 0;
139             Enumeration<? extends ZipEntry> e = zipFile.entries();
140 
141             byte buffer[] = new byte[1024];
142             while (e.hasMoreElements()) {
143                 ZipEntry entry = e.nextElement();
144                 if (!entry.isDirectory()) {
145                     File destinationPath = new File(outDir, entry.getName());
146                     destinationPath.getParentFile().mkdirs();
147 
148                     numFiles ++;
149                     try (
150                          BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
151                          FileOutputStream fos = new FileOutputStream(destinationPath);
152                          BufferedOutputStream bos = new BufferedOutputStream(fos, 1024)) {
153                         int n;
154                         while ((n = bis.read(buffer, 0, 1024)) != -1) {
155                             bos.write(buffer, 0, n);
156                             numBytes += n;
157                         }
158                     }
159                 }
160             }
161             long elapsed = System.currentTimeMillis() - started;
162             System.out.println("Extracted " + numFiles + " file(s) = " + numBytes + " bytes in " + elapsed + " ms");
163         }
164     }
165 
166 
167     static class SpringPetClinicTester extends CDSAppTester {
168         String cp;
169 
170         SpringPetClinicTester(String cp) {
171             super("SpringPetClinic");
172             this.cp = cp;
173         }
174 
175         @Override
176         public String[] vmArgs(RunMode runMode) {
177             return new String[] {
178                 "-Xlog:init,cds",
179                 "-DautoQuit=true",
180                 "-Dspring.output.ansi.enabled=NEVER",
181                 "-Dspring.aot.enabled=true",
182                 "-Dserver.port=0", // use system-assigned port
183                 "--enable-native-access=ALL-UNNAMED",
184 
185                 // PetClinic runs very slowly in debug builds if VerifyDependencies is enabled.
186                 "-XX:+IgnoreUnrecognizedVMOptions", "-XX:-VerifyDependencies",
187               //These don't seem necessary when pet-clinic is run in "Spring AOT" mode
188               //"--add-opens", "java.base/java.io=ALL-UNNAMED",
189               //"--add-opens", "java.base/java.lang=ALL-UNNAMED",
190               //"--add-opens", "java.rmi/sun.rmi.transport=ALL-UNNAMED"
191             };
192         }
193 
194         @Override
195         public String classpath(RunMode runMode) {
196             return cp;
197         }
198 
199         @Override
200         public String[] appCommandLine(RunMode runMode) {
201             String cmdLine[] = new String[] {
202                 "org.springframework.samples.petclinic.PetClinicApplication"
203             };
204 /*
205             if (runMode == RunMode.PRODUCTION) {
206                 // FIXME: bug JDK-8318393
207                 cmdLine = StringArrayUtils.concat("-XX:-LoadCachedCode", cmdLine);
208             }
209 
210             if (runMode.isProductionRun()) {
211                 cmdLine = StringArrayUtils.concat("-Xlog:scc=error", cmdLine);
212             }
213  */
214             return cmdLine;
215         }
216 
217         @Override
218         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
219             if (runMode.isApplicationExecuted()) {
220                 out.shouldContain("Booted and returned in ");
221             }
222         }
223     }
224 }