(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 4 Apr 2010 11:42:51 +0000 (11:42 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 4 Apr 2010 11:42:51 +0000 (11:42 +0000)
support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaCustomizer.java [new file with mode: 0644]
support/test/src/main/java/org/wamblee/support/persistence/CompositeJpaTables.java [new file with mode: 0644]
support/test/src/main/java/org/wamblee/support/persistence/Database.java
support/test/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java
support/test/src/main/java/org/wamblee/support/persistence/JpaBuilder.java
support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizer.java [new file with mode: 0644]
support/test/src/main/java/org/wamblee/support/persistence/JpaCustomizerBuilder.java [new file with mode: 0644]
support/test/src/main/java/org/wamblee/support/persistence/JpaTester.java
support/test/src/main/java/org/wamblee/support/persistence/PersistenceUnitDescription.java
support/test/src/main/java/org/wamblee/support/persistence/ToplinkTables.java [deleted file]

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 (file)
index 0000000..fc6ecc7
--- /dev/null
@@ -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<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; 
+    }
+
+}
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 (file)
index 0000000..7a7e8de
--- /dev/null
@@ -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<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; 
+    }
+
+}
index 56122ed186f874817316c46b69c864729c7bb6b1..f37474ea9ea335ea6afcefcf74c568c5704d7986 100755 (executable)
@@ -15,8 +15,6 @@
  */
 package org.wamblee.support.persistence;
 
-import java.util.List;
-
 import javax.sql.DataSource;
 
 /**
index 781764be18ddb49fd527ab645bab13cd7f077c46..312e38a0e531ff97abae848f578e69b6956d96fb 100644 (file)
@@ -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;
index b46ffa3a2ceaf1caa9c361332462d569b7dcb5d7..e6943197b85e66ca18cbb4eee8dcb3e96bba4387 100644 (file)
@@ -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<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;
        }
 
        /**
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 (file)
index 0000000..7930301
--- /dev/null
@@ -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<String,String> 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 (file)
index 0000000..a824bf6
--- /dev/null
@@ -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<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);
+    }
+}
index c0a22cd354743455ee6c1101a48cf3454c0e4ff6..a6a512f125a5313b7c4da4793d97091cc98c77f1 100644 (file)
@@ -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();
index 502f43db0a1f11b6c03cc68cdc8081527240448e..0d053bc0f462db1172158857b158dfc47a4bc1d7 100644 (file)
@@ -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 (file)
index 4d45d0f..0000000
+++ /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<String> TABLES = Arrays.asList(new String[] { "SEQUENCE" } );
-       
-       public boolean accept(String aTableName) throws DataSetException {
-               return TABLES.contains(aTableName);
-       }
-
-}