001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.test;
018:
019: import org.apache.openejb.test.beans.Database;
020: import org.apache.openejb.test.beans.DatabaseHome;
021:
022: import javax.naming.InitialContext;
023: import java.rmi.RemoteException;
024: import java.util.Properties;
025:
026: public class HsqldbTestDatabase implements TestDatabase {
027:
028: protected Database database;
029: protected InitialContext initialContext;
030:
031: private static String _createAccount = "CREATE TABLE account ( ssn VARCHAR(255), first_name VARCHAR(255), last_name VARCHAR(255), balance integer)";
032:
033: private static String _dropAccount = "DROP TABLE account";
034:
035: private static String _createEntity = "CREATE TABLE entity ( id IDENTITY, first_name VARCHAR(255), last_name VARCHAR(255) )";
036:
037: private static String _dropEntity = "DROP TABLE entity";
038:
039: // CmrMapping
040: private static final String CREATE_ONE_OWNING = "CREATE TABLE oneowning (col_id INTEGER, col_field1 INTEGER)";
041: private static final String DROP_ONE_OWNING = "DROP TABLE oneowning";
042: private static final String CREATE_ONE_INVERSE = "CREATE TABLE oneinverse (col_id INTEGER)";
043: private static final String DROP_ONE_INVERSE = "DROP TABLE oneinverse";
044: private static final String CREATE_MANY_OWNING = "CREATE TABLE manyowning (col_id INTEGER, col_field1 INTEGER)";
045: private static final String DROP_MANY_OWNING = "DROP TABLE manyowning";
046:
047: // Automatically created tables - these must be dropped before the tests run or you will get a duplicate key exception
048: private static final String[] AUTO_CREATED_TABLES = new String[] {
049: "BasicCmpBean", "BasicCmpBeanX", "ComplexCmpBean",
050: "ComplexCmpBeanX", "UnknownCmpBean", "UnknownCmpBeanX",
051: "BasicCmp2Bean", "ComplexCmp2Bean", "UnknownCmp2Bean",
052: "AOBasicCmpBean", "AllowedOperationsCmp2Bean",
053: "EncCmpBean", "EncCmp2Bean", "ContextLookupCmpBean",
054: "Cmp_RMI_IIOP_Bean", "RmiIiopCmp2Bean", "Person",
055: "License", "ComplexPerson", "ComplexLicense", "Artist",
056: "Song", "ComplexArtist", "Complexsong", "Game", "Platform",
057: "ComplexGame", "ComplexPlatform", "Query", "QueryData", };
058:
059: static {
060: System.setProperty("noBanner", "true");
061: }
062:
063: public void createEntityTable() throws java.sql.SQLException {
064: createTable(_createEntity, _dropEntity);
065: createTable(CREATE_ONE_OWNING, DROP_ONE_OWNING);
066: createTable(CREATE_ONE_INVERSE, DROP_ONE_INVERSE);
067: createTable(CREATE_MANY_OWNING, DROP_MANY_OWNING);
068: clearTables(AUTO_CREATED_TABLES);
069: }
070:
071: public void dropEntityTable() throws java.sql.SQLException {
072: dropTable(_dropEntity);
073: dropTable(DROP_ONE_OWNING);
074: dropTable(DROP_ONE_INVERSE);
075: dropTable(DROP_MANY_OWNING);
076: clearTables(AUTO_CREATED_TABLES);
077: }
078:
079: private void createTable(String create, String drop)
080: throws java.sql.SQLException {
081: try {
082: try {
083: database.execute(drop);
084: } catch (Exception e) {
085: // not concerned
086: }
087: database.execute(create);
088: } catch (RemoteException re) {
089: if (re.detail != null
090: && re.detail instanceof java.sql.SQLException) {
091: throw (java.sql.SQLException) re.detail;
092: } else {
093: throw new java.sql.SQLException("Cannot create table: "
094: + re.getMessage(), create);
095: }
096: }
097: }
098:
099: private void clearTables(String... autoCreatedTables) {
100: for (String tableName : autoCreatedTables) {
101: try {
102: database.execute("DELETE FROM " + tableName);
103: } catch (Exception e) {
104: // not concerned
105: }
106: }
107: }
108:
109: private void dropTable(String drop) throws java.sql.SQLException {
110: try {
111: database.execute(drop);
112: } catch (RemoteException re) {
113: if (re.detail != null
114: && re.detail instanceof java.sql.SQLException) {
115: throw (java.sql.SQLException) re.detail;
116: } else {
117: throw new java.sql.SQLException(
118: "Unable to drop table: " + re.getMessage(),
119: drop);
120: }
121: }
122: }
123:
124: public void createAccountTable() throws java.sql.SQLException {
125: try {
126: try {
127: database.execute(_dropAccount);
128: } catch (Exception e) {
129: // not concerned
130: }
131: database.execute(_createAccount);
132: } catch (RemoteException re) {
133: if (re.detail != null
134: && re.detail instanceof java.sql.SQLException) {
135: throw (java.sql.SQLException) re.detail;
136: } else {
137: throw new java.sql.SQLException(
138: "Cannot create account table: "
139: + re.getMessage(), _createAccount);
140: }
141: }
142: }
143:
144: public void dropAccountTable() throws java.sql.SQLException {
145: try {
146: database.execute(_dropAccount);
147: } catch (RemoteException re) {
148: if (re.detail != null
149: && re.detail instanceof java.sql.SQLException) {
150: throw (java.sql.SQLException) re.detail;
151: } else {
152: throw new java.sql.SQLException(
153: "Cannot drop account table: " + re.getMessage(),
154: _dropAccount);
155: }
156: }
157: }
158:
159: public void start() throws IllegalStateException {
160: try {
161: TestServer server = TestManager.getServer();
162: Properties properties = server.getContextEnvironment();
163: initialContext = new InitialContext(properties);
164: } catch (Exception e) {
165: throw (IllegalStateException) new IllegalStateException(
166: "Cannot create initial context: "
167: + e.getClass().getName() + " "
168: + e.getMessage()).initCause(e);
169: }
170:
171: final String databaseHomeJndiName = "client/tools/DatabaseHome";
172:
173: Object obj = null;
174: DatabaseHome databaseHome = null;
175: try {
176: /* Create database */
177: obj = initialContext.lookup(databaseHomeJndiName);
178: databaseHome = (DatabaseHome) javax.rmi.PortableRemoteObject
179: .narrow(obj, DatabaseHome.class);
180: } catch (Exception e) {
181: throw new IllegalStateException("Cannot find "
182: + databaseHomeJndiName + ": "
183: + e.getClass().getName() + " " + e.getMessage());
184: }
185: try {
186: database = databaseHome.create();
187: } catch (Exception e) {
188: throw new IllegalStateException("Cannot start database: "
189: + e.getClass().getName() + " " + e.getMessage());
190: }
191: }
192:
193: public void stop() throws IllegalStateException {
194: }
195:
196: public void init(Properties props) throws IllegalStateException {
197: }
198: }
|