J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 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 6296433 6283873 |
| 27 | * @summary Test that inter-MXBean references work as expected. |
| 28 | * @author Eamonn McManus |
| 29 | * @run clean MXBeanRefTest |
| 30 | * @run build MXBeanRefTest |
| 31 | * @run main MXBeanRefTest |
| 32 | */ |
| 33 | |
| 34 | import java.lang.reflect.Proxy; |
| 35 | import java.lang.reflect.UndeclaredThrowableException; |
| 36 | import javax.management.Attribute; |
| 37 | import javax.management.InstanceAlreadyExistsException; |
| 38 | import javax.management.JMX; |
| 39 | import javax.management.MBeanServer; |
| 40 | import javax.management.MBeanServerDelegate; |
| 41 | import javax.management.MBeanServerFactory; |
| 42 | import javax.management.MBeanServerInvocationHandler; |
| 43 | import javax.management.ObjectName; |
| 44 | import javax.management.openmbean.OpenDataException; |
| 45 | |
| 46 | public class MXBeanRefTest { |
| 47 | public static void main(String[] args) throws Exception { |
| 48 | MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
| 49 | ObjectName productName = new ObjectName("d:type=Product,n=1"); |
| 50 | ObjectName product2Name = new ObjectName("d:type=Product,n=2"); |
| 51 | ObjectName moduleName = new ObjectName("d:type=Module"); |
| 52 | mbs.registerMBean(product, productName); |
| 53 | mbs.registerMBean(product2, product2Name); |
| 54 | mbs.registerMBean(module, moduleName); |
| 55 | ModuleMXBean moduleProxy = |
| 56 | JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class); |
| 57 | |
| 58 | ObjectName on; |
| 59 | on = (ObjectName) mbs.getAttribute(moduleName, "Product"); |
| 60 | check("ObjectName attribute value", on.equals(productName)); |
| 61 | |
| 62 | ProductMXBean productProxy = moduleProxy.getProduct(); |
| 63 | MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler) |
| 64 | Proxy.getInvocationHandler(productProxy); |
| 65 | check("ObjectName in proxy", mbsih.getObjectName().equals(productName)); |
| 66 | |
| 67 | mbs.setAttribute(moduleName, new Attribute("Product", product2Name)); |
| 68 | ProductMXBean product2Proxy = module.getProduct(); |
| 69 | mbsih = (MBeanServerInvocationHandler) |
| 70 | Proxy.getInvocationHandler(product2Proxy); |
| 71 | check("Proxy after setAttribute", |
| 72 | mbsih.getObjectName().equals(product2Name)); |
| 73 | |
| 74 | moduleProxy.setProduct(productProxy); |
| 75 | ProductMXBean productProxyAgain = module.getProduct(); |
| 76 | mbsih = (MBeanServerInvocationHandler) |
| 77 | Proxy.getInvocationHandler(productProxyAgain); |
| 78 | check("Proxy after proxied set", |
| 79 | mbsih.getObjectName().equals(productName)); |
| 80 | |
| 81 | MBeanServer mbs2 = MBeanServerFactory.createMBeanServer(); |
| 82 | ProductMXBean productProxy2 = |
| 83 | JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class); |
| 84 | try { |
| 85 | moduleProxy.setProduct(productProxy2); |
| 86 | check("Proxy for wrong MBeanServer worked but shouldn't", false); |
| 87 | } catch (Exception e) { |
| 88 | if (e instanceof UndeclaredThrowableException && |
| 89 | e.getCause() instanceof OpenDataException) |
| 90 | check("Proxy for wrong MBeanServer correctly rejected", true); |
| 91 | else { |
| 92 | e.printStackTrace(System.out); |
| 93 | check("Proxy for wrong MBeanServer got wrong exception", false); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Test 6283873 |
| 98 | ObjectName dup = new ObjectName("a:b=c"); |
| 99 | mbs.registerMBean(new MBeanServerDelegate(), dup); |
| 100 | try { |
| 101 | mbs.registerMBean(new ProductImpl(), dup); |
| 102 | check("Duplicate register succeeded but should fail", false); |
| 103 | } catch (InstanceAlreadyExistsException e) { |
| 104 | check("Got correct exception from duplicate name", true); |
| 105 | } catch (Exception e) { |
| 106 | e.printStackTrace(System.out); |
| 107 | check("Got wrong exception from duplicate name", false); |
| 108 | } |
| 109 | |
| 110 | if (failure != null) |
| 111 | throw new Exception("TEST FAILED: " + failure); |
| 112 | System.out.println("TEST PASSED"); |
| 113 | } |
| 114 | |
| 115 | private static void check(String what, boolean ok) { |
| 116 | if (ok) |
| 117 | System.out.println("OK: " + what); |
| 118 | else { |
| 119 | System.out.println("FAILED: " + what); |
| 120 | failure = what; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static interface ProductMXBean { |
| 125 | ModuleMXBean[] getModules(); |
| 126 | } |
| 127 | |
| 128 | public static interface ModuleMXBean { |
| 129 | ProductMXBean getProduct(); |
| 130 | void setProduct(ProductMXBean p); |
| 131 | } |
| 132 | |
| 133 | public static class ProductImpl implements ProductMXBean { |
| 134 | public ModuleMXBean[] getModules() { |
| 135 | return modules; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public static class ModuleImpl implements ModuleMXBean { |
| 140 | public ModuleImpl(ProductMXBean p) { |
| 141 | setProduct(p); |
| 142 | } |
| 143 | |
| 144 | public ProductMXBean getProduct() { |
| 145 | return prod; |
| 146 | } |
| 147 | |
| 148 | public void setProduct(ProductMXBean p) { |
| 149 | this.prod = p; |
| 150 | } |
| 151 | |
| 152 | private ProductMXBean prod; |
| 153 | } |
| 154 | |
| 155 | private static final ProductMXBean product = new ProductImpl(); |
| 156 | private static final ProductMXBean product2 = new ProductImpl(); |
| 157 | private static final ModuleMXBean module = new ModuleImpl(product); |
| 158 | private static final ModuleMXBean[] modules = new ModuleMXBean[] {module}; |
| 159 | private static String failure; |
| 160 | } |