1 25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ejbql; 26 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertTrue; 29 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.ejb.Remote ; 34 import javax.ejb.Stateless ; 35 import javax.ejb.TransactionAttribute ; 36 import javax.ejb.TransactionAttributeType ; 37 import javax.persistence.EntityManager; 38 import javax.persistence.PersistenceContext; 39 import javax.persistence.Query; 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 53 @Stateless 54 @Remote (ItfEjbqlTester.class) 55 @TransactionAttribute (TransactionAttributeType.REQUIRES_NEW) 56 public class SLSBEjbqlTester implements ItfEjbqlTester { 57 58 62 private static final int INTERVAL_BETWEEN_ID = 10; 63 64 67 private static final int NUMBER_OF_ORDERS = 8; 68 69 72 private static final String [] DESCRIPTION_VALUES = {"a", "b", "c", "d", "e", "f", "g", "h"}; 73 74 77 @PersistenceContext 78 private EntityManager entityManager; 79 80 89 private List <Product> createProducts(final int quantity, final int initialIdNumber) { 90 String [] strDescriptionValues = DESCRIPTION_VALUES; 91 List <Product> lstProducts = new ArrayList <Product>(); 92 for (int i = 0; i < quantity; i++) { 94 Product product = new Product(); 95 product.setId(i + initialIdNumber); 96 product.setDescription(strDescriptionValues[i]); 97 product.setPrice(i); 98 product.setCategory(entityManager.getReference(Category.class, new Long (i 101 % (strDescriptionValues.length / 2)))); 102 entityManager.persist(product); 103 entityManager.flush(); 104 lstProducts.add(product); 105 } 106 return lstProducts; 107 } 108 109 114 public void insertOrderReference(final List <Product> lstProducts, final ProductOrder productOrder) { 115 for (Object obj : lstProducts) { 116 Product product = (Product) obj; 117 product.setOrder(productOrder); 118 entityManager.merge(product); 119 } 120 } 121 122 125 private void startup() { 126 deleteAll(); 128 129 String [] strDescriptionValues = DESCRIPTION_VALUES; 130 131 for (int i = 0; i < strDescriptionValues.length; i++) { 134 Address address = new Address(); 135 address.setId(i); 136 address.setCountry("France"); 137 address.setNumber(i); 138 address.setStreet(strDescriptionValues[i]); 139 entityManager.persist(address); 140 } 141 entityManager.flush(); 142 143 for (int i = 0; i < strDescriptionValues.length / 2; i++) { 147 Category category = new Category(); 148 category.setId(i); 149 category.setDescription(strDescriptionValues[i]); 150 entityManager.persist(category); 151 } 152 entityManager.flush(); 153 154 for (int i = 0; i < strDescriptionValues.length; i++) { 156 ProductOrder productOrder = new ProductOrder(); 157 productOrder.setId(i); 158 productOrder.setDescription(strDescriptionValues[i]); 159 160 List <Product> lstProducts = createProducts(i, i * INTERVAL_BETWEEN_ID); 164 productOrder.setProducts(lstProducts); 165 entityManager.persist(productOrder); 166 insertOrderReference(lstProducts, productOrder); 168 } 169 entityManager.flush(); 170 171 for (int i = 0; i < strDescriptionValues.length / 2; i++) { 173 Customer customer = new Customer(); 174 customer.setId(i); 175 customer.setName(strDescriptionValues[i]); 176 customer.setAddress(entityManager.getReference(Address.class, new Long (i))); 179 List <ProductOrder> orders = new ArrayList <ProductOrder>(); 181 orders.add(entityManager.getReference(ProductOrder.class, new Long (i))); 182 orders.add(entityManager.getReference(ProductOrder.class, new Long (i + (NUMBER_OF_ORDERS / 2)))); 183 customer.setOrders(orders); 184 entityManager.persist(customer); 185 } 186 entityManager.flush(); 187 } 188 189 193 public void deleteEntity(final String entityName) { 194 Query queryResult = entityManager.createQuery("SELECT x FROM " + entityName+ " x"); 195 List lstEntity = queryResult.getResultList(); 196 for (Object obj : lstEntity) { 197 entityManager.remove(obj); 198 } 199 entityManager.flush(); 200 } 201 202 205 private void deleteAll() { 206 deleteEntity("Product"); 207 deleteEntity("ProductOrder"); 208 deleteEntity("Customer"); 209 deleteEntity("Category"); 210 deleteEntity("Address"); 211 } 212 213 216 public void testPathExpression() { 217 startup(); 218 Query query = entityManager 219 .createQuery("SELECT o.description FROM Customer c, IN(c.orders) o WHERE o.id = :productOrderId"); 220 query.setParameter("productOrderId", new Long (1)); 221 List lstProductOrderDesc = query.getResultList(); 222 assertEquals(lstProductOrderDesc.size(), 1, 223 "The query did not returned the correct value in a path expression."); 224 } 225 226 229 public void testInnerJoin() { 230 startup(); 231 Query query = entityManager 232 .createQuery("SELECT p.category FROM ProductOrder po JOIN po.products p WHERE po.id BETWEEN ?1 AND ?2"); 233 query.setParameter(1, new Long (0)); 234 query.setParameter(2, new Long (NUMBER_OF_ORDERS)); 235 List lstCategory = query.getResultList(); 236 assertEquals(lstCategory.size(), NUMBER_OF_ORDERS / 2, "The inner join does not work properly"); 237 } 238 239 242 public void testIsEmpty() { 243 startup(); 244 Query query = entityManager.createQuery("SELECT po FROM ProductOrder po WHERE po.products IS EMPTY"); 245 List lstProductOrder = query.getResultList(); 246 for (Object obj : lstProductOrder) { 247 ProductOrder order = (ProductOrder) obj; 248 assertEquals(order.getId(), 0, "The query result is incorrect."); 249 } 250 } 251 252 255 public void testDelete() { 256 startup(); 257 Query query = entityManager.createQuery("DELETE FROM Customer c WHERE c.id > 2"); 258 query.executeUpdate(); 259 entityManager.flush(); 260 Query queryTest = entityManager.createQuery("SELECT c FROM Customer c"); 261 List lstCustomer = queryTest.getResultList(); 262 for (Object obj : lstCustomer) { 263 Customer customer = (Customer) obj; 264 assertTrue(customer.getId() <= 2, "The operation delete does not work."); 265 } 266 } 267 268 271 public void testUpdate() { 272 startup(); 273 Query query = entityManager.createQuery("UPDATE Address a SET a.country = 'Brazil' WHERE a.id =1"); 274 query.executeUpdate(); 275 entityManager.flush(); 276 testVerifyUpdate(); 277 } 278 279 283 private void testVerifyUpdate(){ 284 Address address = entityManager.find(Address.class, new Long (1)); 285 assertEquals(address.getCountry(), "Brazil", "The opertion update does not work"); 286 } 287 290 public void testHaving() { 291 startup(); 292 Query query = entityManager 293 .createQuery("SELECT p.price, COUNT(p) FROM Product p GROUP BY p.price HAVING p.price > 2"); 294 query.getResultList(); 295 } 296 } 297 | Popular Tags |