Source Code Cross Referenced for SLSBEjbqlTester.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » tests » common » ejbs » stateless » containermanaged » ejbql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » ow2 easybeans » org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ejbql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: SLSBEjbqlTester.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.ejbql;
025:
026:        import static org.testng.Assert.assertEquals;
027:        import static org.testng.Assert.assertTrue;
028:
029:        import java.util.ArrayList;
030:        import java.util.List;
031:
032:        import javax.ejb.Remote;
033:        import javax.ejb.Stateless;
034:        import javax.ejb.TransactionAttribute;
035:        import javax.ejb.TransactionAttributeType;
036:        import javax.persistence.EntityManager;
037:        import javax.persistence.PersistenceContext;
038:        import javax.persistence.Query;
039:
040:        import org.ow2.easybeans.tests.common.ejbs.entity.customer.Address;
041:        import org.ow2.easybeans.tests.common.ejbs.entity.customer.Category;
042:        import org.ow2.easybeans.tests.common.ejbs.entity.customer.Customer;
043:        import org.ow2.easybeans.tests.common.ejbs.entity.customer.Product;
044:        import org.ow2.easybeans.tests.common.ejbs.entity.customer.ProductOrder;
045:
046:        /**
047:         * Verifies some queries in EJB QL.
048:         * @author Gisele Pinheiro Souza
049:         * @author Eduardo Studzinski Estima de Castro
050:         *
051:         */
052:        @Stateless
053:        @Remote(ItfEjbqlTester.class)
054:        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
055:        public class SLSBEjbqlTester implements  ItfEjbqlTester {
056:
057:            /**
058:             * The identifier interval between the group of products that are in the
059:             * different ProductOrder.
060:             */
061:            private static final int INTERVAL_BETWEEN_ID = 10;
062:
063:            /**
064:             * The number of product order.
065:             */
066:            private static final int NUMBER_OF_ORDERS = 8;
067:
068:            /**
069:             * The list of descriptions.
070:             */
071:            private static final String[] DESCRIPTION_VALUES = { "a", "b", "c",
072:                    "d", "e", "f", "g", "h" };
073:
074:            /**
075:             * The persistence context used during the tests.
076:             */
077:            @PersistenceContext
078:            private EntityManager entityManager;
079:
080:            /**
081:             * Creates a number of products that is defined in the parameter quantity.
082:             * The identifier are sequencial and the initial id is the parameter
083:             * initialIdNumber.The description is a letter. The price is a value between
084:             * 0 and the the quantity of products.
085:             * @param quantity the quantity of products creadtesd
086:             * @param initialIdNumber the initial id number.
087:             * @return the list of the products created.
088:             */
089:            private List<Product> createProducts(final int quantity,
090:                    final int initialIdNumber) {
091:                String[] strDescriptionValues = DESCRIPTION_VALUES;
092:                List<Product> lstProducts = new ArrayList<Product>();
093:                // creates the product.
094:                for (int i = 0; i < quantity; i++) {
095:                    Product product = new Product();
096:                    product.setId(i + initialIdNumber);
097:                    product.setDescription(strDescriptionValues[i]);
098:                    product.setPrice(i);
099:                    // sets the category for a value between the intial category id = 0
100:                    // and the maximum= 4
101:                    product.setCategory(entityManager.getReference(
102:                            Category.class, new Long(i
103:                                    % (strDescriptionValues.length / 2))));
104:                    entityManager.persist(product);
105:                    entityManager.flush();
106:                    lstProducts.add(product);
107:                }
108:                return lstProducts;
109:            }
110:
111:            /**
112:             * Inserts the reference for the order in the product.
113:             * @param lstProducts the list of products that are in the same order.
114:             * @param productOrder the order that has all products.
115:             */
116:            public void insertOrderReference(final List<Product> lstProducts,
117:                    final ProductOrder productOrder) {
118:                for (Object obj : lstProducts) {
119:                    Product product = (Product) obj;
120:                    product.setOrder(productOrder);
121:                    entityManager.merge(product);
122:                }
123:            }
124:
125:            /**
126:             * Creates the entiies that are uses in the test.
127:             */
128:            private void startup() {
129:                // cleans the database
130:                deleteAll();
131:
132:                String[] strDescriptionValues = DESCRIPTION_VALUES;
133:
134:                // Creates the entities Address. The DESCRIPTION_VALUES length is the
135:                // number of address created.
136:                for (int i = 0; i < strDescriptionValues.length; i++) {
137:                    Address address = new Address();
138:                    address.setId(i);
139:                    address.setCountry("France");
140:                    address.setNumber(i);
141:                    address.setStreet(strDescriptionValues[i]);
142:                    entityManager.persist(address);
143:                }
144:                entityManager.flush();
145:
146:                // Creates teh entities Category.The DESCRIPTION_VALUES length divided
147:                // by 2 is the
148:                // number of address created.
149:                for (int i = 0; i < strDescriptionValues.length / 2; i++) {
150:                    Category category = new Category();
151:                    category.setId(i);
152:                    category.setDescription(strDescriptionValues[i]);
153:                    entityManager.persist(category);
154:                }
155:                entityManager.flush();
156:
157:                // Creates the product order and the products associated.
158:                for (int i = 0; i < strDescriptionValues.length; i++) {
159:                    ProductOrder productOrder = new ProductOrder();
160:                    productOrder.setId(i);
161:                    productOrder.setDescription(strDescriptionValues[i]);
162:
163:                    // Creates the same number of products that the order id.
164:                    // Are created 0 + 1 + 2 + ...+7 = 28 products in total.
165:                    // The products id are: 10, 20, 21,30,31,32, 40, 41, 42, 43...
166:                    List<Product> lstProducts = createProducts(i, i
167:                            * INTERVAL_BETWEEN_ID);
168:                    productOrder.setProducts(lstProducts);
169:                    entityManager.persist(productOrder);
170:                    // inserts in the products the reference for the order.
171:                    insertOrderReference(lstProducts, productOrder);
172:                }
173:                entityManager.flush();
174:
175:                // Creates the customer.
176:                for (int i = 0; i < strDescriptionValues.length / 2; i++) {
177:                    Customer customer = new Customer();
178:                    customer.setId(i);
179:                    customer.setName(strDescriptionValues[i]);
180:                    // inserts the address reference. The address has teh same primary
181:                    // key that the customer.
182:                    customer.setAddress(entityManager.getReference(
183:                            Address.class, new Long(i)));
184:                    // inserts the orders. Each customer has 2 orders.
185:                    List<ProductOrder> orders = new ArrayList<ProductOrder>();
186:                    orders.add(entityManager.getReference(ProductOrder.class,
187:                            new Long(i)));
188:                    orders.add(entityManager.getReference(ProductOrder.class,
189:                            new Long(i + (NUMBER_OF_ORDERS / 2))));
190:                    customer.setOrders(orders);
191:                    entityManager.persist(customer);
192:                }
193:                entityManager.flush();
194:            }
195:
196:            /**
197:             * Removes all entities cith the name in the parameter.
198:             * @param entityName the entity class that are removed.
199:             */
200:            public void deleteEntity(final String entityName) {
201:                Query queryResult = entityManager.createQuery("SELECT x FROM "
202:                        + entityName + " x");
203:                List lstEntity = queryResult.getResultList();
204:                for (Object obj : lstEntity) {
205:                    entityManager.remove(obj);
206:                }
207:                entityManager.flush();
208:            }
209:
210:            /**
211:             * Deletes all database.
212:             */
213:            private void deleteAll() {
214:                deleteEntity("Product");
215:                deleteEntity("ProductOrder");
216:                deleteEntity("Customer");
217:                deleteEntity("Category");
218:                deleteEntity("Address");
219:            }
220:
221:            /**
222:             * Verifies if the container manages a path expression.
223:             */
224:            public void testPathExpression() {
225:                startup();
226:                Query query = entityManager
227:                        .createQuery("SELECT o.description FROM Customer c, IN(c.orders) o WHERE o.id = :productOrderId");
228:                query.setParameter("productOrderId", new Long(1));
229:                List lstProductOrderDesc = query.getResultList();
230:                assertEquals(lstProductOrderDesc.size(), 1,
231:                        "The query did not returned the correct value in a path expression.");
232:            }
233:
234:            /**
235:             * Verifies if the inner join works.
236:             */
237:            public void testInnerJoin() {
238:                startup();
239:                Query query = entityManager
240:                        .createQuery("SELECT p.category FROM ProductOrder po JOIN po.products p WHERE po.id BETWEEN ?1 AND  ?2");
241:                query.setParameter(1, new Long(0));
242:                query.setParameter(2, new Long(NUMBER_OF_ORDERS));
243:                List lstCategory = query.getResultList();
244:                assertEquals(lstCategory.size(), NUMBER_OF_ORDERS / 2,
245:                        "The inner join does not work properly");
246:            }
247:
248:            /**
249:             * Verifies if the container can manage the query with is empty.
250:             */
251:            public void testIsEmpty() {
252:                startup();
253:                Query query = entityManager
254:                        .createQuery("SELECT po FROM ProductOrder po  WHERE po.products IS EMPTY");
255:                List lstProductOrder = query.getResultList();
256:                for (Object obj : lstProductOrder) {
257:                    ProductOrder order = (ProductOrder) obj;
258:                    assertEquals(order.getId(), 0,
259:                            "The query result is incorrect.");
260:                }
261:            }
262:
263:            /**
264:             * Verifies if the container can make a bulk operation delete.
265:             */
266:            public void testDelete() {
267:                startup();
268:                Query query = entityManager
269:                        .createQuery("DELETE FROM Customer c  WHERE  c.id > 2");
270:                query.executeUpdate();
271:                entityManager.flush();
272:                Query queryTest = entityManager
273:                        .createQuery("SELECT c FROM Customer c");
274:                List lstCustomer = queryTest.getResultList();
275:                for (Object obj : lstCustomer) {
276:                    Customer customer = (Customer) obj;
277:                    assertTrue(customer.getId() <= 2,
278:                            "The operation delete does not work.");
279:                }
280:            }
281:
282:            /**
283:             * Verifies if the container can make a bulk operation update.
284:             */
285:            public void testUpdate() {
286:                startup();
287:                Query query = entityManager
288:                        .createQuery("UPDATE Address a SET a.country = 'Brazil' WHERE a.id =1");
289:                query.executeUpdate();
290:                entityManager.flush();
291:                testVerifyUpdate();
292:            }
293:
294:            /**
295:             * Verifies if the update was made. This is made in a different method,
296:             * because a different transaction is needed.
297:             */
298:            private void testVerifyUpdate() {
299:                Address address = entityManager
300:                        .find(Address.class, new Long(1));
301:                assertEquals(address.getCountry(), "Brazil",
302:                        "The opertion update does not work");
303:            }
304:
305:            /**
306:             * Verifies if the clause having works properly.
307:             */
308:            public void testHaving() {
309:                startup();
310:                Query query = entityManager
311:                        .createQuery("SELECT p.price, COUNT(p) FROM Product p GROUP BY p.price HAVING p.price > 2");
312:                query.getResultList();
313:            }
314:        }
w__w___w__.ja___va___2___s_.___c_o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.