Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/AddressableTest.java
41159 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 623881526* @summary test the new interface Addressable27* @author Shanliang JIANG28*29* @run clean AddressableTest30* @run build AddressableTest31* @run main AddressableTest32*/3334import java.util.*;35import java.net.MalformedURLException;36import java.io.IOException;3738import javax.management.*;39import javax.management.remote.*;40import javax.management.remote.rmi.*;4142public class AddressableTest {43private static final String[] protocols = {"rmi", "iiop"};44private static final String[] prefixes = {"stub", "ior"};4546private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4748private static boolean isProtocolSupported(String protocol) {49if (protocol.equals("rmi"))50return true;51if (protocol.equals("iiop")) {52try {53Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");54return true;55} catch (ClassNotFoundException x) { }56}57return false;58}5960public static void main(String[] args) throws Exception {61System.out.println(">>> test the new interface Addressable.");62boolean ok = true;6364for (int i = 0; i < protocols.length; i++) {65String protocol = protocols[i];66if (isProtocolSupported(protocol)) {67try {68test(protocol, prefixes[i]);69System.out.println(">>> Test successed for "+protocols[i]);70} catch (Exception e) {71System.out.println(">>> Test failed for "+protocols[i]);72e.printStackTrace(System.out);73ok = false;74}75} else {76System.out.format(">>> Test skipped for %s, protocol not supported%n",77protocol);78}79}8081if (ok) {82System.out.println(">>> All Test passed.");83} else {84System.out.println(">>> Some TESTs FAILED");85throw new RuntimeException("See log for details");86}87}8889public static void test(String proto, String prefix) throws Exception {90JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");91JMXConnectorServer server =92JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);9394server.start();9596JMXServiceURL serverAddr1 = server.getAddress();97System.out.println(">>> Created a server with address "+serverAddr1);9899JMXConnector client1 = JMXConnectorFactory.connect(serverAddr1);100JMXServiceURL clientAddr1 = ((JMXAddressable)client1).getAddress();101102System.out.println(">>> Created a client with address "+clientAddr1);103104if (!serverAddr1.equals(clientAddr1)) {105throw new RuntimeException("The "+proto+" client does not return correct address.");106}107108int i = clientAddr1.toString().indexOf(prefix);109110JMXServiceURL clientAddr2 =111new JMXServiceURL("service:jmx:"+proto+":///"+clientAddr1.toString().substring(i));112113JMXConnector client2 = JMXConnectorFactory.connect(clientAddr2);114115System.out.println(">>> Created a client with address "+clientAddr2);116117if (!clientAddr2.equals(((JMXAddressable)client2).getAddress())) {118throw new RuntimeException("The "+proto+" client does not return correct address.");119}120121System.out.println(">>> The new client's host is "+clientAddr2.getHost()122+", port is "+clientAddr2.getPort());123124client1.close();125client2.close();126server.stop();127}128}129130131