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