1/*
2 * Copyright (c) 2014, 2017, 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 BooleanTest
26 * @bug 8028756
27 * @library /test/lib
28 * @modules java.base/jdk.internal.misc
29 *          java.compiler
30 *          java.management/sun.management
31 *          jdk.internal.jvmstat/sun.jvmstat.monitor
32 * @build sun.hotspot.WhiteBox
33 * @run main ClassFileInstaller sun.hotspot.WhiteBox
34 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
35 * @run main/othervm/timeout=600 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI BooleanTest
36 * @summary testing of WB::set/getBooleanVMFlag()
37 * @author igor.ignatyev@oracle.com
38 */
39
40import sun.hotspot.WhiteBox;
41import jdk.test.lib.process.OutputAnalyzer;
42import jdk.test.lib.process.ProcessTools;
43import sun.management.*;
44import com.sun.management.*;
45
46public class BooleanTest {
47    private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
48    private static final Boolean[] TESTS = {true, false, true, true, false};
49    private static final String TEST_NAME = "BooleanTest";
50    private static final String FLAG_NAME = "PrintCompilation";
51    private static final String FLAG_DEBUG_NAME = "SafepointALot";
52    private static final String METHOD = TEST_NAME + "::method";
53    private static final String METHOD1 = METHOD + "1";
54    private static final String METHOD2 = METHOD + "2";
55
56    public static void main(String[] args) throws Exception {
57        if (args.length == 0) {
58            VmFlagTest.runTest(FLAG_NAME, TESTS,
59                VmFlagTest.WHITE_BOX::setBooleanVMFlag,
60                VmFlagTest.WHITE_BOX::getBooleanVMFlag);
61            testFunctional(false);
62            testFunctional(true);
63            VmFlagTest.runTest(FLAG_DEBUG_NAME, VmFlagTest.WHITE_BOX::getBooleanVMFlag);
64        } else {
65            boolean value = Boolean.valueOf(args[0]);
66            method1();
67            VmFlagTest.WHITE_BOX.setBooleanVMFlag(FLAG_NAME, value);
68            method2();
69        }
70    }
71
72    private static void testFunctional(boolean value) throws Exception {
73        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
74            "-Xbootclasspath/a:.",
75            "-XX:+UnlockDiagnosticVMOptions",
76            "-XX:+WhiteBoxAPI",
77            "-Xcomp",
78            "-XX:CompileCommand=compileonly," + METHOD + "*",
79            "-XX:" + (value ? "-" : "+") + FLAG_NAME,
80            TEST_NAME,
81            "" + value);
82        OutputAnalyzer out = new OutputAnalyzer(pb.start());
83        if (value) {
84            out.shouldNotContain(METHOD1);
85            out.shouldContain(METHOD2);
86        } else {
87            out.shouldContain(METHOD1);
88            out.shouldNotContain(METHOD2);
89        }
90    }
91
92    private static void method1() { }
93    private static void method2() { }
94}
95
96