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