Path: blob/master/test/jdk/javax/management/remote/mandatory/connection/RMIConnectionIdTest.java
41159 views
/*1* Copyright (c) 2003, 2016, 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 IP address of a client network interface27* @author Eamonn McManus28*29* @run clean RMIConnectionIdTest30* @run build RMIConnectionIdTest31* @run main RMIConnectionIdTest32*/3334import java.net.*;35import javax.management.*;36import javax.management.remote.*;3738public class RMIConnectionIdTest {39public static void main(String[] args) throws Exception {40System.out.println("Testing that RMI connection id includes " +41"IP address of a client network interface");42MBeanServer mbs = MBeanServerFactory.createMBeanServer();43JMXServiceURL url = new JMXServiceURL("rmi", null, 0);44JMXConnectorServer cs =45JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);46cs.start();47JMXServiceURL addr = cs.getAddress();48JMXConnector cc = JMXConnectorFactory.connect(addr);49String connectionId = cc.getConnectionId();50System.out.println("Got connection id: " + connectionId);51if (!connectionId.startsWith("rmi://")) {52System.out.println("TEST FAILED: does not begin with \"rmi://\"");53System.exit(1);54}55String rest = connectionId.substring("rmi://".length());56int spaceIndex = rest.indexOf(' ');57if (spaceIndex < 0) {58System.out.println("TEST FAILED: no space");59System.exit(1);60}61String clientAddr = rest.substring(0, spaceIndex);62InetAddress clientInetAddr = InetAddress.getByName(clientAddr);63NetworkInterface ni = NetworkInterface.getByInetAddress(clientInetAddr);64if (ni == null) {65System.out.println("TEST FAILS: address not found");66System.exit(1);67}68cc.close();69cs.stop();70System.out.println("Test passed");71}72}737475