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 /**
  26  * Simple class to check writability, invalid and valid values for concrete VMOption
  27  */
  28 public class TestDynamicVMOption {
  29 
  30     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
  31     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
  32 
  33     private final String name;
  34     private final int value;
  35 
  36     /**
  37      * Constructor
  38      *
  39      * @param name of VM option to test
  40      */
  41     public TestDynamicVMOption(String name) {
  42         this.name = name;
  43         this.value = DynamicVMOptionChecker.getIntValue(name);
  44         System.out.println(this.name + " = " + this.value);
  45     }
  46 
  47     /**
  48      * Checks that this value can accept valid percentage values and cannot accept invalid percentage values
  49      *
  50      * @throws RuntimeException
  51      */
  52     public void testPercentageValues() {
  53         checkInvalidValue(Integer.toString(Integer.MIN_VALUE));
  54         checkInvalidValue(Integer.toString(Integer.MAX_VALUE));
  55         checkInvalidValue("-10");
  56         checkInvalidValue("190");
  57     }
  58 
  59     /**
  60      * Reads VM option from PlatformMXBean and parse it to integer value
  61      *
  62      * @return value
  63      */
  64     public int getIntValue() {
  65         return DynamicVMOptionChecker.getIntValue(this.name);
  66     }
  67 
  68     /**
  69      * Sets VM option value
  70      *
  71      * @param value to set
  72      */
  73     public void setIntValue(int value) {
  74         DynamicVMOptionChecker.setIntValue(this.name, value);
  75     }
  76 
  77     /**
  78      * Checks that this VM option is dynamically writable
  79      *
  80      * @throws RuntimeException if option if not writable
  81      * @return true
  82      */
  83     public boolean checkIsWritable() throws RuntimeException {
  84         return DynamicVMOptionChecker.checkIsWritable(this.name);
  85     }
  86 
  87     /**
  88      * Checks that value for this VM option cannot be set
  89      *
  90      * @param value to check
  91      * @throws RuntimeException on error - when expected exception hasn't been thrown
  92      */
  93     public void checkInvalidValue(String value) {
  94         DynamicVMOptionChecker.checkInvalidValue(this.name, value);
  95     }
  96 
  97     /**
  98      * Checks that value for this VM option can be set
  99      *
 100      * @param value to check
 101      * @throws RuntimeException on error - when value in VM is not equal to origin
 102      */
 103     public void checkValidValue(String value) {
 104         DynamicVMOptionChecker.checkValidValue(this.name, value);
 105     }
 106 
 107 }