1 /*
   2  * Copyright (c) 2015, 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.cds.CDSOptions;
  26 import jdk.test.lib.process.OutputAnalyzer;
  27 import jdk.test.lib.Asserts;
  28 
  29 // A helper/utility class for testing shared strings
  30 public class SharedStringsUtils {
  31     public static final String TEST_JAR_NAME =      "test";
  32     public static final String TEST_JAR_NAME_FULL = "test.jar";
  33     public static final String WHITEBOX_JAR_NAME =  "whitebox";
  34 
  35     public static interface Test {
  36         public void dotest(String args[]) throws Exception ;
  37     }
  38 
  39     private static String[][] vmOptionCombos = {
  40         {},
  41         {"-XX:+UseStringDeduplication"},
  42         {"-XX:-CompactStrings"}
  43     };
  44 
  45     private static String childVMOptionsPrefix[] = {};
  46 
  47     // SharedStringsUtils.run() is for running the main test body multiple times, each with a different
  48     // set of extra VM options that are passed to the child processes.
  49     //
  50     // See ./ExerciseGC.java for an example.
  51     public static void run(String args[], Test t) throws Exception {
  52         int numSetOfChildVMOptions = vmOptionCombos.length;
  53         for (int i=0; i< numSetOfChildVMOptions; i++) {
  54             run(i, numSetOfChildVMOptions, args, t);
  55         }
  56     }
  57 
  58     public static void run(int i, int numSetOfChildVMOptions, String args[], Test t) throws Exception {
  59         // When you add a new set of options to vmOptionCombos, we make sure all
  60         // callers of this method (i.e., IncompatibleOptions.java) knows about it and will
  61         // add new @test blocks accordingly.
  62         Asserts.assertEQ(numSetOfChildVMOptions, vmOptionCombos.length);
  63         String opts[] = vmOptionCombos[i];
  64 
  65         System.out.print("Running with extra VM option prefix for child processes [" + opts.length + "] =");
  66         for (String o : opts) {
  67             System.out.print(" " + o);
  68         }
  69         System.out.println();
  70         childVMOptionsPrefix = opts;
  71         t.dotest(args);
  72     }
  73 
  74     public static String[] getChildVMOptionsPrefix() {
  75         return childVMOptionsPrefix;
  76     }
  77 
  78     public static String getWbParam() {
  79         return "-Xbootclasspath/a:" + TestCommon.getTestJar(WHITEBOX_JAR_NAME + ".jar");
  80     }
  81 
  82     // build the test jar
  83     public static void buildJar(String... classes) throws Exception {
  84         JarBuilder.build(TEST_JAR_NAME, classes);
  85     }
  86 
  87     // build the test jar and a whitebox jar
  88     public static void buildJarAndWhiteBox(String... classes) throws Exception {
  89         JarBuilder.build(true, WHITEBOX_JAR_NAME, "sun/hotspot/WhiteBox");
  90         buildJar(classes);
  91     }
  92 
  93     // execute the "dump" operation, but do not check the output
  94     public static OutputAnalyzer dumpWithoutChecks(String appClasses[],
  95         String sharedDataFile, String... extraOptions) throws Exception {
  96 
  97         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
  98         String[] args =
  99             TestCommon.concat(extraOptions, "-XX:+UseCompressedOops", "-XX:+UseG1GC",
 100             "-XX:SharedArchiveConfigFile=" +
 101             TestCommon.getSourceFile(sharedDataFile));
 102         args = TestCommon.concat(childVMOptionsPrefix, args);
 103 
 104         return TestCommon.dump(appJar, appClasses, args);
 105     }
 106 
 107     // execute the dump operation and check the output
 108     public static OutputAnalyzer dump(String appClasses[],
 109         String sharedDataFile, String... extraOptions) throws Exception {
 110         OutputAnalyzer output = dumpWithoutChecks(appClasses, sharedDataFile, extraOptions);
 111         checkDump(output);
 112         return output;
 113     }
 114 
 115     public static OutputAnalyzer dumpWithWhiteBox(String appClasses[],
 116         String sharedDataFile, String... extraOptions) throws Exception {
 117         return dump(appClasses, sharedDataFile,
 118             TestCommon.concat(extraOptions, getWbParam()) );
 119     }
 120 
 121     // execute/run test with shared archive
 122     public static OutputAnalyzer runWithArchiveAuto(String className,
 123         String... extraOptions) throws Exception {
 124 
 125         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
 126         String[] args = TestCommon.concat(extraOptions,
 127             "-cp", appJar, "-XX:+UseCompressedOops", "-XX:+UseG1GC", className);
 128         args = TestCommon.concat(childVMOptionsPrefix, args);
 129 
 130         OutputAnalyzer output = TestCommon.execAuto(args);
 131         checkExecAuto(output);
 132         return output;
 133     }
 134 
 135     public static OutputAnalyzer runWithArchive(String className,
 136         String... extraOptions) throws Exception {
 137 
 138         return runWithArchive(new String[0], className, extraOptions);
 139     }
 140 
 141     public static OutputAnalyzer runWithArchive(String[] extraMatches,
 142         String className, String... extraOptions) throws Exception {
 143 
 144         String appJar = TestCommon.getTestJar(TEST_JAR_NAME_FULL);
 145         String[] args = TestCommon.concat(extraOptions,
 146             "-XX:+UseCompressedOops", "-XX:+UseG1GC", className);
 147         args = TestCommon.concat(childVMOptionsPrefix, args);
 148 
 149         OutputAnalyzer output = TestCommon.exec(appJar, args);
 150         checkExec(output, extraMatches);
 151         return output;
 152     }
 153 
 154 
 155     // execute/run test with shared archive and white box
 156     public static OutputAnalyzer runWithArchiveAndWhiteBox(String className,
 157         String... extraOptions) throws Exception {
 158 
 159         return runWithArchive(className,
 160             TestCommon.concat(extraOptions, getWbParam(),
 161             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI") );
 162     }
 163 
 164     public static OutputAnalyzer runWithArchiveAndWhiteBox(String[] extraMatches,
 165         String className, String... extraOptions) throws Exception {
 166 
 167         return runWithArchive(extraMatches, className,
 168             TestCommon.concat(extraOptions, getWbParam(),
 169             "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI") );
 170     }
 171 
 172 
 173     public static void checkDump(OutputAnalyzer output) throws Exception {
 174         output.shouldContain("Shared string table stats");
 175         TestCommon.checkDump(output);
 176     }
 177 
 178     public static void checkExec(OutputAnalyzer output) throws Exception {
 179         TestCommon.checkExec(output, new String[0]);
 180     }
 181 
 182     public static void checkExecAuto(OutputAnalyzer output) throws Exception {
 183         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
 184         TestCommon.checkExec(output, opts);
 185     }
 186 
 187     public static void checkExec(OutputAnalyzer output, String[] extraMatches) throws Exception {
 188         TestCommon.checkExec(output, extraMatches);
 189     }
 190 }