001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import junit.framework.TestCase;
020: import javax.sql.DataSource;
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.DatabaseMetaData;
025: import java.util.*;
026: import org.apache.log4j.Logger;
027:
028: /**
029: * Provides database and other methods for simpler test cases.
030: * @author Brautigam Robert
031: * @version Revision: $Revision$
032: */
033: public abstract class AbstractPersistenceTest extends TestCase {
034: private static Logger logger = Logger
035: .getLogger(AbstractPersistenceTest.class);
036: protected Store store;
037: protected Connection conn;
038:
039: public AbstractPersistenceTest(String name) throws Exception {
040: super (name);
041: }
042:
043: public static boolean supportsMultipleStores() {
044: // Read properties file
045: ResourceBundle config = ResourceBundle.getBundle("test");
046: // Allocate class (now only postgres supported)
047: String driverclass = config.getString("db.driverclass");
048: String url = config.getString("db.url");
049: // Well, only hsqldb,derby do not support multiple instances!
050: return (url.indexOf("hsqldb") < 0)
051: && (url.indexOf("derby") < 0);
052: }
053:
054: protected void setUp() throws Exception {
055: // Allocate objects
056: store = getStore();
057: conn = store.getContext().getDatabase().getConnectionSource()
058: .getConnection();
059: }
060:
061: protected void tearDown() {
062: store.close();
063: }
064:
065: public Connection getConnection() {
066: return conn;
067: }
068:
069: public Store getStore() throws Exception {
070: // Read properties file
071: ResourceBundle config = ResourceBundle.getBundle("test");
072: // Allocate class (now only postgres supported)
073: String driverclass = config.getString("db.driverclass");
074: String url = config.getString("db.url");
075: // Return
076: return new Store(driverclass, url);
077: }
078:
079: protected int getCount(String table) throws Exception {
080: PreparedStatement pstmt = conn
081: .prepareStatement("select count(*) from " + table);
082: ResultSet rs = pstmt.executeQuery();
083: int result = -1;
084: if (rs.next())
085: result = rs.getInt(1);
086: rs.close();
087: pstmt.close();
088: return result;
089: }
090:
091: protected void dropTables(String pattern) throws Exception {
092: DatabaseMetaData dmd = conn.getMetaData();
093: String databaseName = dmd.getDatabaseProductName();
094: ResultSet rs;
095: if (databaseName.equalsIgnoreCase("oracle")
096: || databaseName
097: .equalsIgnoreCase("hsql database engine")
098: || databaseName.equalsIgnoreCase("apache derby"))
099: rs = dmd.getTables(null, null, pattern.toUpperCase(),
100: new String[] { "TABLE" });
101: else
102: rs = dmd.getTables(null, null, pattern,
103: new String[] { "TABLE" });
104: logger.debug("dropping tables matching: " + pattern);
105: while (rs.next()) {
106: // Delete found tables
107: String tableName = rs.getString("TABLE_NAME");
108: logger.debug("found table: " + tableName + ", will drop.");
109: PreparedStatement pstmt = conn
110: .prepareStatement("drop table " + tableName);
111: pstmt.executeUpdate();
112: pstmt.close();
113: }
114: rs.close();
115: logger.debug("all tables matching '" + pattern
116: + "' were dropped.");
117: conn.commit();
118: }
119:
120: /**
121: * Utility function, returns the number of objects which are exactly
122: * the given class.
123: */
124: public int getCount(Collection col, Class type) {
125: int result = 0;
126: Iterator it = col.iterator();
127: while (it.hasNext()) {
128: Object obj = it.next();
129: if (obj.getClass().equals(type))
130: result++;
131: }
132: return result;
133: }
134:
135: protected boolean isSuccessful(final Runnable r) {
136: try {
137: final Vector exceptions = new Vector();
138: Thread thread = new Thread(new Runnable() {
139: public void run() {
140: try {
141: r.run();
142: } catch (Exception e) {
143: logger.debug("exception while running thread",
144: e);
145: exceptions.add(e);
146: }
147: }
148: });
149: thread.start();
150: thread.join();
151: return exceptions.size() == 0;
152: } catch (Exception e) {
153: fail("exception caught in thread test: " + e.getMessage());
154: }
155: return false;
156: }
157: }
|