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.sun.management.HotSpotDiagnosticMXBean; 25 import com.sun.management.VMOption; 26 import java.lang.management.ManagementFactory; 27 28 /** 29 * Simple class to check writeability, invalid and valid values for VMOption 30 */ 31 public class TestDynamicIntVMOption { 32 33 private final String name; 34 private final int value; 35 36 /** 37 * Constructor 38 * @param name of VM option to test 39 */ 40 public TestDynamicIntVMOption( String name ) { 41 this.name = name; 42 this.value = getIntValue( name ); 43 System.out.println( this.name + " = " + this.value ); 44 } 45 46 /** 47 * Checks that this value can accept valid percentage values and cannot accept invalid percentage values 48 * 49 * @throws RuntimeException 50 */ 51 public void testPercentageValues() { 52 checkInvalidValue( Integer.toString( Integer.MIN_VALUE ) ); 53 checkInvalidValue( Integer.toString( Integer.MAX_VALUE ) ); 54 checkInvalidValue( "-10" ); 55 checkInvalidValue( "190" ); 56 } 57 58 /** 59 * Reads VM option from PlatformMXBean and parse it to integer value 60 * @return value 61 */ 62 public int getIntValue() { 63 return getIntValue( this.name ); 64 } 65 66 /** 67 * Checks that this VM option is dynamically writable 68 * @throws RuntimeException if option if not writable 69 * @return true 70 */ 71 public boolean checkIsWritable() throws RuntimeException { 72 return checkIsWritable( this.name ); 73 } 74 75 /** 76 * Checks that value for this VM option cannot be set 77 * @param value to check 78 * @throws RuntimeException on error - when expected exception hasn't been thrown 79 */ 80 public void checkInvalidValue( String value ) { 81 checkInvalidValue( this.name, value ); 82 } 83 84 /** 85 * Checks that value for this VM option can be set 86 * @param value to check 87 * @throws RuntimeException on error - when value in VM is not equal to origin 88 */ 89 public void checkValidValue( String value ) { 90 checkValidValue( this.name, value ); 91 } 92 93 /** 94 * Reads VM option from PlatformMXBean and parse it to integer value 95 * 96 * @param name of option 97 * @return parsed value 98 */ 99 public static int getIntValue( String name ) { 100 101 VMOption option = ManagementFactory. 102 getPlatformMXBean( HotSpotDiagnosticMXBean.class ). 103 getVMOption( name ); 104 105 return Integer.parseInt( option.getValue() ); 106 } 107 108 /** 109 * Checks that VM option is dynamically writable 110 * @param name 111 * @throws RuntimeException if option if not writable 112 * @return always true 113 */ 114 public static boolean checkIsWritable( String name ) { 115 VMOption option = ManagementFactory. 116 getPlatformMXBean( HotSpotDiagnosticMXBean.class ). 117 getVMOption( name ); 118 119 if ( !option.isWriteable() ) { 120 throw new RuntimeException( name + " is not writable" ); 121 } 122 123 return true; 124 } 125 126 /** 127 * Checks that value cannot be set 128 * 129 * @param name of flag 130 * @param value string representation of value to set 131 * @throws RuntimeException on error - when expected exception hasn't been thrown 132 */ 133 public static void checkInvalidValue( String name, String value ) { 134 // should throw 135 try { 136 ManagementFactory. 137 getPlatformMXBean( HotSpotDiagnosticMXBean.class ). 138 setVMOption( name, value ); 139 140 } catch ( IllegalArgumentException e ) { 141 return; 142 } 143 144 throw new RuntimeException( "Expected IllegalArgumentException was not thrown, " + name + "= " + value ); 145 } 146 147 /** 148 * Checks that value can be set 149 * 150 * @param name of flag to set 151 * @param value string representation of value to set 152 * @throws RuntimeException on error - when value in VM is not equal to origin 153 */ 154 public static void checkValidValue( String name, String value ) { 155 ManagementFactory. 156 getPlatformMXBean( HotSpotDiagnosticMXBean.class ). 157 setVMOption( name, value ); 158 159 VMOption option = ManagementFactory. 160 getPlatformMXBean( HotSpotDiagnosticMXBean.class ). 161 getVMOption( name ); 162 163 if ( !option.getValue().equals( value ) ) { 164 throw new RuntimeException( "Actual value of " + name + " \"" + option.getValue() 165 + "\" not equal origin \"" + value + "\"" ); 166 } 167 } 168 169 }