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