blob: e58ab7aff10d4b81c6059dff0b2abdc735543e81 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4956978
27 * @summary The capability is disabled regardless of number of times
28 * it was enabled.
29 * @author Mandy Chung
30 */
31
32import java.lang.management.ThreadMXBean;
33import java.lang.management.ManagementFactory;
34
35public class DisableTest {
36 private static ThreadMXBean tm = ManagementFactory.getThreadMXBean();
37
38 public static void main(String args[]) throws Exception {
39 testThreadContentionMonitoring();
40 testThreadCpuMonitoring();
41
42 System.out.println("Test passed.");
43 }
44
45 private static void testThreadContentionMonitoring()
46 throws Exception {
47 if (!tm.isThreadContentionMonitoringSupported()) {
48 System.out.println("JVM does not supports thread contention monitoring");
49 return;
50 }
51
52 // Default is false.
53 tm.setThreadContentionMonitoringEnabled(false);
54 tm.setThreadContentionMonitoringEnabled(false);
55
56 // check if disabled
57 if (tm.isThreadContentionMonitoringEnabled()) {
58 throw new RuntimeException("TEST FAILED: " +
59 "Expected thread contention monitoring to be disabled");
60 }
61
62 tm.setThreadContentionMonitoringEnabled(true);
63 // check if enabled
64 if (!tm.isThreadContentionMonitoringEnabled()) {
65 throw new RuntimeException("TEST FAILED: " +
66 "Expected thread contention monitoring to be enabled");
67 }
68 }
69
70 private static void testThreadCpuMonitoring()
71 throws Exception {
72 if (!tm.isThreadCpuTimeSupported()) {
73 System.out.println("JVM does not support thread CPU time monitoring");
74 return;
75 }
76
77 if (tm.isThreadCpuTimeEnabled()) {
78 tm.setThreadCpuTimeEnabled(false);
79 }
80
81 // check if disabled
82 if (tm.isThreadCpuTimeEnabled()) {
83 throw new RuntimeException("TEST FAILED: " +
84 "Expected thread CPU time monitoring to be disabled");
85 }
86
87 tm.setThreadCpuTimeEnabled(false);
88 tm.setThreadCpuTimeEnabled(false);
89
90 if (tm.isThreadCpuTimeEnabled()) {
91 throw new RuntimeException("TEST FAILED: " +
92 "Expected thread CPU time monitoring to be disabled");
93 }
94
95 tm.setThreadCpuTimeEnabled(true);
96 if (!tm.isThreadCpuTimeEnabled()) {
97 throw new RuntimeException("TEST FAILED: " +
98 "Expected thread CPU time monitoring to be disabled");
99 }
100 }
101
102}