1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.resourceleveltrans; 26 27 import static org.testng.Assert.assertFalse; 28 import static org.testng.Assert.assertNotNull; 29 import static org.testng.Assert.assertNull; 30 import static org.testng.Assert.assertTrue; 31 import static org.testng.Assert.fail; 32 33 import javax.ejb.Remote ; 34 import javax.ejb.Stateful ; 35 import javax.ejb.TransactionAttribute ; 36 import javax.ejb.TransactionAttributeType ; 37 import javax.persistence.EntityManager; 38 import javax.persistence.EntityManagerFactory; 39 import javax.persistence.PersistenceUnit; 40 import javax.persistence.RollbackException; 41 42 import org.objectweb.easybeans.log.JLog; 43 import org.objectweb.easybeans.tests.common.ejbs.entity.book.Book; 44 45 50 @Stateful 51 @Remote (ItfResourceLevelTransTester.class) 52 @TransactionAttribute (TransactionAttributeType.NOT_SUPPORTED) 53 public class SFSBResourceLevelTransTester implements ItfResourceLevelTransTester { 54 55 58 public static final int ID = 1; 59 60 63 public static final String ENTITY_NAME = "test"; 64 65 68 @PersistenceUnit 69 private EntityManagerFactory entityManagerFactory; 70 71 74 private JLog logger; 75 76 79 public void startup() { 80 EntityManager entityManager = entityManagerFactory.createEntityManager(); 81 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 82 if (bookResult != null) { 83 entityManager.remove(bookResult); 84 } 85 } 86 87 90 public void resourceLevelRollback() { 91 EntityManager entityManager = entityManagerFactory.createEntityManager(); 92 entityManager.getTransaction().begin(); 94 Book book = new Book(); 96 book.setId(ID); 97 book.setName(ENTITY_NAME); 98 entityManager.persist(book); 99 entityManager.getTransaction().rollback(); 101 entityManager.close(); 102 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 104 assertNull(bookResult, "The container did not make the rollback"); 105 } 106 107 110 public void setRollbackOnly() { 111 EntityManager entityManager = entityManagerFactory.createEntityManager(); 112 entityManager.getTransaction().begin(); 114 entityManager.getTransaction().setRollbackOnly(); 115 Book book = new Book(); 117 book.setId(ID); 118 book.setName(ENTITY_NAME); 119 entityManager.persist(book); 120 assertTrue(entityManager.getTransaction().getRollbackOnly(), 122 "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()"); 123 124 try { 126 entityManager.getTransaction().commit(); 127 fail("The method setRollbackOnly was called, so the transaction cannot make the commit."); 128 } catch (RollbackException e) { 129 logger.info("The bean threw an expected exception"); 130 } 131 entityManager.close(); 132 133 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 135 assertNull(bookResult, "The container did not make the rollback"); 136 } 137 138 141 public void resourceLevelCommit() { 142 EntityManager entityManager = entityManagerFactory.createEntityManager(); 143 entityManager.getTransaction().begin(); 145 Book book = new Book(); 147 book.setId(ID); 148 book.setName(ENTITY_NAME); 149 entityManager.persist(book); 150 assertTrue(entityManager.getTransaction().isActive(), 152 "The transaction is active, but the container returned false when the method isActive was called."); 153 assertFalse(entityManager.getTransaction().getRollbackOnly(), 155 "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()"); 156 entityManager.getTransaction().commit(); 158 entityManager.close(); 159 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 161 assertNotNull(bookResult, "The container did not make the rollback"); 162 163 } 164 } 165 | Popular Tags |