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 org.dbunit.IDatabaseTester;
25 import org.dbunit.dataset.ITable;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork;
30 import org.wamblee.test.transactions.DefaultUserTransactionFactory;
31 import org.wamblee.test.transactions.SimpleTransactionManager;
32 import org.wamblee.test.transactions.TransactionResult;
35 * This class shows an example of how to test an entity using jpa.
37 public class MyEntityExampleTestBase {
39 // This is the magical object that does all the test setup.
40 private JpaTester jpaTester;
42 // The jpa tester initializes a lot for us....
44 // A JPA builder that provides a transaction scoped entity manager for us.
45 private JpaBuilder builder;
47 // The database tester for dbunit which is appropriately configured for our
49 private IDatabaseTester dbtester;
51 // Database utilities with some additional functionality for working with
53 // such as dropping tables, cleaning tables, etc.
54 private DatabaseUtils dbutils;
57 public void setUp() throws Exception {
59 // First we create the JpaTester by telling us which persistence unit we
61 jpaTester = new JpaTester(new MyPersistenceUnit());
64 // Retrieve some useful objects fromt he jpa tester. It also provides
65 // direct access to the datasource
66 // but we don't need it. We can use datbase utils if we want to execute
67 // straight JDBC calls.
68 builder = jpaTester.getJpaBuilder();
69 dbtester = jpaTester.getDbUtils().createDbTester(new MyTables());
70 dbutils = jpaTester.getDbUtils();
74 public void tearDown() {
80 public void testEntityPersistence() throws Exception {
82 // Use the JPA builder to create a transaction scoped entity manager for
85 builder.execute(new JpaUnitOfWork<Void>() {
86 public Void execute(EntityManager aEm) {
87 MyEntity entity = new MyEntity("a", "b");
93 // Verify one row is written (using Db unit)
94 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
95 assertEquals(1, table.getRowCount());
97 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
98 assertEquals("b", table.getValue(0, "VALUE"));
100 // For this simple test, it can also be done through DatabaseUtils
101 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
106 public void testEntityPersistenceWithBasicApi() throws Exception {
108 // Use the JPA builder to create a transaction scoped entity manager for
109 // as and execute the
111 EntityManager em = builder.begin();
113 MyEntity entity = new MyEntity("a", "b");
118 // Verify one row is written (using Db unit)
119 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
120 assertEquals(1, table.getRowCount());
122 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
123 assertEquals("b", table.getValue(0, "VALUE"));
125 // For this simple test, it can also be done through DatabaseUtils
126 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
131 public void testEntityPersistenceWithContextualEntityManager() throws Exception {
133 // Use the JPA builder to create a transaction scoped entity manager for
134 // as and execute the
138 EntityManager em = builder.getContextualEntityManager();
139 MyEntity entity = new MyEntity("a", "b");
144 // Verify one row is written (using Db unit)
145 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
146 assertEquals(1, table.getRowCount());
148 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
149 assertEquals("b", table.getValue(0, "VALUE"));
151 // For this simple test, it can also be done through DatabaseUtils
152 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
157 public void testEntityPersistenceWithUserTransactions() throws Exception {
159 SimpleTransactionManager manager = new SimpleTransactionManager(new DefaultUserTransactionFactory());
160 manager.addResource(jpaTester.getJpaBuilder());
162 UserTransaction transaction = manager.getTransaction();
165 EntityManager em = builder.getContextualEntityManager();
166 MyEntity entity = new MyEntity("a", "b");
169 transaction.commit();
171 // Verify one row is written (using Db unit)
172 ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
173 assertEquals(1, table.getRowCount());
175 assertEquals("a", table.getValue(0, "SLEUTELTJE"));
176 assertEquals("b", table.getValue(0, "VALUE"));
178 // For this simple test, it can also be done through DatabaseUtils
179 assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
184 public void testTransactionCommittedCallback() throws Exception {
185 TransactionResultCallback callback = mock(TransactionResultCallback.class);
186 builder.execute(new JpaUnitOfWork<Void>() {
187 public Void execute(EntityManager aEm) {
188 MyEntity entity = new MyEntity("a", "b");
193 verify(callback).status(TransactionResult.COMMIT);
194 verifyNoMoreInteractions(callback);
198 public void testTransactionMarkedForRollback() throws Exception {
199 TransactionResultCallback callback = mock(TransactionResultCallback.class);
201 builder.execute(new JpaUnitOfWork<Void>() {
202 public Void execute(EntityManager aEm) {
203 MyEntity entity = new MyEntity("a", "b");
205 aEm.getTransaction().setRollbackOnly();
209 verify(callback).status(TransactionResult.ROLLBACK);
210 verifyNoMoreInteractions(callback);
214 public void testTransactionRolledBack() throws Exception {
215 TransactionResultCallback callback = mock(TransactionResultCallback.class);
217 builder.execute(new JpaUnitOfWork<Void>() {
218 public Void execute(EntityManager aEm) {
219 MyEntity entity = new MyEntity("a", "b");
221 aEm.getTransaction().rollback();
225 verify(callback).status(TransactionResult.UNKNOWN);
226 verifyNoMoreInteractions(callback);
230 public void testTransactionCommitted() throws Exception {
231 TransactionResultCallback callback = mock(TransactionResultCallback.class);
233 builder.execute(new JpaUnitOfWork<Void>() {
234 public Void execute(EntityManager aEm) {
235 MyEntity entity = new MyEntity("a", "b");
237 aEm.getTransaction().commit();
241 verify(callback).status(TransactionResult.UNKNOWN);
242 verifyNoMoreInteractions(callback);