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 com.sun.management.HotSpotDiagnosticMXBean;
  26 import com.sun.management.VMOption;
  27 import java.lang.management.ManagementFactory;
  28 
  29 /**
  30  * Simple class to check writability, invalid and valid values for VMOption
  31  */
  32 public class VMOptionValueChecker {
  33 
  34     /**
  35      * Get vm option from PlatformMXBean and checks that it is writable
  36      *
  37      * @param name of option
  38      * @return parsed value
  39      * @throws RuntimeException if option if not writable
  40      */
  41     public static int getIntWritableValue(String name) {
  42 
  43         VMOption option = ManagementFactory.
  44                 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
  45                 getVMOption(name);
  46 
  47         if (!option.isWriteable()) {
  48             throw new RuntimeException(name + " is not writable");
  49         }
  50 
  51         return Integer.parseInt(option.getValue());
  52     }
  53 
  54     /**
  55      * Checks that value cannot be set
  56      *
  57      * @param name of flag
  58      * @param value string representation of value to set
  59      * @throws RuntimeException on error - when expected exception hasn't been
  60      * thrown
  61      */
  62     public static void checkInvalidValue(String name, String value) {
  63         // should throw
  64         try {
  65             ManagementFactory.
  66                     getPlatformMXBean(HotSpotDiagnosticMXBean.class).
  67                     setVMOption(name, value);
  68 
  69         } catch (IllegalArgumentException e) {
  70             return;
  71         }
  72 
  73         throw new RuntimeException("Expected IllegalArgumentException was not thrown, " + name + "= " + value);
  74     }
  75 
  76     /**
  77      * Checks that value can be set
  78      *
  79      * @param name of flag to set
  80      * @param value string representation of value to set
  81      * @throws RuntimeException on error - when value in vm is not equal to
  82      * origin
  83      */
  84     public static void checkValid(String name, String value) {
  85         ManagementFactory.
  86                 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
  87                 setVMOption(name, value);
  88 
  89         VMOption option = ManagementFactory.
  90                 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
  91                 getVMOption(name);
  92 
  93         if (!option.getValue().equals(value)) {
  94             throw new RuntimeException("Actual value of " + name + " \"" + option.getValue() + "\" not equal origin \"" + value + "\"");
  95         }
  96     }
  97 
  98 }