From 187fce39126c59e974ab86a2af05b0b3ebd2c284 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 12 May 2010 11:21:12 +0000 Subject: [PATCH] Now using connections managed by JPA instead of using JNDI mocking. --- pom.xml | 2 +- .../eclipselink/EclipselinkJpaCustomizer.java | 8 +---- .../support/persistence/JpaBuilder.java | 33 ++++++++++--------- .../support/persistence/JpaTester.java | 15 ++++++++- .../persistence/DatabaseUtilsTestBase.java | 2 +- .../hibernate/HibernateJpaCustomizer.java | 17 ++++++---- .../toplink/ToplinkJpaCustomizer.java | 11 +++---- 7 files changed, 50 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index 1563b00e..9a863ffc 100644 --- a/pom.xml +++ b/pom.xml @@ -320,7 +320,7 @@ toplink.essentials toplink-essentials - 2.0-36 + 2.1-60f org.eclipse.persistence diff --git a/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java b/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java index b03b256d..1679942b 100644 --- a/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java +++ b/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java @@ -40,16 +40,10 @@ public class EclipselinkJpaCustomizer implements JpaCustomizer { @Override public void customize(PersistenceUnitDescription aPersistenceUnit, Map 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"); diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java index 8356ea2e..e4cdcfbf 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java @@ -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() { @@ -122,10 +117,16 @@ public class JpaBuilder { */ public EntityManagerFactory createFactory() { Map jpaProps = new TreeMap(); + + 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( 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 34a79750..19f7f187 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 @@ -15,9 +15,12 @@ */ 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 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 789fabf2..1092149e 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 @@ -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()); diff --git a/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java b/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java index 910ba8dd..71bbb18c 100644 --- a/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java +++ b/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java @@ -38,12 +38,17 @@ public class HibernateJpaCustomizer implements JpaCustomizer { @Override public void customize(PersistenceUnitDescription aPersistenceUnit, Map 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"); diff --git a/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java b/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java index 3a132da2..1b3808f0 100644 --- a/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java +++ b/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java @@ -39,16 +39,15 @@ public class ToplinkJpaCustomizer implements JpaCustomizer { @Override public void customize(PersistenceUnitDescription aPersistenceUnit, Map 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"); -- 2.31.1