Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/mxbean/MXBeanRefTest.java
38841 views
/*1* Copyright (c) 2005, 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* @run clean MXBeanRefTest29* @run build MXBeanRefTest30* @run main MXBeanRefTest31*/3233import java.lang.reflect.Proxy;34import java.lang.reflect.UndeclaredThrowableException;35import javax.management.Attribute;36import javax.management.InstanceAlreadyExistsException;37import javax.management.JMX;38import javax.management.MBeanServer;39import javax.management.MBeanServerDelegate;40import javax.management.MBeanServerFactory;41import javax.management.MBeanServerInvocationHandler;42import javax.management.ObjectName;43import javax.management.openmbean.OpenDataException;4445public class MXBeanRefTest {46public static void main(String[] args) throws Exception {47MBeanServer mbs = MBeanServerFactory.createMBeanServer();48ObjectName productName = new ObjectName("d:type=Product,n=1");49ObjectName product2Name = new ObjectName("d:type=Product,n=2");50ObjectName moduleName = new ObjectName("d:type=Module");51mbs.registerMBean(product, productName);52mbs.registerMBean(product2, product2Name);53mbs.registerMBean(module, moduleName);54ModuleMXBean moduleProxy =55JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);5657ObjectName on;58on = (ObjectName) mbs.getAttribute(moduleName, "Product");59check("ObjectName attribute value", on.equals(productName));6061ProductMXBean productProxy = moduleProxy.getProduct();62MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)63Proxy.getInvocationHandler(productProxy);64check("ObjectName in proxy", mbsih.getObjectName().equals(productName));6566mbs.setAttribute(moduleName, new Attribute("Product", product2Name));67ProductMXBean product2Proxy = module.getProduct();68mbsih = (MBeanServerInvocationHandler)69Proxy.getInvocationHandler(product2Proxy);70check("Proxy after setAttribute",71mbsih.getObjectName().equals(product2Name));7273moduleProxy.setProduct(productProxy);74ProductMXBean productProxyAgain = module.getProduct();75mbsih = (MBeanServerInvocationHandler)76Proxy.getInvocationHandler(productProxyAgain);77check("Proxy after proxied set",78mbsih.getObjectName().equals(productName));7980MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();81ProductMXBean productProxy2 =82JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);83try {84moduleProxy.setProduct(productProxy2);85check("Proxy for wrong MBeanServer worked but shouldn't", false);86} catch (Exception e) {87if (e instanceof UndeclaredThrowableException &&88e.getCause() instanceof OpenDataException)89check("Proxy for wrong MBeanServer correctly rejected", true);90else {91e.printStackTrace(System.out);92check("Proxy for wrong MBeanServer got wrong exception", false);93}94}9596// Test 628387397ObjectName dup = new ObjectName("a:b=c");98mbs.registerMBean(new MBeanServerDelegate(), dup);99try {100mbs.registerMBean(new ProductImpl(), dup);101check("Duplicate register succeeded but should fail", false);102} catch (InstanceAlreadyExistsException e) {103check("Got correct exception from duplicate name", true);104} catch (Exception e) {105e.printStackTrace(System.out);106check("Got wrong exception from duplicate name", false);107}108109if (failure != null)110throw new Exception("TEST FAILED: " + failure);111System.out.println("TEST PASSED");112}113114private static void check(String what, boolean ok) {115if (ok)116System.out.println("OK: " + what);117else {118System.out.println("FAILED: " + what);119failure = what;120}121}122123public static interface ProductMXBean {124ModuleMXBean[] getModules();125}126127public static interface ModuleMXBean {128ProductMXBean getProduct();129void setProduct(ProductMXBean p);130}131132public static class ProductImpl implements ProductMXBean {133public ModuleMXBean[] getModules() {134return modules;135}136}137138public static class ModuleImpl implements ModuleMXBean {139public ModuleImpl(ProductMXBean p) {140setProduct(p);141}142143public ProductMXBean getProduct() {144return prod;145}146147public void setProduct(ProductMXBean p) {148this.prod = p;149}150151private ProductMXBean prod;152}153154private static final ProductMXBean product = new ProductImpl();155private static final ProductMXBean product2 = new ProductImpl();156private static final ModuleMXBean module = new ModuleImpl(product);157private static final ModuleMXBean[] modules = new ModuleMXBean[] {module};158private static String failure;159}160161162