提交 1cd5cc39 编写于 作者: D dfazunen

8039489: Refactor test framework for dynamic VM options

Reviewed-by: jmasa, ehelin, jwilhelm
上级 b7e19473
......@@ -21,6 +21,11 @@
* questions.
*/
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.oracle.java.testlibrary.DynamicVMOption;
/**
* @test TestDynMaxHeapFreeRatio
* @bug 8028391
......@@ -33,32 +38,45 @@
* @run main/othervm -XX:MinHeapFreeRatio=51 -XX:MaxHeapFreeRatio=52 TestDynMaxHeapFreeRatio
* @run main/othervm -XX:MinHeapFreeRatio=75 -XX:MaxHeapFreeRatio=100 TestDynMaxHeapFreeRatio
*/
import com.oracle.java.testlibrary.TestDynamicVMOption;
import com.oracle.java.testlibrary.DynamicVMOptionChecker;
public class TestDynMaxHeapFreeRatio {
public class TestDynMaxHeapFreeRatio extends TestDynamicVMOption {
public static void main(String args[]) throws Exception {
public static final String MinFreeRatioFlagName = "MinHeapFreeRatio";
public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio";
// low boundary value
int minValue = DynamicVMOption.getInt("MinHeapFreeRatio");
System.out.println("MinHeapFreeRatio= " + minValue);
public TestDynMaxHeapFreeRatio() {
super(MaxFreeRatioFlagName);
}
String badValues[] = {
null,
"",
"not a number",
"8.5", "-0.01",
Integer.toString(Integer.MIN_VALUE),
Integer.toString(Integer.MAX_VALUE),
Integer.toString(minValue - 1),
"-1024", "-1", "101", "1997"
};
public void test() {
String goodValues[] = {
Integer.toString(minValue),
Integer.toString(minValue + 1),
Integer.toString((minValue + 100) / 2),
"99", "100"
};
int minHeapFreeValue = DynamicVMOptionChecker.getIntValue(MinFreeRatioFlagName);
System.out.println(MinFreeRatioFlagName + " = " + minHeapFreeValue);
DynamicVMOption option = new DynamicVMOption("MaxHeapFreeRatio");
testPercentageValues();
assertTrue(option.isWriteable(), "Option " + option.name
+ " is expected to be writable");
checkInvalidValue(Integer.toString(minHeapFreeValue - 1));
checkValidValue(Integer.toString(minHeapFreeValue));
checkValidValue("100");
for (String v : badValues) {
assertFalse(option.isValidValue(v),
"'" + v + "' is expected to be illegal for flag " + option.name);
}
for (String v : goodValues) {
option.setValue(v);
String newValue = option.getValue();
assertEQ(v, newValue);
}
public static void main(String args[]) throws Exception {
new TestDynMaxHeapFreeRatio().test();
}
}
......@@ -33,30 +33,52 @@
* @run main/othervm -XX:MinHeapFreeRatio=51 -XX:MaxHeapFreeRatio=52 TestDynMinHeapFreeRatio
* @run main/othervm -XX:MinHeapFreeRatio=75 -XX:MaxHeapFreeRatio=100 TestDynMinHeapFreeRatio
*/
import com.oracle.java.testlibrary.TestDynamicVMOption;
import com.oracle.java.testlibrary.DynamicVMOptionChecker;
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.oracle.java.testlibrary.DynamicVMOption;
public class TestDynMinHeapFreeRatio extends TestDynamicVMOption {
public class TestDynMinHeapFreeRatio {
public static final String MinFreeRatioFlagName = "MinHeapFreeRatio";
public static final String MaxFreeRatioFlagName = "MaxHeapFreeRatio";
public static void main(String args[]) throws Exception {
public TestDynMinHeapFreeRatio() {
super(MinFreeRatioFlagName);
}
// high boundary value
int maxValue = DynamicVMOption.getInt("MaxHeapFreeRatio");
System.out.println("MaxHeapFreeRatio= " + maxValue);
String badValues[] = {
null,
"",
"not a number",
"8.5", "-0.01",
Integer.toString(Integer.MIN_VALUE),
Integer.toString(Integer.MAX_VALUE),
Integer.toString(maxValue + 1),
"-1024", "-1", "101", "1997"
};
public void test() {
int maxHeapFreeValue = DynamicVMOptionChecker.getIntValue(MaxFreeRatioFlagName);
System.out.println(MaxFreeRatioFlagName + " = " + maxHeapFreeValue);
String goodValues[] = {
Integer.toString(maxValue),
Integer.toString(maxValue - 1),
Integer.toString(maxValue / 2),
"0", "1"
};
testPercentageValues();
// option under test
DynamicVMOption option = new DynamicVMOption("MinHeapFreeRatio");
checkInvalidValue(Integer.toString(maxHeapFreeValue + 1));
checkValidValue(Integer.toString(maxHeapFreeValue));
checkValidValue("0");
assertTrue(option.isWriteable(), "Option " + option.name
+ " is expected to be writable");
for (String v : badValues) {
assertFalse(option.isValidValue(v),
"'" + v + "' is expected to be illegal for flag " + option.name);
}
public static void main(String args[]) throws Exception {
new TestDynMinHeapFreeRatio().test();
for (String v : goodValues) {
option.setValue(v);
String newValue = option.getValue();
assertEQ(v, newValue);
}
}
}
/*
* 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 java.lang.management.ManagementFactory;
/**
* A utility class to work with VM options which could be altered during
* execution.
*
* This class is a wrapper around {@code com.sun.management.VMOption}.
* It provides more convenient interface to read/write the values.
*
*/
public class DynamicVMOption {
private final HotSpotDiagnosticMXBean mxBean;
/**
* VM option name, like "MinHeapFreeRatio".
*/
public final String name;
/**
* Creates an instance of DynamicVMOption.
*
* @param name the VM option name
*/
public DynamicVMOption(String name) {
this.name = name;
mxBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
}
/**
* Sets a new value for the option.
* Trying to set not applicable value will cause IllegalArgumentException.
* Behavior with null is undefined, most likely NPE will be thrown.
*
* @param newValue the value to be set
* @see #getValue()
* @throws IllegalArgumentException if newValue is not applicable to the option
*/
public final void setValue(String newValue) {
mxBean.setVMOption(name, newValue);
}
/**
* Returns the value of option.
*
* @return the current option value
* @see #setValue(java.lang.String)
*/
public final String getValue() {
return mxBean.getVMOption(name).getValue();
}
/**
* Returns true, if option is writable, false otherwise.
*
* @return true, if option is writable, false otherwise
*/
public final boolean isWriteable() {
return mxBean.getVMOption(name).isWriteable();
}
/**
* Checks if the given value is applicable for the option.
*
* This method tries to set the option to the new value. If no exception
* has been thrown the value is treated as valid.
*
* Calling this method will not change the option value. After an attempt
* to set a new value, the option will be restored to its previous value.
*
* @param value the value to verify
* @return true if option could be set to the given value
*/
public boolean isValidValue(String value) {
boolean isValid = true;
String oldValue = getValue();
try {
setValue(value);
} catch (NullPointerException e) {
if (value == null) {
isValid = false;
}
} catch (IllegalArgumentException e) {
isValid = false;
} finally {
setValue(oldValue);
}
return isValid;
}
/**
* Returns the value of the given VM option as String.
*
* This is a simple shortcut for {@code new DynamicVMOption(name).getValue()}
*
* @param name the name of VM option
* @return value as a string
* @see #getValue()
*/
public static String getString(String name) {
return new DynamicVMOption(name).getValue();
}
/**
* Returns the value of the given option as int.
*
* @param name the name of VM option
* @return value parsed as integer
* @see #getString(java.lang.String)
*
*/
public static int getInt(String name) {
return Integer.parseInt(getString(name));
}
/**
* Sets the VM option to a new value.
*
* This is a simple shortcut for {@code new DynamicVMOption(name).setValue(value)}
*
* @param name the name of VM option
* @param value the value to be set
* @see #setValue(java.lang.String)
*/
public static void setString(String name, String value) {
new DynamicVMOption(name).setValue(value);
}
/**
* Sets the VM option value to a new integer value.
*
* @param name the name of VM option
* @param value the integer value to be set
* @see #setString(java.lang.String, java.lang.String)
*/
public static void setInt(String name, int value) {
new DynamicVMOption(name).setValue(Integer.toString(value));
}
}
/*
* 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 + "\"");
}
}
}
/*
* 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册