Now using connections managed by JPA instead of using JNDI mocking.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 12 May 2010 11:21:12 +0000 (11:21 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 12 May 2010 11:21:12 +0000 (11:21 +0000)
pom.xml
test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java
test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java
test/enterprise/src/main/java/org/wamblee/support/persistence/JpaTester.java
test/enterprise/src/test/java/org/wamblee/support/persistence/DatabaseUtilsTestBase.java
test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java
test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java

diff --git a/pom.xml b/pom.xml
index 1563b00ecd0d6f61914a8965e421ad4b9b1bc5c6..9a863ffcca64d4baf9c973621d727ab0fac46639 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <dependency>
                 <groupId>toplink.essentials</groupId>
                 <artifactId>toplink-essentials</artifactId>
-                <version>2.0-36</version>
+                <version>2.1-60f</version>
             </dependency>
             <dependency>
                 <groupId>org.eclipse.persistence</groupId>
index b03b256df7c795c5a9143720b8dcfcebc244e5fe..1679942b15a6a9716152422fe292b517ef304180 100644 (file)
@@ -40,16 +40,10 @@ public class EclipselinkJpaCustomizer implements JpaCustomizer {
     @Override
     public void customize(PersistenceUnitDescription aPersistenceUnit,
         Map<String, String> aJpaProperties) {
-        // Hack to make JNDI lookup of the datasource work with toplink
-        aJpaProperties.put("eclipselink.session.customizer",
-            JndiSessionCustomizer.class.getName());
-
+  
         // DDL generation
         aJpaProperties.put("eclipselink.ddl-generation", "create-tables");
         
-        // Use JTA transaction type
-        aJpaProperties.put("javax.persistence.transactionType", "JTA");
-        
         // DDL generation
         FileSystemUtils.createDir(new File("target/sql"));
         aJpaProperties.put("eclipselink.application-location", "target/sql");
index 8356ea2e52019b1d707df0d9d79ab4189a4eff76..e4cdcfbf7ce3110a88c622f1328d90be22804326 100644 (file)
@@ -20,14 +20,11 @@ import java.util.TreeMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
 import javax.persistence.PersistenceException;
-import javax.sql.DataSource;
 
 import org.wamblee.support.jndi.StubInitialContextFactory;
 
@@ -63,22 +60,26 @@ public class JpaBuilder {
     }
 
     private PersistenceUnitDescription persistenceUnit;
-    private DataSource dataSource;
+    private String url; 
+    private String user; 
+    private String password; 
     private EntityManagerFactory factory;
 
     /**
      * Constructs the builder.
      * 
-     * @param aDataSource
-     *            Datasource of database.
+     * @param aUrl JDBC URL
+     * @param aUser User name
+     * @param aPassword Password.
      * @param aPersistenceUnit
      *            Persistence unit.
      */
-    public JpaBuilder(DataSource aDataSource,
+    public JpaBuilder(String aUrl, String aUser, String aPassword,
         PersistenceUnitDescription aPersistenceUnit) {
         persistenceUnit = aPersistenceUnit;
-        dataSource = aDataSource;
-        StubInitialContextFactory.register();
+        url = aUrl; 
+        user = aUser; 
+        password = aPassword; 
     }
 
     /**
@@ -87,12 +88,6 @@ public class JpaBuilder {
      * manager factory, and forces creation of the database schema.
      */
     public void start() throws Exception {
-        try {
-            InitialContext ctx = new InitialContext();
-            ctx.bind(persistenceUnit.getJndiName(), dataSource);
-        } catch (NamingException e) {
-            throw new RuntimeException("JNDI problem", e);
-        }
         factory = createFactory();
         try {
             execute(new JpaUnitOfWork<Void>() {
@@ -122,10 +117,16 @@ public class JpaBuilder {
      */
     public EntityManagerFactory createFactory() {
         Map<String, String> jpaProps = new TreeMap<String, String>();
+        
+        jpaProps.put("javax.persistence.jtaDataSource", null);
+        jpaProps.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
+        jpaProps.put("javax.persistence.jdbc.url", url);
+        jpaProps.put("javax.persistence.jdbc.user", user);
+        jpaProps.put("javax.persistence.jdbc.password", password);
 
         JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit,
             jpaProps);
-
+        
         // jpaProps.put("javax.persistence.provider",
         // HibernatePersistence.class.getName());
         EntityManagerFactory emf = Persistence.createEntityManagerFactory(
index 34a79750cd090d4d6858f4c9a91d96669d1a2939..19f7f187d5838f5ef1272db9d67d8934d3a1bb48 100644 (file)
  */
 package org.wamblee.support.persistence;
 
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.sql.DataSource;
 
 import org.dbunit.IDatabaseTester;
+import org.wamblee.support.jndi.StubInitialContextFactory;
 
 /**
  * This class is the entry point for JPA tests. Test code should construct a
@@ -80,13 +83,23 @@ public class JpaTester {
     public void start() throws Exception {
         db = DatabaseBuilder.getDatabase();
         dataSource = db.start();
+        
+        // NOTE: adding datasource to JNDI is no longer needed for 
+        //       JPA testing, but is nice to have available for other uses.
+        StubInitialContextFactory.register();
+        try {
+            InitialContext ctx = new InitialContext();
+            ctx.bind(persistenceUnit.getJndiName(), dataSource);
+        } catch (NamingException e) {
+            throw new RuntimeException("JNDI problem", e);
+        }
 
         dbUtils = new DatabaseUtils(dataSource, persistenceUnit.getTables());
         dbUtils.start();
         dbUtils.dropTables();
         dbUtils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
 
-        jpaBuilder = new JpaBuilder(dataSource, persistenceUnit);
+        jpaBuilder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
         jpaBuilder.start();
 
         // db tester should be created after Jpa builder because jpa builder
index 789fabf25f85dc834a7221990946220c78caa466..1092149ec7f7ef265806bc27001ca613d530d1be 100644 (file)
@@ -51,7 +51,7 @@ public class DatabaseUtilsTestBase {
         dbutils.dropTables();
         dbutils.dropTables(JpaCustomizerBuilder.getCustomizer().getJpaTables());
 
-        builder = new JpaBuilder(dataSource, persistenceUnit);
+        builder = new JpaBuilder(db.getJdbcUrl(), db.getUsername(), db.getPassword(), persistenceUnit);
         builder.start();
 
         assertEquals(0, db.getActiveConnections());
index 910ba8dd01da661e7ce84caeaf0706c93e90f2cc..71bbb18c10028fd4c50583c941836816e477f9c9 100644 (file)
@@ -38,12 +38,17 @@ public class HibernateJpaCustomizer implements JpaCustomizer {
     @Override
     public void customize(PersistenceUnitDescription aPersistenceUnit,
         Map<String, String> aJpaProperties) {
-        // Hibernate: Override transaction type and datasource
-        aJpaProperties.put("javax.persistence.transactionType",
-            "RESOURCE_LOCAL");
-        aJpaProperties.put("javax.persistence.jtaDataSource", null);
-        aJpaProperties.put("javax.persistence.nonJtaDataSource",
-            aPersistenceUnit.getJndiName());
+        
+        System.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "true");
+        
+        // Set non-JPA connection properties for older versions of hibernate
+        System.getProperties().remove("hibernate.connection.datasource");
+        if (System.getProperty("hibernate.connection.datasource") != null ) { 
+            throw new RuntimeException("ERROR");
+        }
+        System.setProperty("hibernate.connection.url", aJpaProperties.get("javax.persistence.jdbc.url"));
+        System.setProperty("hibernate.connection.username", aJpaProperties.get("javax.persistence.jdbc.user"));
+        System.setProperty("hibernate.connection.password", aJpaProperties.get("javax.persistence.jdbc.password"));
 
         // Hibernate schema generation
         aJpaProperties.put("hibernate.hbm2ddl.auto", "create");
index 3a132da20c54065934edda98f23e86adb7b87e11..1b3808f099cf07ff3c2c4f86eab53cb80dbd364a 100644 (file)
@@ -39,16 +39,15 @@ public class ToplinkJpaCustomizer implements JpaCustomizer {
     @Override
     public void customize(PersistenceUnitDescription aPersistenceUnit,
         Map<String, String> aJpaProperties) {
-        // Hack to make JNDI lookup of the datasource work with toplink
-        aJpaProperties.put("toplink.session.customizer",
-            JndiSessionCustomizer.class.getName());
+
+        // Custom datasource properties.
+        aJpaProperties.put("toplink.jdbc.url", aJpaProperties.get("javax.persistence.jdbc.url"));
+        aJpaProperties.put("toplink.jdbc.user", aJpaProperties.get("javax.persistence.jdbc.user"));
+        aJpaProperties.put("toplink.jdbc.password", aJpaProperties.get("javax.persistence.jdbc.password"));
 
         // DDL generation for toplink
         aJpaProperties.put("toplink.ddl-generation", "create-tables");
         
-        // Use JTA transaction type
-        aJpaProperties.put("javax.persistence.transactionType", "JTA");
-        
         // DDL generation
         FileSystemUtils.createDir(new File("target/sql"));
         aJpaProperties.put("toplink.create-ddl-jdbc-file-name", "target/sql/create-schema.sql");