1 /*
   2  * Copyright (c) 2014, 2016, 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 package compiler.rtm.cli;
  25 
  26 import jdk.test.lib.process.ExitCode;
  27 import jdk.test.lib.cli.CommandLineOptionTest;
  28 
  29 import java.util.LinkedList;
  30 import java.util.List;
  31 
  32 /**
  33  * Base for all RTM-related CLI tests on options whose processing depends
  34  * on UseRTMLocking value.
  35  *
  36  * Since UseRTMLocking option could be used when both CPU and VM supports RTM
  37  * locking, this test will be skipped on all unsupported configurations.
  38  */
  39 public abstract class RTMLockingAwareTest
  40         extends RTMGenericCommandLineOptionTest {
  41     protected final String warningMessage;
  42     protected final String[] correctValues;
  43     protected final String[] incorrectValues;
  44     /**
  45      * Constructs new test for option {@code optionName} that should be executed
  46      * only on CPU with RTM support.
  47      * Test will be executed using set of correct values from
  48      * {@code correctValues} and set of incorrect values from
  49      * {@code incorrectValues}.
  50      *
  51      * @param optionName name of option to be tested
  52      * @param isBoolean {@code true} if tested option is binary
  53      * @param isExperimental {@code true} if tested option is experimental
  54      * @param defaultValue default value of tested option
  55      * @param correctValues array with correct values, that should not emit
  56      *                      {@code warningMessage} to VM output
  57      * @param incorrectValues array with incorrect values, that should emit
  58      *                        {@code waningMessage} to VM output
  59      * @param warningMessage warning message associated with tested option
  60      */
  61     protected RTMLockingAwareTest(String optionName, boolean isBoolean,
  62             boolean isExperimental, String defaultValue,
  63             String[] correctValues, String[] incorrectValues,
  64             String warningMessage) {
  65         super(optionName, isBoolean, isExperimental, defaultValue);
  66         this.correctValues = correctValues;
  67         this.incorrectValues = incorrectValues;
  68         this.warningMessage = warningMessage;
  69     }
  70 
  71     @Override
  72     protected void verifyJVMStartup() throws Throwable {
  73         // Run generic sanity checks
  74         super.verifyJVMStartup();
  75         // Verify how option values will be processed depending on
  76         // UseRTMLocking value.
  77         if (correctValues != null) {
  78             for (String correctValue : correctValues) {
  79                 // For correct values it is expected to see no warnings
  80                 // regardless to UseRTMLocking
  81                 verifyStartupWarning(correctValue, true, false);
  82                 verifyStartupWarning(correctValue, false, false);
  83             }
  84         }
  85 
  86         if (incorrectValues != null) {
  87             for (String incorrectValue : incorrectValues) {
  88                 // For incorrect values it is expected to see warning
  89                 // only with -XX:+UseRTMLocking
  90                 verifyStartupWarning(incorrectValue, true, true);
  91                 verifyStartupWarning(incorrectValue, false, false);
  92             }
  93         }
  94     }
  95 
  96     @Override
  97     protected void verifyOptionValues() throws Throwable {
  98         super.verifyOptionValues();
  99         // Verify how option values will be setup after processing
 100         // depending on UseRTMLocking value
 101         if (correctValues != null) {
 102             for (String correctValue : correctValues) {
 103                 // Correct value could be set up regardless to UseRTMLocking
 104                 verifyOptionValues(correctValue, false, correctValue);
 105                 verifyOptionValues(correctValue, true, correctValue);
 106             }
 107         }
 108 
 109         if (incorrectValues != null) {
 110             for (String incorrectValue : incorrectValues) {
 111                 // With -XX:+UseRTMLocking, incorrect value will be changed to
 112                 // default value.
 113                 verifyOptionValues(incorrectValue, false, incorrectValue);
 114                 verifyOptionValues(incorrectValue, true, defaultValue);
 115             }
 116         }
 117     }
 118 
 119     private void verifyStartupWarning(String value, boolean useRTMLocking,
 120             boolean isWarningExpected) throws Throwable {
 121         String warnings[] = new String[] { warningMessage };
 122         List<String> options = new LinkedList<>();
 123         options.add(CommandLineOptionTest.prepareBooleanFlag("UseRTMLocking",
 124                 useRTMLocking));
 125 
 126         if (isExperimental) {
 127             options.add(CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 128         }
 129         options.add(prepareOptionValue(value));
 130 
 131         String errorString =  String.format("JVM should start with option '%s'"
 132                 + "'%nWarnings should be shown: %s", optionName,
 133                 isWarningExpected);
 134         CommandLineOptionTest.verifySameJVMStartup(
 135                 (isWarningExpected ? warnings : null),
 136                 (isWarningExpected ? null : warnings),
 137                 errorString, errorString, ExitCode.OK,
 138                 options.toArray(new String[options.size()]));
 139     }
 140 
 141     private void verifyOptionValues(String value, boolean useRTMLocking,
 142             String expectedValue) throws Throwable {
 143         List<String> options = new LinkedList<>();
 144         options.add(CommandLineOptionTest.prepareBooleanFlag("UseRTMLocking",
 145                 useRTMLocking));
 146 
 147         if (isExperimental) {
 148             options.add(CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 149         }
 150         options.add(prepareOptionValue(value));
 151 
 152         CommandLineOptionTest.verifyOptionValueForSameVM(optionName,
 153                 expectedValue, String.format("Option '%s' should have '%s' "
 154                         + "value if '%s' flag set",
 155                         optionName, expectedValue, prepareOptionValue(value)),
 156                 options.toArray(new String[options.size()]));
 157     }
 158 }