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