1 /*
   2  * Copyright (c) 2015, 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 package optionsvalidation;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import java.util.Locale;
  29 
  30 public class DoubleJVMOption extends JVMOption {
  31 
  32     /**
  33      * Additional double values to test
  34      */
  35     private static final double ADDITIONAL_TEST_DOUBLE_NEGATIVE = -1.5;
  36     private static final double ADDITIONAL_TEST_DOUBLE_ZERO = 0.0;
  37     private static final double ADDITIONAL_TEST_DOUBLE_POSITIVE = 1.75;
  38 
  39     /**
  40      * Mininum option value
  41      */
  42     private double min;
  43     /**
  44      * Maximum option value
  45      */
  46     private double max;
  47 
  48     /**
  49      * Initialize double option with passed name
  50      *
  51      * @param name name of the option
  52      */
  53     DoubleJVMOption(String name) {
  54         this.name = name;
  55         min = Double.MIN_VALUE;
  56         max = Double.MAX_VALUE;
  57     }
  58 
  59     /**
  60      * Initialize double option with passed name, min and max values
  61      *
  62      * @param name name of the option
  63      * @param min minimum value of the option
  64      * @param max maximum value of the option
  65      */
  66     public DoubleJVMOption(String name, double min, double max) {
  67         this(name);
  68         this.min = min;
  69         this.max = max;
  70     }
  71 
  72     /**
  73      * Set new minimum option value
  74      *
  75      * @param min new minimum value
  76      */
  77     @Override
  78     void setMin(String min) {
  79         this.min = new Double(min);
  80     }
  81 
  82     /**
  83      * Get string with minimum value of the option
  84      *
  85      * @return string with minimum value of the option
  86      */
  87     @Override
  88     String getMin() {
  89         return formatValue(min);
  90     }
  91 
  92     /**
  93      * Set new maximum option value
  94      *
  95      * @param max new maximum value
  96      */
  97     @Override
  98     void setMax(String max) {
  99         this.max = new Double(max);
 100     }
 101 
 102     /**
 103      * Get string with maximum value of the option
 104      *
 105      * @return string with maximum value of the option
 106      */
 107     @Override
 108     String getMax() {
 109         return formatValue(max);
 110     }
 111 
 112     private String formatValue(double value) {
 113         return String.format(Locale.US, "%f", value);
 114     }
 115 
 116     /**
 117      * Return list of strings with valid option values which used for testing
 118      * using jcmd, attach and etc.
 119      *
 120      * @return list of strings which contain valid values for option
 121      */
 122     @Override
 123     protected List<String> getValidValues() {
 124         List<String> validValues = new ArrayList<>();
 125 
 126         if (testMinRange) {
 127             validValues.add(formatValue(min));
 128         }
 129         if (testMaxRange) {
 130             validValues.add(formatValue(max));
 131         }
 132 
 133         if (testMinRange) {
 134             if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_NEGATIVE) < 0)
 135                     && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_NEGATIVE) > 0)) {
 136                 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_NEGATIVE));
 137             }
 138 
 139             if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_ZERO) < 0)
 140                     && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_ZERO) > 0)) {
 141                 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_ZERO));
 142             }
 143 
 144             if ((Double.compare(min, ADDITIONAL_TEST_DOUBLE_POSITIVE) < 0)
 145                     && (Double.compare(max, ADDITIONAL_TEST_DOUBLE_POSITIVE) > 0)) {
 146                 validValues.add(formatValue(ADDITIONAL_TEST_DOUBLE_POSITIVE));
 147             }
 148         }
 149 
 150         return validValues;
 151     }
 152 
 153     /**
 154      * Return list of strings with invalid option values which used for testing
 155      * using jcmd, attach and etc.
 156      *
 157      * @return list of strings which contain invalid values for option
 158      */
 159     @Override
 160     protected List<String> getInvalidValues() {
 161         List<String> invalidValues = new ArrayList<>();
 162 
 163         if (withRange) {
 164             /* Return invalid values only for options which have defined range in VM */
 165             if (Double.compare(min, Double.MIN_VALUE) != 0) {
 166                 if ((Double.compare(min, 0.0) > 0)
 167                         && (Double.isNaN(min * 0.999) == false)) {
 168                     invalidValues.add(formatValue(min * 0.999));
 169                 } else if ((Double.compare(min, 0.0) < 0)
 170                         && (Double.isNaN(min * 1.001) == false)) {
 171                     invalidValues.add(formatValue(min * 1.001));
 172                 }
 173             }
 174 
 175             if (Double.compare(max, Double.MAX_VALUE) != 0) {
 176                 if ((Double.compare(max, 0.0) > 0)
 177                         && (Double.isNaN(max * 1.001) == false)) {
 178                     invalidValues.add(formatValue(max * 1.001));
 179                 } else if ((Double.compare(max, 0.0) < 0)
 180                         && (Double.isNaN(max * 0.999) == false)) {
 181                     invalidValues.add(formatValue(max * 0.999));
 182                 }
 183             }
 184         }
 185 
 186         return invalidValues;
 187     }
 188 
 189     /**
 190      * Return expected error message for option with value "value" when it used
 191      * on command line with passed value
 192      *
 193      * @param value option value
 194      * @return expected error message
 195      */
 196     @Override
 197     protected String getErrorMessageCommandLine(String value) {
 198         String errorMsg;
 199 
 200         if (withRange) {
 201             /* Option have defined range in VM */
 202             errorMsg = "is outside the allowed range";
 203         } else {
 204             errorMsg = "";
 205         }
 206 
 207         return errorMsg;
 208     }
 209 }