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 package com.oracle.java.testlibrary;
  24 
  25 import static com.oracle.java.testlibrary.Asserts.assertEQ;
  26 import static com.oracle.java.testlibrary.Asserts.assertFalse;
  27 import static com.oracle.java.testlibrary.Asserts.assertTrue;
  28 import com.sun.management.HotSpotDiagnosticMXBean;
  29 import com.sun.management.VMOption;
  30 import java.lang.management.ManagementFactory;
  31 import java.util.Arrays;
  32 import java.util.Collection;
  33 
  34 /**
  35  * A utility class to verify VMOptions which could be altered during execution.
  36  * 
  37  * Implementation is based on {@code com.sun.management.VMOption}.
  38  * 
  39  * The class provides methods to set and values for given options.
  40  * It also contains a number of assert methods to be invoked from tests to 
  41  * very that an option under test correctly sets valid values and rejects
  42  * invalid ones.
  43  * 
  44  */
  45 public class DynamicVMOption {
  46     
  47     /**
  48      * Private factory method to hide implementation details from tests.
  49      * 
  50      * Those who need to work with VMOption could create it directly.
  51      * 
  52      * @param name the VM option name, like {@code MaxHeapFreeRatio}
  53      * @return an instance of com.sun.management.VMOption
  54      */
  55     private static VMOption getVMOption(String name) {
  56         return ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class).
  57                 getVMOption(name);
  58     }
  59     
  60     /**
  61      * Returns value of the given VM option.
  62      * 
  63      * @param name the name of VM option
  64      * @return value as string
  65      */
  66     public static String getValue(String name) {
  67         return getVMOption(name).getValue();
  68     }
  69 
  70     /**
  71      * Returns value of the given option as int.
  72      *
  73      * @param name the name of VM option
  74      * @return value parsed as integer
  75      * 
  76      */
  77     public static int getIntValue(String name) {
  78         return Integer.parseInt(getValue(name));
  79     }
  80 
  81     /**
  82      * Sets VM option value.
  83      *
  84      * @param name the name of VM option
  85      * @param value the value to be set
  86      */
  87     public static void setValue(String name, String value) {
  88         ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class)
  89                 .setVMOption(name, value);
  90     }
  91 
  92 
  93     /**
  94      * Sets VM option value.
  95      *
  96      * @param name the name of VM option
  97      * @param value the value to be set
  98      */
  99     public static void setIntValue(String name, int value) {
 100         setValue(name, Integer.toString(value));
 101     }
 102 
 103     /**
 104      * Tests VM Option for being writable.
 105      * 
 106      * @param name the name of VM option
 107      */
 108     public static void assertOptionIsWritable(String name) {
 109         assertTrue(getVMOption(name).isWriteable(),
 110                 "Option " + name + " is expected to be writable");
 111     }
 112 
 113        
 114     /**
 115      * Tests VM Option for being immutable.
 116      * 
 117      * @param name of option
 118      */
 119     public static void assertOptionIsNotWritable(String name) {
 120         assertFalse(getVMOption(name).isWriteable(),
 121                 "Option " + name + " is expected to be immutable");
 122     }
 123     
 124     /**
 125      * Tests VM Option for a valid value.
 126      * The option should be successfully set to a new value.
 127      * 
 128      * @param name the VM option name
 129      * @param value the valid value
 130      */
 131     public static void assertValidValue(String name, String value) {
 132         setValue(name, value);
 133         String newValue = getValue(name);
 134         assertEQ(value, newValue,
 135                 "set('" + name + "'," + value + "); get('" + name + "')=" + newValue);        
 136     }
 137 
 138     /**
 139      * Tests VM Option for valid values.
 140      * 
 141      * @param name the VM option name
 142      * @param values the collection of valid values
 143      */
 144     public static void assertValidValues(String name, Collection<String> values) {
 145         for (String v: values) {
 146             assertValidValue(name, v);
 147         }
 148     }
 149     
 150     /**
 151      * Tests VM Option for an valid values.
 152      * 
 153      * @param name the VM option name
 154      * @param values the collection if invalid values
 155      */
 156     public static void assertValidValues(String name, String[] values) {
 157         assertValidValues(name, Arrays.asList(values));
 158     }
 159     
 160     
 161     /**
 162      * Tests VM Option for an invalid value.
 163      * Attempts to set option to invalid value should cause 
 164      * throwing IllegalArgumentExecption.
 165      * 
 166      * @param name the VM option name
 167      * @param value the invalid value
 168      */
 169     public static void assertInvalidValue(String name, String value) {
 170         boolean passed = false;
 171         try {
 172             setValue(name, value);
 173         } catch (NullPointerException e) {
 174             if (value == null) {
 175                 passed = true;
 176             }
 177         } catch (IllegalArgumentException e) {
 178             passed = true;
 179         }
 180         assertTrue(passed, 
 181                 "Expected IllegalArgumentException was not thrown, " + name + "= " + value);
 182         
 183     }
 184     
 185     /**
 186      * Tests VM Option for an invalid values.
 187      * 
 188      * @param name the VM option name
 189      * @param values the collection if invalid values
 190      */
 191     public static void assertInvalidValues(String name, Collection<String> values) {
 192         for (String v: values) {
 193             assertInvalidValue(name, v);
 194         }
 195     }
 196 
 197     /**
 198      * Tests VM Option for an invalid values.
 199      * 
 200      * @param name the VM option name
 201      * @param values the array if invalid values
 202      */
 203     public static void assertInvalidValues(String name, String[] values) {
 204         assertInvalidValues(name, Arrays.asList(values));
 205     }            
 206 }