001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test;
017:
018: import java.rmi.RemoteException;
019: import java.util.Properties;
020:
021: import javax.naming.InitialContext;
022:
023: import org.apache.openejb.test.beans.Database;
024: import org.apache.openejb.test.beans.DatabaseHome;
025:
026: /**
027: *
028: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
029: */
030: public class InstantDbTestDatabase implements TestDatabase {
031:
032: protected Database database;
033: protected InitialContext initialContext;
034:
035: private static String _createAccount = "CREATE TABLE account ( ssn CHAR(11) PRIMARY KEY, first_name CHAR(20), last_name CHAR(20), balance INT)";
036: //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)";
037: private static String _dropAccount = "DROP TABLE account";
038:
039: //private static String _createEntity = "CREATE TABLE entity ( id INT PRIMARY KEY, first_name CHAR(20), last_name CHAR(20) )";
040: private static String _createEntity = "CREATE TABLE entity ( id INT PRIMARY KEY AUTO INCREMENT, first_name CHAR(20), last_name CHAR(20) )";
041: private static String _dropEntity = "DROP TABLE entity";
042:
043: private static final String CREATE_ONE_TO_ONE_A = "CREATE TABLE OneToOneA(A1 INTEGER, A2 VARCHAR(50))";
044: private static final String DROP_ONE_TO_ONE_A = "DROP TABLE OneToOneA";
045: private static final String CREATE_ONE_TO_ONE_B = "CREATE TABLE OneToOneB(B1 INTEGER, B2 VARCHAR(50), B3 INTEGER, B4 VARCHAR(50), FKA1 INTEGER)";
046: private static final String DROP_ONE_TO_ONE_B = "DROP TABLE OneToOneB";
047:
048: private static final String CREATE_ONE_OWNING = "CREATE TABLE oneowning (col_id INTEGER, col_field1 INTEGER)";
049:
050: private static final String DROP_ONE_OWNING = "DROP TABLE oneowning";
051:
052: private static final String CREATE_ONE_INVERSE = "CREATE TABLE oneinverse (col_id INTEGER)";
053:
054: private static final String DROP_ONE_INVERSE = "DROP TABLE oneinverse";
055:
056: private static final String CREATE_MANY_OWNING = "CREATE TABLE manyowning (col_id INTEGER, col_field1 INTEGER)";
057:
058: private static final String DROP_MANY_OWNING = "DROP TABLE manyowning";
059:
060: static {
061: System.setProperty("noBanner", "true");
062: }
063:
064: public void createEntityTable() throws java.sql.SQLException {
065: createTable(_createEntity, _dropEntity);
066: createTable(CREATE_ONE_TO_ONE_A, DROP_ONE_TO_ONE_A);
067: createTable(CREATE_ONE_TO_ONE_B, DROP_ONE_TO_ONE_B);
068: createTable(CREATE_ONE_OWNING, DROP_ONE_OWNING);
069: createTable(CREATE_ONE_INVERSE, DROP_ONE_INVERSE);
070: createTable(CREATE_MANY_OWNING, DROP_MANY_OWNING);
071: }
072:
073: public void dropEntityTable() throws java.sql.SQLException {
074: dropTable(_dropEntity);
075: dropTable(DROP_ONE_TO_ONE_A);
076: dropTable(DROP_ONE_TO_ONE_B);
077: dropTable(DROP_ONE_OWNING);
078: dropTable(DROP_ONE_INVERSE);
079: dropTable(DROP_MANY_OWNING);
080: }
081:
082: public void createAccountTable() throws java.sql.SQLException {
083: createTable(_createAccount, _dropAccount);
084: }
085:
086: public void dropAccountTable() throws java.sql.SQLException {
087: dropTable(_dropAccount);
088: }
089:
090: private void createTable(String create, String drop)
091: throws java.sql.SQLException {
092: try {
093: try {
094: database.execute(drop);
095: } catch (Exception e) {
096: // not concerned
097: }
098: database.execute(create);
099: } catch (RemoteException re) {
100: if (re.detail != null
101: && re.detail instanceof java.sql.SQLException) {
102: throw (java.sql.SQLException) re.detail;
103: } else {
104: throw new java.sql.SQLException("Cannot create table: "
105: + re.getMessage(), create);
106: }
107: }
108: }
109:
110: private void dropTable(String drop) throws java.sql.SQLException {
111: try {
112: database.execute(drop);
113: } catch (RemoteException re) {
114: if (re.detail != null
115: && re.detail instanceof java.sql.SQLException) {
116: throw (java.sql.SQLException) re.detail;
117: } else {
118: throw new java.sql.SQLException(
119: "Unable to drop table: " + re.getMessage(),
120: drop);
121: }
122: }
123: }
124:
125: public void start() throws IllegalStateException {
126: try {
127: Properties properties = TestManager.getServer()
128: .getContextEnvironment();
129: initialContext = new InitialContext(properties);
130: } catch (Exception e) {
131: throw (IllegalStateException) new IllegalStateException(
132: "Cannot create initial context: "
133: + e.getClass().getName() + " "
134: + e.getMessage()).initCause(e);
135: }
136:
137: Object obj = null;
138: DatabaseHome databaseHome = null;
139: try {
140: /* Create database */
141: obj = initialContext.lookup("client/tools/DatabaseHome");
142: databaseHome = (DatabaseHome) javax.rmi.PortableRemoteObject
143: .narrow(obj, DatabaseHome.class);
144: } catch (Exception e) {
145: throw new IllegalStateException(
146: "Cannot find 'client/tools/DatabaseHome': "
147: + e.getClass().getName() + " "
148: + e.getMessage());
149: }
150: try {
151: database = databaseHome.create();
152: } catch (Exception e) {
153: throw new IllegalStateException("Cannot start database: "
154: + e.getClass().getName() + " " + e.getMessage());
155: }
156: }
157:
158: public void stop() throws IllegalStateException {
159: }
160:
161: public void init(Properties props) throws IllegalStateException {
162: }
163: }
|