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