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