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.*;
19 import static org.mockito.Mockito.*;
21 import javax.persistence.EntityManager;
22 import javax.transaction.UserTransaction;
24 import junit.framework.AssertionFailedError;
26 import org.dbunit.IDatabaseTester;
27 import org.dbunit.dataset.ITable;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
32 import org.wamblee.test.transactions.DefaultUserTransactionFactory;
33 import org.wamblee.test.transactions.SimpleTransactionManager;
34 import org.wamblee.test.transactions.TransactionResult;
37 * This class shows an example of how to test an entity using jpa.
39 public class MyEntityExampleTestBase {
41 // This is the magical object that does all the test setup.
42 private JpaTester jpaTester;
44 // The jpa tester initializes a lot for us....
46 // A JPA builder that provides a transaction scoped entity manager for us.
47 private JpaBuilder builder;
49 // The database tester for dbunit which is appropriately configured for our
51 private IDatabaseTester dbtester;
53 // Database utilities with some additional functionality for working with
55 // such as dropping tables, cleaning tables, etc.
56 private DatabaseUtils dbutils;
59 public void setUp() throws Exception {
61 // First we create the JpaTester by telling us which persistence unit we
63 jpaTester = new JpaTester(new MyPersistenceUnit());
66 // Retrieve some useful objects fromt he jpa tester. It also provides
67 // direct access to the datasource
68 // but we don't need it. We can use datbase utils if we want to execute
69 // straight JDBC calls.
70 builder = jpaTester.getJpaBuilder();
71 dbtester = jpaTester.getDbUtils().createDbTester(new MyTables());
72 dbutils = jpaTester.getDbUtils();
76 public void tearDown() {
82 public void testEntityPersistence() throws Exception {
84 // Use the JPA builder to create a transaction scoped entity manager for
87 builder.execute(new JpaUnitOfWork<Void>() {
88 public Void execute(EntityManager aEm) {
89 MyEntity entity = new MyEntity("a", "b");
95 // Verify one row is written (using Db unit)
96 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
97 assertEquals(1, table.getRowCount());
99 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
100 assertEquals("b", table.getValue(0, "VALUE"));
102 // For this simple test, it can also be done through DatabaseUtils
103 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
108 public void testEntityPersistenceWithBasicApi() throws Exception {
110 // Use the JPA builder to create a transaction scoped entity manager for
111 // as and execute the
113 EntityManager em = builder.begin();
115 MyEntity entity = new MyEntity("a", "b");
120 // Verify one row is written (using Db unit)
121 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
122 assertEquals(1, table.getRowCount());
124 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
125 assertEquals("b", table.getValue(0, "VALUE"));
127 // For this simple test, it can also be done through DatabaseUtils
128 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
133 public void testEntityPersistenceWithContextualEntityManager()
136 // Use the JPA builder to create a transaction scoped entity manager for
137 // as and execute the
141 EntityManager em = builder.getContextualEntityManager();
142 MyEntity entity = new MyEntity("a", "b");
147 // Verify one row is written (using Db unit)
148 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
149 assertEquals(1, table.getRowCount());
151 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
152 assertEquals("b", table.getValue(0, "VALUE"));
154 // For this simple test, it can also be done through DatabaseUtils
155 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
160 public void testEntityPersistenceWithUserTransactions() throws Exception {
162 SimpleTransactionManager manager = new SimpleTransactionManager(
163 new DefaultUserTransactionFactory());
164 manager.addResource(jpaTester.getJpaBuilder());
166 UserTransaction transaction = manager.getTransaction();
169 EntityManager em = builder.getContextualEntityManager();
170 MyEntity entity = new MyEntity("a", "b");
173 transaction.commit();
175 // Verify one row is written (using Db unit)
176 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
177 assertEquals(1, table.getRowCount());
179 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
180 assertEquals("b", table.getValue(0, "VALUE"));
182 // For this simple test, it can also be done through DatabaseUtils
183 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
188 public void testTransactionCommittedCallback() throws Exception {
189 TransactionResultCallback callback = mock(TransactionResultCallback.class);
190 builder.execute(new JpaUnitOfWork<Void>() {
191 public Void execute(EntityManager aEm) {
192 MyEntity entity = new MyEntity("a", "b");
197 verify(callback).status(TransactionResult.COMMIT);
198 verifyNoMoreInteractions(callback);
202 public void testTransactionMarkedForRollback() throws Exception {
203 TransactionResultCallback callback = mock(TransactionResultCallback.class);
205 builder.execute(new JpaUnitOfWork<Void>() {
206 public Void execute(EntityManager aEm) {
207 MyEntity entity = new MyEntity("a", "b");
209 aEm.getTransaction().setRollbackOnly();
213 verify(callback).status(TransactionResult.ROLLBACK);
214 verifyNoMoreInteractions(callback);
218 public void testTransactionRolledBack() throws Exception {
219 TransactionResultCallback callback = mock(TransactionResultCallback.class);
221 builder.execute(new JpaUnitOfWork<Void>() {
222 public Void execute(EntityManager aEm) {
223 MyEntity entity = new MyEntity("a", "b");
225 aEm.getTransaction().rollback();
229 verify(callback).status(TransactionResult.UNKNOWN);
230 verifyNoMoreInteractions(callback);
234 public void testTransactionCommitted() throws Exception {
235 TransactionResultCallback callback = mock(TransactionResultCallback.class);
237 builder.execute(new JpaUnitOfWork<Void>() {
238 public Void execute(EntityManager aEm) {
239 MyEntity entity = new MyEntity("a", "b");
241 aEm.getTransaction().commit();
245 verify(callback).status(TransactionResult.UNKNOWN);
246 verifyNoMoreInteractions(callback);
249 @Test(expected = AssertionFailedError.class)
250 public void testTransactionNotCommittedUnexpectedly() throws Exception {
251 builder.execute(new JpaUnitOfWork<Void>() {
252 public Void execute(EntityManager aEm) {
253 MyEntity entity = new MyEntity("a", "b");
255 aEm.getTransaction().rollback();