From c63e76e400f06f51ba235b9e6658bfa3149c1d48 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 5 Jul 2010 14:36:13 +0000 Subject: [PATCH] Removed the table names from the persistence unit description. 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. --- .../jpa/JpaUserAdministrationTest.java | 2 +- .../authentication/jpa/JpaUserSetTest.java | 2 +- .../jpa/SecurityPersistenceUnit.java | 10 +----- .../support/persistence/DatabaseUtils.java | 33 +++---------------- .../support/persistence/JpaTester.java | 12 +------ .../PersistenceUnitDescription.java | 15 +-------- .../persistence/DatabaseUtilsLeakTest.java | 15 +++------ .../persistence/DatabaseUtilsTestBase.java | 16 ++++----- .../persistence/MyEntityExampleTestBase.java | 2 +- .../persistence/MyPersistenceUnit.java | 2 +- 10 files changed, 24 insertions(+), 85 deletions(-) diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java index c5bd6dbb..2a0048e9 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserAdministrationTest.java @@ -72,7 +72,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { 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() { diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java index f55048d6..e5430fb6 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/JpaUserSetTest.java @@ -230,7 +230,7 @@ public class JpaUserSetTest extends InMemoryUserSetTest { * */ 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()); diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java index ce82c9ec..d41500ac 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/SecurityPersistenceUnit.java @@ -1,18 +1,10 @@ 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"); } } diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java index 23938768..b57cd461 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java @@ -65,7 +65,6 @@ public class DatabaseUtils { */ private static final String SCHEMA_PATTERN = "%"; private DataSource dataSource; - private ITableFilterSimple tables; private IDatabaseTester dbtester; /** @@ -77,12 +76,10 @@ public class DatabaseUtils { /** * 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(); } @@ -111,8 +108,8 @@ public class DatabaseUtils { 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 { @@ -122,10 +119,6 @@ public class DatabaseUtils { return dbtester; } - public void cleanDatabase() throws Exception { - cleanDatabase(tables); - } - public void executeOnTables(ITableFilterSimple aTables, final TableSetOperation aOperation) throws Exception { final String[] tableNames = getTableNames(aTables); @@ -170,11 +163,7 @@ public class DatabaseUtils { connection.close(); } } - - public String[] getTableNames() throws Exception { - return getTableNames(tables); - } - + /** * @throws SQLException */ @@ -201,14 +190,6 @@ public class DatabaseUtils { } } - public void emptyTables() throws Exception { - executeOnTables(tables, new TableSetOperation() { - public void execute(String aTable) throws Exception { - emptyTable(aTable); - } - }); - } - /** * @return * @throws SQLException @@ -230,10 +211,6 @@ public class DatabaseUtils { 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() { diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaTester.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaTester.java index 19f7f187..e74003f1 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaTester.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaTester.java @@ -94,18 +94,12 @@ public class JpaTester { 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(); } /** @@ -131,10 +125,6 @@ public class JpaTester { return dataSource; } - public IDatabaseTester getDbTester() { - return dbTester; - } - public DatabaseUtils getDbUtils() { return dbUtils; } diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java index 4cd06d04..c97667b6 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java @@ -15,7 +15,6 @@ */ package org.wamblee.support.persistence; -import org.dbunit.dataset.filter.ITableFilterSimple; /** * Describes a persistence unit. @@ -26,19 +25,15 @@ public class PersistenceUnitDescription { 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; } /** @@ -54,12 +49,4 @@ public class PersistenceUnitDescription { public String getUnitName() { return unitName; } - - /** - * Tables to delete. - * @return Tables. - */ - public ITableFilterSimple getTables() { - return tables; - } } diff --git a/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsLeakTest.java b/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsLeakTest.java index 75cabee6..b7770b0a 100644 --- a/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsLeakTest.java +++ b/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsLeakTest.java @@ -1,15 +1,13 @@ 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; @@ -20,12 +18,7 @@ public class DatabaseUtilsLeakTest { 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 @@ -36,7 +29,7 @@ public class DatabaseUtilsLeakTest { @Test public void testLeak() throws Exception { assertEquals(0, db.getActiveConnections()); - dbutils.dropTables(); + dbutils.dropTables(new MyTables()); assertEquals(0, db.getActiveConnections()); } diff --git a/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsTestBase.java b/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsTestBase.java index 1092149e..79745297 100644 --- a/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsTestBase.java +++ b/test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsTestBase.java @@ -47,15 +47,15 @@ public class DatabaseUtilsTestBase { 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 @@ -69,14 +69,14 @@ public class DatabaseUtilsTestBase { @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]); @@ -94,14 +94,14 @@ public class DatabaseUtilsTestBase { 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); } diff --git a/test/enterprise/src/test/java/org/wamblee/support/persistence/MyEntityExampleTestBase.java b/test/enterprise/src/test/java/org/wamblee/support/persistence/MyEntityExampleTestBase.java index f6da97e8..c3771775 100644 --- a/test/enterprise/src/test/java/org/wamblee/support/persistence/MyEntityExampleTestBase.java +++ b/test/enterprise/src/test/java/org/wamblee/support/persistence/MyEntityExampleTestBase.java @@ -61,7 +61,7 @@ public class MyEntityExampleTestBase { // 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(); } diff --git a/test/enterprise/src/test/java/org/wamblee/support/persistence/MyPersistenceUnit.java b/test/enterprise/src/test/java/org/wamblee/support/persistence/MyPersistenceUnit.java index 7a09d245..5b1b278b 100644 --- a/test/enterprise/src/test/java/org/wamblee/support/persistence/MyPersistenceUnit.java +++ b/test/enterprise/src/test/java/org/wamblee/support/persistence/MyPersistenceUnit.java @@ -25,6 +25,6 @@ public class MyPersistenceUnit extends PersistenceUnitDescription { 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); } } -- 2.31.1