0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6296433 6283873
0N/A * @summary Test that inter-MXBean references work as expected.
0N/A * @author Eamonn McManus
0N/A * @run clean MXBeanRefTest
0N/A * @run build MXBeanRefTest
0N/A * @run main MXBeanRefTest
0N/A */
0N/A
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport java.lang.reflect.UndeclaredThrowableException;
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.InstanceAlreadyExistsException;
0N/Aimport javax.management.JMX;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerDelegate;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.MBeanServerInvocationHandler;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.openmbean.OpenDataException;
0N/A
0N/Apublic class MXBeanRefTest {
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A ObjectName productName = new ObjectName("d:type=Product,n=1");
0N/A ObjectName product2Name = new ObjectName("d:type=Product,n=2");
0N/A ObjectName moduleName = new ObjectName("d:type=Module");
0N/A mbs.registerMBean(product, productName);
0N/A mbs.registerMBean(product2, product2Name);
0N/A mbs.registerMBean(module, moduleName);
0N/A ModuleMXBean moduleProxy =
0N/A JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);
0N/A
0N/A ObjectName on;
0N/A on = (ObjectName) mbs.getAttribute(moduleName, "Product");
0N/A check("ObjectName attribute value", on.equals(productName));
0N/A
0N/A ProductMXBean productProxy = moduleProxy.getProduct();
0N/A MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler)
0N/A Proxy.getInvocationHandler(productProxy);
0N/A check("ObjectName in proxy", mbsih.getObjectName().equals(productName));
0N/A
0N/A mbs.setAttribute(moduleName, new Attribute("Product", product2Name));
0N/A ProductMXBean product2Proxy = module.getProduct();
0N/A mbsih = (MBeanServerInvocationHandler)
0N/A Proxy.getInvocationHandler(product2Proxy);
0N/A check("Proxy after setAttribute",
0N/A mbsih.getObjectName().equals(product2Name));
0N/A
0N/A moduleProxy.setProduct(productProxy);
0N/A ProductMXBean productProxyAgain = module.getProduct();
0N/A mbsih = (MBeanServerInvocationHandler)
0N/A Proxy.getInvocationHandler(productProxyAgain);
0N/A check("Proxy after proxied set",
0N/A mbsih.getObjectName().equals(productName));
0N/A
0N/A MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();
0N/A ProductMXBean productProxy2 =
0N/A JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);
0N/A try {
0N/A moduleProxy.setProduct(productProxy2);
0N/A check("Proxy for wrong MBeanServer worked but shouldn't", false);
0N/A } catch (Exception e) {
0N/A if (e instanceof UndeclaredThrowableException &&
0N/A e.getCause() instanceof OpenDataException)
0N/A check("Proxy for wrong MBeanServer correctly rejected", true);
0N/A else {
0N/A e.printStackTrace(System.out);
0N/A check("Proxy for wrong MBeanServer got wrong exception", false);
0N/A }
0N/A }
0N/A
0N/A // Test 6283873
0N/A ObjectName dup = new ObjectName("a:b=c");
0N/A mbs.registerMBean(new MBeanServerDelegate(), dup);
0N/A try {
0N/A mbs.registerMBean(new ProductImpl(), dup);
0N/A check("Duplicate register succeeded but should fail", false);
0N/A } catch (InstanceAlreadyExistsException e) {
0N/A check("Got correct exception from duplicate name", true);
0N/A } catch (Exception e) {
0N/A e.printStackTrace(System.out);
0N/A check("Got wrong exception from duplicate name", false);
0N/A }
0N/A
0N/A if (failure != null)
0N/A throw new Exception("TEST FAILED: " + failure);
0N/A System.out.println("TEST PASSED");
0N/A }
0N/A
0N/A private static void check(String what, boolean ok) {
0N/A if (ok)
0N/A System.out.println("OK: " + what);
0N/A else {
0N/A System.out.println("FAILED: " + what);
0N/A failure = what;
0N/A }
0N/A }
0N/A
0N/A public static interface ProductMXBean {
0N/A ModuleMXBean[] getModules();
0N/A }
0N/A
0N/A public static interface ModuleMXBean {
0N/A ProductMXBean getProduct();
0N/A void setProduct(ProductMXBean p);
0N/A }
0N/A
0N/A public static class ProductImpl implements ProductMXBean {
0N/A public ModuleMXBean[] getModules() {
0N/A return modules;
0N/A }
0N/A }
0N/A
0N/A public static class ModuleImpl implements ModuleMXBean {
0N/A public ModuleImpl(ProductMXBean p) {
0N/A setProduct(p);
0N/A }
0N/A
0N/A public ProductMXBean getProduct() {
0N/A return prod;
0N/A }
0N/A
0N/A public void setProduct(ProductMXBean p) {
0N/A this.prod = p;
0N/A }
0N/A
0N/A private ProductMXBean prod;
0N/A }
0N/A
0N/A private static final ProductMXBean product = new ProductImpl();
0N/A private static final ProductMXBean product2 = new ProductImpl();
0N/A private static final ModuleMXBean module = new ModuleImpl(product);
0N/A private static final ModuleMXBean[] modules = new ModuleMXBean[] {module};
0N/A private static String failure;
0N/A}