(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / persistence / MyEntityExampleTestBase.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.test.persistence;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Mockito.*;
20
21 import javax.persistence.EntityManager;
22 import javax.transaction.UserTransaction;
23
24 import junit.framework.AssertionFailedError;
25
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;
35
36 /**
37  * This class shows an example of how to test an entity using jpa.
38  */
39 public class MyEntityExampleTestBase {
40
41     // This is the magical object that does all the test setup.
42     private JpaTester jpaTester;
43
44     // The jpa tester initializes a lot for us....
45
46     // A JPA builder that provides a transaction scoped entity manager for us.
47     private JpaBuilder builder;
48
49     // The database tester for dbunit which is appropriately configured for our
50     // persistence unit.
51     private IDatabaseTester dbtester;
52
53     // Database utilities with some additional functionality for working with
54     // the databse
55     // such as dropping tables, cleaning tables, etc.
56     private DatabaseUtils dbutils;
57
58     @Before
59     public void setUp() throws Exception {
60
61         // First we create the JpaTester by telling us which persistence unit we
62         // are going to test
63         jpaTester = new JpaTester(new MyPersistenceUnit());
64         jpaTester.start();
65
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();
73     }
74
75     @After
76     public void tearDown() {
77         dbutils.stop();
78         jpaTester.stop();
79     }
80
81     @Test
82     public void testEntityPersistence() throws Exception {
83
84         // Use the JPA builder to create a transaction scoped entity manager for
85         // as and execute the
86         // unit of work.
87         builder.execute(new JpaUnitOfWork<Void>() {
88             public Void execute(EntityManager aEm) {
89                 MyEntity entity = new MyEntity("a", "b");
90                 aEm.persist(entity);
91                 return null;
92             }
93         });
94
95         // Verify one row is written (using Db unit)
96         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
97         assertEquals(1, table.getRowCount());
98
99         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
100         assertEquals("b", table.getValue(0, "VALUE"));
101
102         // For this simple test, it can also be done through DatabaseUtils
103         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
104
105     }
106
107     @Test
108     public void testEntityPersistenceWithBasicApi() throws Exception {
109
110         // Use the JPA builder to create a transaction scoped entity manager for
111         // as and execute the
112         // unit of work.
113         EntityManager em = builder.begin();
114
115         MyEntity entity = new MyEntity("a", "b");
116         em.persist(entity);
117
118         builder.commit(em);
119
120         // Verify one row is written (using Db unit)
121         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
122         assertEquals(1, table.getRowCount());
123
124         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
125         assertEquals("b", table.getValue(0, "VALUE"));
126
127         // For this simple test, it can also be done through DatabaseUtils
128         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
129
130     }
131
132     @Test
133     public void testEntityPersistenceWithContextualEntityManager()
134         throws Exception {
135
136         // Use the JPA builder to create a transaction scoped entity manager for
137         // as and execute the
138         // unit of work.
139         builder.begin();
140
141         EntityManager em = builder.getContextualEntityManager();
142         MyEntity entity = new MyEntity("a", "b");
143         em.persist(entity);
144
145         builder.commit(em);
146
147         // Verify one row is written (using Db unit)
148         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
149         assertEquals(1, table.getRowCount());
150
151         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
152         assertEquals("b", table.getValue(0, "VALUE"));
153
154         // For this simple test, it can also be done through DatabaseUtils
155         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
156
157     }
158
159     @Test
160     public void testEntityPersistenceWithUserTransactions() throws Exception {
161
162         SimpleTransactionManager manager = new SimpleTransactionManager(
163             new DefaultUserTransactionFactory());
164         manager.addResource(jpaTester.getJpaBuilder());
165
166         UserTransaction transaction = manager.getTransaction();
167         transaction.begin();
168
169         EntityManager em = builder.getContextualEntityManager();
170         MyEntity entity = new MyEntity("a", "b");
171         em.persist(entity);
172
173         transaction.commit();
174
175         // Verify one row is written (using Db unit)
176         ITable table = dbtester.getDataSet().getTable("XYZ_MYENTITY");
177         assertEquals(1, table.getRowCount());
178
179         assertEquals("a", table.getValue(0, "SLEUTELTJE"));
180         assertEquals("b", table.getValue(0, "VALUE"));
181
182         // For this simple test, it can also be done through DatabaseUtils
183         assertEquals(1, dbutils.getTableSize("XYZ_MYENTITY"));
184
185     }
186
187     @Test
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");
193                 aEm.persist(entity);
194                 return null;
195             }
196         }, callback);
197         verify(callback).status(TransactionResult.COMMIT);
198         verifyNoMoreInteractions(callback);
199     }
200
201     @Test
202     public void testTransactionMarkedForRollback() throws Exception {
203         TransactionResultCallback callback = mock(TransactionResultCallback.class);
204
205         builder.execute(new JpaUnitOfWork<Void>() {
206             public Void execute(EntityManager aEm) {
207                 MyEntity entity = new MyEntity("a", "b");
208                 aEm.persist(entity);
209                 aEm.getTransaction().setRollbackOnly();
210                 return null;
211             }
212         }, callback);
213         verify(callback).status(TransactionResult.ROLLBACK);
214         verifyNoMoreInteractions(callback);
215     }
216
217     @Test
218     public void testTransactionRolledBack() throws Exception {
219         TransactionResultCallback callback = mock(TransactionResultCallback.class);
220
221         builder.execute(new JpaUnitOfWork<Void>() {
222             public Void execute(EntityManager aEm) {
223                 MyEntity entity = new MyEntity("a", "b");
224                 aEm.persist(entity);
225                 aEm.getTransaction().rollback();
226                 return null;
227             }
228         }, callback);
229         verify(callback).status(TransactionResult.UNKNOWN);
230         verifyNoMoreInteractions(callback);
231     }
232
233     @Test
234     public void testTransactionCommitted() throws Exception {
235         TransactionResultCallback callback = mock(TransactionResultCallback.class);
236
237         builder.execute(new JpaUnitOfWork<Void>() {
238             public Void execute(EntityManager aEm) {
239                 MyEntity entity = new MyEntity("a", "b");
240                 aEm.persist(entity);
241                 aEm.getTransaction().commit();
242                 return null;
243             }
244         }, callback);
245         verify(callback).status(TransactionResult.UNKNOWN);
246         verifyNoMoreInteractions(callback);
247     }
248
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");
254                 aEm.persist(entity);
255                 aEm.getTransaction().rollback();
256                 return null;
257             }
258         });
259     }
260
261 }