test/java/rmi/testlibrary/TestLibrary.java

Print this page




 118                                e.getMessage());
 119             e.printStackTrace(System.err);
 120         }
 121         throw new TestFailedException(testFailed, e);
 122     }
 123     public static void bomb(String message) {
 124         bomb(message, null);
 125     }
 126     public static void bomb(Exception e) {
 127         bomb(null, e);
 128     }
 129 
 130     /**
 131      * Helper method to determine if registry has started
 132      *
 133      * @param port The port number to check
 134      * @param msTimeout The amount of milliseconds to spend checking
 135      */
 136 
 137     public static boolean checkIfRegistryRunning(int port, int msTimeout) {
 138         long stopTime = System.currentTimeMillis() + msTimeout;

 139         do {
 140             try {
 141                 Registry r = LocateRegistry.getRegistry(port);
 142                 String[] s = r.list();
 143                 // no exception. We're now happy that registry is running
 144                 return true;
 145             } catch (RemoteException e) {
 146                 // problem - not ready ? Try again
 147                 try {
 148                     Thread.sleep(500);
 149                 } catch (InterruptedException ie) {
 150                     // not expected
 151                 }
 152             }
 153         } while (stopTime > System.currentTimeMillis());
 154         return false;
 155     }
 156 
 157     public static String getProperty(final String property,
 158                                      final String defaultVal) {
 159         try {
 160             return java.security.AccessController.doPrivileged(
 161                 new java.security.PrivilegedAction<String>() {
 162                     public String run() {
 163                         return System.getProperty(property, defaultVal);
 164                     }
 165                 });
 166         } catch (Exception ex) {
 167             bomb("Exception getting property " + property, ex);
 168             throw new AssertionError("this should be unreachable");
 169         }
 170     }
 171 

























 172     /**
 173      * Property mutators
 174      */
 175     public static void setBoolean(String property, boolean value) {
 176         setProperty(property, (new Boolean(value)).toString());
 177     }
 178     public static void setInteger(String property, int value) {
 179         setProperty(property, Integer.toString(value));
 180     }
 181     public static void setProperty(String property, String value) {
 182         final String prop = property;
 183         final String val = value;
 184         java.security.AccessController.doPrivileged(
 185             new java.security.PrivilegedAction<Void>() {
 186                 public Void run() {
 187                     System.setProperty(prop, val);
 188                     return null;
 189                 }
 190         });
 191     }




 118                                e.getMessage());
 119             e.printStackTrace(System.err);
 120         }
 121         throw new TestFailedException(testFailed, e);
 122     }
 123     public static void bomb(String message) {
 124         bomb(message, null);
 125     }
 126     public static void bomb(Exception e) {
 127         bomb(null, e);
 128     }
 129 
 130     /**
 131      * Helper method to determine if registry has started
 132      *
 133      * @param port The port number to check
 134      * @param msTimeout The amount of milliseconds to spend checking
 135      */
 136 
 137     public static boolean checkIfRegistryRunning(int port, int msTimeout) {
 138         final long POLLTIME_MS = 100L;
 139         long stopTime = computeDeadline(System.currentTimeMillis(), msTimeout);
 140         do {
 141             try {
 142                 Registry r = LocateRegistry.getRegistry(port);
 143                 String[] s = r.list();
 144                 // no exception. We're now happy that registry is running
 145                 return true;
 146             } catch (RemoteException e) {
 147                 // problem - not ready ? Try again
 148                 try {
 149                     Thread.sleep(POLLTIME_MS);
 150                 } catch (InterruptedException ie) {
 151                     // not expected
 152                 }
 153             }
 154         } while (System.currentTimeMillis() < stopTime);
 155         return false;
 156     }
 157 
 158     public static String getProperty(final String property,
 159                                      final String defaultVal) {
 160         try {
 161             return java.security.AccessController.doPrivileged(
 162                 new java.security.PrivilegedAction<String>() {
 163                     public String run() {
 164                         return System.getProperty(property, defaultVal);
 165                     }
 166                 });
 167         } catch (Exception ex) {
 168             bomb("Exception getting property " + property, ex);
 169             throw new AssertionError("this should be unreachable");
 170         }
 171     }
 172 
 173     public static double getTimeoutFactor() {
 174         String prop = getProperty("test.timeout.factor", "1.0");
 175         double timeoutFactor = 1.0;
 176 
 177         try {
 178             timeoutFactor = Double.parseDouble(prop);
 179         } catch (NumberFormatException ignore) { }
 180 
 181         return timeoutFactor;
 182     }
 183 
 184     /**
 185      * Computes a deadline from a timestamp and a timeout value.
 186      * Maximum timeout (before multipliers are applied) is one hour.
 187      */
 188     public static long computeDeadline(long timestamp, long timeout) {
 189         final long MAX_TIMEOUT_MS = 3_600_000L;
 190 
 191         if (timeout < 0L || timeout > MAX_TIMEOUT_MS) {
 192             throw new IllegalArgumentException("timeout " + timeout + "ms out of range");
 193         }
 194 
 195         return timestamp + (long)(timeout * getTimeoutFactor());
 196     }
 197 
 198     /**
 199      * Property mutators
 200      */
 201     public static void setBoolean(String property, boolean value) {
 202         setProperty(property, (new Boolean(value)).toString());
 203     }
 204     public static void setInteger(String property, int value) {
 205         setProperty(property, Integer.toString(value));
 206     }
 207     public static void setProperty(String property, String value) {
 208         final String prop = property;
 209         final String val = value;
 210         java.security.AccessController.doPrivileged(
 211             new java.security.PrivilegedAction<Void>() {
 212                 public Void run() {
 213                     System.setProperty(prop, val);
 214                     return null;
 215                 }
 216         });
 217     }