Path: blob/master/test/jdk/java/rmi/Naming/legalRegistryNames/LegalRegistryNames.java
41155 views
/*1* Copyright (c) 1999, 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 425480826* @summary Naming assumes '/' is present in relative URL; change in URL causes regression27* @author Dana Burns28* @library ../../testlibrary29* @modules java.rmi/sun.rmi.registry30* java.rmi/sun.rmi.server31* java.rmi/sun.rmi.transport32* java.rmi/sun.rmi.transport.tcp33* @build TestLibrary Legal LegalRegistryNames_Stub34* @run main/othervm LegalRegistryNames35* @key intermittent36*/3738import java.net.InetAddress;39import java.net.UnknownHostException;40import java.rmi.Naming;41import java.rmi.RemoteException;42import java.rmi.Remote;43import java.rmi.registry.LocateRegistry;44import java.rmi.registry.Registry;45import java.rmi.server.UnicastRemoteObject;46import java.util.Enumeration;47import java.util.Vector;4849/**50* Ensure that all legal forms of Naming URLs operate with the51* java.rmi.Naming interface. This test requires using the default RMI Registry52* port as it tests all of the RMI naming URL's, including the ones which do not53* take a port (and therefore uses the default port).54*/55public class LegalRegistryNames extends UnicastRemoteObject56implements Legal57{5859public LegalRegistryNames() throws java.rmi.RemoteException {}6061public static void main(String args[]) throws RuntimeException {6263System.err.println("\nRegression test for bug/rfe 4254808\n");6465Registry registry = null;66LegalRegistryNames legal = null;6768boolean oneFormFailed = false;69String[] names = null;70Vector legalForms = getLegalForms();71Remote shouldFind = null;7273// create a registry and the test object74try {75legal = new LegalRegistryNames();7677System.err.println("Starting registry on default port");78registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);79} catch (Exception e) {80TestLibrary.bomb("registry already running on test port");81}8283// enumerate through all legal URLs to verify that a remote84// object can be bound and unbound85String s = null;86Enumeration en = legalForms.elements();87while (en.hasMoreElements()) {88s = (String) en.nextElement();8990System.err.println("\ntesting form: " + s);9192try {93Naming.rebind(s, legal);94names = registry.list();9596// ensure that the name in the registry is what is expected97if ((names.length > 0) &&98(names[0].compareTo("MyName") != 0))99{100oneFormFailed = true;101System.err.println("\tRegistry entry for form: " +102s + " is incorrect: " + names[0]);103}104105// ensure that the object can be unbound under the URL string106shouldFind = Naming.lookup(s);107Naming.unbind(s);108System.err.println("\tform " + s + " OK");109110} catch (Exception e) {111112e.printStackTrace();113oneFormFailed = true;114System.err.println("\tunexpected lookup or unbind " +115"exception for form: " + s + e.getMessage() );116}117}118if (oneFormFailed) {119TestLibrary.bomb("Test failed");120}121122// get the test to exit quickly123TestLibrary.unexport(legal);124}125126/**127* return a vector of valid legal RMI naming URLs.128*/129private static Vector getLegalForms() {130String localHostAddress = null;131String localHostName = null;132133// get the local host name and address134try {135localHostName = InetAddress.getLocalHost().getHostName();136localHostAddress = InetAddress.getLocalHost().getHostAddress();137} catch(UnknownHostException e) {138TestLibrary.bomb("Test failed: unexpected exception", e);139}140141Vector legalForms = new Vector();142legalForms.add("///MyName");143legalForms.add("//:" + Registry.REGISTRY_PORT + "/MyName");144legalForms.add("//" + localHostAddress + "/MyName");145legalForms.add("//" + localHostAddress + ":" +146Registry.REGISTRY_PORT + "/MyName");147legalForms.add("//localhost/MyName");148legalForms.add("//localhost:" + Registry.REGISTRY_PORT + "/MyName");149legalForms.add("//" + localHostName + "/MyName");150legalForms.add("//" + localHostName + ":" + Registry.REGISTRY_PORT +151"/MyName");152legalForms.add("MyName");153legalForms.add("/MyName");154legalForms.add("rmi:///MyName");155legalForms.add("rmi://:" + Registry.REGISTRY_PORT + "/MyName");156legalForms.add("rmi://" + localHostAddress + "/MyName");157legalForms.add("rmi://" + localHostAddress + ":" +158Registry.REGISTRY_PORT + "/MyName");159legalForms.add("rmi://localhost/MyName");160legalForms.add("rmi://localhost:" + Registry.REGISTRY_PORT + "/MyName");161legalForms.add("rmi://" + localHostName + "/MyName");162legalForms.add("rmi://" + localHostName + ":" +163Registry.REGISTRY_PORT + "/MyName");164return legalForms;165}166}167168169