1 /* 2 * Copyright (c) 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import java.lang.management.ManagementFactory; 27 import com.sun.management.HotSpotDiagnosticMXBean; 28 import jdk.testlibrary.OutputAnalyzer; 29 import static jdk.testlibrary.Platform.isSolaris; 30 import static jdk.testlibrary.Asserts.assertEquals; 31 import static jdk.testlibrary.Asserts.assertNotEquals; 32 import static jdk.testlibrary.Asserts.assertTrue; 33 34 /** 35 * @test 36 * @summary The test sanity checks 'jinfo -flag' option. 37 * @library /lib/testlibrary 38 * @modules java.management 39 * @build jdk.testlibrary.* JInfoHelper 40 * @run main/othervm -XX:+HeapDumpOnOutOfMemoryError JInfoRunningProcessFlagTest 41 */ 42 public class JInfoRunningProcessFlagTest { 43 44 public static void main(String[] args) throws Exception { 45 testFlag(); 46 testFlagPlus(); 47 testFlagMinus(); 48 testFlagEqual(); 49 50 testInvalidFlag(); 51 52 testSolarisSpecificFlag(); 53 } 54 55 private static void testFlag() throws Exception { 56 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "HeapDumpOnOutOfMemoryError"); 57 output.shouldHaveExitValue(0); 58 assertTrue(output.getStderr().isEmpty(), "'jinfo -flag HeapDumpOnOutOfMemoryError' stderr should be empty"); 59 output.shouldContain("+HeapDumpOnOutOfMemoryError"); 60 } 61 62 private static void testFlagPlus() throws Exception { 63 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "+PrintGC"); 64 output.shouldHaveExitValue(0); 65 output = JInfoHelper.jinfo("-flag", "PrintGC"); 66 output.shouldHaveExitValue(0); 67 output.shouldContain("+PrintGC"); 68 verifyIsEnabled("PrintGC"); 69 } 70 71 private static void testFlagMinus() throws Exception { 72 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "-PrintGC"); 73 output.shouldHaveExitValue(0); 74 output = JInfoHelper.jinfo("-flag", "PrintGC"); 75 output.shouldHaveExitValue(0); 76 output.shouldContain("-PrintGC"); 77 verifyIsDisabled("PrintGC"); 78 } 79 80 private static void testFlagEqual() throws Exception { 81 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "PrintGC=1"); 82 output.shouldHaveExitValue(0); 83 output = JInfoHelper.jinfo("-flag", "PrintGC"); 84 output.shouldHaveExitValue(0); 85 output.shouldContain("+PrintGC"); 86 verifyIsEnabled("PrintGC"); 87 } 88 89 private static void testInvalidFlag() throws Exception { 90 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "monkey"); 91 assertNotEquals(output.getExitValue(), 0, "A non-zero exit code should be returned for invalid flag"); 92 } 93 94 private static void testSolarisSpecificFlag() throws Exception { 95 if (!isSolaris()) 96 return; 97 98 OutputAnalyzer output = JInfoHelper.jinfo("-flag", "+ExtendedDTraceProbes"); 99 output.shouldHaveExitValue(0); 100 output = JInfoHelper.jinfo(); 101 output.shouldContain("+ExtendedDTraceProbes"); 102 verifyIsEnabled("ExtendedDTraceProbes"); 103 104 output = JInfoHelper.jinfo("-flag", "-ExtendedDTraceProbes"); 105 output.shouldHaveExitValue(0); 106 output = JInfoHelper.jinfo(); 107 output.shouldContain("-ExtendedDTraceProbes"); 108 verifyIsDisabled("ExtendedDTraceProbes"); 109 110 output = JInfoHelper.jinfo("-flag", "ExtendedDTraceProbes"); 111 output.shouldContain("-ExtendedDTraceProbes"); 112 output.shouldHaveExitValue(0); 113 } 114 115 private static void verifyIsEnabled(String flag) { 116 HotSpotDiagnosticMXBean hotspotDiagnostic = 117 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 118 String flagValue = hotspotDiagnostic.getVMOption(flag).getValue(); 119 assertEquals(flagValue, "true", "Expected '" + flag + "' flag be enabled"); 120 } 121 122 private static void verifyIsDisabled(String flag) { 123 HotSpotDiagnosticMXBean hotspotDiagnostic = 124 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 125 String flagValue = hotspotDiagnostic.getVMOption(flag).getValue(); 126 assertEquals(flagValue, "false", "Expected '" + flag + "' flag be disabled"); 127 } 128 129 }