1 /*
   2  * Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * @test TestArgumentRanges
  26  * @summary Test that Shenandoah arguments are checked for ranges where applicable
  27  * @key gc
  28  * @library /testlibrary
  29  *
  30  * @run driver TestArgumentRanges
  31  */
  32 
  33 import com.oracle.java.testlibrary.*;
  34 
  35 public class TestArgumentRanges {
  36     public static void main(String[] args) throws Exception {
  37         testRange("ShenandoahGarbageThreshold", 0, 100);
  38         testRange("ShenandoahFreeThreshold", 0, 100);
  39         testRange("ShenandoahAllocationThreshold", 0, 100);
  40         testHeuristics();
  41     }
  42 
  43     private static void testHeuristics() throws Exception {
  44 
  45         {
  46             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  47                     "-XX:+UnlockExperimentalVMOptions",
  48                     "-XX:+UseShenandoahGC",
  49                     "-XX:ShenandoahGCHeuristics=aggressive",
  50                     "-version");
  51             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  52             output.shouldHaveExitValue(0);
  53         }
  54         {
  55             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  56                     "-XX:+UnlockExperimentalVMOptions",
  57                     "-XX:+UseShenandoahGC",
  58                     "-XX:ShenandoahGCHeuristics=static",
  59                     "-version");
  60             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  61             output.shouldHaveExitValue(0);
  62         }
  63         {
  64             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  65                     "-XX:+UnlockExperimentalVMOptions",
  66                     "-XX:+UseShenandoahGC",
  67                     "-XX:ShenandoahGCHeuristics=fluff",
  68                     "-version");
  69             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  70             output.shouldMatch("Unknown -XX:ShenandoahGCHeuristics option");
  71             output.shouldHaveExitValue(1);
  72         }
  73     }
  74 
  75     private static void testRange(String option, int min, int max) throws Exception {
  76         {
  77             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  78                     "-XX:+UnlockExperimentalVMOptions",
  79                     "-XX:+UseShenandoahGC",
  80                     "-XX:" + option + "=" + (max + 1),
  81                     "-version");
  82             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  83             output.shouldHaveExitValue(1);
  84         }
  85         {
  86             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  87                     "-XX:+UnlockExperimentalVMOptions",
  88                     "-XX:+UseShenandoahGC",
  89                     "-XX:" + option + "=" + max,
  90                     "-version");
  91             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  92             output.shouldHaveExitValue(0);
  93         }
  94         {
  95             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
  96                     "-XX:+UnlockExperimentalVMOptions",
  97                     "-XX:+UseShenandoahGC",
  98                     "-XX:" + option + "=" + (min - 1),
  99                     "-version");
 100             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 101             output.shouldHaveExitValue(1);
 102         }
 103         {
 104             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UnlockDiagnosticVMOptions",
 105                     "-XX:+UnlockExperimentalVMOptions",
 106                     "-XX:+UseShenandoahGC",
 107                     "-XX:" + option + "=" + min,
 108                     "-version");
 109             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 110             output.shouldHaveExitValue(0);
 111         }
 112     }
 113 }