1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.cascadeoperation; 26 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertFalse; 29 import static org.testng.Assert.assertTrue; 30 31 import java.util.ArrayList ; 32 import java.util.List ; 33 34 import javax.ejb.Remote ; 35 import javax.ejb.Stateful ; 36 import javax.ejb.TransactionAttribute ; 37 import javax.ejb.TransactionAttributeType ; 38 import javax.persistence.EntityManager; 39 import javax.persistence.PersistenceContext; 40 41 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Address; 42 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Category; 43 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Customer; 44 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Product; 45 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.ProductOrder; 46 47 52 @Stateful 53 @Remote (ItfCascadeTester.class) 54 @TransactionAttribute (TransactionAttributeType.REQUIRES_NEW) 55 public class SFSBCascadeTester implements ItfCascadeTester { 56 57 60 @PersistenceContext 61 private EntityManager entityManager; 62 63 66 public static final long ID = 1; 67 68 71 public static final long ALTERNATIVE_ID = 2; 72 73 76 public static final float PRODUCT_PRICE = 2.4f; 77 78 81 public static final float PRODUCT_PRICE_2 = 4.6f; 82 83 88 private <T> void removeBean(final Class <T> entityClass) { 89 T objBean; 90 objBean = entityManager.find(entityClass, new Long (ID)); 91 if (objBean != null) { 92 entityManager.remove(objBean); 93 } 94 } 95 96 100 private void removeAll() { 101 removeBean(Category.class); 102 removeBean(Product.class); 103 removeBean(ProductOrder.class); 104 removeBean(Customer.class); 105 removeBean(Address.class); 106 } 107 108 111 public void startup() { 112 removeAll(); 113 114 Category category = new Category(); 116 category.setId(ID); 117 category.setDescription("description"); 118 entityManager.persist(category); 119 120 Product product = new Product(ID, "description", PRODUCT_PRICE, category); 122 entityManager.persist(product); 123 124 Address address = new Address(ID, "street", "country", 1); 126 entityManager.persist(address); 127 128 Customer customer = new Customer(); 130 customer.setId(ID); 131 customer.setAddress(address); 132 customer.setName("customer"); 133 entityManager.persist(customer); 134 135 List <Product> products = new ArrayList <Product>(); 137 products.add(product); 138 ProductOrder order = new ProductOrder(ID, "description", customer, products); 139 entityManager.persist(order); 140 141 } 142 143 146 public void verifyCascadeTypeRefresh() { 147 Category category = entityManager.find(Category.class, new Long (ID)); 148 category.setDescription("new description"); 149 150 Product product = entityManager.find(Product.class, new Long (ID)); 151 product.setPrice(PRODUCT_PRICE_2); 152 153 entityManager.refresh(product); 154 155 assertEquals(category.getDescription(), "description", "The container did not make the refresh in cascade."); 156 } 157 158 161 public void verifyCascadeTypeRemove() { 162 ProductOrder order = entityManager.find(ProductOrder.class, new Long (ID)); 163 assertFalse(order == null, "The container did not find the order"); 164 165 entityManager.remove(order); 166 entityManager.flush(); 167 168 Product productResult = entityManager.find(Product.class, new Long (ID)); 169 assertTrue(productResult == null, "The container did not make the delete in cascade."); 170 } 171 172 175 public void verifyCascadeTypeMerge() { 176 ProductOrder order = entityManager.find(ProductOrder.class, new Long (ID)); 177 Customer customer = entityManager.find(Customer.class, new Long (ID)); 178 179 entityManager.clear(); 180 order.setDescription("new description"); 181 customer.setName("new customer"); 182 183 entityManager.merge(customer); 184 entityManager.flush(); 185 186 assertEquals(order.getDescription(), "new description", "The container did not make the merge in cascade"); 187 } 188 189 192 public void verifyCascadeTypePersist() { 193 Customer customer = entityManager.find(Customer.class, new Long (ID)); 194 Address addressNew = new Address(ALTERNATIVE_ID, "street", "country", 1); 195 customer.setAddress(addressNew); 196 197 entityManager.persist(customer); 198 entityManager.flush(); 199 200 Address addressResultAfterPersist = entityManager.find(Address.class, new Long (2)); 201 assertFalse(addressResultAfterPersist == null, "The container did not make a persist in cascade."); 202 } 203 } 204 | Popular Tags |