jdk/test/java/rmi/server/RemoteObject/notExtending/NotExtending.java
changeset 28108 ded6ce6679a4
parent 22565 2f3102102bd9
equal deleted inserted replaced
28107:587cd87d5c98 28108:ded6ce6679a4
    28  *          can be successfully compared to RemoteObject instances
    28  *          can be successfully compared to RemoteObject instances
    29  *          (specifically: stubs) that contain the instance's RemoteRef.
    29  *          (specifically: stubs) that contain the instance's RemoteRef.
    30  * @author Peter Jones
    30  * @author Peter Jones
    31  *
    31  *
    32  * @build NotExtending_Stub NotExtending_Skel
    32  * @build NotExtending_Stub NotExtending_Skel
    33  * @run main/othervm/timeout=240 NotExtending
    33  * @run main/othervm NotExtending
    34  */
    34  */
    35 
    35 
    36 
    36 
    37 import java.rmi.*;
    37 import java.rmi.*;
    38 import java.rmi.server.*;
    38 import java.rmi.server.*;
    44     /** value of stub's hash code */
    44     /** value of stub's hash code */
    45     private int hashValue;
    45     private int hashValue;
    46     /** true if the hashValue field has been initialized */
    46     /** true if the hashValue field has been initialized */
    47     private boolean hashValueInitialized = false;
    47     private boolean hashValueInitialized = false;
    48 
    48 
    49     public NotExtending() throws RemoteException {
    49     // no declared constructor - rely on implicit no-arg contructor
       
    50 
       
    51     public Remote export() throws RemoteException {
    50         stub = UnicastRemoteObject.exportObject(this);
    52         stub = UnicastRemoteObject.exportObject(this);
    51         setHashValue(stub.hashCode());
    53         setHashValue(stub.hashCode());
       
    54         return stub;
       
    55     }
       
    56 
       
    57     public void unexport() throws RemoteException {
       
    58         UnicastRemoteObject.unexportObject(this, true);
    52     }
    59     }
    53 
    60 
    54     private void setHashValue(int value) {
    61     private void setHashValue(int value) {
    55         hashValue = value;
    62         hashValue = value;
    56         hashValueInitialized = true;
    63         hashValueInitialized = true;
    57     }
    64     }
    58 
    65 
    59     public int hashCode() {
    66     public int hashCode() {
    60         /*
    67         /*
    61          * Test fails with a RuntimeException if the hashCode() method is
    68          * Test fails if the hashCode() method is called (during export)
    62          * called (during the export procedure) before the correct hash
    69          * before the correct hash value has been initialized.
    63          * value has been initialized.
       
    64          */
    70          */
    65         if (!hashValueInitialized) {
    71         if (!hashValueInitialized) {
    66             throw new RuntimeException(
    72             throw new AssertionError(
    67                 "hashCode() invoked before hashValue initialized");
    73                 "hashCode() invoked before hashValue initialized");
    68         }
    74         }
    69         return hashValue;
    75         return hashValue;
    70     }
    76     }
    71 
    77 
    72     public boolean equals(Object obj) {
    78     public boolean equals(Object obj) {
    73         return stub.equals(obj);
    79         return stub.equals(obj);
    74     }
    80     }
    75 
    81 
    76     public static void main(String[] args) throws Exception {
    82     public static void main(String[] args) throws Exception {
    77         /*
    83         NotExtending server = null;
    78          * The following line is required with the JDK 1.2 VM so that the
       
    79          * VM can exit gracefully when this test completes.  Otherwise, the
       
    80          * conservative garbage collector will find a handle to the server
       
    81          * object on the native stack and not clear the weak reference to
       
    82          * it in the RMI runtime's object table.
       
    83          */
       
    84         Object dummy = new Object();
       
    85 
    84 
    86         NotExtending server;
       
    87         try {
    85         try {
    88             /*
    86             /*
    89              * Verify that hashCode() is not invoked before it is
    87              * Verify that hashCode() is not invoked before it is
    90              * initialized.  Tests bugid 4102938.
    88              * initialized.  Tests bugid 4102938.
    91              */
    89              */
    92             server = new NotExtending();
    90             server = new NotExtending();
       
    91             Remote stub = server.export();
    93             System.err.println("Server exported without invoking hashCode().");
    92             System.err.println("Server exported without invoking hashCode().");
    94 
    93 
    95             /*
    94             /*
    96              * Verify that passing stub to server's equals() method
    95              * Verify that passing stub to server's equals() method
    97              * returns true.
    96              * returns true.
    98              */
    97              */
    99             if (server.equals(server.stub)) {
    98             if (server.equals(stub)) {
   100                 System.err.println(
    99                 System.err.println("server.equals(stub) returns true");
   101                     "Passing stub to server's equals() method succeeded.");
       
   102             } else {
   100             } else {
   103                 throw new RuntimeException(
   101                 throw new AssertionError("server.equals(stub) returns false");
   104                     "passing stub to server's equals() method failed");
       
   105             }
   102             }
   106 
   103 
   107             /*
   104             /*
   108              * Verify that passing server to stub's equals() method
   105              * Verify that passing server to stub's equals() method
   109              * returns true.  Tests bugid 4099660.
   106              * returns true.  Tests bugid 4099660.
   110              */
   107              */
   111             if (server.stub.equals(server)) {
   108             if (stub.equals(server)) {
   112                 System.err.println(
   109                 System.err.println("stub.equals(server) returns true");
   113                     "Passing server to stub's equals() method succeeded.");
       
   114             } else {
   110             } else {
   115                 throw new RuntimeException(
   111                 throw new AssertionError("stub.equals(server) returns false");
   116                     "passing server to stub's equals() method failed");
       
   117             }
   112             }
   118 
       
   119         } finally {
   113         } finally {
   120             server = null;
   114             if (server != null) {
   121             flushCachedRefs();
   115                 server.unexport();
   122         }
       
   123     }
       
   124 
       
   125     /**
       
   126      * Force desperate garbage collection so that soft references
       
   127      * will be cleared.
       
   128      *
       
   129      * This method is required with the JDK 1.1.x RMI runtime so that the
       
   130      * VM can exit gracefully when this test completes.  See bugid 4006356.
       
   131      */
       
   132     public static void flushCachedRefs() {
       
   133         java.util.Vector chain = new java.util.Vector();
       
   134         try {
       
   135             while (true) {
       
   136                 int[] hungry = new int[65536];
       
   137                 chain.addElement(hungry);
       
   138             }
   116             }
   139         } catch (OutOfMemoryError e) {
       
   140         }
   117         }
   141     }
   118     }
   142 }
   119 }