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