1 /*
   2  * Copyright (c) 2014, 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 import com.oracle.java.testlibrary.ExitCode;
  25 import com.oracle.java.testlibrary.Utils;
  26 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  27 
  28 import java.util.Collections;
  29 import java.util.LinkedList;
  30 import java.util.List;
  31 
  32 /**
  33  * @test
  34  * @bug 8031323
  35  * @summary Verify SurvivorAlignmentInBytes option processing.
  36  * @library /testlibrary
  37  * @run main TestSurvivorAlignmentInBytesOption
  38  */
  39 public class TestSurvivorAlignmentInBytesOption {
  40     private static final String[] FILTERED_VM_OPTIONS
  41             = Utils.getFilteredTestJavaOpts(
  42             "UnlockExperimentalVMOptions",
  43             "SurvivorAlignmentInBytes",
  44             "ObjectAlignmentInBytes");
  45 
  46     public static void main(String args[]) throws Throwable {
  47         String optionName = "SurvivorAlignmentInBytes";
  48         String optionIsExperimental
  49                 = CommandLineOptionTest.getExperimentalOptionErrorMessage(
  50                 optionName);
  51         String valueIsTooSmall= ".*SurvivorAlignmentInBytes=.*must be greater"
  52                 + " than ObjectAlignmentInBytes.*";
  53         String mustBePowerOf2 = ".*SurvivorAlignmentInBytes=.*must be "
  54                 + "power of 2.*";
  55 
  56         // Verify that without -XX:+UnlockExperimentalVMOptions usage of
  57         // SurvivorAlignmentInBytes option will cause JVM startup failure
  58         // with the warning message saying that that option is experimental.
  59         CommandLineOptionTest.verifyJVMStartup(
  60                 new String[]{optionIsExperimental}, null, ExitCode.FAIL, false,
  61                 TestSurvivorAlignmentInBytesOption.prepareOptions(
  62                         "-XX:-UnlockExperimentalVMOptions",
  63                         CommandLineOptionTest.prepareNumericFlag(
  64                                 optionName, 64)));
  65 
  66         // Verify that with -XX:+UnlockExperimentalVMOptions passed to JVM
  67         // usage of SurvivorAlignmentInBytes option won't cause JVM startup
  68         // failure.
  69         CommandLineOptionTest.verifyJVMStartup(
  70                 null, new String[]{optionIsExperimental}, ExitCode.OK, false,
  71                 TestSurvivorAlignmentInBytesOption.prepareOptions(
  72                         CommandLineOptionTest.prepareNumericFlag(
  73                                 optionName, 64)));
  74 
  75         // Verify that if specified SurvivorAlignmentInBytes is lower then
  76         // ObjectAlignmentInBytes, then the JVM startup will fail with
  77         // appropriate error message.
  78         CommandLineOptionTest.verifyJVMStartup(
  79                 new String[]{valueIsTooSmall}, null, ExitCode.FAIL, false,
  80                 TestSurvivorAlignmentInBytesOption.prepareOptions(
  81                         CommandLineOptionTest.prepareNumericFlag(
  82                                 optionName, 2)));
  83 
  84         // Verify that if specified SurvivorAlignmentInBytes value is not
  85         // a power of 2 then the JVM startup will fail with appropriate error
  86         // message.
  87         CommandLineOptionTest.verifyJVMStartup(
  88                 new String[]{mustBePowerOf2}, null, ExitCode.FAIL, false,
  89                 TestSurvivorAlignmentInBytesOption.prepareOptions(
  90                         CommandLineOptionTest.prepareNumericFlag(
  91                                 optionName, 127)));
  92 
  93         // Verify that if SurvivorAlignmentInBytes has correct value, then
  94         // the JVM will be started without errors.
  95         CommandLineOptionTest.verifyJVMStartup(
  96                 null, new String[]{".*SurvivorAlignmentInBytes.*"},
  97                 ExitCode.OK, false,
  98                 TestSurvivorAlignmentInBytesOption.prepareOptions(
  99                         CommandLineOptionTest.prepareNumericFlag(
 100                                 optionName, 128)));
 101 
 102         // Verify that we can setup different SurvivorAlignmentInBytes values.
 103         for (int alignment = 32; alignment <= 128; alignment *= 2) {
 104             CommandLineOptionTest.verifyOptionValue(optionName,
 105                     Integer.toString(alignment), false,
 106                     TestSurvivorAlignmentInBytesOption.prepareOptions(
 107                             CommandLineOptionTest.prepareNumericFlag(
 108                                     optionName, alignment)));
 109         }
 110     }
 111 
 112     private static String[] prepareOptions(String... options) {
 113         List<String> finalOptions = new LinkedList<>();
 114         Collections.addAll(finalOptions,
 115                 TestSurvivorAlignmentInBytesOption.FILTERED_VM_OPTIONS);
 116         finalOptions.add(CommandLineOptionTest.UNLOCK_EXPERIMENTAL_VM_OPTIONS);
 117         Collections.addAll(finalOptions, options);
 118         return finalOptions.toArray(new String[finalOptions.size()]);
 119     }
 120 }