The JPA providers no drop the tables themselves.
This is also more robust.
The databasetester should now be created through the dbutils instead of
through the JPA support.
for (final Method method : methods) {
if (method.getName().startsWith("test")) {
- userAdminTester.getJpaTester().getDbUtils().cleanDatabase();
+ userAdminTester.getJpaTester().getDbUtils().cleanDatabase(new SecurityTables());
clearUserCache();
userAdminTester.getJpaTester().getDbUtils().executeInTransaction(
new JdbcUnitOfWork<Void>() {
*
*/
public void testVerifyAddRemove() throws Exception {
- jpaTester.getDbUtils().cleanDatabase(); // super class setup always creates one group.
+ jpaTester.getDbUtils().cleanDatabase(new SecurityTables()); // super class setup always creates one group.
GroupSet groups = getGroups();
assertEquals(0, groups.size());
package org.wamblee.security.authentication.jpa;
-import org.dbunit.dataset.DataSetException;
-import org.dbunit.dataset.filter.ITableFilterSimple;
import org.wamblee.support.persistence.PersistenceUnitDescription;
public class SecurityPersistenceUnit extends PersistenceUnitDescription {
public SecurityPersistenceUnit() {
- super("jdbc/security", "securitytest", new ITableFilterSimple() {
-
- @Override
- public boolean accept(String aTableName) throws DataSetException {
- return aTableName.startsWith("SEC_");
- }
- });
+ super("jdbc/security", "securitytest");
}
}
*/
private static final String SCHEMA_PATTERN = "%";
private DataSource dataSource;
- private ITableFilterSimple tables;
private IDatabaseTester dbtester;
/**
/**
* Constructs the database utils.
* Before use, {@link #start()} must be called.
- * @param aDataSource Datasource.
- * @param aTables Tables to work with.
+ * @param aDataSource Datasource.
*/
- public DatabaseUtils(DataSource aDataSource, ITableFilterSimple aTables) {
+ public DatabaseUtils(DataSource aDataSource) {
dataSource = aDataSource;
- tables = aTables;
dbtester = new DataSourceDatabaseTester(dataSource);
connections = new ArrayList<IDatabaseConnection>();
}
connections.clear();
}
- public IDatabaseTester createDbTester() throws Exception {
- return createDbTester(getTableNames(tables));
+ public IDatabaseTester createDbTester(ITableFilterSimple aTables) throws Exception {
+ return createDbTester(getTableNames(aTables));
}
public IDatabaseTester createDbTester(String[] aTables) throws Exception {
return dbtester;
}
- public void cleanDatabase() throws Exception {
- cleanDatabase(tables);
- }
-
public void executeOnTables(ITableFilterSimple aTables,
final TableSetOperation aOperation) throws Exception {
final String[] tableNames = getTableNames(aTables);
connection.close();
}
}
-
- public String[] getTableNames() throws Exception {
- return getTableNames(tables);
- }
-
+
/**
* @throws SQLException
*/
}
}
- public void emptyTables() throws Exception {
- executeOnTables(tables, new TableSetOperation() {
- public void execute(String aTable) throws Exception {
- emptyTable(aTable);
- }
- });
- }
-
/**
* @return
* @throws SQLException
executeSql("delete from " + aTable);
}
- public void dropTables() throws Exception {
- dropTables(tables);
- }
-
public void dropTables(ITableFilterSimple aTables) throws Exception {
final String[] tableNames = getTableNames(aTables);
String[] sortedTables = executeInTransaction(new JdbcUnitOfWork<String[]>() {
throw new RuntimeException("JNDI problem", e);
}
- dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
+ dbUtils = new DatabaseUtils(dataSource);
dbUtils.start();
- dbUtils.dropTables();
dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
jpaBuilder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
jpaBuilder.start();
-
- // db tester should be created after Jpa builder because jpa builder
- // creates the
- // tables that the tester looks at when it is initialized.
- dbTester = dbUtils.createDbTester();
}
/**
return dataSource;
}
- public IDatabaseTester getDbTester() {
- return dbTester;
- }
-
public DatabaseUtils getDbUtils() {
return dbUtils;
}
*/
package org.wamblee.support.persistence;
-import org.dbunit.dataset.filter.ITableFilterSimple;
/**
* Describes a persistence unit.
private String jndiName;
private String unitName;
- private ITableFilterSimple tables;
/**
* Constructs the description.
* @param aJndiName Jndi name.
* @param aUnitName Persistence unit name.
- * @param aTables Tables to delete.
*/
- public PersistenceUnitDescription(String aJndiName, String aUnitName,
- ITableFilterSimple aTables) {
+ public PersistenceUnitDescription(String aJndiName, String aUnitName) {
jndiName = aJndiName;
unitName = aUnitName;
- tables = aTables;
}
/**
public String getUnitName() {
return unitName;
}
-
- /**
- * Tables to delete.
- * @return Tables.
- */
- public ITableFilterSimple getTables() {
- return tables;
- }
}
package org.wamblee.support.persistence;
+import static junit.framework.Assert.*;
+
import javax.sql.DataSource;
-import org.dbunit.dataset.DataSetException;
-import org.dbunit.dataset.filter.ITableFilterSimple;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import static junit.framework.TestCase.*;
-
public class DatabaseUtilsLeakTest {
private Database db;
db = DatabaseBuilder.getDatabase();
DataSource ds = db.start();
- dbutils = new DatabaseUtils(ds, new ITableFilterSimple() {
- @Override
- public boolean accept(String aTableName) throws DataSetException {
- return false;
- }
- });
+ dbutils = new DatabaseUtils(ds);
}
@After
@Test
public void testLeak() throws Exception {
assertEquals(0, db.getActiveConnections());
- dbutils.dropTables();
+ dbutils.dropTables(new MyTables());
assertEquals(0, db.getActiveConnections());
}
persistenceUnit = new MyPersistenceUnit();
- dbutils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
- dbutils.dropTables();
+ dbutils = new DatabaseUtils(dataSource);
+ dbutils.dropTables(new MyTables());
dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
builder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
builder.start();
assertEquals(0, db.getActiveConnections());
- dbtester = dbutils.createDbTester();
+ dbtester = dbutils.createDbTester(new MyTables());
}
@After
@Test
public void testTablesCorrect() throws Exception {
- String[] tables = dbutils.getTableNames();
+ String[] tables = dbutils.getTableNames(new MyTables());
assertEquals(1, tables.length);
assertEquals("XYZ_MYENTITY", tables[0]);
}
@Test
public void testDeleteTables() throws Exception {
- String[] tables = dbutils.getTableNames();
+ String[] tables = dbutils.getTableNames(new MyTables());
assertEquals(1, tables.length);
assertEquals("XYZ_MYENTITY", tables[0]);
assertEquals(1, table.getRowCount());
// Clean the database
- dbutils.cleanDatabase();
+ dbutils.cleanDatabase(new MyTables());
table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
assertEquals(0, table.getRowCount());
// Now drop the database
- dbutils.dropTables();
+ dbutils.dropTables(new MyTables());
dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
- tables = dbutils.getTableNames();
+ tables = dbutils.getTableNames(new MyTables());
assertEquals(0, tables.length);
}
// but we don't need it. We can use datbase utils if we want to execute
// straight JDBC calls.
builder = jpaTester.getJpaBuilder();
- dbtester = jpaTester.getDbTester();
+ dbtester = jpaTester.getDbUtils().createDbTester(new MyTables());
dbutils = jpaTester.getDbUtils();
dbutils.start();
}
private static final String PERSISTENCE_UNIT_NAME = "org.wamblee.jee.support-test";
public MyPersistenceUnit() {
- super(JNDI_NAME, PERSISTENCE_UNIT_NAME, new MyTables());
+ super(JNDI_NAME, PERSISTENCE_UNIT_NAME);
}
}