0N/A/*
2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
5551N/A/*
0N/A * @test
0N/A * @bug 4251010
0N/A * @summary equals does not works on stub objects created with
0N/A * custom socket AndFactory
5551N/A * @author Laird Dornin
0N/A *
5551N/A * @library ../../../testlibrary
5551N/A * @build TestLibrary
0N/A * @run main/othervm/timeout=40 VerifyRemoteEquals
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.server.*;
0N/A
0N/A/**
0N/A * Test ensures that a stub that has never been serialized and one
0N/A * that has can be .equals(). Test also ensures that stubs with
0N/A * custom socket factories can be .equals() with equivalent stubs.
0N/A */
0N/Apublic class VerifyRemoteEquals {
0N/A
0N/A /**
0N/A * Remote interface.
0N/A */
0N/A public interface Test extends Remote {
0N/A }
0N/A
0N/A /**
0N/A * Implementation of Remote interface passing custom socket
0N/A * factories
0N/A */
0N/A public static final class TestImpl
0N/A extends UnicastRemoteObject implements Test
0N/A {
0N/A public TestImpl() throws RemoteException {
0N/A super();
0N/A }
0N/A
0N/A public TestImpl(RMIClientSocketFactory clientFactory,
0N/A RMIServerSocketFactory serverFactory)
0N/A throws RemoteException
0N/A {
0N/A
0N/A super(0, clientFactory, serverFactory);
0N/A }
0N/A
0N/A public TestImpl(RMISocketFactory factory)
0N/A throws RemoteException
0N/A {
0N/A super(0, factory, factory);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Remote interface for retrieving Test object.
0N/A */
0N/A public interface TestHome extends Remote {
0N/A public Test get() throws RemoteException;
0N/A }
0N/A
0N/A /**
0N/A * Implementation of interface TestHome.
0N/A */
0N/A public static final class TestHomeImpl
0N/A extends UnicastRemoteObject implements TestHome
0N/A {
0N/A private Test test;
0N/A
0N/A public TestHomeImpl(Test test)
0N/A throws RemoteException {
0N/A
0N/A super();
0N/A
0N/A this.test = test;
0N/A }
0N/A
0N/A public Test get() {
0N/A return test;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Custom server socket factory.
0N/A */
0N/A public static final class ServerSocketAndFactory
0N/A extends ServerSocket implements RMIServerSocketFactory, Serializable
0N/A {
0N/A ServerSocketAndFactory() throws IOException, java.net.UnknownHostException {
0N/A // I am forced to do something useless with the parent
0N/A // constructor
0N/A super(0);
0N/A }
0N/A ServerSocketAndFactory(int port) throws IOException,
0N/A java.net.UnknownHostException
0N/A {
0N/A super(port);
0N/A }
0N/A
0N/A public ServerSocket createServerSocket(int port)
0N/A throws IOException
0N/A {
0N/A
0N/A return new ServerSocketAndFactory(port);
0N/A }
0N/A
0N/A public int hashCode() {
0N/A return getLocalPort();
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof ServerSocketAndFactory) {
0N/A ServerSocketAndFactory ssf = (ServerSocketAndFactory) obj;
0N/A if (getLocalPort() == ssf.getLocalPort()) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Custom socket factory.
0N/A */
0N/A public static final class ClientSocketAndFactory
0N/A extends Socket implements RMIClientSocketFactory, Serializable
0N/A {
0N/A ClientSocketAndFactory() {
0N/A }
0N/A ClientSocketAndFactory(String host, int port) throws IOException {
0N/A super(host, port);
0N/A }
0N/A
0N/A public Socket createSocket(String host, int port)
0N/A throws IOException {
0N/A
0N/A return new ClientSocketAndFactory(host, port);
0N/A }
0N/A
0N/A public int hashCode() {
0N/A return getPort();
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A
0N/A if (obj instanceof ClientSocketAndFactory) {
0N/A ClientSocketAndFactory csf = (ClientSocketAndFactory) obj;
0N/A if (getPort() == csf.getPort()) {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) {
0N/A
0N/A try {
0N/A System.out.println("\n\nRegression test for, 4251010\n\n");
0N/A
0N/A Test test = new TestImpl(new ClientSocketAndFactory(),
0N/A new ServerSocketAndFactory());
0N/A TestHome home = new TestHomeImpl(test);
0N/A
0N/A Test test0 = ((Test) RemoteObject.toStub(test));
0N/A Test test1 = ((TestHome) RemoteObject.toStub(home)).get();
0N/A Test test2 = ((TestHome) RemoteObject.toStub(home)).get();
0N/A Test test3 = ((Test) (new MarshalledObject(test)).get());
0N/A
0N/A if (test0.equals(test1)) {
0N/A System.out.println("test0, test1, stubs equal");
0N/A } else {
0N/A TestLibrary.bomb("test0, test1, stubs not equal");
0N/A }
0N/A
0N/A if (test1.equals(test2)) {
0N/A System.out.println("test1, test2, stubs equal");
0N/A } else {
0N/A TestLibrary.bomb("test1, test2, stubs not equal");
0N/A }
0N/A
0N/A // explicitly compare an unmarshalled object with toStub
0N/A // return
0N/A if (test2.equals(test3)) {
0N/A System.out.println("test2, test3, stubs equal");
0N/A } else {
0N/A TestLibrary.bomb("test2, test3, stubs not equal");
0N/A }
0N/A
0N/A test0 = null;
0N/A test1 = null;
0N/A test2 = null;
0N/A test3 = null;
0N/A
0N/A TestLibrary.unexport(test);
0N/A TestLibrary.unexport(home);
0N/A
0N/A System.err.println("test passed: stubs were equal");
0N/A
0N/A } catch (Exception e) {
0N/A TestLibrary.bomb("test got unexpected exception", e);
0N/A }
0N/A }
0N/A}