2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test.persistence;
18 import static junit.framework.Assert.*;
20 import javax.persistence.EntityManager;
21 import javax.transaction.UserTransaction;
23 import org.dbunit.IDatabaseTester;
24 import org.dbunit.dataset.ITable;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
29 import org.wamblee.test.transactions.DefaultUserTransactionFactory;
30 import org.wamblee.test.transactions.SimpleTransactionManager;
33 * This class shows an example of how to test an entity using jpa.
35 public class MyEntityExampleTestBase {
37 // This is the magical object that does all the test setup.
38 private JpaTester jpaTester;
40 // The jpa tester initializes a lot for us....
42 // A JPA builder that provides a transaction scoped entity manager for us.
43 private JpaBuilder builder;
45 // The database tester for dbunit which is appropriately configured for our
47 private IDatabaseTester dbtester;
49 // Database utilities with some additional functionality for working with
51 // such as dropping tables, cleaning tables, etc.
52 private DatabaseUtils dbutils;
55 public void setUp() throws Exception {
57 // First we create the JpaTester by telling us which persistence unit we
59 jpaTester = new JpaTester(new MyPersistenceUnit());
62 // Retrieve some useful objects fromt he jpa tester. It also provides
63 // direct access to the datasource
64 // but we don't need it. We can use datbase utils if we want to execute
65 // straight JDBC calls.
66 builder = jpaTester.getJpaBuilder();
67 dbtester = jpaTester.getDbUtils().createDbTester(new MyTables());
68 dbutils = jpaTester.getDbUtils();
72 public void tearDown() {
78 public void testEntityPersistence() throws Exception {
80 // Use the JPA builder to create a transaction scoped entity manager for
83 builder.execute(new JpaUnitOfWork<Void>() {
84 public Void execute(EntityManager aEm) {
85 MyEntity entity = new MyEntity("a", "b");
91 // Verify one row is written (using Db unit)
92 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
93 assertEquals(1, table.getRowCount());
95 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
96 assertEquals("b", table.getValue(0, "VALUE"));
98 // For this simple test, it can also be done through DatabaseUtils
99 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
104 public void testEntityPersistenceWithBasicApi() throws Exception {
106 // Use the JPA builder to create a transaction scoped entity manager for
107 // as and execute the
109 EntityManager em = builder.begin();
111 MyEntity entity = new MyEntity("a", "b");
116 // Verify one row is written (using Db unit)
117 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
118 assertEquals(1, table.getRowCount());
120 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
121 assertEquals("b", table.getValue(0, "VALUE"));
123 // For this simple test, it can also be done through DatabaseUtils
124 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
129 public void testEntityPersistenceWithContextualEntityManager() throws Exception {
131 // Use the JPA builder to create a transaction scoped entity manager for
132 // as and execute the
136 EntityManager em = builder.getContextualEntityManager();
137 MyEntity entity = new MyEntity("a", "b");
142 // Verify one row is written (using Db unit)
143 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
144 assertEquals(1, table.getRowCount());
146 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
147 assertEquals("b", table.getValue(0, "VALUE"));
149 // For this simple test, it can also be done through DatabaseUtils
150 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
155 public void testEntityPersistenceWithUserTransactions() throws Exception {
157 SimpleTransactionManager manager = new SimpleTransactionManager(new DefaultUserTransactionFactory());
158 manager.addResource(jpaTester.getJpaBuilder());
160 UserTransaction transaction = manager.getTransaction();
163 EntityManager em = builder.getContextualEntityManager();
164 MyEntity entity = new MyEntity("a", "b");
167 transaction.commit();
169 // Verify one row is written (using Db unit)
170 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
171 assertEquals(1, table.getRowCount());
173 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
174 assertEquals("b", table.getValue(0, "VALUE"));
176 // For this simple test, it can also be done through DatabaseUtils
177 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));