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