1 /* 2 * Copyright (c) 2014, 2018, 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 jdk.test.lib.Utils; 26 import jdk.test.lib.BuildHelper; 27 import jdk.test.lib.JDKToolFinder; 28 import jdk.test.lib.Platform; 29 import jdk.test.lib.cds.CDSOptions; 30 import jdk.test.lib.cds.CDSTestUtils; 31 import jdk.test.lib.cds.CDSTestUtils.Result; 32 import jdk.test.lib.process.ProcessTools; 33 import jdk.test.lib.process.OutputAnalyzer; 34 import java.io.File; 35 import java.text.SimpleDateFormat; 36 import java.util.Arrays; 37 import java.util.ArrayList; 38 import java.util.Date; 39 40 /** 41 * This is a test utility class for common AppCDS test functionality. 42 * 43 * Various methods use (String ...) for passing VM options. Note that the order 44 * of the VM options are important in certain cases. Many methods take arguments like 45 * 46 * (String prefix[], String suffix[], String... opts) 47 * 48 * Note that the order of the VM options is: 49 * 50 * prefix + opts + suffix 51 */ 52 public class TestCommon extends CDSTestUtils { 53 private static final String JSA_FILE_PREFIX = System.getProperty("user.dir") + 54 File.separator + "appcds-"; 55 56 private static final SimpleDateFormat timeStampFormat = 57 new SimpleDateFormat("HH'h'mm'm'ss's'SSS"); 58 59 private static final String timeoutFactor = 60 System.getProperty("test.timeout.factor", "1.0"); 61 62 private static String currentArchiveName; 63 64 // Call this method to start new archive with new unique name 65 public static void startNewArchiveName() { 66 deletePriorArchives(); 67 currentArchiveName = JSA_FILE_PREFIX + 68 timeStampFormat.format(new Date()) + ".jsa"; 69 } 70 71 // Call this method to get current archive name 72 public static String getCurrentArchiveName() { 73 return currentArchiveName; 74 } 75 76 // Attempt to clean old archives to preserve space 77 // Archives are large artifacts (20Mb or more), and much larger than 78 // most other artifacts created in jtreg testing. 79 // Therefore it is a good idea to clean the old archives when they are not needed. 80 // In most cases the deletion attempt will succeed; on rare occasion the 81 // delete operation will fail since the system or VM process still holds a handle 82 // to the file; in such cases the File.delete() operation will silently fail, w/o 83 // throwing an exception, thus allowing testing to continue. 84 public static void deletePriorArchives() { 85 File dir = new File(System.getProperty("user.dir")); 86 String files[] = dir.list(); 87 for (String name : files) { 88 if (name.startsWith("appcds-") && name.endsWith(".jsa")) { 89 if (!(new File(dir, name)).delete()) 90 System.out.println("deletePriorArchives(): delete failed for file " + name); 91 } 92 } 93 } 94 95 96 // Create AppCDS archive using most common args - convenience method 97 // Legacy name preserved for compatibility 98 public static OutputAnalyzer dump(String appJar, String appClasses[], 99 String... suffix) throws Exception { 100 return createArchive(appJar, appClasses, suffix); 101 } 102 103 104 // Create AppCDS archive using most common args - convenience method 105 public static OutputAnalyzer createArchive(String appJar, String appClasses[], 106 String... suffix) throws Exception { 107 AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar) 108 .setAppClasses(appClasses); 109 opts.addSuffix(suffix); 110 return createArchive(opts); 111 } 112 113 public static String[] makeCommandLineForAppCDS(String... args) throws Exception { 114 return args; 115 } 116 117 // Create AppCDS archive using appcds options 118 public static OutputAnalyzer createArchive(AppCDSOptions opts) 119 throws Exception { 120 121 ArrayList<String> cmd = new ArrayList<String>(); 122 File classList = makeClassList(opts.appClasses); 123 startNewArchiveName(); 124 125 for (String p : opts.prefix) cmd.add(p); 126 127 if (opts.appJar != null) { 128 cmd.add("-cp"); 129 cmd.add(opts.appJar); 130 } else { 131 cmd.add("-cp"); 132 cmd.add("\"\""); 133 } 134 135 cmd.add("-Xshare:dump"); 136 cmd.add("-XX:ExtraSharedClassListFile=" + classList.getPath()); 137 138 if (opts.archiveName == null) 139 opts.archiveName = getCurrentArchiveName(); 140 141 cmd.add("-XX:SharedArchiveFile=" + opts.archiveName); 142 143 for (String s : opts.suffix) cmd.add(s); 144 145 String[] cmdLine = cmd.toArray(new String[cmd.size()]); 146 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, makeCommandLineForAppCDS(cmdLine)); 147 return executeAndLog(pb, "dump"); 148 } 149 150 151 // Execute JVM using AppCDS archive with specified AppCDSOptions 152 public static OutputAnalyzer runWithArchive(AppCDSOptions opts) 153 throws Exception { 154 155 ArrayList<String> cmd = new ArrayList<String>(); 156 157 for (String p : opts.prefix) cmd.add(p); 158 159 cmd.add("-Xshare:" + opts.xShareMode); 160 cmd.add("-showversion"); 161 cmd.add("-XX:SharedArchiveFile=" + getCurrentArchiveName()); 162 cmd.add("-Dtest.timeout.factor=" + timeoutFactor); 163 164 if (opts.appJar != null) { 165 cmd.add("-cp"); 166 cmd.add(opts.appJar); 167 } 168 169 for (String s : opts.suffix) cmd.add(s); 170 171 String[] cmdLine = cmd.toArray(new String[cmd.size()]); 172 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, makeCommandLineForAppCDS(cmdLine)); 173 return executeAndLog(pb, "exec"); 174 } 175 176 177 public static OutputAnalyzer execCommon(String... suffix) throws Exception { 178 AppCDSOptions opts = (new AppCDSOptions()); 179 opts.addSuffix(suffix); 180 return runWithArchive(opts); 181 } 182 183 // This is the new API for running a Java process with CDS enabled. 184 // See comments in the CDSTestUtils.Result class for how to use this method. 185 public static Result run(String... suffix) throws Exception { 186 AppCDSOptions opts = (new AppCDSOptions()); 187 opts.addSuffix(suffix); 188 return new Result(opts, runWithArchive(opts)); 189 } 190 191 public static OutputAnalyzer exec(String appJar, String... suffix) throws Exception { 192 AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar); 193 opts.addSuffix(suffix); 194 return runWithArchive(opts); 195 } 196 197 public static Result runWithModules(String prefix[], String upgrademodulepath, String modulepath, 198 String mid, String... testClassArgs) throws Exception { 199 AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath, 200 mid, testClassArgs); 201 return new Result(opts, runWithArchive(opts)); 202 } 203 204 public static OutputAnalyzer execAuto(String... suffix) throws Exception { 205 AppCDSOptions opts = (new AppCDSOptions()); 206 opts.addSuffix(suffix).setXShareMode("auto"); 207 return runWithArchive(opts); 208 } 209 210 public static OutputAnalyzer execOff(String... suffix) throws Exception { 211 AppCDSOptions opts = (new AppCDSOptions()); 212 opts.addSuffix(suffix).setXShareMode("off"); 213 return runWithArchive(opts); 214 } 215 216 217 private static AppCDSOptions makeModuleOptions(String prefix[], String upgrademodulepath, String modulepath, 218 String mid, String testClassArgs[]) { 219 AppCDSOptions opts = (new AppCDSOptions()); 220 221 opts.addPrefix(prefix); 222 if (upgrademodulepath == null) { 223 opts.addSuffix("-p", modulepath, "-m", mid); 224 } else { 225 opts.addSuffix("--upgrade-module-path", upgrademodulepath, 226 "-p", modulepath, "-m", mid); 227 } 228 opts.addSuffix(testClassArgs); 229 return opts; 230 } 231 232 public static OutputAnalyzer execModule(String prefix[], String upgrademodulepath, String modulepath, 233 String mid, String... testClassArgs) 234 throws Exception { 235 AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath, 236 mid, testClassArgs); 237 return runWithArchive(opts); 238 } 239 240 241 // A common operation: dump, then check results 242 public static OutputAnalyzer testDump(String appJar, String appClasses[], 243 String... suffix) throws Exception { 244 OutputAnalyzer output = dump(appJar, appClasses, suffix); 245 output.shouldContain("Loading classes to share"); 246 output.shouldHaveExitValue(0); 247 return output; 248 } 249 250 251 /** 252 * Simple test -- dump and execute appJar with the given appClasses in classlist. 253 */ 254 public static OutputAnalyzer test(String appJar, String appClasses[], String... args) 255 throws Exception { 256 testDump(appJar, appClasses); 257 258 OutputAnalyzer output = exec(appJar, args); 259 return checkExec(output); 260 } 261 262 263 public static OutputAnalyzer checkExecReturn(OutputAnalyzer output, int ret, 264 boolean checkContain, String... matches) throws Exception { 265 try { 266 for (String s : matches) { 267 if (checkContain) { 268 output.shouldContain(s); 269 } else { 270 output.shouldNotContain(s); 271 } 272 } 273 output.shouldHaveExitValue(ret); 274 } catch (Exception e) { 275 checkCommonExecExceptions(output, e); 276 } 277 278 return output; 279 } 280 281 282 // Convenience concatenation utils 283 public static String[] list(String ...args) { 284 return args; 285 } 286 287 288 public static String[] list(String arg, int count) { 289 ArrayList<String> stringList = new ArrayList<String>(); 290 for (int i = 0; i < count; i++) { 291 stringList.add(arg); 292 } 293 294 String outputArray[] = stringList.toArray(new String[stringList.size()]); 295 return outputArray; 296 } 297 298 299 public static String[] concat(String... args) { 300 return list(args); 301 } 302 303 304 public static String[] concat(String prefix[], String... extra) { 305 ArrayList<String> list = new ArrayList<String>(); 306 for (String s : prefix) { 307 list.add(s); 308 } 309 for (String s : extra) { 310 list.add(s); 311 } 312 313 return list.toArray(new String[list.size()]); 314 } 315 316 317 // ===================== Concatenate paths 318 public static String concatPaths(String... paths) { 319 String prefix = ""; 320 String s = ""; 321 for (String p : paths) { 322 s += prefix; 323 s += p; 324 prefix = File.pathSeparator; 325 } 326 return s; 327 } 328 329 330 public static String getTestJar(String jar) { 331 File jarFile = CDSTestUtils.getTestArtifact(jar, true); 332 if (!jarFile.isFile()) { 333 throw new RuntimeException("Not a regular file: " + jarFile.getPath()); 334 } 335 return jarFile.getPath(); 336 } 337 338 339 public static String getTestDir(String d) { 340 File dirFile = CDSTestUtils.getTestArtifact(d, true); 341 if (!dirFile.isDirectory()) { 342 throw new RuntimeException("Not a directory: " + dirFile.getPath()); 343 } 344 return dirFile.getPath(); 345 } 346 347 public static boolean checkOutputStrings(String outputString1, 348 String outputString2, 349 String split_regex) { 350 String[] sa1 = outputString1.split(split_regex); 351 String[] sa2 = outputString2.split(split_regex); 352 Arrays.sort(sa1); 353 Arrays.sort(sa2); 354 355 int i = 0; 356 for (String s : sa1) { 357 if (!s.equals(sa2[i])) { 358 throw new RuntimeException(s + " is different from " + sa2[i]); 359 } 360 i ++; 361 } 362 return true; 363 } 364 }