Path: blob/master/test/jdk/javax/management/mxbean/MXBeanRefTest.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6296433 628387326* @summary Test that inter-MXBean references work as expected.27* @author Eamonn McManus28*29* @run clean MXBeanRefTest30* @run build MXBeanRefTest31* @run main MXBeanRefTest32*/3334import java.lang.reflect.Proxy;35import java.lang.reflect.UndeclaredThrowableException;36import javax.management.Attribute;37import javax.management.InstanceAlreadyExistsException;38import javax.management.JMX;39import javax.management.MBeanServer;40import javax.management.MBeanServerDelegate;41import javax.management.MBeanServerFactory;42import javax.management.MBeanServerInvocationHandler;43import javax.management.ObjectName;44import javax.management.openmbean.OpenDataException;4546public class MXBeanRefTest {47public static void main(String[] args) throws Exception {48MBeanServer mbs = MBeanServerFactory.createMBeanServer();49ObjectName productName = new ObjectName("d:type=Product,n=1");50ObjectName product2Name = new ObjectName("d:type=Product,n=2");51ObjectName moduleName = new ObjectName("d:type=Module");52mbs.registerMBean(product, productName);53mbs.registerMBean(product2, product2Name);54mbs.registerMBean(module, moduleName);55ModuleMXBean moduleProxy =56JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);5758ObjectName on;59on = (ObjectName) mbs.getAttribute(moduleName, "Product");60check("ObjectName attribute value", on.equals(productName));6162ProductMXBean productProxy = moduleProxy.getProduct();63MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)64Proxy.getInvocationHandler(productProxy);65check("ObjectName in proxy", mbsih.getObjectName().equals(productName));6667mbs.setAttribute(moduleName, new Attribute("Product", product2Name));68ProductMXBean product2Proxy = module.getProduct();69mbsih = (MBeanServerInvocationHandler)70Proxy.getInvocationHandler(product2Proxy);71check("Proxy after setAttribute",72mbsih.getObjectName().equals(product2Name));7374moduleProxy.setProduct(productProxy);75ProductMXBean productProxyAgain = module.getProduct();76mbsih = (MBeanServerInvocationHandler)77Proxy.getInvocationHandler(productProxyAgain);78check("Proxy after proxied set",79mbsih.getObjectName().equals(productName));8081MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();82ProductMXBean productProxy2 =83JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);84try {85moduleProxy.setProduct(productProxy2);86check("Proxy for wrong MBeanServer worked but shouldn't", false);87} catch (Exception e) {88if (e instanceof UndeclaredThrowableException &&89e.getCause() instanceof OpenDataException)90check("Proxy for wrong MBeanServer correctly rejected", true);91else {92e.printStackTrace(System.out);93check("Proxy for wrong MBeanServer got wrong exception", false);94}95}9697// Test 628387398ObjectName dup = new ObjectName("a:b=c");99mbs.registerMBean(new MBeanServerDelegate(), dup);100try {101mbs.registerMBean(new ProductImpl(), dup);102check("Duplicate register succeeded but should fail", false);103} catch (InstanceAlreadyExistsException e) {104check("Got correct exception from duplicate name", true);105} catch (Exception e) {106e.printStackTrace(System.out);107check("Got wrong exception from duplicate name", false);108}109110if (failure != null)111throw new Exception("TEST FAILED: " + failure);112System.out.println("TEST PASSED");113}114115private static void check(String what, boolean ok) {116if (ok)117System.out.println("OK: " + what);118else {119System.out.println("FAILED: " + what);120failure = what;121}122}123124public static interface ProductMXBean {125ModuleMXBean[] getModules();126}127128public static interface ModuleMXBean {129ProductMXBean getProduct();130void setProduct(ProductMXBean p);131}132133public static class ProductImpl implements ProductMXBean {134public ModuleMXBean[] getModules() {135return modules;136}137}138139public static class ModuleImpl implements ModuleMXBean {140public ModuleImpl(ProductMXBean p) {141setProduct(p);142}143144public ProductMXBean getProduct() {145return prod;146}147148public void setProduct(ProductMXBean p) {149this.prod = p;150}151152private ProductMXBean prod;153}154155private static final ProductMXBean product = new ProductImpl();156private static final ProductMXBean product2 = new ProductImpl();157private static final ModuleMXBean module = new ModuleImpl(product);158private static final ModuleMXBean[] modules = new ModuleMXBean[] {module};159private static String failure;160}161162163