KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.openejb.test;
2
3 import java.rmi.RemoteException JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import javax.naming.InitialContext JavaDoc;
7
8 import org.openejb.test.beans.Database;
9 import org.openejb.test.beans.DatabaseHome;
10
11 /**
12  *
13  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
14  */

15 public class InstantDbTestDatabase implements TestDatabase{
16     
17     protected Database database;
18     protected InitialContext JavaDoc initialContext;
19     
20     private static String JavaDoc _createAccount = "CREATE TABLE account ( ssn CHAR(11) PRIMARY KEY, first_name CHAR(20), last_name CHAR(20), balance INT)";
21     //private static String _createAccount = "CREATE TABLE Account ( AcctID INT PRIMARY KEY AUTO INCREMENT, SSN CHAR(11), first_name CHAR(20), last_name CHAR(20), BALANCE INT)";
22
private static String JavaDoc _dropAccount = "DROP TABLE account";
23     
24     //private static String _createEntity = "CREATE TABLE entity ( id INT PRIMARY KEY, first_name CHAR(20), last_name CHAR(20) )";
25
private static String JavaDoc _createEntity = "CREATE TABLE entity ( id INT PRIMARY KEY AUTO INCREMENT, first_name CHAR(20), last_name CHAR(20) )";
26     private static String JavaDoc _dropEntity = "DROP TABLE entity";
27
28     static{
29         System.setProperty("noBanner", "true");
30     }
31     
32
33     public void createEntityTable() throws java.sql.SQLException JavaDoc {
34         try{
35             try{
36                 database.execute(_dropEntity);
37             } catch (Exception JavaDoc e){
38                 // not concerned
39
}
40             database.execute(_createEntity);
41         } catch (RemoteException JavaDoc re){
42             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
43                 throw (java.sql.SQLException JavaDoc)re.detail;
44             } else {
45                 throw new java.sql.SQLException JavaDoc("Cannot create entity table: "+re.getMessage(), _createEntity);
46             }
47         }
48     }
49     public void dropEntityTable() throws java.sql.SQLException JavaDoc {
50         try {
51             database.execute(_dropEntity);
52         } catch (RemoteException JavaDoc re){
53             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
54                 throw (java.sql.SQLException JavaDoc)re.detail;
55             } else {
56                 throw new java.sql.SQLException JavaDoc("Unable to drop entity table: "+re.getMessage(), _dropEntity);
57             }
58         }
59     }
60     
61     
62     public void createAccountTable() throws java.sql.SQLException JavaDoc {
63         try{
64             try{
65                 database.execute(_dropAccount);
66             } catch (Exception JavaDoc e){
67                 // not concerned
68
}
69             database.execute(_createAccount);
70         } catch (RemoteException JavaDoc re){
71             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
72                 throw (java.sql.SQLException JavaDoc)re.detail;
73             } else {
74                 throw new java.sql.SQLException JavaDoc("Cannot create account table: "+re.getMessage(), _createAccount);
75             }
76         }
77     }
78     
79     public void dropAccountTable() throws java.sql.SQLException JavaDoc {
80         try {
81             database.execute(_dropAccount);
82         } catch (RemoteException JavaDoc re){
83             if (re.detail != null && re.detail instanceof java.sql.SQLException JavaDoc) {
84                 throw (java.sql.SQLException JavaDoc)re.detail;
85             } else {
86                 throw new java.sql.SQLException JavaDoc("Cannot drop account table: "+re.getMessage(), _dropAccount);
87             }
88         }
89     }
90     
91     public void start() throws IllegalStateException JavaDoc {
92         try {
93             Properties JavaDoc properties = TestManager.getServer().getContextEnvironment();
94             initialContext = new InitialContext JavaDoc(properties);
95         } catch (Exception JavaDoc e){
96             throw new IllegalStateException JavaDoc("Cannot create initial context: "+e.getClass().getName()+" "+e.getMessage());
97         }
98
99     Object JavaDoc obj =null;
100     DatabaseHome databaseHome =null;
101         try {
102             /* Create database */
103             obj = initialContext.lookup("client/tools/DatabaseHome");
104             databaseHome = (DatabaseHome)javax.rmi.PortableRemoteObject.narrow( obj, DatabaseHome.class);
105         } catch (Exception JavaDoc e){
106             throw new IllegalStateException JavaDoc("Cannot find 'client/tools/DatabaseHome': "+e.getClass().getName()+" "+e.getMessage());
107         }
108         try {
109             database = databaseHome.create();
110         } catch (Exception JavaDoc e){
111             throw new IllegalStateException JavaDoc("Cannot start database: "+e.getClass().getName()+" "+e.getMessage());
112         }
113     }
114     
115     
116     public void stop() throws IllegalStateException JavaDoc {
117     }
118     
119     public void init(Properties JavaDoc props) throws IllegalStateException JavaDoc {
120     }
121 }
122
123     
124
125
Popular Tags