Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/connection/RMIConnectionIdTest.java
38867 views
/*1* Copyright (c) 2003, 2013, 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 4901808 718380026* @summary Check that RMI connection ids include client host name27* @author Eamonn McManus28* @run clean RMIConnectionIdTest29* @run build RMIConnectionIdTest30* @run main RMIConnectionIdTest31*/3233import java.net.*;34import javax.management.*;35import javax.management.remote.*;3637public class RMIConnectionIdTest {38public static void main(String[] args) throws Exception {39System.out.println("Testing that RMI connection id includes " +40"client host name");41MBeanServer mbs = MBeanServerFactory.createMBeanServer();42JMXServiceURL url = new JMXServiceURL("rmi", null, 0);43JMXConnectorServer cs =44JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);45cs.start();46JMXServiceURL addr = cs.getAddress();47JMXConnector cc = JMXConnectorFactory.connect(addr);48String connectionId = cc.getConnectionId();49System.out.println("Got connection id: " + connectionId);50if (!connectionId.startsWith("rmi://")) {51System.out.println("TEST FAILED: does not begin with \"rmi://\"");52System.exit(1);53}54String rest = connectionId.substring("rmi://".length());55int spaceIndex = rest.indexOf(' ');56if (spaceIndex < 0) {57System.out.println("TEST FAILED: no space");58System.exit(1);59}60String clientAddr = rest.substring(0, spaceIndex);61InetAddress clientInetAddr = InetAddress.getByName(clientAddr);62InetAddress localAddr = clientInetAddr.isLoopbackAddress() ? InetAddress.getLoopbackAddress() : InetAddress.getLocalHost();63System.out.println("InetAddresses: local=" + localAddr + "; " +64"connectionId=" + clientInetAddr);65if (!localAddr.equals(clientInetAddr)) {66System.out.println("TEST FAILS: addresses differ");67System.exit(1);68}69cc.close();70cs.stop();71System.out.println("Test passed");72}73}747576