1 package org.openejb.test; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.util.Properties ; 6 7 11 public class TestManager { 12 13 private static TestServer server; 14 private static TestDatabase database; 15 private static boolean warn = true; 16 17 protected static void init(String propertiesFileName) throws Exception { 18 Properties props = null; 19 20 try{ 21 props = new Properties (System.getProperties()); 22 warn = props.getProperty("openejb.test.nowarn") != null; 23 } catch (SecurityException e){ 24 throw new IllegalArgumentException ("Cannot access the system properties: "+e.getClass().getName()+" "+e.getMessage()); 25 } 26 27 if (propertiesFileName == null) { 28 try{ 29 propertiesFileName = System.getProperty("openejb.testsuite.properties"); 30 31 if (propertiesFileName == null) { 32 if (warn) System.out.println("Warning: No test suite configuration file specified, assuming system properties contain all needed information. To specify a test suite configuration file by setting its location using the system property \"openejb.testsuite.properties\""); 33 } else { 34 props.putAll( getProperties( propertiesFileName ) ); 35 } 36 37 } catch (SecurityException e){ 38 throw new IllegalArgumentException ("Cannot access the system property \"openejb.testsuite.properties\": "+e.getClass().getName()+" "+e.getMessage()); 39 } 40 } else { 41 props.putAll( getProperties( propertiesFileName ) ); 42 } 43 initServer(props); 44 initDatabase(props); 45 } 46 47 protected static void start() throws Exception { 48 try{ 49 server.start(); 50 } catch (Exception e){ 51 if (warn) System.out.println("Cannot start the test server: "+e.getClass().getName()+" "+e.getMessage()); 52 throw e; 53 } 54 try{ 55 database.start(); 56 } catch (Exception e){ 57 if (warn) System.out.println("Cannot start the test database: "+e.getClass().getName()+" "+e.getMessage()); 58 throw e; 59 } 60 } 61 62 protected static void stop() throws Exception { 63 try{ 64 server.stop(); 65 } catch (Exception e){ 66 if (warn) System.out.println("Cannot stop the test server: "+e.getClass().getName()+" "+e.getMessage()); 67 throw e; 68 } 69 try{ 70 database.stop(); 71 } catch (Exception e){ 72 if (warn) System.out.println("Cannot stop the test database: "+e.getClass().getName()+" "+e.getMessage()); 73 throw e; 74 } 75 } 76 77 private static Properties getProperties(String fileName) throws Exception { 78 File file = new File (fileName); 79 file = file.getAbsoluteFile(); 80 Properties props = (Properties )System.getProperties().clone(); 81 props.load(new FileInputStream (file)); 82 return props; 83 } 84 85 private static ClassLoader getContextClassLoader() { 86 return (ClassLoader ) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction () { 87 public Object run() { 88 return Thread.currentThread().getContextClassLoader(); 89 } 90 }); 91 } 92 93 private static void initServer(Properties props){ 94 try{ 95 96 String className = props.getProperty("openejb.test.server"); 97 if (className == null) throw new IllegalArgumentException ("Must specify a test server by setting its class name using the system property \"openejb.test.server\""); 98 ClassLoader cl = getContextClassLoader(); 99 Class testServerClass = Class.forName( className, true, cl ); 100 server = (TestServer)testServerClass.newInstance(); 101 server.init( props ); 102 } catch (Exception e){ 103 if (warn) e.printStackTrace(); 104 if (warn) System.out.println("Cannot instantiate or initialize the test server: "+e.getClass().getName()+" "+e.getMessage()); 105 throw new RuntimeException ("Cannot instantiate or initialize the test server: "+e.getClass().getName()+" "+e.getMessage()); 106 } 107 } 108 109 private static void initDatabase(Properties props){ 110 try{ 111 String className = props.getProperty("openejb.test.database"); 112 if (className == null) throw new IllegalArgumentException ("Must specify a test database by setting its class name using the system property \"openejb.test.database\""); 113 ClassLoader cl = getContextClassLoader(); 114 Class testDatabaseClass = Class.forName( className , true, cl); 115 database = (TestDatabase)testDatabaseClass.newInstance(); 116 database.init( props ); 117 } catch (Exception e){ 118 if (warn) System.out.println("Cannot instantiate or initialize the test database: "+e.getClass().getName()+" "+e.getMessage()); 119 throw new RuntimeException ("Cannot instantiate or initialize the test database: "+e.getClass().getName()+" "+e.getMessage()); 120 } 121 } 122 123 124 public static TestServer getServer(){ 125 return server; 126 } 127 128 public static TestDatabase getDatabase(){ 129 return database; 130 } 131 132 public static Properties getContextEnvironment(){ 133 return server.getContextEnvironment(); 134 } 135 } 136 | Popular Tags |