23 24 /** 25 * 26 * 27 * @author Adrian Colley 28 * @author Laird Dornin 29 * @author Peter Jones 30 * @author Ann Wollrath 31 * 32 * The rmi library directory contains a set of simple utiltity classes 33 * for use in rmi regression tests. 34 * 35 * NOTE: The JavaTest group has recommended that regression tests do 36 * not make use of packages. 37 */ 38 39 import java.io.File; 40 import java.io.FileInputStream; 41 import java.io.FileOutputStream; 42 import java.io.IOException; 43 import java.io.OutputStream; 44 import java.io.PrintStream; 45 import java.net.URL; 46 import java.net.MalformedURLException; 47 import java.rmi.activation.Activatable; 48 import java.rmi.activation.ActivationID; 49 import java.rmi.NoSuchObjectException; 50 import java.rmi.registry.Registry; 51 import java.rmi.Remote; 52 import java.rmi.server.UnicastRemoteObject; 53 import java.util.Enumeration; 54 import java.util.Hashtable; 55 import java.util.Properties; 56 import java.io.ByteArrayOutputStream; 57 import java.security.AccessController; 58 import java.security.PrivilegedAction; 59 60 /** 61 * Class of utility/library methods (i.e. procedures) that assist with 62 * the writing and maintainance of rmi regression tests. 63 */ 64 public class TestLibrary { 65 66 /** standard test port number for registry */ 67 public final static int REGISTRY_PORT = 2006; 68 /** port for rmid necessary: not used to actually start rmid */ 69 public final static int RMID_PORT = 1098; 70 71 static void mesg(Object mesg) { 72 System.err.println("TEST_LIBRARY: " + mesg.toString()); 73 } 74 75 /** 76 * Routines that enable rmi tests to fail in a uniformly 77 * informative fashion. 78 */ 79 public static void bomb(String message, Exception e) { 80 String testFailed = "TEST FAILED: "; 81 82 if ((message == null) && (e == null)) { 83 testFailed += " No relevant information"; 84 } else if (e == null) { 85 testFailed += message; 86 } 87 88 System.err.println(testFailed); 89 if (e != null) { 320 */ 321 public static void suggestSecurityManager(String managerClassName) { 322 SecurityManager manager = null; 323 324 if (System.getSecurityManager() == null) { 325 try { 326 if (managerClassName == null) { 327 managerClassName = TestParams.defaultSecurityManager; 328 } 329 manager = ((SecurityManager) Class. 330 forName(managerClassName).newInstance()); 331 } catch (ClassNotFoundException cnfe) { 332 bomb("Security manager could not be found: " + 333 managerClassName, cnfe); 334 } catch (Exception e) { 335 bomb("Error creating security manager. ", e); 336 } 337 338 System.setSecurityManager(manager); 339 } 340 } 341 342 /** 343 * Method to capture the stack trace of an exception and return it 344 * as a string. 345 */ 346 public String stackTraceToString(Exception e) { 347 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 348 PrintStream ps = new PrintStream(bos); 349 350 e.printStackTrace(ps); 351 return bos.toString(); 352 } 353 354 /** extra properties */ 355 private static Properties props; 356 357 /** 358 * Returns extra test properties. Looks for the file "../../test.props" 359 * and reads it in as a Properties file. Assuming the working directory | 23 24 /** 25 * 26 * 27 * @author Adrian Colley 28 * @author Laird Dornin 29 * @author Peter Jones 30 * @author Ann Wollrath 31 * 32 * The rmi library directory contains a set of simple utiltity classes 33 * for use in rmi regression tests. 34 * 35 * NOTE: The JavaTest group has recommended that regression tests do 36 * not make use of packages. 37 */ 38 39 import java.io.File; 40 import java.io.FileInputStream; 41 import java.io.FileOutputStream; 42 import java.io.IOException; 43 import java.io.PrintStream; 44 import java.net.URL; 45 import java.net.MalformedURLException; 46 import java.net.ServerSocket; 47 import java.rmi.NoSuchObjectException; 48 import java.rmi.registry.Registry; 49 import java.rmi.Remote; 50 import java.rmi.server.UnicastRemoteObject; 51 import java.util.Enumeration; 52 import java.util.Properties; 53 import java.io.ByteArrayOutputStream; 54 import java.rmi.RemoteException; 55 import java.rmi.registry.LocateRegistry; 56 import java.rmi.server.RemoteRef; 57 import sun.rmi.registry.RegistryImpl; 58 import sun.rmi.server.UnicastServerRef; 59 import sun.rmi.transport.Endpoint; 60 import sun.rmi.transport.LiveRef; 61 import sun.rmi.transport.tcp.TCPEndpoint; 62 63 /** 64 * Class of utility/library methods (i.e. procedures) that assist with 65 * the writing and maintainance of rmi regression tests. 66 */ 67 public class TestLibrary { 68 /** 69 * IMPORTANT! 70 * 71 * RMI tests are run concurrently and port conflicts result when a single 72 * port number is used by multiple tests for the RMI registry. Use 73 * getUnusedRandomPort() wherever possible. If getUnusedRandomPort() cannot 74 * be used, specify a port to use for the RMI registry for your test here. 75 * This will ensure there are no port conflicts amongst the RMI tests. The 76 * port numbers specified here may also be specified in the respective 77 * tests. Do not change these port numbers without also changing the port 78 * numbers in the respective tests. 79 * 80 * Reserve port range: 64001-64100 for tests which cannot use a random 81 * port. 82 */ 83 public final static int PORT_MIN = 1024; 84 public final static int PORT_MAX = 64000; 85 public final static int RMIDVIAINHERITEDCHANNEL_ACTIVATION_PORT = 64001; 86 public final static int RMIDVIAINHERITEDCHANNEL_REGISTRY_PORT = 64002; 87 public final static int INHERITEDCHANNELNOTSERVERSOCKET_ACTIVATION_PORT = 64003; 88 public final static int INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT = 64004; 89 public final static int READTEST_REGISTRY_PORT = 64005; 90 91 static void mesg(Object mesg) { 92 System.err.println("TEST_LIBRARY: " + mesg.toString()); 93 } 94 95 /** 96 * Routines that enable rmi tests to fail in a uniformly 97 * informative fashion. 98 */ 99 public static void bomb(String message, Exception e) { 100 String testFailed = "TEST FAILED: "; 101 102 if ((message == null) && (e == null)) { 103 testFailed += " No relevant information"; 104 } else if (e == null) { 105 testFailed += message; 106 } 107 108 System.err.println(testFailed); 109 if (e != null) { 340 */ 341 public static void suggestSecurityManager(String managerClassName) { 342 SecurityManager manager = null; 343 344 if (System.getSecurityManager() == null) { 345 try { 346 if (managerClassName == null) { 347 managerClassName = TestParams.defaultSecurityManager; 348 } 349 manager = ((SecurityManager) Class. 350 forName(managerClassName).newInstance()); 351 } catch (ClassNotFoundException cnfe) { 352 bomb("Security manager could not be found: " + 353 managerClassName, cnfe); 354 } catch (Exception e) { 355 bomb("Error creating security manager. ", e); 356 } 357 358 System.setSecurityManager(manager); 359 } 360 } 361 362 /** 363 * Creates an RMI Registry on a random port. Tries up to 10 times before 364 * giving up and returning null. 365 * 366 * Note: if this method is called multiple times within a single VM, it will 367 * throw an exception. 368 * 369 * @returns a registry, started on a random port, or {@code null} if one 370 * could not be started. 371 */ 372 public static Registry createRegistryOnUnusedPort() throws RemoteException { 373 Registry registry = null; 374 int numTries = 0; 375 376 while ((numTries++ < 10) && (registry == null)) { 377 try { 378 registry = LocateRegistry.createRegistry(0); 379 } catch (RemoteException e) { 380 // Do nothing 381 } 382 } 383 384 return registry; 385 } 386 387 /** 388 * Returns the port number the RMI Registry is running on, or -1 if it could 389 * not be determined. 390 * 391 * @param registry the registry to find the port of. 392 * @return the port number the registry is using, or -1 if it could not be 393 * determined. 394 */ 395 public static int getRegistryPort(Registry registry) { 396 int port = -1; 397 398 try { 399 RemoteRef remoteRef = ((RegistryImpl)registry).getRef(); 400 LiveRef liveRef = ((UnicastServerRef)remoteRef).getLiveRef(); 401 Endpoint endpoint = liveRef.getChannel().getEndpoint(); 402 TCPEndpoint tcpEndpoint = (TCPEndpoint) endpoint; 403 port = tcpEndpoint.getPort(); 404 } catch (Exception ex) { 405 ex.printStackTrace(System.err); 406 } 407 408 return port; 409 } 410 411 /** 412 * Returns an unused random port number greater then or equal to PORT_MIN 413 * and less then or equal to PORT_MAX. Will try up to 10 times to get a 414 * random port before giving up and returning -1. 415 * 416 * @return an unused random port number, or -1. 417 */ 418 public static int getUnusedRandomPort() { 419 int numTries = 0; 420 int unusedRandomPort = -1; 421 422 while ((numTries++ < 10) && ((unusedRandomPort < PORT_MIN) || 423 (unusedRandomPort > PORT_MAX))) 424 { 425 try (ServerSocket ss = new ServerSocket(0)) { 426 unusedRandomPort = ss.getLocalPort(); 427 } catch (Exception e) { 428 // Do nothing 429 e.printStackTrace(); //djm 430 } 431 } 432 433 return unusedRandomPort; 434 } 435 436 /** 437 * Method to capture the stack trace of an exception and return it 438 * as a string. 439 */ 440 public String stackTraceToString(Exception e) { 441 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 442 PrintStream ps = new PrintStream(bos); 443 444 e.printStackTrace(ps); 445 return bos.toString(); 446 } 447 448 /** extra properties */ 449 private static Properties props; 450 451 /** 452 * Returns extra test properties. Looks for the file "../../test.props" 453 * and reads it in as a Properties file. Assuming the working directory |