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