1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.persistencectx; 26 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertTrue; 29 30 import javax.ejb.Remote ; 31 import javax.ejb.Stateful ; 32 import javax.ejb.TransactionAttribute ; 33 import javax.ejb.TransactionAttributeType ; 34 import javax.persistence.EntityManager; 35 import javax.persistence.PersistenceContext; 36 import javax.persistence.PersistenceContextType; 37 38 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore; 39 40 45 @Stateful 46 @TransactionAttribute (TransactionAttributeType.REQUIRES_NEW) 47 @Remote (ItfPersistenceContextTester.class) 48 public class SFSBPersistenceContextTester implements ItfPersistenceContextTester { 49 50 53 @PersistenceContext(type = PersistenceContextType.TRANSACTION) 54 private EntityManager entityManagerTransaction; 55 56 59 @PersistenceContext(type = PersistenceContextType.EXTENDED) 60 private EntityManager entityManagerExtended; 61 62 65 private EBStore ebstoreTransaction; 66 67 70 private EBStore ebstoreExtended; 71 72 78 private EBStore createBean(final Integer id, final String name) { 79 EBStore ebstore = new EBStore(); 80 ebstore.setId(id.intValue()); 81 ebstore.setName(name); 82 return ebstore; 83 } 84 85 89 private void removeEBStore(){ 90 EBStore ebstoreTransaction = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION); 91 if(ebstoreTransaction != null){ 92 entityManagerTransaction.remove(ebstoreTransaction); 93 } 94 95 EBStore ebstoreExtended = entityManagerExtended.find(EBStore.class, ID_EXTENDED); 96 if(ebstoreExtended != null){ 97 entityManagerExtended.remove(ebstoreExtended); 98 } 99 100 } 101 102 106 public void startup() { 107 removeEBStore(); 109 ebstoreTransaction = createBean(ID_TRANSACTION, NAME_TRANSACTION); 111 entityManagerTransaction.persist(ebstoreTransaction); 112 ebstoreTransaction = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION); 113 114 ebstoreExtended = createBean(ID_EXTENDED, NAME_EXTENDED); 116 entityManagerExtended.persist(ebstoreExtended); 117 ebstoreExtended = entityManagerExtended.find(EBStore.class, ID_EXTENDED); 118 } 119 120 125 public void testTransactionPersistenceContext() { 126 EBStore ebstoreResult = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION); 127 assertFalse(ebstoreResult == ebstoreTransaction, 128 "The entities were goten in diferents persistence context, so they should not have the same reference."); 129 } 130 131 136 public void testExtendedPersistenceContext() { 137 EBStore ebstoreResult = entityManagerExtended.find(EBStore.class, ID_EXTENDED); 138 assertTrue(ebstoreResult == ebstoreExtended, 139 "The entities were goten in the same persistence context, so they should have the same reference."); 140 } 141 142 } 143 | Popular Tags |