1 /*
   2  * Copyright (c) 2015, 2017, 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.cds.CDSOptions;
  26 import jdk.test.lib.process.OutputAnalyzer;
  27 
  28 // A helper/utility class for testing shared strings
  29 public class SharedStringsUtils {
  30     public static final String TEST_JAR_NAME =      "test";
  31     public static final String TEST_JAR_NAME_FULL = "test.jar";
  32     public static final String WHITEBOX_JAR_NAME =  "whitebox";
  33     private static String vmOptionsPrefix[] = {};
  34 
  35     static void setVMOptionsPrefix(String[] opts) {
  36         vmOptionsPrefix = opts;
  37     }
  38 
  39     public static String getWbParam() {
  40         return "-Xbootclasspath/a:" + TestCommon.getTestJar(WHITEBOX_JAR_NAME + ".jar");
  41     }
  42 
  43     // build the test jar
  44     public static void buildJar(String... classes) throws Exception {
  45         JarBuilder.build(TEST_JAR_NAME, classes);
  46     }
  47 
  48     // build the test jar and a whitebox jar
  49     public static void buildJarAndWhiteBox(String... classes) throws Exception {
  50         JarBuilder.build(true, WHITEBOX_JAR_NAME, "sun/hotspot/WhiteBox");
  51         buildJar(classes);
  52     }
  53 
  54     // execute the "dump" operation, but do not check the output
  55     public static OutputAnalyzer dumpWithoutChecks(String appClasses[],
  56         String sharedDataFile, String... extraOptions) throws Exception {
  57 
  58         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
  59         String[] args =
  60             TestCommon.concat(extraOptions, "-XX:+UseCompressedOops", "-XX:+UseG1GC",
  61             "-XX:SharedArchiveConfigFile=" +
  62             TestCommon.getSourceFile(sharedDataFile));
  63         args = TestCommon.concat(vmOptionsPrefix, args);
  64 
  65         return TestCommon.dump(appJar, appClasses, args);
  66     }
  67 
  68     // execute the dump operation and check the output
  69     public static OutputAnalyzer dump(String appClasses[],
  70         String sharedDataFile, String... extraOptions) throws Exception {
  71         OutputAnalyzer output = dumpWithoutChecks(appClasses, sharedDataFile, extraOptions);
  72         checkDump(output);
  73         return output;
  74     }
  75 
  76     public static OutputAnalyzer dumpWithWhiteBox(String appClasses[],
  77         String sharedDataFile, String... extraOptions) throws Exception {
  78         return dump(appClasses, sharedDataFile,
  79             TestCommon.concat(extraOptions, getWbParam()) );
  80     }
  81 
  82     // execute/run test with shared archive
  83     public static OutputAnalyzer runWithArchiveAuto(String className,
  84         String... extraOptions) throws Exception {
  85 
  86         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
  87         String[] args = TestCommon.concat(extraOptions,
  88             "-cp", appJar, "-XX:+UseCompressedOops", "-XX:+UseG1GC", className);
  89         args = TestCommon.concat(vmOptionsPrefix, args);
  90 
  91         OutputAnalyzer output = TestCommon.execAuto(args);
  92         checkExecAuto(output);
  93         return output;
  94     }
  95 
  96     public static OutputAnalyzer runWithArchive(String className,
  97         String... extraOptions) throws Exception {
  98 
  99         return runWithArchive(new String[0], className, extraOptions);
 100     }
 101 
 102     public static OutputAnalyzer runWithArchive(String[] extraMatches,
 103         String className, String... extraOptions) throws Exception {
 104 
 105         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
 106         String[] args = TestCommon.concat(extraOptions,
 107             "-XX:+UseCompressedOops", "-XX:+UseG1GC", className);
 108         args = TestCommon.concat(vmOptionsPrefix, args);
 109 
 110         OutputAnalyzer output = TestCommon.exec(appJar, args);
 111         checkExec(output, extraMatches);
 112         return output;
 113     }
 114 
 115 
 116     // execute/run test with shared archive and white box
 117     public static OutputAnalyzer runWithArchiveAndWhiteBox(String className,
 118         String... extraOptions) throws Exception {
 119 
 120         return runWithArchive(className,
 121             TestCommon.concat(extraOptions, getWbParam(),
 122             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI") );
 123     }
 124 
 125     public static OutputAnalyzer runWithArchiveAndWhiteBox(String[] extraMatches,
 126         String className, String... extraOptions) throws Exception {
 127 
 128         return runWithArchive(extraMatches, className,
 129             TestCommon.concat(extraOptions, getWbParam(),
 130             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI") );
 131     }
 132 
 133 
 134     public static void checkDump(OutputAnalyzer output) throws Exception {
 135         output.shouldContain("Shared string table stats");
 136         TestCommon.checkDump(output);
 137     }
 138 
 139     public static void checkExec(OutputAnalyzer output) throws Exception {
 140         TestCommon.checkExec(output, new String[0]);
 141     }
 142 
 143     public static void checkExecAuto(OutputAnalyzer output) throws Exception {
 144         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
 145         TestCommon.checkExec(output, opts);
 146     }
 147 
 148     public static void checkExec(OutputAnalyzer output, String[] extraMatches) throws Exception {
 149         TestCommon.checkExec(output, extraMatches);
 150     }
 151 }