X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2Fpersistence%2FMyEntityExampleTestBase.java;h=d8e80440530cd495762fe227d86a8809cc6991cd;hb=9ed4408d10a124990e1b9cc6697872ee96c00c1d;hp=737455ce77ae6f0c75fa149172452342a739f254;hpb=cb91054f35281c6fc5619f93ff71df46bf4686b9;p=utils diff --git a/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java b/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java index 737455ce..d8e80440 100644 --- a/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java +++ b/test/enterprise/src/test/java/org/wamblee/test/persistence/MyEntityExampleTestBase.java @@ -12,22 +12,22 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.test.persistence; import static junit.framework.Assert.*; import javax.persistence.EntityManager; +import javax.transaction.UserTransaction; import org.dbunit.IDatabaseTester; import org.dbunit.dataset.ITable; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.wamblee.test.persistence.DatabaseUtils; -import org.wamblee.test.persistence.JpaBuilder; -import org.wamblee.test.persistence.JpaTester; import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork; +import org.wamblee.test.transactions.DefaultUserTransactionFactory; +import org.wamblee.test.transactions.SimpleTransactionManager; /** * This class shows an example of how to test an entity using jpa. @@ -100,4 +100,81 @@ public class MyEntityExampleTestBase { } + @Test + public void testEntityPersistenceWithBasicApi() throws Exception { + + // Use the JPA builder to create a transaction scoped entity manager for + // as and execute the + // unit of work. + EntityManager em = builder.begin(); + + MyEntity entity = new MyEntity("a", "b"); + em.persist(entity); + + builder.commit(em); + + // Verify one row is written (using Db unit) + ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY"); + assertEquals(1, table.getRowCount()); + + assertEquals("a", table.getValue(0, "SLEUTELTJE")); + assertEquals("b", table.getValue(0, "VALUE")); + + // For this simple test, it can also be done through DatabaseUtils + assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY")); + + } + + @Test + public void testEntityPersistenceWithContextualEntityManager() throws Exception { + + // Use the JPA builder to create a transaction scoped entity manager for + // as and execute the + // unit of work. + builder.begin(); + + EntityManager em = builder.getContextualEntityManager(); + MyEntity entity = new MyEntity("a", "b"); + em.persist(entity); + + builder.commit(em); + + // Verify one row is written (using Db unit) + ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY"); + assertEquals(1, table.getRowCount()); + + assertEquals("a", table.getValue(0, "SLEUTELTJE")); + assertEquals("b", table.getValue(0, "VALUE")); + + // For this simple test, it can also be done through DatabaseUtils + assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY")); + + } + + @Test + public void testEntityPersistenceWithUserTransactions() throws Exception { + + SimpleTransactionManager manager = new SimpleTransactionManager(new DefaultUserTransactionFactory()); + manager.addResource(jpaTester.getJpaBuilder()); + + UserTransaction transaction = manager.getTransaction(); + transaction.begin(); + + EntityManager em = builder.getContextualEntityManager(); + MyEntity entity = new MyEntity("a", "b"); + em.persist(entity); + + transaction.commit(); + + // Verify one row is written (using Db unit) + ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY"); + assertEquals(1, table.getRowCount()); + + assertEquals("a", table.getValue(0, "SLEUTELTJE")); + assertEquals("b", table.getValue(0, "VALUE")); + + // For this simple test, it can also be done through DatabaseUtils + assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY")); + + } }