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 /*
  25  * @test
  26  * @summary Verify jcmd error message for out-of-range value and for
  27  *          value which is not allowed by constraint. Also check that
  28  *          jcmd does not print an error message to the target process output.
  29  * @library /testlibrary
  30  * @run main TestJcmdOutput
  31  */
  32 
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.DynamicVMOption;
  35 import jdk.test.lib.OutputAnalyzer;
  36 import jdk.test.lib.ProcessTools;
  37 import jdk.test.lib.dcmd.PidJcmdExecutor;
  38 
  39 public class TestJcmdOutput {
  40 
  41     /* Message printed by jcmd for value which is out-of-range */
  42     static final String JCMD_OUT_OF_RANGE_MESSAGE = "error: must have value in range";
  43     /* Message printed by jcmd for value which is not allowed by constraint */
  44     static final String JCMD_CONSTRAINT_MESSAGE = "value violates its flag's constraint";
  45 
  46     public static void main(String[] args) throws Exception {
  47         OutputAnalyzer output;
  48 
  49         System.out.println("Verify jcmd error message and that jcmd does not write errors to the target process output");
  50         output = new OutputAnalyzer((ProcessTools.createJavaProcessBuilder(
  51                 "-Dtest.jdk=" + System.getProperty("test.jdk"),
  52                 "-XX:MinHeapFreeRatio=20", "-XX:MaxHeapFreeRatio=80", runJcmd.class.getName())).start());
  53 
  54         output.shouldHaveExitValue(0);
  55         /* Verify that jcmd not print error message to the target process output */
  56         output.shouldNotContain(JCMD_OUT_OF_RANGE_MESSAGE);
  57         output.shouldNotContain(JCMD_CONSTRAINT_MESSAGE);
  58     }
  59 
  60     public static class runJcmd {
  61 
  62         public static void main(String[] args) throws Exception {
  63             int minHeapFreeRatio = new Integer((new DynamicVMOption("MinHeapFreeRatio")).getValue());
  64             int maxHeapFreeRatio = new Integer((new DynamicVMOption("MaxHeapFreeRatio")).getValue());
  65             PidJcmdExecutor executor = new PidJcmdExecutor();
  66 
  67             Asserts.assertGT(minHeapFreeRatio, 0, "MinHeapFreeRatio must be greater than 0");
  68             Asserts.assertLT(maxHeapFreeRatio, 100, "MaxHeapFreeRatio must be less than 100");
  69 
  70             /* Check out-of-range values */
  71             executor.execute("VM.set_flag MinHeapFreeRatio -1", true).shouldContain(JCMD_OUT_OF_RANGE_MESSAGE);
  72             executor.execute("VM.set_flag MaxHeapFreeRatio 101", true).shouldContain(JCMD_OUT_OF_RANGE_MESSAGE);
  73 
  74             /* Check values which not allowed by constraint */
  75             executor.execute(
  76                     String.format("VM.set_flag MinHeapFreeRatio %d", maxHeapFreeRatio + 1), true)
  77                     .shouldContain(JCMD_CONSTRAINT_MESSAGE);
  78             executor.execute(
  79                     String.format("VM.set_flag MaxHeapFreeRatio %d", minHeapFreeRatio - 1), true)
  80                     .shouldContain(JCMD_CONSTRAINT_MESSAGE);
  81         }
  82     }
  83 }