From 89005f148467a5f735a385b20bfb4f37a35e75e6 Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 4 Apr 2010 14:28:51 +0000 Subject: [PATCH] hibernate support for jpa testing. --- support/test/hibernate/pom.xml | 50 +++++++++++++++++++ .../hibernate/HibernateJpaCustomizer.java | 31 ++++++++++++ .../hibernate/HibernateTables.java | 20 ++++++++ ....wamblee.support.persistence.JpaCustomizer | 1 - .../hibernate/DatabaseUtilsTest.java | 23 +++++++++ .../hibernate/MyEntityExampleTest.java | 29 +++++++++++ 6 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 support/test/hibernate/pom.xml create mode 100644 support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java create mode 100644 support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateTables.java rename support/test/{enterprise => hibernate}/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer (51%) create mode 100644 support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/DatabaseUtilsTest.java create mode 100644 support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/MyEntityExampleTest.java diff --git a/support/test/hibernate/pom.xml b/support/test/hibernate/pom.xml new file mode 100644 index 00000000..45457e1b --- /dev/null +++ b/support/test/hibernate/pom.xml @@ -0,0 +1,50 @@ + + + + org.wamblee + wamblee-utils + 0.2-SNAPSHOT + + + 4.0.0 + org.wamblee + wamblee-support-test-hibernate + jar + wamblee.org support general library + http://wamblee.org + + + + org.wamblee + wamblee-support-test-enterprise + 0.2-SNAPSHOT + + + + org.wamblee + wamblee-support-test-enterprise + 0.2-SNAPSHOT + test-jar + + + + org.dbunit + dbunit + + + + javax.persistence + persistence-api + + + + org.wamblee + wamblee-hibernate-jpa + 0.2-SNAPSHOT + + + + + diff --git a/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java b/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java new file mode 100644 index 00000000..fa4def8f --- /dev/null +++ b/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateJpaCustomizer.java @@ -0,0 +1,31 @@ +package org.wamblee.support.persistence.hibernate; + +import java.util.Map; + +import org.dbunit.dataset.filter.ITableFilterSimple; +import org.wamblee.support.persistence.JpaCustomizer; +import org.wamblee.support.persistence.PersistenceUnitDescription; + +public class HibernateJpaCustomizer implements JpaCustomizer { + + public HibernateJpaCustomizer() { + // Empty + } + + @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()); + + // Hibernate schema generation + aJpaProperties.put("hibernate.hbm2ddl.auto", "create"); + } + + @Override + public ITableFilterSimple getJpaTables() { + return new HibernateTables(); + } + +} diff --git a/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateTables.java b/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateTables.java new file mode 100644 index 00000000..4b2c8a09 --- /dev/null +++ b/support/test/hibernate/src/main/java/org/wamblee/support/persistence/hibernate/HibernateTables.java @@ -0,0 +1,20 @@ +package org.wamblee.support.persistence.hibernate; + +import java.util.Arrays; +import java.util.List; + +import org.dbunit.dataset.DataSetException; +import org.dbunit.dataset.filter.ITableFilterSimple; + +/** + * Toplink-specific tables. + */ +public class HibernateTables implements ITableFilterSimple { + + private static final List TABLES = Arrays.asList(new String[] { "" } ); + + public boolean accept(String aTableName) throws DataSetException { + return TABLES.contains(aTableName); + } + +} diff --git a/support/test/enterprise/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer b/support/test/hibernate/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer similarity index 51% rename from support/test/enterprise/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer rename to support/test/hibernate/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer index adb67544..d057634d 100644 --- a/support/test/enterprise/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer +++ b/support/test/hibernate/src/main/resources/META-INF/services/org.wamblee.support.persistence.JpaCustomizer @@ -1,2 +1 @@ -org.wamblee.support.persistence.toplink.ToplinkJpaCustomizer org.wamblee.support.persistence.hibernate.HibernateJpaCustomizer diff --git a/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/DatabaseUtilsTest.java b/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/DatabaseUtilsTest.java new file mode 100644 index 00000000..68edf22a --- /dev/null +++ b/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/DatabaseUtilsTest.java @@ -0,0 +1,23 @@ +package org.wamblee.support.persistence.hibernate; + +import static junit.framework.Assert.assertEquals; + +import javax.persistence.EntityManager; +import javax.sql.DataSource; + +import org.dbunit.IDatabaseTester; +import org.dbunit.dataset.ITable; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.support.persistence.Database; +import org.wamblee.support.persistence.DatabaseBuilder; +import org.wamblee.support.persistence.DatabaseUtils; +import org.wamblee.support.persistence.DatabaseUtilsTestBase; +import org.wamblee.support.persistence.JpaBuilder; +import org.wamblee.support.persistence.PersistenceUnitDescription; +import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; + +public class DatabaseUtilsTest extends DatabaseUtilsTestBase { + // Empty, all tests inherited +} diff --git a/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/MyEntityExampleTest.java b/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/MyEntityExampleTest.java new file mode 100644 index 00000000..537cb549 --- /dev/null +++ b/support/test/hibernate/src/test/java/org/wamblee/support/persistence/hibernate/MyEntityExampleTest.java @@ -0,0 +1,29 @@ +package org.wamblee.support.persistence.hibernate; + +import javax.persistence.EntityManager; +import javax.persistence.Persistence; +import javax.sql.DataSource; + +import org.dbunit.DataSourceDatabaseTester; +import org.dbunit.DatabaseTestCase; +import org.dbunit.IDatabaseTester; +import org.dbunit.dataset.ITable; +import org.dbunit.dataset.filter.ITableFilterSimple; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.support.persistence.DatabaseUtils; +import org.wamblee.support.persistence.JpaBuilder; +import org.wamblee.support.persistence.JpaTester; +import org.wamblee.support.persistence.MyEntityExampleTestBase; +import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; + +import static junit.framework.Assert.*; + + +/** + * This class shows an example of how to test an entity using jpa. + */ +public class MyEntityExampleTest extends MyEntityExampleTestBase { + // Empty, all tests are inherited +} -- 2.31.1