1 /*
2  * Copyright (c) 2013, 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 javax.management.JMX;
25 import javax.management.MBeanServer;
26 import javax.management.MBeanServerFactory;
27 import javax.management.NotCompliantMBeanException;
28 import javax.management.ObjectName;
29 
30 /*
31  * @test
32  * @bug 8010285
33  * @summary Tests that javax.management.JMX creates proxies only for the
34  *          compliant MBeans/MXBeans
35  * @author Jaroslav Bachorik
36  * @run clean JMXProxyTest
37  * @run build JMXProxyTest
38  * @run main JMXProxyTest
39  */
40 public class JMXProxyTest {
41     private static interface PrivateMBean {
getInts()42         public int[] getInts();
43     }
44 
45     private static interface PrivateMXBean {
getInts()46         public int[] getInts();
47     }
48 
49     public static class Private implements PrivateMXBean, PrivateMBean {
getInts()50         public int[] getInts() {
51             return new int[]{1,2,3};
52         }
53     }
54 
55     public static interface NonCompliantMBean {
getInt()56         public boolean getInt();
isInt()57         public boolean isInt();
setInt(int a)58         public void setInt(int a);
setInt(long b)59         public void setInt(long b);
60     }
61 
62     public static interface NonCompliantMXBean {
getInt()63         public boolean getInt();
isInt()64         public boolean isInt();
setInt(int a)65         public void setInt(int a);
setInt(long b)66         public void setInt(long b);
67     }
68 
69     public static class NonCompliant implements NonCompliantMXBean, NonCompliantMBean {
getInt()70         public boolean getInt() {
71             return false;
72         }
73 
isInt()74         public boolean isInt() {
75             return true;
76         }
77 
setInt(int a)78         public void setInt(int a) {
79         }
80 
setInt(long b)81         public void setInt(long b) {
82         }
83     }
84 
85     public static interface CompliantMBean {
isFlag()86         public boolean isFlag();
getInt()87         public int getInt();
setInt(int value)88         public void setInt(int value);
89     }
90 
91     public static interface CompliantMXBean {
isFlag()92         public boolean isFlag();
getInt()93         public int getInt();
setInt(int value)94         public void setInt(int value);
95     }
96 
97     public static class Compliant implements CompliantMXBean, CompliantMBean {
isFlag()98         public boolean isFlag() {
99             return false;
100         }
101 
getInt()102         public int getInt() {
103             return 1;
104         }
105 
setInt(int value)106         public void setInt(int value) {
107         }
108     }
109 
110     private static int failures = 0;
111 
main(String[] args)112     public static void main(String[] args) throws Exception {
113         testCompliant(CompliantMBean.class, false);
114         testCompliant(CompliantMXBean.class, true);
115         testNonCompliant(PrivateMBean.class, false);
116         testNonCompliant(PrivateMXBean.class, true);
117         testNonCompliant(NonCompliantMBean.class, false);
118         testNonCompliant(NonCompliantMXBean.class, true);
119 
120         if (failures == 0)
121             System.out.println("Test passed");
122         else
123             throw new Exception("TEST FAILURES: " + failures);
124     }
125 
fail(String msg)126     private static void fail(String msg) {
127         failures++;
128         System.out.println("FAIL: " + msg);
129     }
130 
success(String msg)131     private static void success(String msg) {
132         System.out.println("OK: " + msg);
133     }
134 
testNonCompliant(Class<?> iface, boolean isMx)135     private static void testNonCompliant(Class<?> iface, boolean isMx) throws Exception {
136         try {
137             System.out.println("Creating a proxy for non-compliant " +
138                                (isMx ? "MXBean" : "MBean") + " " +
139                                iface.getName() + " ...");
140 
141             MBeanServer mbs = MBeanServerFactory.newMBeanServer();
142             ObjectName on = new ObjectName("test:type=Proxy");
143 
144             if (isMx) {
145                 JMX.newMXBeanProxy(mbs, on, iface);
146             } else {
147                 JMX.newMBeanProxy(mbs, on, iface);
148             }
149             fail("Created a proxy for non-compliant " +
150                  (isMx ? "MXBean" : "MBean") + " - " + iface.getName());
151         } catch (Exception e) {
152             Throwable t = e;
153             while (t != null && !(t instanceof NotCompliantMBeanException)) {
154                 t = t.getCause();
155             }
156             if (t != null) {
157                 success("Proxy not created");
158             } else {
159                 throw e;
160             }
161         }
162     }
testCompliant(Class<?> iface, boolean isMx)163     private static void testCompliant(Class<?> iface, boolean isMx) throws Exception {
164         try {
165             System.out.println("Creating a proxy for compliant " +
166                                (isMx ? "MXBean" : "MBean") + " " +
167                                iface.getName() + " ...");
168 
169             MBeanServer mbs = MBeanServerFactory.newMBeanServer();
170             ObjectName on = new ObjectName("test:type=Proxy");
171 
172             if (isMx) {
173                 JMX.newMXBeanProxy(mbs, on, iface);
174             } else {
175                 JMX.newMBeanProxy(mbs, on, iface);
176             }
177             success("Created a proxy for compliant " +
178                     (isMx ? "MXBean" : "MBean") + " - " + iface.getName());
179         } catch (Exception e) {
180             Throwable t = e;
181             while (t != null && !(t instanceof NotCompliantMBeanException)) {
182                 t = t.getCause();
183             }
184             if (t != null) {
185                 fail("Proxy not created");
186             } else {
187                 throw e;
188             }
189         }
190     }
191 }
192