Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/DescriptorSupportSerialTest.java
38840 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 633296226* @summary Test that DescriptorSupport does not serialize targetObject27* @author Eamonn McManus28* @run clean DescriptorSupportSerialTest29* @run build DescriptorSupportSerialTest30* @run main DescriptorSupportSerialTest31*/3233import java.lang.reflect.Method;34import javax.management.*;35import javax.management.remote.*;36import javax.management.modelmbean.*;3738public class DescriptorSupportSerialTest {39public static void main(String[] args) throws Exception {40if (java.io.Serializable.class.isAssignableFrom(Thread.class))41throw new Exception("TEST BAD: Thread is Serializable!");42Method getName = Thread.class.getMethod("getName");43Descriptor d = new DescriptorSupport();44d.setField("name", "getName");45d.setField("descriptorType", "operation");46d.setField("TARGETObject", Thread.currentThread());47d.setField("foo", "bar");48ModelMBeanOperationInfo getNameInfo =49new ModelMBeanOperationInfo("Get name", getName, d);50ModelMBeanInfo mmbi =51new ModelMBeanInfoSupport(Thread.class.getName(),52"Thread!",53null, // no attributes54null, // no constructors55new ModelMBeanOperationInfo[] {getNameInfo},56null); // no notifications57ModelMBean mmb = new RequiredModelMBean(mmbi);5859ObjectName on = new ObjectName("d:type=Thread");60MBeanServer mbs = MBeanServerFactory.newMBeanServer();61mbs.registerMBean(mmb, on);6263JMXServiceURL url = new JMXServiceURL("rmi", null, 0);64JMXConnectorServer cs =65JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);66cs.start();67JMXServiceURL addr = cs.getAddress();68JMXConnector cc = JMXConnectorFactory.connect(addr);69MBeanServerConnection mbsc = cc.getMBeanServerConnection();7071try {72test(mbsc, on);73System.out.println("TEST PASSED");74} finally {75cc.close();76cs.stop();77}78}7980private static void test(MBeanServerConnection mbsc, ObjectName on)81throws Exception {82ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbsc.getMBeanInfo(on);83// previous line will fail if serialization includes targetObject8485Descriptor d2 = mmbi2.getDescriptor("getName", "operation");86if (!"bar".equals(d2.getFieldValue("foo")))87throw new Exception("TEST FAILED: bad descriptor: " + d2);8889String name = (String) mbsc.invoke(on, "getName", null, null);90String thisName = Thread.currentThread().getName();91if (!thisName.equals(name)) {92throw new Exception("TEST FAILED: wrong thread name: " +93name + " should be " + thisName);94}95}96}979899