--- /dev/null
+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<JpaCustomizer> _customizers;
+ private CompositeJpaTables _tables;
+
+ public CompositeJpaCustomizer(List<JpaCustomizer> aCustomizers) {
+ _customizers = aCustomizers;
+ _tables = new CompositeJpaTables();
+ for (JpaCustomizer customizer: _customizers) {
+ _tables.add(customizer.getJpaTables());
+ }
+ }
+
+ @Override
+ public void customize(PersistenceUnitDescription aPersistenceUnit, Map<String, String> aJpaProperties) {
+ for (JpaCustomizer customizer: _customizers) {
+ customizer.customize(aPersistenceUnit, aJpaProperties);
+ }
+ }
+
+ @Override
+ public ITableFilterSimple getJpaTables() {
+ return _tables;
+ }
+
+}
--- /dev/null
+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<ITableFilterSimple> _tables;
+
+ public CompositeJpaTables() {
+ _tables = new ArrayList<ITableFilterSimple>();
+ }
+
+ 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;
+ }
+
+}
*/
package org.wamblee.support.persistence;
-import java.util.List;
-
import javax.sql.DataSource;
/**
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;
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;
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;
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;
*/
public EntityManagerFactory createFactory() {
Map<String, String> jpaProps = new TreeMap<String, String>();
- 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;
}
/**
--- /dev/null
+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<String,String> aJpaProperties);
+
+ ITableFilterSimple getJpaTables();
+}
--- /dev/null
+package org.wamblee.support.persistence;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ServiceLoader;
+
+public class JpaCustomizerBuilder {
+
+ private static final ServiceLoader<JpaCustomizer> CUSTOMIZERS =
+ ServiceLoader.load(JpaCustomizer.class);
+
+
+ public static JpaCustomizer getCustomizer() {
+ List<JpaCustomizer> customizers = new ArrayList<JpaCustomizer>();
+ for (JpaCustomizer customizer: CUSTOMIZERS) {
+ customizers.add(customizer);
+ }
+ return new CompositeJpaCustomizer(customizers);
+ }
+}
dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
dbUtils.dropTables();
- dbUtils.dropTables(persistenceUnit.getJpaTables());
+ dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
jpaBuilder.start();
import java.util.List;
import org.dbunit.dataset.filter.ITableFilterSimple;
+import org.wamblee.support.persistence.toplink.ToplinkTables;
public class PersistenceUnitDescription {
public ITableFilterSimple getTables() {
return tables;
}
-
- /**
- * JPA provider specific tables.
- * @return
- */
- public ITableFilterSimple getJpaTables() {
- return new ToplinkTables();
- }
}
+++ /dev/null
-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<String> TABLES = Arrays.asList(new String[] { "SEQUENCE" } );
-
- public boolean accept(String aTableName) throws DataSetException {
- return TABLES.contains(aTableName);
- }
-
-}