Path: blob/master/test/jdk/javax/management/modelmbean/DescriptorSupportSerialTest.java
41152 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 633296226* @summary Test that DescriptorSupport does not serialize targetObject27* @author Eamonn McManus28*29* @run clean DescriptorSupportSerialTest30* @run build DescriptorSupportSerialTest31* @run main DescriptorSupportSerialTest32*/3334import java.lang.reflect.Method;35import javax.management.*;36import javax.management.remote.*;37import javax.management.modelmbean.*;3839public class DescriptorSupportSerialTest {40public static void main(String[] args) throws Exception {41if (java.io.Serializable.class.isAssignableFrom(Thread.class))42throw new Exception("TEST BAD: Thread is Serializable!");43Method getName = Thread.class.getMethod("getName");44Descriptor d = new DescriptorSupport();45d.setField("name", "getName");46d.setField("descriptorType", "operation");47d.setField("TARGETObject", Thread.currentThread());48d.setField("foo", "bar");49ModelMBeanOperationInfo getNameInfo =50new ModelMBeanOperationInfo("Get name", getName, d);51ModelMBeanInfo mmbi =52new ModelMBeanInfoSupport(Thread.class.getName(),53"Thread!",54null, // no attributes55null, // no constructors56new ModelMBeanOperationInfo[] {getNameInfo},57null); // no notifications58ModelMBean mmb = new RequiredModelMBean(mmbi);5960ObjectName on = new ObjectName("d:type=Thread");61MBeanServer mbs = MBeanServerFactory.newMBeanServer();62mbs.registerMBean(mmb, on);6364JMXServiceURL url = new JMXServiceURL("rmi", null, 0);65JMXConnectorServer cs =66JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);67cs.start();68JMXServiceURL addr = cs.getAddress();69JMXConnector cc = JMXConnectorFactory.connect(addr);70MBeanServerConnection mbsc = cc.getMBeanServerConnection();7172try {73test(mbsc, on);74System.out.println("TEST PASSED");75} finally {76cc.close();77cs.stop();78}79}8081private static void test(MBeanServerConnection mbsc, ObjectName on)82throws Exception {83ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbsc.getMBeanInfo(on);84// previous line will fail if serialization includes targetObject8586Descriptor d2 = mmbi2.getDescriptor("getName", "operation");87if (!"bar".equals(d2.getFieldValue("foo")))88throw new Exception("TEST FAILED: bad descriptor: " + d2);8990String name = (String) mbsc.invoke(on, "getName", null, null);91String thisName = Thread.currentThread().getName();92if (!thisName.equals(name)) {93throw new Exception("TEST FAILED: wrong thread name: " +94name + " should be " + thisName);95}96}97}9899100