1 17 18 package org.apache.geronimo.persistence; 19 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 import javax.persistence.EntityManager; 24 import javax.persistence.TransactionRequiredException; 25 import javax.ejb.EJBException ; 26 27 import junit.framework.TestCase; 28 import org.apache.geronimo.transaction.manager.GeronimoTransactionManager; 29 import org.apache.geronimo.persistence.mockjpa.MockEntityManagerFactory; 30 import org.apache.geronimo.persistence.mockjpa.MockEntityManager; 31 32 35 public class CMPEntityManagerTest extends TestCase { 36 37 private GeronimoTransactionManager tm; 38 private String persistenceUnit = "foo"; 39 private MockEntityManagerFactory entityManagerFactory; 40 41 protected void setUp() throws Exception { 42 tm = new GeronimoTransactionManager(); 43 tm.addTransactionAssociationListener(new TransactionListener()); 44 entityManagerFactory = new MockEntityManagerFactory(); 45 } 46 47 51 public void testGetTransaction() throws Exception { 52 CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null); 53 try { 54 entityManager1.getTransaction(); 55 fail("Expected IllegalStateException"); 56 } catch(IllegalStateException e) { 57 } catch (Exception e) { 59 fail("Wrong exception " + e); 60 } 61 tm.begin(); 62 try { 63 entityManager1.getTransaction(); 64 fail("Expected IllegalStateException"); 65 } catch(IllegalStateException e) { 66 } catch (Exception e) { 68 fail("Wrong exception " + e); 69 } 70 tm.commit(); 71 } 72 73 public void testJoinTransaction() throws Exception { 74 CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null); 75 try { 76 entityManager1.joinTransaction(); 77 fail("Expected IllegalStateException"); 78 } catch(IllegalStateException e) { 79 } catch (Exception e) { 81 fail("Wrong exception " + e); 82 } 83 tm.begin(); 84 try { 85 entityManager1.joinTransaction(); 86 fail("Expected IllegalStateException"); 87 } catch(IllegalStateException e) { 88 } catch (Exception e) { 90 fail("Wrong exception " + e); 91 } 92 tm.commit(); 93 } 94 95 99 public void testIsOpen() throws Exception { 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 116 public void testExtendedClosedOnBeanRemove() throws Exception { 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 130 public void testInheritedExtendedClosedOnBeanRemove() throws Exception { 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 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 entityManager1.beanRemoved(); 145 assertTrue("base EntityManager should not be closed", !pc1.isClosed()); 146 assertNotNull("InternalEntityManager should be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit)); 147 148 entityManager2.beanRemoved(); 150 assertTrue("base EntityManager should be closed", pc1.isClosed()); 151 assertNull("InternalEntityManager should not be registered", EntityManagerExtendedRegistry.getEntityManager(persistenceUnit)); 152 } 153 154 159 public void testNoSimultaneousEntityManagers() throws Exception { 160 CMPEntityManagerExtended entityManager1 = new CMPEntityManagerExtended(tm, persistenceUnit, entityManagerFactory, null); 162 CMPEntityManagerTxScoped entityManager2 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null); 164 tm.begin(); 165 MockEntityManager pc1 = (MockEntityManager) entityManager2.find(EntityManager.class, "this"); 167 InternalCMPEntityManagerExtended internalEntityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit); 169 try { 170 internalEntityManager.joinTransaction(); 171 fail("Expected EJBException"); 172 } catch (EJBException e) { 173 } catch (Exception e) { 175 fail("Unexpected exception " + e); 176 } 177 tm.commit(); 178 } 179 180 184 public void testSamePersistenceContext() throws Exception { 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 198 public void testCloseOnCommit() throws Exception { 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 217 public void testTransactionRequired() throws Exception { 218 CMPEntityManagerTxScoped entityManager1 = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null); 219 try { 220 entityManager1.persist("foo"); 221 fail("expected TransactionRequiredException"); 222 } catch (TransactionRequiredException e) { 223 } catch (Exception e) { 225 fail("Wrong exception" + e); 226 } 227 try { 228 entityManager1.remove("foo"); 229 fail("expected TransactionRequiredException"); 230 } catch (TransactionRequiredException e) { 231 } catch (Exception e) { 233 fail("Wrong exception" + e); 234 } 235 try { 236 entityManager1.merge("foo"); 237 fail("expected TransactionRequiredException"); 238 } catch (TransactionRequiredException e) { 239 } catch (Exception e) { 241 fail("Wrong exception" + e); 242 } 243 try { 244 entityManager1.refresh("foo"); 245 fail("expected TransactionRequiredException"); 246 } catch (TransactionRequiredException e) { 247 } catch (Exception e) { 249 fail("Wrong exception" + e); 250 } 251 } 252 253 257 public void testExtendedEntityManagerJoinsNewTransactions() throws Exception { 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 269 public void testAppCallsCloseForbidden() throws Exception { 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 e) { 275 } 277 tm.begin(); 278 try { 279 entityManager1.close(); 280 fail("Application should not be able to call close on its EntityManager"); 281 } catch (IllegalStateException e) { 282 } 284 tm.commit(); 285 } 286 287 288 293 public void testNoPropertiesUsed() throws Exception { 294 295 CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, null); 296 tm.begin(); 297 entityManager.contains("bar"); 298 Map props = entityManager.find(Map .class, "properties"); 299 assertSame("Props are not null", props, null); 300 tm.commit(); 301 } 302 303 308 public void testPropertiesUsed() throws Exception { 309 Map properties = new HashMap (); 310 CMPEntityManagerTxScoped entityManager = new CMPEntityManagerTxScoped(tm, persistenceUnit, entityManagerFactory, properties); 311 tm.begin(); 312 entityManager.contains("bar"); 313 Map props = entityManager.find(Map .class, "properties"); 314 assertSame("Props are not what was passed in", props, properties); 315 tm.commit(); 316 } 317 } 318 | Popular Tags |