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