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