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 org.apache.openejb.test.beans.Database;
019: import org.apache.openejb.test.beans.DatabaseHome;
020:
021: import javax.naming.InitialContext;
022: import java.rmi.RemoteException;
023: import java.util.Properties;
024:
025: /**
026: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
027: *
028: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
029: */
030: public class DerbyTestDatabase implements TestDatabase {
031:
032: protected Database database;
033: protected InitialContext initialContext;
034:
035: private static String _createAccount = "CREATE TABLE account ( ssn VARCHAR(25), first_name VARCHAR(256), last_name VARCHAR(256), balance integer)";
036:
037: private static String _dropAccount = "DROP TABLE account";
038:
039: private static String _createEntity = "CREATE TABLE entity ( id integer GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), first_name VARCHAR(256), last_name VARCHAR(256) )";
040:
041: private static String _dropEntity = "DROP TABLE entity";
042:
043: static {
044: System.setProperty("noBanner", "true");
045: }
046:
047: public void createEntityTable() throws java.sql.SQLException {
048: try {
049: try {
050: database.execute(DerbyTestDatabase._dropEntity);
051: } catch (Exception e) {
052: // not concerned
053: }
054: database.execute(DerbyTestDatabase._createEntity);
055: } catch (RemoteException re) {
056: if (re.detail != null
057: && re.detail instanceof java.sql.SQLException) {
058: throw (java.sql.SQLException) re.detail;
059: } else {
060: throw new java.sql.SQLException(
061: "Cannot create entity table: "
062: + re.getMessage(),
063: DerbyTestDatabase._createEntity);
064: }
065: }
066: }
067:
068: public void dropEntityTable() throws java.sql.SQLException {
069: try {
070: database.execute(DerbyTestDatabase._dropEntity);
071: } catch (RemoteException re) {
072: if (re.detail != null
073: && re.detail instanceof java.sql.SQLException) {
074: throw (java.sql.SQLException) re.detail;
075: } else {
076: throw new java.sql.SQLException(
077: "Unable to drop entity table: "
078: + re.getMessage(),
079: DerbyTestDatabase._dropEntity);
080: }
081: }
082: }
083:
084: public void createAccountTable() throws java.sql.SQLException {
085: try {
086: try {
087: database.execute(DerbyTestDatabase._dropAccount);
088: } catch (Exception e) {
089: // not concerned
090: }
091: database.execute(DerbyTestDatabase._createAccount);
092: } catch (RemoteException re) {
093: if (re.detail != null
094: && re.detail instanceof java.sql.SQLException) {
095: throw (java.sql.SQLException) re.detail;
096: } else {
097: throw new java.sql.SQLException(
098: "Cannot create account table: "
099: + re.getMessage(),
100: DerbyTestDatabase._createAccount);
101: }
102: }
103: }
104:
105: public void dropAccountTable() throws java.sql.SQLException {
106: try {
107: database.execute(DerbyTestDatabase._dropAccount);
108: } catch (RemoteException re) {
109: if (re.detail != null
110: && re.detail instanceof java.sql.SQLException) {
111: throw (java.sql.SQLException) re.detail;
112: } else {
113: throw new java.sql.SQLException(
114: "Cannot drop account table: " + re.getMessage(),
115: DerbyTestDatabase._dropAccount);
116: }
117: }
118: }
119:
120: public void start() throws IllegalStateException {
121: try {
122: Properties properties = TestManager.getServer()
123: .getContextEnvironment();
124: initialContext = new InitialContext(properties);
125: } catch (Exception e) {
126: throw (IllegalStateException) new IllegalStateException(
127: "Cannot create initial context: "
128: + e.getClass().getName() + " "
129: + e.getMessage()).initCause(e);
130: }
131:
132: Object obj = null;
133: DatabaseHome databaseHome = null;
134: try {
135: /* Create database */
136: obj = initialContext.lookup("client/tools/DatabaseHome");
137: databaseHome = (DatabaseHome) javax.rmi.PortableRemoteObject
138: .narrow(obj, DatabaseHome.class);
139: } catch (Exception e) {
140: throw new IllegalStateException(
141: "Cannot find 'client/tools/DatabaseHome': "
142: + e.getClass().getName() + " "
143: + e.getMessage());
144: }
145: try {
146: database = databaseHome.create();
147: } catch (Exception e) {
148: throw new IllegalStateException("Cannot start database: "
149: + e.getClass().getName() + " " + e.getMessage());
150: }
151: }
152:
153: public void stop() throws IllegalStateException {
154: }
155:
156: public void init(Properties props) throws IllegalStateException {
157: }
158: }
|