KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > ejbql > SLSBEjbqlTester


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SLSBEjbqlTester.java 822 2006-07-04 14:35:35Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

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 JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.ejb.Remote JavaDoc;
34 import javax.ejb.Stateless JavaDoc;
35 import javax.ejb.TransactionAttribute JavaDoc;
36 import javax.ejb.TransactionAttributeType JavaDoc;
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 /**
48  * Verifies some queries in EJB QL.
49  * @author Gisele Pinheiro Souza
50  * @author Eduardo Studzinski Estima de Castro
51  *
52  */

53 @Stateless JavaDoc
54 @Remote JavaDoc(ItfEjbqlTester.class)
55 @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRES_NEW)
56 public class SLSBEjbqlTester implements ItfEjbqlTester {
57
58     /**
59      * The identifier interval between the group of products that are in the
60      * different ProductOrder.
61      */

62     private static final int INTERVAL_BETWEEN_ID = 10;
63
64     /**
65      * The number of product order.
66      */

67     private static final int NUMBER_OF_ORDERS = 8;
68
69     /**
70      * The list of descriptions.
71      */

72     private static final String JavaDoc[] DESCRIPTION_VALUES = {"a", "b", "c", "d", "e", "f", "g", "h"};
73
74     /**
75      * The persistence context used during the tests.
76      */

77     @PersistenceContext
78     private EntityManager entityManager;
79
80     /**
81      * Creates a number of products that is defined in the parameter quantity.
82      * The identifier are sequencial and the initial id is the parameter
83      * initialIdNumber.The description is a letter. The price is a value between
84      * 0 and the the quantity of products.
85      * @param quantity the quantity of products creadtesd
86      * @param initialIdNumber the initial id number.
87      * @return the list of the products created.
88      */

89     private List JavaDoc<Product> createProducts(final int quantity, final int initialIdNumber) {
90         String JavaDoc[] strDescriptionValues = DESCRIPTION_VALUES;
91         List JavaDoc<Product> lstProducts = new ArrayList JavaDoc<Product>();
92         // creates the product.
93
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             // sets the category for a value between the intial category id = 0
99
// and the maximum= 4
100
product.setCategory(entityManager.getReference(Category.class, new Long JavaDoc(i
101                     % (strDescriptionValues.length / 2))));
102             entityManager.persist(product);
103             entityManager.flush();
104             lstProducts.add(product);
105         }
106         return lstProducts;
107     }
108
109     /**
110      * Inserts the reference for the order in the product.
111      * @param lstProducts the list of products that are in the same order.
112      * @param productOrder the order that has all products.
113      */

114     public void insertOrderReference(final List JavaDoc<Product> lstProducts, final ProductOrder productOrder) {
115         for (Object JavaDoc obj : lstProducts) {
116             Product product = (Product) obj;
117             product.setOrder(productOrder);
118             entityManager.merge(product);
119         }
120     }
121
122     /**
123      * Creates the entiies that are uses in the test.
124      */

125     private void startup() {
126         // cleans the database
127
deleteAll();
128
129         String JavaDoc[] strDescriptionValues = DESCRIPTION_VALUES;
130
131         // Creates the entities Address. The DESCRIPTION_VALUES length is the
132
// number of address created.
133
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         // Creates teh entities Category.The DESCRIPTION_VALUES length divided
144
// by 2 is the
145
// number of address created.
146
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         // Creates the product order and the products associated.
155
for (int i = 0; i < strDescriptionValues.length; i++) {
156             ProductOrder productOrder = new ProductOrder();
157             productOrder.setId(i);
158             productOrder.setDescription(strDescriptionValues[i]);
159
160             // Creates the same number of products that the order id.
161
// Are created 0 + 1 + 2 + ...+7 = 28 products in total.
162
// The products id are: 10, 20, 21,30,31,32, 40, 41, 42, 43...
163
List JavaDoc<Product> lstProducts = createProducts(i, i * INTERVAL_BETWEEN_ID);
164             productOrder.setProducts(lstProducts);
165             entityManager.persist(productOrder);
166             // inserts in the products the reference for the order.
167
insertOrderReference(lstProducts, productOrder);
168         }
169         entityManager.flush();
170
171         // Creates the customer.
172
for (int i = 0; i < strDescriptionValues.length / 2; i++) {
173             Customer customer = new Customer();
174             customer.setId(i);
175             customer.setName(strDescriptionValues[i]);
176             // inserts the address reference. The address has teh same primary
177
// key that the customer.
178
customer.setAddress(entityManager.getReference(Address.class, new Long JavaDoc(i)));
179             // inserts the orders. Each customer has 2 orders.
180
List JavaDoc<ProductOrder> orders = new ArrayList JavaDoc<ProductOrder>();
181             orders.add(entityManager.getReference(ProductOrder.class, new Long JavaDoc(i)));
182             orders.add(entityManager.getReference(ProductOrder.class, new Long JavaDoc(i + (NUMBER_OF_ORDERS / 2))));
183             customer.setOrders(orders);
184             entityManager.persist(customer);
185         }
186         entityManager.flush();
187     }
188
189     /**
190      * Removes all entities cith the name in the parameter.
191      * @param entityName the entity class that are removed.
192      */

193     public void deleteEntity(final String JavaDoc entityName) {
194         Query queryResult = entityManager.createQuery("SELECT x FROM " + entityName+ " x");
195         List JavaDoc lstEntity = queryResult.getResultList();
196         for (Object JavaDoc obj : lstEntity) {
197             entityManager.remove(obj);
198         }
199         entityManager.flush();
200     }
201
202     /**
203      * Deletes all database.
204      */

205     private void deleteAll() {
206         deleteEntity("Product");
207         deleteEntity("ProductOrder");
208         deleteEntity("Customer");
209         deleteEntity("Category");
210         deleteEntity("Address");
211     }
212
213     /**
214      * Verifies if the container manages a path expression.
215      */

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 JavaDoc(1));
221         List JavaDoc lstProductOrderDesc = query.getResultList();
222         assertEquals(lstProductOrderDesc.size(), 1,
223                 "The query did not returned the correct value in a path expression.");
224     }
225
226     /**
227      * Verifies if the inner join works.
228      */

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 JavaDoc(0));
234         query.setParameter(2, new Long JavaDoc(NUMBER_OF_ORDERS));
235         List JavaDoc lstCategory = query.getResultList();
236         assertEquals(lstCategory.size(), NUMBER_OF_ORDERS / 2, "The inner join does not work properly");
237     }
238
239     /**
240      * Verifies if the container can manage the query with is empty.
241      */

242     public void testIsEmpty() {
243         startup();
244         Query query = entityManager.createQuery("SELECT po FROM ProductOrder po WHERE po.products IS EMPTY");
245         List JavaDoc lstProductOrder = query.getResultList();
246         for (Object JavaDoc obj : lstProductOrder) {
247             ProductOrder order = (ProductOrder) obj;
248             assertEquals(order.getId(), 0, "The query result is incorrect.");
249         }
250     }
251
252     /**
253      * Verifies if the container can make a bulk operation delete.
254      */

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 JavaDoc lstCustomer = queryTest.getResultList();
262         for (Object JavaDoc obj : lstCustomer) {
263             Customer customer = (Customer) obj;
264             assertTrue(customer.getId() <= 2, "The operation delete does not work.");
265         }
266     }
267
268     /**
269      * Verifies if the container can make a bulk operation update.
270      */

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     /**
280      * Verifies if the update was made. This is made in a different method,
281      * because a different transaction is needed.
282      */

283     private void testVerifyUpdate(){
284         Address address = entityManager.find(Address.class, new Long JavaDoc(1));
285         assertEquals(address.getCountry(), "Brazil", "The opertion update does not work");
286     }
287     /**
288      * Verifies if the clause having works properly.
289      */

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