jdk/test/java/rmi/testlibrary/TestLibrary.java
changeset 14760 d815de0ca6f0
parent 13256 5886d7607acd
child 15273 fc0a33508cf7
equal deleted inserted replaced
14759:3c8434051b3e 14760:d815de0ca6f0
    52 import java.rmi.registry.Registry;
    52 import java.rmi.registry.Registry;
    53 import java.rmi.server.RemoteRef;
    53 import java.rmi.server.RemoteRef;
    54 import java.rmi.server.UnicastRemoteObject;
    54 import java.rmi.server.UnicastRemoteObject;
    55 import java.util.Enumeration;
    55 import java.util.Enumeration;
    56 import java.util.Properties;
    56 import java.util.Properties;
       
    57 
    57 import sun.rmi.registry.RegistryImpl;
    58 import sun.rmi.registry.RegistryImpl;
    58 import sun.rmi.server.UnicastServerRef;
    59 import sun.rmi.server.UnicastServerRef;
    59 import sun.rmi.transport.Endpoint;
    60 import sun.rmi.transport.Endpoint;
    60 import sun.rmi.transport.LiveRef;
    61 import sun.rmi.transport.LiveRef;
    61 import sun.rmi.transport.tcp.TCPEndpoint;
    62 import sun.rmi.transport.tcp.TCPEndpoint;
    90     public final static int RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT = 64001;
    91     public final static int RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT = 64001;
    91     public final static int RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT = 64002;
    92     public final static int RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT = 64002;
    92     public final static int INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT = 64003;
    93     public final static int INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT = 64003;
    93     public final static int INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT = 64004;
    94     public final static int INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT = 64004;
    94     public final static int READTEST_REGISTRY_PORT = 64005;
    95     public final static int READTEST_REGISTRY_PORT = 64005;
       
    96     private final static int MAX_SERVER_SOCKET_TRIES = 10;
    95 
    97 
    96     static void mesg(Object mesg) {
    98     static void mesg(Object mesg) {
    97         System.err.println("TEST_LIBRARY: " + mesg.toString());
    99         System.err.println("TEST_LIBRARY: " + mesg.toString());
    98     }
   100     }
    99 
   101 
   123     }
   125     }
   124     public static void bomb(Exception e) {
   126     public static void bomb(Exception e) {
   125         bomb(null, e);
   127         bomb(null, e);
   126     }
   128     }
   127 
   129 
   128     /**
       
   129      * Property accessors
       
   130      */
       
   131     private static boolean getBoolean(String name) {
       
   132         return (new Boolean(getProperty(name, "false")).booleanValue());
       
   133     }
       
   134     private static Integer getInteger(String name) {
       
   135         int val = 0;
       
   136         Integer value = null;
       
   137 
       
   138         String propVal = getProperty(name, null);
       
   139         if (propVal == null) {
       
   140             return null;
       
   141         }
       
   142 
       
   143         try {
       
   144             value = new Integer(Integer.parseInt(propVal));
       
   145         } catch (NumberFormatException nfe) {
       
   146         }
       
   147         return value;
       
   148     }
       
   149     public static String getProperty(String property, String defaultVal) {
   130     public static String getProperty(String property, String defaultVal) {
   150         final String prop = property;
   131         final String prop = property;
   151         final String def = defaultVal;
   132         final String def = defaultVal;
   152         return ((String) java.security.AccessController.doPrivileged
   133         return java.security.AccessController.doPrivileged(
   153             (new java.security.PrivilegedAction() {
   134             new java.security.PrivilegedAction<String>() {
   154                 public Object run() {
   135                 public String run() {
   155                     return System.getProperty(prop, def);
   136                     return System.getProperty(prop, def);
   156                 }
   137                 }
   157             }));
   138             });
   158     }
   139     }
   159 
   140 
   160     /**
   141     /**
   161      * Property mutators
   142      * Property mutators
   162      */
   143      */
   167         setProperty(property, Integer.toString(value));
   148         setProperty(property, Integer.toString(value));
   168     }
   149     }
   169     public static void setProperty(String property, String value) {
   150     public static void setProperty(String property, String value) {
   170         final String prop = property;
   151         final String prop = property;
   171         final String val = value;
   152         final String val = value;
   172         java.security.AccessController.doPrivileged
   153         java.security.AccessController.doPrivileged(
   173             (new java.security.PrivilegedAction() {
   154             new java.security.PrivilegedAction<Void>() {
   174                 public Object run() {
   155                 public Void run() {
   175                     System.setProperty(prop, val);
   156                     System.setProperty(prop, val);
   176                     return null;
   157                     return null;
   177                 }
   158                 }
   178         });
   159         });
   179     }
   160     }
   186     }
   167     }
   187     public static void printEnvironment(PrintStream out) {
   168     public static void printEnvironment(PrintStream out) {
   188         out.println("-------------------Test environment----------" +
   169         out.println("-------------------Test environment----------" +
   189                     "---------");
   170                     "---------");
   190 
   171 
   191         for(Enumeration keys = System.getProperties().keys();
   172         for(Enumeration<?> keys = System.getProperties().keys();
   192             keys.hasMoreElements();) {
   173             keys.hasMoreElements();) {
   193 
   174 
   194             String property = (String) keys.nextElement();
   175             String property = (String) keys.nextElement();
   195             out.println(property + " = " + getProperty(property, null));
   176             out.println(property + " = " + getProperty(property, null));
   196         }
   177         }
   250         File dstFile = new File(dstDir, classFileName);
   231         File dstFile = new File(dstDir, classFileName);
   251 
   232 
   252         /*
   233         /*
   253          * Obtain the URL for the codebase.
   234          * Obtain the URL for the codebase.
   254          */
   235          */
   255         URL codebaseURL = dstDir.toURL();
   236         URL codebaseURL = dstDir.toURI().toURL();
   256 
   237 
   257         /*
   238         /*
   258          * Specify where we will copy the class definition from, if
   239          * Specify where we will copy the class definition from, if
   259          * necessary.  After the test is built, the class file can be
   240          * necessary.  After the test is built, the class file can be
   260          * found in the "test.classes" directory.
   241          * found in the "test.classes" directory.
   405      * @return an unused random port number.
   386      * @return an unused random port number.
   406      * @throws RuntimeException if there was a problem getting a port.
   387      * @throws RuntimeException if there was a problem getting a port.
   407      */
   388      */
   408     public static int getUnusedRandomPort() {
   389     public static int getUnusedRandomPort() {
   409         int numTries = 0;
   390         int numTries = 0;
   410         int unusedRandomPort = FIXED_PORT_MIN;
   391         IOException ex = null;
   411         Exception ex = null;
   392 
   412 
   393         while (numTries++ < MAX_SERVER_SOCKET_TRIES) {
   413         while (numTries++ < 10) {
   394             int unusedRandomPort = -1;
   414             ex = null; //reset
   395             ex = null; //reset
   415 
   396 
   416             try (ServerSocket ss = new ServerSocket(0)) {
   397             try (ServerSocket ss = new ServerSocket(0)) {
   417                 unusedRandomPort = ss.getLocalPort();
   398                 unusedRandomPort = ss.getLocalPort();
   418             } catch (Exception e) {
   399             } catch (IOException e) {
   419                 ex = e;
   400                 ex = e;
   420             }
   401                 // temporarily print stack trace here until we find out why
   421 
   402                 // tests are failing.
   422             if (!isReservedPort(unusedRandomPort)) {
   403                 System.err.println("TestLibrary.getUnusedRandomPort() caught "
   423                 return unusedRandomPort;
   404                         + "exception on iteration " + numTries
       
   405                         + (numTries==MAX_SERVER_SOCKET_TRIES ? " (the final try)."
       
   406                         : "."));
       
   407                 ex.printStackTrace();
       
   408             }
       
   409 
       
   410             if (unusedRandomPort >= 0) {
       
   411                 if (isReservedPort(unusedRandomPort)) {
       
   412                     System.out.println("INFO: On try # " + numTries
       
   413                         + (numTries==MAX_SERVER_SOCKET_TRIES ? ", the final try, ": ",")
       
   414                         + " ServerSocket(0) returned the reserved port "
       
   415                         + unusedRandomPort
       
   416                         + " in TestLibrary.getUnusedRandomPort() ");
       
   417                 } else {
       
   418                     return unusedRandomPort;
       
   419                 }
   424             }
   420             }
   425         }
   421         }
   426 
   422 
   427         // If we're here, then either an exception was thrown or the port is
   423         // If we're here, then either an exception was thrown or the port is
   428         // a reserved port.
   424         // a reserved port.
   429         throw new RuntimeException("Error getting unused random port.", ex);
   425         if (ex==null) {
       
   426             throw new RuntimeException("Error getting unused random port. The"
       
   427                     +" last port returned by ServerSocket(0) was a reserved port");
       
   428         } else {
       
   429             throw new RuntimeException("Error getting unused random port.", ex);
       
   430         }
   430     }
   431     }
   431 
   432 
   432     /**
   433     /**
   433      * Determines if a port is one of the reserved port numbers.
   434      * Determines if a port is one of the reserved port numbers.
   434      *
   435      *