From 91f6b980ee1762a034eb2481805656d1f59761ca Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 4 Apr 2010 11:42:51 +0000 Subject: [PATCH] --- .../persistence/CompositeJpaCustomizer.java | 33 +++++++++++++++++++ .../persistence/CompositeJpaTables.java | 31 +++++++++++++++++ .../wamblee/support/persistence/Database.java | 2 -- .../support/persistence/DatabaseUtils.java | 5 --- .../support/persistence/JpaBuilder.java | 14 +++++--- .../support/persistence/JpaCustomizer.java | 22 +++++++++++++ .../persistence/JpaCustomizerBuilder.java | 20 +++++++++++ .../support/persistence/JpaTester.java | 2 +- .../PersistenceUnitDescription.java | 9 +---- .../support/persistence/ToplinkTables.java | 20 ----------- 10 files changed, 118 insertions(+), 40 deletions(-) create mode 100644 support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaCustomizer.java create mode 100644 support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaTables.java create mode 100644 support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizer.java create mode 100644 support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizerBuilder.java delete mode 100644 support/test/src/main/java/org/wamblee/support/persistence/ToplinkTables.java diff --git a/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaCustomizer.java b/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaCustomizer.java new file mode 100644 index 00000000..fc6ecc7c --- /dev/null +++ b/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaCustomizer.java @@ -0,0 +1,33 @@ +package org.wamblee.support.persistence; + +import java.util.List; +import java.util.Map; + +import org.dbunit.dataset.filter.ITableFilterSimple; + +public class CompositeJpaCustomizer implements JpaCustomizer { + + private List _customizers; + private CompositeJpaTables _tables; + + public CompositeJpaCustomizer(List aCustomizers) { + _customizers = aCustomizers; + _tables = new CompositeJpaTables(); + for (JpaCustomizer customizer: _customizers) { + _tables.add(customizer.getJpaTables()); + } + } + + @Override + public void customize(PersistenceUnitDescription aPersistenceUnit, Map aJpaProperties) { + for (JpaCustomizer customizer: _customizers) { + customizer.customize(aPersistenceUnit, aJpaProperties); + } + } + + @Override + public ITableFilterSimple getJpaTables() { + return _tables; + } + +} diff --git a/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaTables.java b/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaTables.java new file mode 100644 index 00000000..7a7e8de6 --- /dev/null +++ b/support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaTables.java @@ -0,0 +1,31 @@ +package org.wamblee.support.persistence; + +import java.util.ArrayList; +import java.util.List; + +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.filter.ITableFilterSimple; + +public class CompositeJpaTables implements ITableFilterSimple { + + private List _tables; + + public CompositeJpaTables() { + _tables = new ArrayList(); + } + + public void add(ITableFilterSimple aFilter) { + _tables.add(aFilter); + } + + @Override + public boolean accept(String aTableName) throws DataSetException { + for (ITableFilterSimple filter: _tables) { + if (filter.accept(aTableName)) { + return true; + } + } + return false; + } + +} diff --git a/support/test/src/main/java/org/wamblee/support/persistence/Database.java b/support/test/src/main/java/org/wamblee/support/persistence/Database.java index 56122ed1..f37474ea 100755 --- a/support/test/src/main/java/org/wamblee/support/persistence/Database.java +++ b/support/test/src/main/java/org/wamblee/support/persistence/Database.java @@ -15,8 +15,6 @@ */ package org.wamblee.support.persistence; -import java.util.List; - import javax.sql.DataSource; /** diff --git a/support/test/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java b/support/test/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java index 781764be..312e38a0 100644 --- a/support/test/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java +++ b/support/test/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java @@ -5,10 +5,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.TreeMap; import java.util.logging.Logger; import javax.sql.DataSource; @@ -16,8 +13,6 @@ import javax.sql.DataSource; import junit.framework.TestCase; import org.dbunit.DataSourceDatabaseTester; -import org.dbunit.DatabaseTestCase; -import org.dbunit.DatabaseUnitException; import org.dbunit.IDatabaseTester; import org.dbunit.database.DatabaseConnection; import org.dbunit.database.DatabaseSequenceFilter; diff --git a/support/test/src/main/java/org/wamblee/support/persistence/JpaBuilder.java b/support/test/src/main/java/org/wamblee/support/persistence/JpaBuilder.java index b46ffa3a..e6943197 100644 --- a/support/test/src/main/java/org/wamblee/support/persistence/JpaBuilder.java +++ b/support/test/src/main/java/org/wamblee/support/persistence/JpaBuilder.java @@ -2,6 +2,7 @@ package org.wamblee.support.persistence; import java.sql.SQLException; import java.util.Map; +import java.util.ServiceLoader; import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,6 +15,7 @@ import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.sql.DataSource; +import org.hibernate.ejb.HibernatePersistence; import org.wamblee.support.jndi.StubInitialContextFactory; import org.wamblee.support.persistence.toplink.JndiSessionCustomizer; @@ -100,11 +102,15 @@ public class JpaBuilder { */ public EntityManagerFactory createFactory() { Map jpaProps = new TreeMap(); - jpaProps.put("toplink.session.customizer", JndiSessionCustomizer.class - .getName()); - jpaProps.put("toplink.ddl-generation", "create-tables"); - return Persistence.createEntityManagerFactory(persistenceUnit + + JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit, jpaProps); + + jpaProps.put("javax.persistence.provider", HibernatePersistence.class.getName() +"unknown"); + EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnit .getUnitName(), jpaProps); + + LOGGER.info("Using " + factory.getClass()); + return factory; } /** diff --git a/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizer.java b/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizer.java new file mode 100644 index 00000000..79303012 --- /dev/null +++ b/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizer.java @@ -0,0 +1,22 @@ +package org.wamblee.support.persistence; + +import java.util.Map; +import java.util.ServiceLoader; + +import org.dbunit.dataset.filter.ITableFilterSimple; + +/** + * JPA customizer is used to customize properties for a given JPA implementation. + * + * Implementations of JpaCustomizer are found using {@link ServiceLoader}. In case + * of testing with a specific JPA provider, the customizer library for that JPA provider + * must be on the classpath as well. + * + * @author Erik Brakkee + */ +public interface JpaCustomizer { + + void customize(PersistenceUnitDescription aPersistenceUnit, Map aJpaProperties); + + ITableFilterSimple getJpaTables(); +} diff --git a/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizerBuilder.java b/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizerBuilder.java new file mode 100644 index 00000000..a824bf6f --- /dev/null +++ b/support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizerBuilder.java @@ -0,0 +1,20 @@ +package org.wamblee.support.persistence; + +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; + +public class JpaCustomizerBuilder { + + private static final ServiceLoader CUSTOMIZERS = + ServiceLoader.load(JpaCustomizer.class); + + + public static JpaCustomizer getCustomizer() { + List customizers = new ArrayList(); + for (JpaCustomizer customizer: CUSTOMIZERS) { + customizers.add(customizer); + } + return new CompositeJpaCustomizer(customizers); + } +} diff --git a/support/test/src/main/java/org/wamblee/support/persistence/JpaTester.java b/support/test/src/main/java/org/wamblee/support/persistence/JpaTester.java index c0a22cd3..a6a512f1 100644 --- a/support/test/src/main/java/org/wamblee/support/persistence/JpaTester.java +++ b/support/test/src/main/java/org/wamblee/support/persistence/JpaTester.java @@ -63,7 +63,7 @@ public class JpaTester { dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables()); dbUtils.dropTables(); - dbUtils.dropTables(persistenceUnit.getJpaTables()); + dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables()); jpaBuilder = new JpaBuilder(dataSource, persistenceUnit); jpaBuilder.start(); diff --git a/support/test/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java b/support/test/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java index 502f43db..0d053bc0 100644 --- a/support/test/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java +++ b/support/test/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.List; import org.dbunit.dataset.filter.ITableFilterSimple; +import org.wamblee.support.persistence.toplink.ToplinkTables; public class PersistenceUnitDescription { @@ -29,12 +30,4 @@ public class PersistenceUnitDescription { public ITableFilterSimple getTables() { return tables; } - - /** - * JPA provider specific tables. - * @return - */ - public ITableFilterSimple getJpaTables() { - return new ToplinkTables(); - } } diff --git a/support/test/src/main/java/org/wamblee/support/persistence/ToplinkTables.java b/support/test/src/main/java/org/wamblee/support/persistence/ToplinkTables.java deleted file mode 100644 index 4d45d0f4..00000000 --- a/support/test/src/main/java/org/wamblee/support/persistence/ToplinkTables.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.wamblee.support.persistence; - -import java.util.Arrays; -import java.util.List; - -import org.dbunit.dataset.DataSetException; -import org.dbunit.dataset.filter.ITableFilterSimple; - -/** - * Toplink-specific tables. - */ -public class ToplinkTables implements ITableFilterSimple { - - private static final List TABLES = Arrays.asList(new String[] { "SEQUENCE" } ); - - public boolean accept(String aTableName) throws DataSetException { - return TABLES.contains(aTableName); - } - -} -- 2.31.1