1 25 package org.objectweb.easybeans.tests.javasesupport; 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.persistence.EntityManager; 34 import javax.persistence.EntityManagerFactory; 35 import javax.persistence.Persistence; 36 import javax.persistence.RollbackException; 37 38 import org.objectweb.easybeans.log.JLog; 39 import org.objectweb.easybeans.tests.common.ejbs.entity.book.Book; 40 41 46 public class ResourceLevelTransTester { 47 48 51 public static final int ID = 1; 52 53 56 public static final String ENTITY_NAME = "test"; 57 58 61 private EntityManagerFactory entityManagerFactory; 62 63 66 private JLog logger; 67 68 71 public ResourceLevelTransTester() { 72 entityManagerFactory = Persistence.createEntityManagerFactory("book"); 74 EntityManager entityManager = entityManagerFactory.createEntityManager(); 75 76 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 77 if (bookResult != null) { 78 entityManager.remove(bookResult); 79 } 80 } 81 82 85 public void resourceLevelRollback() { 86 EntityManager entityManager = entityManagerFactory.createEntityManager(); 87 entityManager.getTransaction().begin(); 89 Book book = new Book(); 91 book.setId(ID); 92 book.setName(ENTITY_NAME); 93 entityManager.persist(book); 94 entityManager.getTransaction().rollback(); 96 entityManager.close(); 97 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 99 assertNull(bookResult, "The container did not make the rollback"); 100 } 101 102 105 public void setRollbackOnly() { 106 EntityManager entityManager = entityManagerFactory.createEntityManager(); 107 entityManager.getTransaction().begin(); 109 entityManager.getTransaction().setRollbackOnly(); 110 Book book = new Book(); 112 book.setId(ID); 113 book.setName(ENTITY_NAME); 114 entityManager.persist(book); 115 assertTrue(entityManager.getTransaction().getRollbackOnly(), 117 "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()"); 118 119 try { 121 entityManager.getTransaction().commit(); 122 fail("The method setRollbackOnly was called, so the transaction cannot make the commit."); 123 } catch (RollbackException e) { 124 logger.info("The bean threw an expected exception"); 125 } 126 entityManager.close(); 127 128 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 130 assertNull(bookResult, "The container did not make the rollback"); 131 } 132 133 136 public void resourceLevelCommit() { 137 EntityManager entityManager = entityManagerFactory.createEntityManager(); 138 entityManager.getTransaction().begin(); 140 Book book = new Book(); 142 book.setId(ID); 143 book.setName(ENTITY_NAME); 144 entityManager.persist(book); 145 assertTrue(entityManager.getTransaction().isActive(), 147 "The transaction is active, but the container returned false when the method isActive was called."); 148 assertFalse(entityManager.getTransaction().getRollbackOnly(), 150 "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()"); 151 entityManager.getTransaction().commit(); 153 entityManager.close(); 154 Book bookResult = entityManager.find(Book.class, new Integer (ID)); 156 assertNotNull(bookResult, "The container did not make the rollback"); 157 158 } 159 } 160 | Popular Tags |