KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > TestManager


1 package org.openejb.test;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 /**
8  *
9  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
10  */

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 JavaDoc propertiesFileName) throws Exception JavaDoc{
18         Properties JavaDoc props = null;
19
20         try{
21             props = new Properties JavaDoc(System.getProperties());
22             warn = props.getProperty("openejb.test.nowarn") != null;
23         } catch (SecurityException JavaDoc e){
24             throw new IllegalArgumentException JavaDoc("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 JavaDoc e){
38                 throw new IllegalArgumentException JavaDoc("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 JavaDoc{
48         try{
49             server.start();
50         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc{
63         try{
64             server.stop();
65         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc getProperties(String JavaDoc fileName) throws Exception JavaDoc{
78         File JavaDoc file = new File JavaDoc(fileName);
79         file = file.getAbsoluteFile();
80         Properties JavaDoc props = (Properties JavaDoc)System.getProperties().clone();
81         props.load(new FileInputStream JavaDoc(file));
82         return props;
83     }
84
85     private static ClassLoader JavaDoc getContextClassLoader() {
86         return (ClassLoader JavaDoc) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction JavaDoc() {
87             public Object JavaDoc run() {
88                 return Thread.currentThread().getContextClassLoader();
89             }
90         });
91     }
92
93     private static void initServer(Properties JavaDoc props){
94         try{
95
96             String JavaDoc className = props.getProperty("openejb.test.server");
97             if (className == null) throw new IllegalArgumentException JavaDoc("Must specify a test server by setting its class name using the system property \"openejb.test.server\"");
98             ClassLoader JavaDoc cl = getContextClassLoader();
99             Class JavaDoc testServerClass = Class.forName( className, true, cl );
100             server = (TestServer)testServerClass.newInstance();
101             server.init( props );
102         } catch (Exception JavaDoc 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 JavaDoc("Cannot instantiate or initialize the test server: "+e.getClass().getName()+" "+e.getMessage());
106         }
107     }
108
109     private static void initDatabase(Properties JavaDoc props){
110         try{
111             String JavaDoc className = props.getProperty("openejb.test.database");
112             if (className == null) throw new IllegalArgumentException JavaDoc("Must specify a test database by setting its class name using the system property \"openejb.test.database\"");
113             ClassLoader JavaDoc cl = getContextClassLoader();
114             Class JavaDoc testDatabaseClass = Class.forName( className , true, cl);
115             database = (TestDatabase)testDatabaseClass.newInstance();
116             database.init( props );
117         } catch (Exception JavaDoc e){
118             if (warn) System.out.println("Cannot instantiate or initialize the test database: "+e.getClass().getName()+" "+e.getMessage());
119             throw new RuntimeException JavaDoc("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 JavaDoc getContextEnvironment(){
133         return server.getContextEnvironment();
134     }
135 }
136
Popular Tags