1 /*
   2  * Copyright (c) 2013, 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 /*
  25  * @test TestHeapFreeRatio
  26  * @key gc
  27  * @bug 8025661
  28  * @summary Test parsing of -Xminf and -Xmaxf
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @run main/othervm TestHeapFreeRatio
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 
  38 public class TestHeapFreeRatio {
  39 
  40   enum Validation {
  41     VALID,
  42     MIN_INVALID,
  43     MAX_INVALID,
  44     OUT_OF_RANGE,
  45     COMBINATION_INVALID
  46   }
  47 
  48   private static void testMinMaxFreeRatio(String min, String max, Validation type) throws Exception {
  49     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  50         "-Xminf" + min,
  51         "-Xmaxf" + max,
  52         "-version");
  53     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  54 
  55     switch (type) {
  56     case VALID:
  57       output.shouldNotContain("Error");
  58       output.shouldHaveExitValue(0);
  59       break;
  60     case MIN_INVALID:
  61       output.shouldContain("Bad min heap free percentage size: -Xminf" + min);
  62       output.shouldContain("Error");
  63       output.shouldHaveExitValue(1);
  64       break;
  65     case MAX_INVALID:
  66       output.shouldContain("Bad max heap free percentage size: -Xmaxf" + max);
  67       output.shouldContain("Error");
  68       output.shouldHaveExitValue(1);
  69       break;
  70     case OUT_OF_RANGE:
  71       output.shouldContain("outside the allowed range");
  72       output.shouldContain("Error");
  73       output.shouldHaveExitValue(1);
  74       break;
  75     case COMBINATION_INVALID:
  76       output.shouldContain("must be less than or equal to MaxHeapFreeRatio");
  77       output.shouldContain("Error");
  78       output.shouldHaveExitValue(1);
  79       break;
  80     default:
  81       throw new IllegalStateException("Must specify expected validation type");
  82     }
  83 
  84     System.out.println(output.getOutput());
  85   }
  86 
  87   public static void main(String args[]) throws Exception {
  88     testMinMaxFreeRatio( "0.1", "0.5", Validation.VALID);
  89     testMinMaxFreeRatio(  ".1",  ".5", Validation.VALID);
  90     testMinMaxFreeRatio( "0.5", "0.5", Validation.VALID);
  91 
  92     testMinMaxFreeRatio("=0.1", "0.5", Validation.MIN_INVALID);
  93     testMinMaxFreeRatio("0.1f", "0.5", Validation.MIN_INVALID);
  94     testMinMaxFreeRatio(
  95                      "INVALID", "0.5", Validation.MIN_INVALID);
  96 
  97     testMinMaxFreeRatio( "0.1", "0.5f", Validation.MAX_INVALID);
  98     testMinMaxFreeRatio( "0.1", "=0.5", Validation.MAX_INVALID);
  99     testMinMaxFreeRatio(
 100                      "0.1",  "INVALID", Validation.MAX_INVALID);
 101 
 102     testMinMaxFreeRatio("-0.1", "0.5", Validation.OUT_OF_RANGE);
 103     testMinMaxFreeRatio( "1.1", "0.5", Validation.OUT_OF_RANGE);
 104     testMinMaxFreeRatio(
 105                   "2147483647", "0.5", Validation.OUT_OF_RANGE);
 106     testMinMaxFreeRatio( "0.1", "-0.5", Validation.OUT_OF_RANGE);
 107     testMinMaxFreeRatio( "0.1",  "1.5", Validation.OUT_OF_RANGE);
 108     testMinMaxFreeRatio(
 109                    "0.1", "2147483647", Validation.OUT_OF_RANGE);
 110 
 111     testMinMaxFreeRatio( "0.5",  "0.1", Validation.COMBINATION_INVALID);
 112     testMinMaxFreeRatio(  ".5",  ".10", Validation.COMBINATION_INVALID);
 113     testMinMaxFreeRatio("0.12","0.100", Validation.COMBINATION_INVALID);
 114   }
 115 }