hotspot/test/gc/arguments/TestG1PercentageOptions.java
author kbarrett
Thu, 29 Jan 2015 00:08:38 -0500
changeset 28832 4c2704386850
child 29678 dd2f3932c21e
permissions -rw-r--r--
8068942: Improve validation of -XX:G1ConfidencePercent value Summary: Validate during argument processing and simplify usage by assuming validated. Reviewed-by: jmasa, tschatzl

/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
 * @test TestG1PercentageOptions
 * @key gc
 * @bug 8068942
 * @summary Test argument processing of various percentage options
 * @library /testlibrary
 * @run driver TestG1PercentageOptions
 */

import com.oracle.java.testlibrary.*;

public class TestG1PercentageOptions {

    private static final class OptionDescription {
        public final String name;
        public final String[] valid;
        public final String[] invalid;

        OptionDescription(String name_, String[] valid_, String[] invalid_) {
            name = name_;
            valid = valid_;
            invalid = invalid_;
        }
    }

    private static final String[] defaultValid = new String[] {
        "0", "1", "50", "95", "100" };
    private static final String[] defaultInvalid = new String[] {
        "-10", "110", "bad" };

    // All of the G1 product arguments that are percentages.
    private static final OptionDescription[] percentOptions = new OptionDescription[] {
        new OptionDescription("G1ConfidencePercent", defaultValid, defaultInvalid)
        // Other percentage options are not yet validated by argument processing.
    };

    private static void check(String flag, boolean is_valid) throws Exception {
        String[] flags = new String[] { "-XX:+UseG1GC", flag, "-version" };
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
        OutputAnalyzer output = new OutputAnalyzer(pb.start());
        if (is_valid) {
            output.shouldHaveExitValue(0);
        } else {
            output.shouldHaveExitValue(1);
        }
    }

    private static
    void check(String name, String value, boolean is_valid) throws Exception {
        check("-XX:" + name + "=" + value, is_valid);
    }

    public static void main(String args[]) throws Exception {
        for (OptionDescription option : percentOptions) {
            for (String value : option.valid) {
                check(option.name, value, true);
            }
            for (String value : option.invalid) {
                check(option.name, value, false);
            }
            check("-XX:" + option.name, false);
            check("-XX:+" + option.name, false);
            check("-XX:-" + option.name, false);
        }
    }
}