001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.persistence;
017:
018: import java.util.Map;
019: import java.util.HashMap;
020:
021: import javax.persistence.EntityManager;
022: import javax.persistence.TransactionRequiredException;
023: import javax.ejb.EJBException;
024:
025: import junit.framework.TestCase;
026: import org.apache.geronimo.transaction.manager.GeronimoTransactionManager;
027: import org.apache.geronimo.persistence.mockjpa.MockEntityManagerFactory;
028: import org.apache.geronimo.persistence.mockjpa.MockEntityManager;
029:
030: /**
031: * @version $Rev: 538730 $ $Date: 2007-05-16 14:07:51 -0700 (Wed, 16 May 2007) $
032: */
033: public class CMPEntityManagerTest extends TestCase {
034:
035: private GeronimoTransactionManager tm;
036: private String persistenceUnit = "foo";
037: private MockEntityManagerFactory entityManagerFactory;
038:
039: protected void setUp() throws Exception {
040: tm = new GeronimoTransactionManager();
041: tm.addTransactionAssociationListener(new TransactionListener());
042: entityManagerFactory = new MockEntityManagerFactory();
043: }
044:
045: /**
046: * section 3.1.1
047: * (not very clear). getTransaction, joinTransaction throw IllegalStateException
048: */
049: public void testGetTransaction() throws Exception {
050: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
051: tm, persistenceUnit, entityManagerFactory, null);
052: try {
053: entityManager1.getTransaction();
054: fail("Expected IllegalStateException");
055: } catch (IllegalStateException e) {
056: //expected
057: } catch (Exception e) {
058: fail("Wrong exception " + e);
059: }
060: tm.begin();
061: try {
062: entityManager1.getTransaction();
063: fail("Expected IllegalStateException");
064: } catch (IllegalStateException e) {
065: //expected
066: } catch (Exception e) {
067: fail("Wrong exception " + e);
068: }
069: tm.commit();
070: }
071:
072: public void testJoinTransaction() throws Exception {
073: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
074: tm, persistenceUnit, entityManagerFactory, null);
075: try {
076: entityManager1.joinTransaction();
077: fail("Expected IllegalStateException");
078: } catch (IllegalStateException e) {
079: //expected
080: } catch (Exception e) {
081: fail("Wrong exception " + e);
082: }
083: tm.begin();
084: try {
085: entityManager1.joinTransaction();
086: fail("Expected IllegalStateException");
087: } catch (IllegalStateException e) {
088: //expected
089: } catch (Exception e) {
090: fail("Wrong exception " + e);
091: }
092: tm.commit();
093: }
094:
095: /**
096: * section 3.1.1 ????
097: * isOpen returns true
098: */
099: public void testIsOpen() throws Exception {
100: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
101: tm, persistenceUnit, entityManagerFactory, null);
102: assertTrue(entityManager1.isOpen());
103: tm.begin();
104: assertTrue(entityManager1.isOpen());
105: tm.commit();
106: assertTrue(entityManager1.isOpen());
107: tm.begin();
108: assertTrue(entityManager1.isOpen());
109: tm.rollback();
110: assertTrue(entityManager1.isOpen());
111: }
112:
113: /**
114: * section 5.6.2
115: * extended context is closed when the SFSB that caused it is removed
116: */
117: // public void testExtendedClosedOnBeanRemove() throws Exception {
118: // CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(entityManagerRegistry, entityManagerFactory, null);
119: // MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
120: // assertTrue("base EntityManager should not be closed", !pc1.isClosed());
121: // assertNotNull("InternalEntityManager should be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
122: // entityManager1.beanRemoved();
123: // assertTrue("base EntityManager should be closed", pc1.isClosed());
124: // assertNull("InternalEntityManager should not be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit));
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 {
131: // CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(entityManagerRegistry, 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(entityManagerRegistry, 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: * section 5.6.3.1
155: * Trying to propagate a JTA tx with a persistence context bound into a SFSB with Extended persistence context
156: * results in an EJBException
157: */
158: // public void testNoSimultaneousEntityManagers() throws Exception {
159: // //set up the extended persistence context:
160: // CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(entityManagerRegistry, entityManagerFactory, null);
161: // //set up the caller
162: // CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null);
163: // tm.begin();
164: // //register the caller
165: // MockEntityManager pc1 = (MockEntityManager) entityManager2.find(EntityManager.class, "this");
166: // //caller calling SFSB means entityManager1 tries to join the trasaction:
167: // InternalCMPEntityManagerExtended internalEntityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit);
168: // try {
169: // internalEntityManager.joinTransaction();
170: // fail("Expected EJBException");
171: // } catch (EJBException e) {
172: // //expected
173: // } catch (Exception e) {
174: // fail("Unexpected exception " + e);
175: // }
176: // tm.commit();
177: // }
178: /**
179: * section 5.8.2
180: * use the same persistence context for all work in a tx
181: */
182: public void testSamePersistenceContext() throws Exception {
183: tm.begin();
184: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
185: tm, persistenceUnit, entityManagerFactory, null);
186: EntityManager pc1 = entityManager1.find(EntityManager.class,
187: "this");
188: CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(
189: tm, persistenceUnit, entityManagerFactory, null);
190: EntityManager pc2 = entityManager2.find(EntityManager.class,
191: "this");
192: assertSame(
193: "Should get same entity manager for all work in a tx",
194: pc1, pc2);
195: tm.commit();
196: }
197:
198: /**
199: * section 5.9.1
200: * close or cleared is called when tx commits
201: */
202: public void testCloseOnCommit() throws Exception {
203: tm.begin();
204: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
205: tm, persistenceUnit, entityManagerFactory, null);
206: MockEntityManager pc1 = (MockEntityManager) entityManager1
207: .find(EntityManager.class, "this");
208: assertTrue("entityManager should not be closed or cleared",
209: !pc1.isClosed() & !pc1.isCleared());
210: tm.commit();
211: assertTrue("entityManager should be closed or cleared", pc1
212: .isClosed()
213: || pc1.isCleared());
214: tm.begin();
215: CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(
216: tm, persistenceUnit, entityManagerFactory, null);
217: MockEntityManager pc2 = (MockEntityManager) entityManager2
218: .find(EntityManager.class, "this");
219: assertTrue("entityManager should not be closed or cleared",
220: !pc2.isClosed() & !pc2.isCleared());
221: tm.rollback();
222: assertTrue("entityManager should be closed or cleared", pc2
223: .isClosed()
224: || pc2.isCleared());
225: }
226:
227: /**
228: * section 5.9.1
229: * transaction required for persist, remove, merge, refresh
230: */
231: public void testTransactionRequired() throws Exception {
232: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
233: tm, persistenceUnit, entityManagerFactory, null);
234: try {
235: entityManager1.persist("foo");
236: fail("expected TransactionRequiredException");
237: } catch (TransactionRequiredException e) {
238: //expected
239: } catch (Exception e) {
240: fail("Wrong exception" + e);
241: }
242: try {
243: entityManager1.remove("foo");
244: fail("expected TransactionRequiredException");
245: } catch (TransactionRequiredException e) {
246: //expected
247: } catch (Exception e) {
248: fail("Wrong exception" + e);
249: }
250: try {
251: entityManager1.merge("foo");
252: fail("expected TransactionRequiredException");
253: } catch (TransactionRequiredException e) {
254: //expected
255: } catch (Exception e) {
256: fail("Wrong exception" + e);
257: }
258: try {
259: entityManager1.refresh("foo");
260: fail("expected TransactionRequiredException");
261: } catch (TransactionRequiredException e) {
262: //expected
263: } catch (Exception e) {
264: fail("Wrong exception" + e);
265: }
266: }
267:
268: /**
269: * section 5.9.1
270: * when a SFSB/extended context starts a UserTransaction or a CMT tx starts the EM must join the transaction
271: */
272: // public void testExtendedEntityManagerJoinsNewTransactions() throws Exception {
273: // CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(entityManagerRegistry, entityManagerFactory, null);
274: // tm.begin();
275: // MockEntityManager pc1 = (MockEntityManager) entityManager1.find(EntityManager.class, "this");
276: //
277: // assertTrue("EntityManager was supposed to join the tx", pc1.isJoined());
278: // }
279: /**
280: * section 5.9.1
281: * application must not call close on its entityManager
282: */
283: public void testAppCallsCloseForbidden() throws Exception {
284: CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(
285: tm, persistenceUnit, entityManagerFactory, null);
286: try {
287: entityManager1.close();
288: fail("Application should not be able to call close on its EntityManager");
289: } catch (IllegalStateException e) {
290: //expected
291: }
292: tm.begin();
293: try {
294: entityManager1.close();
295: fail("Application should not be able to call close on its EntityManager");
296: } catch (IllegalStateException e) {
297: //expected
298: }
299: tm.commit();
300: }
301:
302: /**
303: * section 5.9.1
304: *
305: * @throws Exception
306: */
307: public void testNoPropertiesUsed() throws Exception {
308:
309: CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(
310: tm, persistenceUnit, entityManagerFactory, null);
311: tm.begin();
312: entityManager.contains("bar");
313: Map props = entityManager.find(Map.class, "properties");
314: assertSame("Props are not null", props, null);
315: tm.commit();
316: }
317:
318: /**
319: * section 5.9.1
320: *
321: * @throws Exception
322: */
323: public void testPropertiesUsed() throws Exception {
324: Map properties = new HashMap();
325: CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(
326: tm, persistenceUnit, entityManagerFactory, properties);
327: tm.begin();
328: entityManager.contains("bar");
329: Map props = entityManager.find(Map.class, "properties");
330: assertSame("Props are not what was passed in", props,
331: properties);
332: tm.commit();
333: }
334: }
|