Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/AddressableTest.java
38867 views
/*1* Copyright (c) 2005, 2012, 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* @run clean AddressableTest29* @run build AddressableTest30* @run main AddressableTest31*/3233import java.util.*;34import java.net.MalformedURLException;35import java.io.IOException;3637import javax.management.*;38import javax.management.remote.*;39import javax.management.remote.rmi.*;4041public class AddressableTest {42private static final String[] protocols = {"rmi", "iiop"};43private static final String[] prefixes = {"stub", "ior"};4445private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();4647private static boolean isProtocolSupported(String protocol) {48if (protocol.equals("rmi"))49return true;50if (protocol.equals("iiop")) {51try {52Class.forName("javax.management.remote.rmi._RMIConnectionImpl_Tie");53return true;54} catch (ClassNotFoundException x) { }55}56return false;57}5859public static void main(String[] args) throws Exception {60System.out.println(">>> test the new interface Addressable.");61boolean ok = true;6263for (int i = 0; i < protocols.length; i++) {64String protocol = protocols[i];65if (isProtocolSupported(protocol)) {66try {67test(protocol, prefixes[i]);68System.out.println(">>> Test successed for "+protocols[i]);69} catch (Exception e) {70System.out.println(">>> Test failed for "+protocols[i]);71e.printStackTrace(System.out);72ok = false;73}74} else {75System.out.format(">>> Test skipped for %s, protocol not supported%n",76protocol);77}78}7980if (ok) {81System.out.println(">>> All Test passed.");82} else {83System.out.println(">>> Some TESTs FAILED");84throw new RuntimeException("See log for details");85}86}8788public static void test(String proto, String prefix) throws Exception {89JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");90JMXConnectorServer server =91JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);9293server.start();9495JMXServiceURL serverAddr1 = server.getAddress();96System.out.println(">>> Created a server with address "+serverAddr1);9798JMXConnector client1 = JMXConnectorFactory.connect(serverAddr1);99JMXServiceURL clientAddr1 = ((JMXAddressable)client1).getAddress();100101System.out.println(">>> Created a client with address "+clientAddr1);102103if (!serverAddr1.equals(clientAddr1)) {104throw new RuntimeException("The "+proto+" client does not return correct address.");105}106107int i = clientAddr1.toString().indexOf(prefix);108109JMXServiceURL clientAddr2 =110new JMXServiceURL("service:jmx:"+proto+":///"+clientAddr1.toString().substring(i));111112JMXConnector client2 = JMXConnectorFactory.connect(clientAddr2);113114System.out.println(">>> Created a client with address "+clientAddr2);115116if (!clientAddr2.equals(((JMXAddressable)client2).getAddress())) {117throw new RuntimeException("The "+proto+" client does not return correct address.");118}119120System.out.println(">>> The new client's host is "+clientAddr2.getHost()121+", port is "+clientAddr2.getPort());122123client1.close();124client2.close();125server.stop();126}127}128129130