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.sql.Connection;
024: import java.sql.DriverManager;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.util.Properties;
030:
031: /**
032: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
033: */
034: public class PostgreSqlTestDatabase implements TestDatabase {
035:
036: protected Database database;
037: protected InitialContext initialContext;
038:
039: private static String _createAccount = "CREATE TABLE account ( ssn CHAR(11), first_name CHAR(20), last_name CHAR(20), balance INT, Constraint \"account_pkey\" Primary Key (\"ssn\"))";
040: private static String _dropAccount = "DROP TABLE account";
041: //private static String _createEntity = "CREATE TABLE entity ( id INT NOT NULL, first_name CHAR(20), last_name CHAR(20), Constraint \"entity_pkey\" Primary Key (\"id\") )";
042: private static String _createEntity = "CREATE TABLE entity ( id INT DEFAULT nextval('entity_id_seq') , first_name CHAR(20), last_name CHAR(20), Constraint \"entity_pkey\" Primary Key (\"id\") )";
043: private static String _dropEntity = "DROP TABLE entity";
044:
045: public void createEntityTable() throws java.sql.SQLException {
046: try {
047: database.execute("DROP SEQUENCE entity_id_seq");
048: } catch (Exception e) {
049: // not concerned
050: }
051: try {
052: database.execute(_dropEntity);
053: } catch (Exception e) {
054: // not concerned
055: }
056: try {
057: database.execute("CREATE SEQUENCE entity_id_seq");
058: } catch (Exception e) {
059: // not concerned
060: }
061: try {
062: database.execute(_createEntity);
063: } catch (RemoteException re) {
064: if (re.detail != null
065: && re.detail instanceof java.sql.SQLException) {
066: throw (java.sql.SQLException) re.detail;
067: } else {
068: throw new java.sql.SQLException(
069: "Cannot create entity table: "
070: + re.getMessage(), _createEntity);
071: }
072: }
073: }
074:
075: public void dropEntityTable() throws java.sql.SQLException {
076: try {
077: database.execute("DROP SEQUENCE entity_id_seq");
078: } catch (Exception e) {
079: // not concerned
080: }
081: try {
082: database.execute(_dropEntity);
083: } catch (RemoteException re) {
084: if (re.detail != null
085: && re.detail instanceof java.sql.SQLException) {
086: throw (java.sql.SQLException) re.detail;
087: } else {
088: throw new java.sql.SQLException(
089: "Unable to drop entity table: "
090: + re.getMessage(), _dropEntity);
091: }
092: }
093: }
094:
095: public void createAccountTable() throws java.sql.SQLException {
096: try {
097: database.execute("DROP SEQUENCE account_id_seq");
098: } catch (Exception e) {
099: // not concerned
100: }
101: try {
102: database.execute("DROP TABLE account");
103: } catch (Exception e) {
104: // not concerned
105: }
106: try {
107: database.execute("CREATE SEQUENCE account_id_seq");
108: } catch (Exception e) {
109: // not concerned
110: }
111: try {
112: database.execute(_createAccount);
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: "Cannot create account table: "
120: + re.getMessage(), _createAccount);
121: }
122: }
123: }
124:
125: public void dropAccountTable() throws java.sql.SQLException {
126: try {
127: try {
128: database.execute("DROP SEQUENCE account_id_seq");
129: } catch (Exception e) {
130: // not concerned
131: }
132: database.execute(_dropAccount);
133: } catch (RemoteException re) {
134: if (re.detail != null
135: && re.detail instanceof java.sql.SQLException) {
136: throw (java.sql.SQLException) re.detail;
137: } else {
138: throw new java.sql.SQLException(
139: "Cannot drop account table: " + re.getMessage(),
140: _dropAccount);
141: }
142: }
143: }
144:
145: public void start() throws IllegalStateException {
146: try {
147: Properties properties = TestManager.getServer()
148: .getContextEnvironment();
149: initialContext = new InitialContext(properties);
150:
151: /* Create database */
152: Object obj = initialContext
153: .lookup("client/tools/DatabaseHome");
154: DatabaseHome databaseHome = (DatabaseHome) javax.rmi.PortableRemoteObject
155: .narrow(obj, DatabaseHome.class);
156: database = databaseHome.create();
157: } catch (Exception e) {
158: throw new IllegalStateException("Cannot start database: "
159: + e.getClass().getName() + " " + e.getMessage());
160: }
161: }
162:
163: public void stop() throws IllegalStateException {
164: }
165:
166: public void init(Properties props) throws IllegalStateException {
167: }
168:
169: public static void main(String[] args) {
170: System.out
171: .println("Checking if driver is registered with DriverManager.");
172: try {
173: ClassLoader cl = (ClassLoader) java.security.AccessController
174: .doPrivileged(new java.security.PrivilegedAction() {
175: public Object run() {
176: return Thread.currentThread()
177: .getContextClassLoader();
178: }
179: });
180: Class.forName("org.postgresql.Driver", true, cl);
181: } catch (ClassNotFoundException e) {
182: System.out.println("Couldn't find the driver!");
183: e.printStackTrace();
184: System.exit(1);
185: }
186:
187: System.out
188: .println("Registered the driver, so let's make a connection.");
189:
190: Connection conn = null;
191:
192: try {
193: conn = DriverManager.getConnection(
194: "jdbc:postgresql://localhost/openejbtest",
195: "openejbuser", "javaone");
196: } catch (SQLException e) {
197: System.out.println("Couldn't connect.");
198: e.printStackTrace();
199: System.exit(1);
200: }
201:
202: if (conn == null) {
203: System.out.println("No connection!");
204: }
205:
206: Statement stmt = null;
207:
208: try {
209: stmt = conn.createStatement();
210: } catch (SQLException e) {
211: System.out.println("Couldn't create a statement.");
212: e.printStackTrace();
213: System.exit(1);
214: }
215:
216: ResultSet rs = null;
217:
218: try {
219: stmt.execute("DROP TABLE entity");
220: } catch (SQLException e) {
221: }
222:
223: System.out.println("Creating entity table.");
224: try {
225: stmt.execute(_createEntity);
226: } catch (SQLException e) {
227: System.out.println("Couldn't create the entity table");
228: e.printStackTrace();
229: System.exit(1);
230: }
231:
232: System.out.println("Inserting record.");
233: try {
234: PreparedStatement pstmt = conn
235: .prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)");
236: pstmt.setInt(1, 101);
237: pstmt.setString(2, "Bunson");
238: pstmt.setString(3, "Honeydew");
239: pstmt.executeUpdate();
240: } catch (SQLException e) {
241: System.out.println("Couldn't create the entity table");
242: e.printStackTrace();
243: System.exit(1);
244: }
245:
246: System.out.println("Selecting the record.");
247: try {
248: PreparedStatement pstmt = conn
249: .prepareStatement("select id from entity where first_name = ? AND last_name = ?");
250: pstmt.setString(1, "Bunson");
251: pstmt.setString(2, "Honeydew");
252: ResultSet set = pstmt.executeQuery();
253: } catch (SQLException e) {
254: System.out.println("Couldn't select the entry");
255: e.printStackTrace();
256: System.exit(1);
257: }
258:
259: System.out.println("Dropping the entity table.");
260: try {
261: stmt.execute(_dropEntity);
262: } catch (SQLException e) {
263: System.out.println("Couldn't drop the entity table");
264: e.printStackTrace();
265: System.exit(1);
266: }
267:
268: try {
269: conn.close();
270: } catch (SQLException e) {
271: System.out.println("Couldn't create the sequense");
272: e.printStackTrace();
273: System.exit(1);
274: }
275:
276: }
277: }
|