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 import java.lang.management.ManagementFactory;
25 import java.lang.management.PlatformManagedObject;
26 
27 /*
28  * @test
29  * @bug     8042901
30  * @summary If jdk.management is present, GarbageCollectorMXBean and ThreadMXBean
31  *          must be from com.sun.management.internal
32  * @author  Shanliang Jiang
33  */
34 public class CheckSomeMXBeanImplPackage {
35     private static String implPackageName = "com.sun.management.internal";
36 
37     public static void main(String[] args) throws Exception {
38         boolean present = false;
39         try {
40             Class.forName("com.sun.management.GarbageCollectorMXBean");
41             present = true;
42         } catch (ClassNotFoundException cnfe) {}
43 
44         if (present) {
45             Class <? extends PlatformManagedObject> klazz =
46                     java.lang.management.GarbageCollectorMXBean.class;
47             for (Object obj :
48                     ManagementFactory.getPlatformMXBeans(klazz)) {
49                 check(klazz.getName(), obj);
50             }
51 
52             klazz = com.sun.management.GarbageCollectorMXBean.class;
53             for (Object obj :
54                     ManagementFactory.getPlatformMXBeans(klazz)) {
55                 check(klazz.getName(), obj);
56             }
57 
58             klazz = java.lang.management.ThreadMXBean.class;
59             check(klazz.getName(),
60                     ManagementFactory.getPlatformMXBean(klazz));
61 
62             klazz = com.sun.management.ThreadMXBean.class;
63             check(klazz.getName(),
64                     ManagementFactory.getPlatformMXBean(klazz));
65 
66             System.out.println("--- PASSED!");
67         } else {
68             System.out.println("--- Skip the test, jdk.management module is not present!");
69         }
70     }
71 
72     private static void check(String mbeanName, Object impl) {
73         if (!impl.getClass().getName().startsWith(implPackageName)) {
74             throw new RuntimeException(mbeanName+" implementation package "
75                     + "should be: " + implPackageName
76                     + ", but got: " + impl.getClass());
77         } else {
78             System.out.println("--- Good, "+mbeanName+" got right implementation: " + impl);
79         }
80     }
81 }
82