--- old/test/testlibrary/com/oracle/java/testlibrary/TestDynamicVMOption.java Wed Apr 30 18:56:38 2014 +++ /dev/null Wed Apr 30 18:57:50 2014 @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2014, 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. - */ -package com.oracle.java.testlibrary; - -/** - * Simple class to check writeability, invalid and valid values for concrete VMOption - */ -public class TestDynamicVMOption { - - private final String name; - private final int value; - - /** - * Constructor - * - * @param name of VM option to test - */ - public TestDynamicVMOption(String name) { - this.name = name; - this.value = DynamicVMOptionChecker.getIntValue(name); - System.out.println(this.name + " = " + this.value); - } - - /** - * Checks that this value can accept valid percentage values and cannot accept invalid percentage values - * - * @throws RuntimeException - */ - public void testPercentageValues() { - checkInvalidValue(Integer.toString(Integer.MIN_VALUE)); - checkInvalidValue(Integer.toString(Integer.MAX_VALUE)); - checkInvalidValue("-10"); - checkInvalidValue("190"); - } - - /** - * Reads VM option from PlatformMXBean and parse it to integer value - * - * @return value - */ - public int getIntValue() { - return DynamicVMOptionChecker.getIntValue(this.name); - } - - /** - * Sets VM option value - * - * @param value to set - */ - public void setIntValue(int value) { - DynamicVMOptionChecker.setIntValue(this.name, value); - } - - /** - * Checks that this VM option is dynamically writable - * - * @throws RuntimeException if option if not writable - * @return true - */ - public boolean checkIsWritable() throws RuntimeException { - return DynamicVMOptionChecker.checkIsWritable(this.name); - } - - /** - * Checks that value for this VM option cannot be set - * - * @param value to check - * @throws RuntimeException on error - when expected exception hasn't been thrown - */ - public void checkInvalidValue(String value) { - DynamicVMOptionChecker.checkInvalidValue(this.name, value); - } - - /** - * Checks that value for this VM option can be set - * - * @param value to check - * @throws RuntimeException on error - when value in VM is not equal to origin - */ - public void checkValidValue(String value) { - DynamicVMOptionChecker.checkValidValue(this.name, value); - } - -} --- /dev/null Wed Apr 30 18:57:51 2014 +++ new/test/testlibrary/com/oracle/java/testlibrary/DynamicVMOption.java Wed Apr 30 18:56:39 2014 @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2014, 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. + */ +package com.oracle.java.testlibrary; + +import static com.oracle.java.testlibrary.Asserts.assertEQ; +import static com.oracle.java.testlibrary.Asserts.assertFalse; +import static com.oracle.java.testlibrary.Asserts.assertTrue; +import com.sun.management.HotSpotDiagnosticMXBean; +import com.sun.management.VMOption; +import java.lang.management.ManagementFactory; +import java.util.Arrays; +import java.util.Collection; + +/** + * A utility class to verify VMOptions which could be altered during execution. + * + * Implementation is based on {@code com.sun.management.VMOption}. + * + * The class provides methods to set and values for given options. + * It also contains a number of assert methods to be invoked from tests to + * very that an option under test correctly sets valid values and rejects + * invalid ones. + * + */ +public class DynamicVMOption { + + /** + * Private factory method to hide implementation details from tests. + * + * Those who need to work with VMOption could create it directly. + * + * @param name the VM option name, like {@code MaxHeapFreeRatio} + * @return an instance of com.sun.management.VMOption + */ + private static VMOption getVMOption(String name) { + return ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class). + getVMOption(name); + } + + /** + * Returns value of the given VM option. + * + * @param name the name of VM option + * @return value as string + */ + public static String getValue(String name) { + return getVMOption(name).getValue(); + } + + /** + * Returns value of the given option as int. + * + * @param name the name of VM option + * @return value parsed as integer + * + */ + public static int getIntValue(String name) { + return Integer.parseInt(getValue(name)); + } + + /** + * Sets VM option value. + * + * @param name the name of VM option + * @param value the value to be set + */ + public static void setValue(String name, String value) { + ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class) + .setVMOption(name, value); + } + + + /** + * Sets VM option value. + * + * @param name the name of VM option + * @param value the value to be set + */ + public static void setIntValue(String name, int value) { + setValue(name, Integer.toString(value)); + } + + /** + * Tests VM Option for being writable. + * + * @param name the name of VM option + */ + public static void assertOptionIsWritable(String name) { + assertTrue(getVMOption(name).isWriteable(), + "Option " + name + " is expected to be writable"); + } + + + /** + * Tests VM Option for being immutable. + * + * @param name of option + */ + public static void assertOptionIsNotWritable(String name) { + assertFalse(getVMOption(name).isWriteable(), + "Option " + name + " is expected to be immutable"); + } + + /** + * Tests VM Option for a valid value. + * The option should be successfully set to a new value. + * + * @param name the VM option name + * @param value the valid value + */ + public static void assertValidValue(String name, String value) { + setValue(name, value); + String newValue = getValue(name); + assertEQ(value, newValue, + "set('" + name + "'," + value + "); get('" + name + "')=" + newValue); + } + + /** + * Tests VM Option for valid values. + * + * @param name the VM option name + * @param values the collection of valid values + */ + public static void assertValidValues(String name, Collection values) { + for (String v: values) { + assertValidValue(name, v); + } + } + + /** + * Tests VM Option for an valid values. + * + * @param name the VM option name + * @param values the collection if invalid values + */ + public static void assertValidValues(String name, String[] values) { + assertValidValues(name, Arrays.asList(values)); + } + + + /** + * Tests VM Option for an invalid value. + * Attempts to set option to invalid value should cause + * throwing IllegalArgumentExecption. + * + * @param name the VM option name + * @param value the invalid value + */ + public static void assertInvalidValue(String name, String value) { + boolean passed = false; + try { + setValue(name, value); + } catch (NullPointerException e) { + if (value == null) { + passed = true; + } + } catch (IllegalArgumentException e) { + passed = true; + } + assertTrue(passed, + "Expected IllegalArgumentException was not thrown, " + name + "= " + value); + + } + + /** + * Tests VM Option for an invalid values. + * + * @param name the VM option name + * @param values the collection if invalid values + */ + public static void assertInvalidValues(String name, Collection values) { + for (String v: values) { + assertInvalidValue(name, v); + } + } + + /** + * Tests VM Option for an invalid values. + * + * @param name the VM option name + * @param values the array if invalid values + */ + public static void assertInvalidValues(String name, String[] values) { + assertInvalidValues(name, Arrays.asList(values)); + } +} --- old/test/testlibrary/com/oracle/java/testlibrary/DynamicVMOptionChecker.java Wed Apr 30 18:56:39 2014 +++ /dev/null Wed Apr 30 18:57:51 2014 @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2014, 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. - */ -package com.oracle.java.testlibrary; - -import com.sun.management.HotSpotDiagnosticMXBean; -import com.sun.management.VMOption; -import java.lang.management.ManagementFactory; - -/** - * Simple class to check writeability, invalid and valid values for VMOption - */ -public class DynamicVMOptionChecker { - - /** - * Reads VM option from PlatformMXBean and parse it to integer value - * - * @param name of option - * @return parsed value - */ - public static int getIntValue(String name) { - - VMOption option = ManagementFactory. - getPlatformMXBean(HotSpotDiagnosticMXBean.class). - getVMOption(name); - - return Integer.parseInt(option.getValue()); - } - - /** - * Sets VM option value - * - * @param name of option - * @param value to set - */ - public static void setIntValue(String name, int value) { - ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class).setVMOption(name, Integer.toString(value)); - } - - /** - * Checks that VM option is dynamically writable - * - * @param name - * @throws RuntimeException if option if not writable - * @return always true - */ - public static boolean checkIsWritable(String name) { - VMOption option = ManagementFactory. - getPlatformMXBean(HotSpotDiagnosticMXBean.class). - getVMOption(name); - - if (!option.isWriteable()) { - throw new RuntimeException(name + " is not writable"); - } - - return true; - } - - /** - * Checks that value cannot be set - * - * @param name of flag - * @param value string representation of value to set - * @throws RuntimeException on error - when expected exception hasn't been thrown - */ - public static void checkInvalidValue(String name, String value) { - // should throw - try { - ManagementFactory. - getPlatformMXBean(HotSpotDiagnosticMXBean.class). - setVMOption(name, value); - - } catch (IllegalArgumentException e) { - return; - } - - throw new RuntimeException("Expected IllegalArgumentException was not thrown, " + name + "= " + value); - } - - /** - * Checks that value can be set - * - * @param name of flag to set - * @param value string representation of value to set - * @throws RuntimeException on error - when value in VM is not equal to origin - */ - public static void checkValidValue(String name, String value) { - ManagementFactory. - getPlatformMXBean(HotSpotDiagnosticMXBean.class). - setVMOption(name, value); - - VMOption option = ManagementFactory. - getPlatformMXBean(HotSpotDiagnosticMXBean.class). - getVMOption(name); - - if (!option.getValue().equals(value)) { - throw new RuntimeException("Actual value of " + name + " \"" + option.getValue() - + "\" not equal origin \"" + value + "\""); - } - } - -}