J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @bug 4099660 4102938 |
| 26 | * @summary Remote classes not extending RemoteObject should be able to |
| 27 | * implement hashCode() and equals() methods so that instances |
| 28 | * can be successfully compared to RemoteObject instances |
| 29 | * (specifically: stubs) that contain the instance's RemoteRef. |
| 30 | * @author Peter Jones |
| 31 | * |
| 32 | * @build NotExtending |
| 33 | * @build NotExtending_Stub |
| 34 | * @build NotExtending_Skel |
| 35 | * @run main/othervm/timeout=240 NotExtending |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | import java.rmi.*; |
| 40 | import java.rmi.server.*; |
| 41 | |
| 42 | public class NotExtending implements Remote { |
| 43 | |
| 44 | /** remote stub for this server instance */ |
| 45 | private Remote stub; |
| 46 | /** value of stub's hash code */ |
| 47 | private int hashValue; |
| 48 | /** true if the hashValue field has been initialized */ |
| 49 | private boolean hashValueInitialized = false; |
| 50 | |
| 51 | public NotExtending() throws RemoteException { |
| 52 | stub = UnicastRemoteObject.exportObject(this); |
| 53 | setHashValue(stub.hashCode()); |
| 54 | } |
| 55 | |
| 56 | private void setHashValue(int value) { |
| 57 | hashValue = value; |
| 58 | hashValueInitialized = true; |
| 59 | } |
| 60 | |
| 61 | public int hashCode() { |
| 62 | /* |
| 63 | * Test fails with a RuntimeException if the hashCode() method is |
| 64 | * called (during the export procedure) before the correct hash |
| 65 | * value has been initialized. |
| 66 | */ |
| 67 | if (!hashValueInitialized) { |
| 68 | throw new RuntimeException( |
| 69 | "hashCode() invoked before hashValue initialized"); |
| 70 | } |
| 71 | return hashValue; |
| 72 | } |
| 73 | |
| 74 | public boolean equals(Object obj) { |
| 75 | return stub.equals(obj); |
| 76 | } |
| 77 | |
| 78 | public static void main(String[] args) throws Exception { |
| 79 | /* |
| 80 | * The following line is required with the JDK 1.2 VM so that the |
| 81 | * VM can exit gracefully when this test completes. Otherwise, the |
| 82 | * conservative garbage collector will find a handle to the server |
| 83 | * object on the native stack and not clear the weak reference to |
| 84 | * it in the RMI runtime's object table. |
| 85 | */ |
| 86 | Object dummy = new Object(); |
| 87 | |
| 88 | NotExtending server; |
| 89 | try { |
| 90 | /* |
| 91 | * Verify that hashCode() is not invoked before it is |
| 92 | * initialized. Tests bugid 4102938. |
| 93 | */ |
| 94 | server = new NotExtending(); |
| 95 | System.err.println("Server exported without invoking hashCode()."); |
| 96 | |
| 97 | /* |
| 98 | * Verify that passing stub to server's equals() method |
| 99 | * returns true. |
| 100 | */ |
| 101 | if (server.equals(server.stub)) { |
| 102 | System.err.println( |
| 103 | "Passing stub to server's equals() method succeeded."); |
| 104 | } else { |
| 105 | throw new RuntimeException( |
| 106 | "passing stub to server's equals() method failed"); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Verify that passing server to stub's equals() method |
| 111 | * returns true. Tests bugid 4099660. |
| 112 | */ |
| 113 | if (server.stub.equals(server)) { |
| 114 | System.err.println( |
| 115 | "Passing server to stub's equals() method succeeded."); |
| 116 | } else { |
| 117 | throw new RuntimeException( |
| 118 | "passing server to stub's equals() method failed"); |
| 119 | } |
| 120 | |
| 121 | } finally { |
| 122 | server = null; |
| 123 | flushCachedRefs(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Force desparate garbage collection so that all sun.misc.Ref instances |
| 129 | * will be cleared. |
| 130 | * |
| 131 | * This method is required with the JDK 1.1.x RMI runtime so that the |
| 132 | * VM can exit gracefully when this test completes. See bugid 4006356. |
| 133 | */ |
| 134 | public static void flushCachedRefs() { |
| 135 | java.util.Vector chain = new java.util.Vector(); |
| 136 | try { |
| 137 | while (true) { |
| 138 | int[] hungry = new int[65536]; |
| 139 | chain.addElement(hungry); |
| 140 | } |
| 141 | } catch (OutOfMemoryError e) { |
| 142 | } |
| 143 | } |
| 144 | } |