KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > jpa > CMPEntityManagerTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.jpa;
19
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import javax.persistence.EntityManager;
24 import javax.persistence.TransactionRequiredException;
25 import javax.ejb.EJBException JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.apache.geronimo.transaction.jta11.GeronimoTransactionManagerJTA11;
29 import org.apache.geronimo.transaction.mockjpa.MockEntityManagerFactory;
30 import org.apache.geronimo.transaction.mockjpa.MockEntityManager;
31
32 /**
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  */

35 public class CMPEntityManagerTest extends TestCase {
36
37     private GeronimoTransactionManagerJTA11 tm;
38     private String JavaDoc persistenceUnit = "foo";
39     private MockEntityManagerFactory entityManagerFactory;
40
41     protected void setUp() throws Exception JavaDoc {
42         tm = new GeronimoTransactionManagerJTA11();
43         tm.addTransactionAssociationListener(new TransactionListener());
44         entityManagerFactory = new MockEntityManagerFactory();
45     }
46
47     /**
48      * section 3.1.1
49      * (not very clear). getTransaction, joinTransaction throw IllegalStateException
50      */

51     public void testGetTransaction() throws Exception JavaDoc {
52         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
53         try {
54             entityManager1.getTransaction();
55             fail("Expected IllegalStateException");
56         } catch(IllegalStateException JavaDoc e) {
57             //expected
58
} catch (Exception JavaDoc e) {
59             fail("Wrong exception " + e);
60         }
61         tm.begin();
62         try {
63             entityManager1.getTransaction();
64             fail("Expected IllegalStateException");
65         } catch(IllegalStateException JavaDoc e) {
66             //expected
67
} catch (Exception JavaDoc e) {
68             fail("Wrong exception " + e);
69         }
70         tm.commit();
71     }
72
73     public void testJoinTransaction() throws Exception JavaDoc {
74         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
75         try {
76             entityManager1.joinTransaction();
77             fail("Expected IllegalStateException");
78         } catch(IllegalStateException JavaDoc e) {
79             //expected
80
} catch (Exception JavaDoc e) {
81             fail("Wrong exception " + e);
82         }
83         tm.begin();
84         try {
85             entityManager1.joinTransaction();
86             fail("Expected IllegalStateException");
87         } catch(IllegalStateException JavaDoc e) {
88             //expected
89
} catch (Exception JavaDoc e) {
90             fail("Wrong exception " + e);
91         }
92         tm.commit();
93     }
94
95     /**
96      * section 3.1.1 ????
97      * isOpen returns true
98      */

99     public void testIsOpen() throws Exception JavaDoc {
100         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
101         assertTrue(entityManager1.isOpen());
102         tm.begin();
103         assertTrue(entityManager1.isOpen());
104         tm.commit();
105         assertTrue(entityManager1.isOpen());
106         tm.begin();
107         assertTrue(entityManager1.isOpen());
108         tm.rollback();
109         assertTrue(entityManager1.isOpen());
110     }
111
112     /**
113      * section 5.6.2
114      * extended context is closed when the SFSB that caused it is removed
115      */

116     public void testExtendedClosedOnBeanRemove() throws Exception JavaDoc {
117         CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null);
118         MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
119         assertTrue("base EntityManager should not be closed", !pc1.isClosed());
120         assertNotNull("InternalEntityManager should be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
121         entityManager1.beanRemoved();
122         assertTrue("base EntityManager should be closed", pc1.isClosed());
123         assertNull("InternalEntityManager should not be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
124     }
125
126     /**
127      * section 5.6.2.1
128      * extended context is closed when the SFSB that caused it and all others that share it are removed
129      */

130     public void testInheritedExtendedClosedOnBeanRemove() throws Exception JavaDoc {
131         CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null);
132         MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
133         assertTrue("base EntityManager should not be closed", !pc1.isClosed());
134         InternalCMPEntityManagerExtended internalEntityManager1 = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
135         assertNotNull("InternalEntityManager should be registered", internalEntityManager1);
136         CMPEntityManagerExtended entityManager2 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null);
137         InternalCMPEntityManagerExtended internalEntityManager2 = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
138         //we should have got an exception if this isn't true
139
assertSame("2nd entity manager registering should use same internal entity manager", internalEntityManager1, internalEntityManager2);
140         MockEntityManager pc2 = (MockEntityManager) entityManager2.find(EntityManager.class, "this");
141         assertSame("2nd entity manager registering should use same mock entity manager", pc1, pc2);
142
143         //remove one bean, internal and mock entity managers should not change state
144
entityManager1.beanRemoved();
145         assertTrue("base EntityManager should not be closed", !pc1.isClosed());
146         assertNotNull("InternalEntityManager should be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
147
148         //close other bean, everything should close and unregister
149
entityManager2.beanRemoved();
150         assertTrue("base EntityManager should be closed", pc1.isClosed());
151         assertNull("InternalEntityManager should not be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
152     }
153
154     /**
155      * section 5.6.3.1
156      * Trying to propagate a JTA tx with a persistence context bound into a SFSB with Extended persistence context
157      * results in an EJBException
158      */

159     public void testNoSimultaneousEntityManagers() throws Exception JavaDoc {
160         //set up the extended persistence context:
161
CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null);
162         //set up the caller
163
CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
164         tm.begin();
165         //register the caller
166
MockEntityManager pc1 = (MockEntityManager) entityManager2.find(EntityManager.class, "this");
167         //caller calling SFSB means entityManager1 tries to join the trasaction:
168
InternalCMPEntityManagerExtended internalEntityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
169         try {
170             internalEntityManager.joinTransaction();
171             fail("Expected EJBException");
172         } catch (EJBException JavaDoc e) {
173             //expected
174
} catch (Exception JavaDoc e) {
175             fail("Unexpected exception " + e);
176         }
177         tm.commit();
178     }
179
180     /**
181      * section 5.8.2
182      * use the same persistence context for all work in a tx
183      */

184     public void testSamePersistenceContext() throws Exception JavaDoc {
185         tm.begin();
186         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
187         EntityManager pc1 = entityManager1.find(EntityManager.class, "this");
188         CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
189         EntityManager pc2 = entityManager2.find(EntityManager.class, "this");
190         assertSame("Should get same entity manager for all work in a tx", pc1, pc2);
191         tm.commit();
192     }
193
194     /**
195      * section 5.9.1
196      * close or cleared is called when tx commits
197      */

198     public void testCloseOnCommit() throws Exception JavaDoc {
199         tm.begin();
200         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
201         MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
202         assertTrue("entityManager should not be closed or cleared", !pc1.isClosed() & !pc1.isCleared());
203         tm.commit();
204         assertTrue("entityManager should be closed or cleared", pc1.isClosed() || pc1.isCleared());
205         tm.begin();
206         CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
207         MockEntityManager pc2 = (MockEntityManager) entityManager2.find(EntityManager.class, "this");
208         assertTrue("entityManager should not be closed or cleared", !pc2.isClosed() & !pc2.isCleared());
209         tm.rollback();
210         assertTrue("entityManager should be closed or cleared", pc2.isClosed() || pc2.isCleared());
211     }
212
213     /**
214      * section 5.9.1
215      * transaction required for persist, remove, merge, refresh
216      */

217     public void testTransactionRequired() throws Exception JavaDoc {
218         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
219         try {
220             entityManager1.persist("foo");
221             fail("expected TransactionRequiredException");
222         } catch (TransactionRequiredException e) {
223             //expected
224
} catch (Exception JavaDoc e) {
225             fail("Wrong exception" + e);
226         }
227         try {
228             entityManager1.remove("foo");
229             fail("expected TransactionRequiredException");
230         } catch (TransactionRequiredException e) {
231             //expected
232
} catch (Exception JavaDoc e) {
233             fail("Wrong exception" + e);
234         }
235         try {
236             entityManager1.merge("foo");
237             fail("expected TransactionRequiredException");
238         } catch (TransactionRequiredException e) {
239             //expected
240
} catch (Exception JavaDoc e) {
241             fail("Wrong exception" + e);
242         }
243         try {
244             entityManager1.refresh("foo");
245             fail("expected TransactionRequiredException");
246         } catch (TransactionRequiredException e) {
247             //expected
248
} catch (Exception JavaDoc e) {
249             fail("Wrong exception" + e);
250         }
251     }
252
253     /**
254      * section 5.9.1
255      * when a SFSB/extended context starts a UserTransaction or a CMT tx starts the EM must join the transaction
256      */

257     public void testExtendedEntityManagerJoinsNewTransactions() throws Exception JavaDoc {
258         CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null);
259         tm.begin();
260         MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
261
262         assertTrue("EntityManager was supposed to join the tx", pc1.isJoined());
263     }
264
265     /**
266      * section 5.9.1
267      * application must not call close on its entityManager
268      */

269     public void testAppCallsCloseForbidden() throws Exception JavaDoc {
270         CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
271         try {
272             entityManager1.close();
273             fail("Application should not be able to call close on its EntityManager");
274         } catch (IllegalStateException JavaDoc e) {
275             //expected
276
}
277         tm.begin();
278         try {
279             entityManager1.close();
280             fail("Application should not be able to call close on its EntityManager");
281         } catch (IllegalStateException JavaDoc e) {
282             //expected
283
}
284         tm.commit();
285     }
286
287
288     /**
289      * section 5.9.1
290      *
291      * @throws Exception
292      */

293     public void testNoPropertiesUsed() throws Exception JavaDoc {
294
295         CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
296         tm.begin();
297         entityManager.contains("bar");
298         Map JavaDoc props = entityManager.find(Map JavaDoc.class, "properties");
299         assertSame("Props are not null", props, null);
300         tm.commit();
301     }
302
303     /**
304      * section 5.9.1
305      *
306      * @throws Exception
307      */

308     public void testPropertiesUsed() throws Exception JavaDoc {
309         Map JavaDoc properties = new HashMap JavaDoc();
310         CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, properties);
311         tm.begin();
312         entityManager.contains("bar");
313         Map JavaDoc props = entityManager.find(Map JavaDoc.class, "properties");
314         assertSame("Props are not what was passed in", props, properties);
315         tm.commit();
316     }
317 }
318
Popular Tags