Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/proxy/ProxyObjectMethodsTest.java
38838 views
/*1* Copyright (c) 2004, 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 617752426* @summary Test how to execute the 3 Object methods by a Proxy.27* @author Shanliang JIANG28* @run clean ProxyObjectMethodsTest29* @run build ProxyObjectMethodsTest30* @run main ProxyObjectMethodsTest31*/3233import java.lang.management.ManagementFactory;34import java.lang.reflect.*;35import java.util.*;3637import javax.management.*;38import javax.management.remote.*;3940public class ProxyObjectMethodsTest {4142public static void main(String[] args) throws Exception {43System.out.println("<<< Test how to execute the 3 Object methods by a Proxy.");4445MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();46final ObjectName name = new ObjectName(":class=Simple");4748JMXServiceURL url = new JMXServiceURL("rmi", null, 0);49final JMXConnectorServer server =50JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);51server.start();52url = server.getAddress();5354final JMXConnector client = JMXConnectorFactory.connect(url);5556System.out.println("<<< Test the methods at local side.");5758final Simple simple = new Simple();59mbs.registerMBean(simple, name);6061SimpleMBean simple0 =62MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),63name,64SimpleMBean.class,65false);6667SimpleMBean simple1 =68MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),69name,70SimpleMBean.class,71false);7273Simplest simple3 =74MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),75name,76Simplest.class,77false);7879if (!simple0.equals(simple1) ||80simple0.equals(simple) ||81simple0.equals(simple3)) {82throw new RuntimeException("The method equals does not work correctly.");83}8485if (simple0.hashCode() != simple1.hashCode() ||86simple.hashCode() == simple0.hashCode()) {87throw new RuntimeException("The method hashCode does not work correctly.");88}8990if (!simple0.toString().equals(simple1.toString()) ||91simple.toString().equals(simple0.toString())) {92throw new RuntimeException("The method toString does not work correctly.");93}9495/* Sorry about this. This is the equals(String) method,96which returns String, not boolean. */97if (!simple0.equals("foo").equals("foo"))98throw new RuntimeException("The method equals(String) was not forwarded.");99100ArrayList al = new ArrayList();101al.add(simple0);102103if (!al.contains(simple0) || !al.contains(simple1)) {104throw new RuntimeException("Cannot find correctly a proxy in an ArrayList.");105}106107System.out.println("<<< Test whether the methods are done at server side.");108109final ObjectName name1 = new ObjectName(":class=Test");110mbs.registerMBean(new Test(), name1);111112TestMBean test0 = MBeanServerInvocationHandler.newProxyInstance(mbs,113name1,114TestMBean.class,115false);116117if(test0.equals(test0)) {118throw new RuntimeException("The method equals is not done remotely as expected.");119}120121if (!test0.toString().equals("Test-toString")) {122throw new RuntimeException("The method toString is not done remotely as expected.");123}124125if (test0.hashCode() != 123) {126throw new RuntimeException("The method hashCode is not done remotely as expected.");127}128129System.out.println("<<< Test on using a null connection or a null name.");130SimpleMBean simple2;131try {132simple2 = MBeanServerInvocationHandler.newProxyInstance(null,133name,134SimpleMBean.class,135false);136throw new RuntimeException(137"Null connection does not cause an IllegalArgumentException.");138} catch (IllegalArgumentException ie) {139// as expected140}141142try {143simple2 = MBeanServerInvocationHandler.newProxyInstance(mbs,144null,145SimpleMBean.class,146false);147throw new RuntimeException(148"Null object name does not cause an IllegalArgumentException.");149} catch (IllegalArgumentException ie) {150// as expected151}152}153154public static interface Simplest {155156}157158public static interface SimpleMBean extends Simplest {159public String equals(String x);160}161162private static class Simple implements SimpleMBean {163public String equals(String x) {164return x;165}166}167168public static interface TestMBean {169public boolean equals(Object o);170171public String toString();172173public int hashCode();174}175176private static class Test implements TestMBean {177public boolean equals(Object o) {178// what can do here?179180return false;181}182183public String toString() {184return "Test-toString";185}186187public int hashCode() {188return 123;189}190}191}192193194