1 /*
   2  * Copyright (c) 2016 Red Hat, Inc. and/or its affiliates.
   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 TestShenandoahArgumentRanges
  26  * @summary Test that Shenandoah arguments are checked for ranges where applicable
  27  * @key gc
  28  * @library /testlibrary
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @run driver TestShenandoahArgumentRanges
  32  */
  33 
  34 import com.oracle.java.testlibrary.*;
  35 
  36 public class TestShenandoahArgumentRanges {
  37     public static void main(String[] args) throws Exception {
  38         testRange("ShenandoahGarbageThreshold", 0, 100);
  39         testRange("ShenandoahFreeThreshold", 0, 100);
  40         testRange("ShenandoahAllocationThreshold", 0, 100);
  41         testHeuristics();
  42     }
  43 
  44     private static void testHeuristics() throws Exception {
  45 
  46         {
  47             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  48                                                                       "-XX:+UnlockDiagnosticVMOptions",
  49                                                                       "-XX:+UnlockExperimentalVMOptions",
  50                                                                       "-XX:ShenandoahGCHeuristics=aggressive",
  51                                                                       "-version");
  52             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  53             output.shouldHaveExitValue(0);
  54         }
  55         {
  56             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  57                                                                       "-XX:+UnlockDiagnosticVMOptions",
  58                                                                       "-XX:+UnlockExperimentalVMOptions",
  59                                                                       "-XX:ShenandoahGCHeuristics=static",
  60                                                                       "-version");
  61             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  62             output.shouldHaveExitValue(0);
  63         }
  64         {
  65             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  66                                                                       "-XX:+UnlockDiagnosticVMOptions",
  67                                                                       "-XX:+UnlockExperimentalVMOptions",
  68                                                                       "-XX:ShenandoahGCHeuristics=fluff",
  69                                                                       "-version");
  70             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  71             output.shouldMatch("Unknown -XX:ShenandoahGCHeuristics option");
  72             output.shouldHaveExitValue(1);
  73         }
  74     }
  75 
  76     private static void testRange(String option, int min, int max) throws Exception {
  77         {
  78             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  79                                                                       "-XX:" + option + "=" + (max + 1),
  80                                                                       "-version");
  81             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  82             output.shouldHaveExitValue(1);
  83         }
  84         {
  85             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  86                                                                       "-XX:" + option + "=" + max,
  87                                                                       "-version");
  88             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  89             output.shouldHaveExitValue(0);
  90         }
  91         {
  92             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
  93                                                                       "-XX:" + option + "=" + (min - 1),
  94                                                                       "-version");
  95             OutputAnalyzer output = new OutputAnalyzer(pb.start());
  96             output.shouldHaveExitValue(1);
  97         }
  98         {
  99             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseShenandoahGC",
 100                                                                       "-XX:" + option + "=" + min,
 101                                                                       "-version");
 102             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 103             output.shouldHaveExitValue(0);
 104         }
 105     }
 106 }