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