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