Path: blob/master/test/jdk/javax/management/proxy/ProxyObjectMethodsTest.java
41149 views
/*1* Copyright (c) 2004, 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 617752426* @summary Test how to execute the 3 Object methods by a Proxy.27* @author Shanliang JIANG28*29* @run clean ProxyObjectMethodsTest30* @run build ProxyObjectMethodsTest31* @run main ProxyObjectMethodsTest32*/3334import java.lang.management.ManagementFactory;35import java.lang.reflect.*;36import java.util.*;3738import javax.management.*;39import javax.management.remote.*;4041public class ProxyObjectMethodsTest {4243public static void main(String[] args) throws Exception {44System.out.println("<<< Test how to execute the 3 Object methods by a Proxy.");4546MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();47final ObjectName name = new ObjectName(":class=Simple");4849JMXServiceURL url = new JMXServiceURL("rmi", null, 0);50final JMXConnectorServer server =51JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);52server.start();53url = server.getAddress();5455final JMXConnector client = JMXConnectorFactory.connect(url);5657System.out.println("<<< Test the methods at local side.");5859final Simple simple = new Simple();60mbs.registerMBean(simple, name);6162SimpleMBean simple0 =63MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),64name,65SimpleMBean.class,66false);6768SimpleMBean simple1 =69MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),70name,71SimpleMBean.class,72false);7374Simplest simple3 =75MBeanServerInvocationHandler.newProxyInstance(client.getMBeanServerConnection(),76name,77Simplest.class,78false);7980if (!simple0.equals(simple1) ||81simple0.equals(simple) ||82simple0.equals(simple3)) {83throw new RuntimeException("The method equals does not work correctly.");84}8586if (simple0.hashCode() != simple1.hashCode() ||87simple.hashCode() == simple0.hashCode()) {88throw new RuntimeException("The method hashCode does not work correctly.");89}9091if (!simple0.toString().equals(simple1.toString()) ||92simple.toString().equals(simple0.toString())) {93throw new RuntimeException("The method toString does not work correctly.");94}9596/* Sorry about this. This is the equals(String) method,97which returns String, not boolean. */98if (!simple0.equals("foo").equals("foo"))99throw new RuntimeException("The method equals(String) was not forwarded.");100101ArrayList al = new ArrayList();102al.add(simple0);103104if (!al.contains(simple0) || !al.contains(simple1)) {105throw new RuntimeException("Cannot find correctly a proxy in an ArrayList.");106}107108System.out.println("<<< Test whether the methods are done at server side.");109110final ObjectName name1 = new ObjectName(":class=Test");111mbs.registerMBean(new Test(), name1);112113TestMBean test0 = MBeanServerInvocationHandler.newProxyInstance(mbs,114name1,115TestMBean.class,116false);117118if(test0.equals(test0)) {119throw new RuntimeException("The method equals is not done remotely as expected.");120}121122if (!test0.toString().equals("Test-toString")) {123throw new RuntimeException("The method toString is not done remotely as expected.");124}125126if (test0.hashCode() != 123) {127throw new RuntimeException("The method hashCode is not done remotely as expected.");128}129130System.out.println("<<< Test on using a null connection or a null name.");131SimpleMBean simple2;132try {133simple2 = MBeanServerInvocationHandler.newProxyInstance(null,134name,135SimpleMBean.class,136false);137throw new RuntimeException(138"Null connection does not cause an IllegalArgumentException.");139} catch (IllegalArgumentException ie) {140// as expected141}142143try {144simple2 = MBeanServerInvocationHandler.newProxyInstance(mbs,145null,146SimpleMBean.class,147false);148throw new RuntimeException(149"Null object name does not cause an IllegalArgumentException.");150} catch (IllegalArgumentException ie) {151// as expected152}153}154155public static interface Simplest {156157}158159public static interface SimpleMBean extends Simplest {160public String equals(String x);161}162163private static class Simple implements SimpleMBean {164public String equals(String x) {165return x;166}167}168169public static interface TestMBean {170public boolean equals(Object o);171172public String toString();173174public int hashCode();175}176177private static class Test implements TestMBean {178public boolean equals(Object o) {179// what can do here?180181return false;182}183184public String toString() {185return "Test-toString";186}187188public int hashCode() {189return 123;190}191}192}193194195