Source Code Cross Referenced for SFSBEntityManagerQueriesTester.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » tests » common » ejbs » stateful » containermanaged » entitymanager » 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.stateful.containermanaged.entitymanager 
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: SFSBEntityManagerQueriesTester.java 1970 2007-10-16 11:49:25Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.entitymanager;
025:
026:        import static org.testng.Assert.assertEquals;
027:
028:        import java.util.List;
029:
030:        import javax.ejb.Remote;
031:        import javax.ejb.Stateful;
032:        import javax.persistence.EntityManager;
033:        import javax.persistence.PersistenceContext;
034:        import javax.persistence.Query;
035:
036:        import org.ow2.easybeans.tests.common.ejbs.entity.simpleentity.SimpleEntity;
037:
038:        /**
039:         * Verifies if the EntityManager methods that treats the queries work correctly.
040:         * @author Gisele Pinheiro Souza
041:         * @author Eduardo Studzinski Estima de Castro
042:         */
043:        @Stateful
044:        @Remote(ItfEntityManagerQueriesTester.class)
045:        public class SFSBEntityManagerQueriesTester implements 
046:                ItfEntityManagerQueriesTester {
047:
048:            /**
049:             * The persistence context used during the tests.
050:             */
051:            @PersistenceContext
052:            private EntityManager entityManager;
053:
054:            /**
055:             * The sql native query used during the tests.
056:             */
057:            private static final String NATIVE_QUERY = "SELECT e.id FROM SIMPLE e ORDER BY e.id";
058:
059:            /**
060:             * Other sql native query. This query has a parameter.
061:             */
062:            private static final String NATIVE_QUERY_2 = "SELECT e.name AS entity_name, e.id AS entity_id "
063:                    + "FROM SIMPLE e WHERE e.id >= ? ORDER BY e.id";
064:
065:            /**
066:             * The EJB QL query used during the test.
067:             */
068:            private static final String EJBQL_QUERY = "SELECT e FROM SimpleEntity e WHERE e.name LIKE :entityname ORDER BY e.id";
069:
070:            /**
071:             * Verifies if the query returned all beans created.
072:             * @param queryResult the result to be compared.
073:             */
074:            private void checkQueryResult(final List queryResult) {
075:                // verifies if the list returned has the same number of entities that
076:                // was created
077:                assertEquals(MAX_ENTITIES, queryResult.size(),
078:                        "The method did not returned all entities.");
079:
080:                for (int i = 0; i < queryResult.size(); i++) {
081:                    SimpleEntity simpleEntity = (SimpleEntity) queryResult
082:                            .get(i);
083:                    assertEquals(simpleEntity.getId(), i,
084:                            "The bean id was not correctly returned");
085:                    assertEquals(simpleEntity.getName(), ENTITY_NAME_ROOT
086:                            + Integer.toString(i),
087:                            "The bean name was not correctly returned");
088:                }
089:            }
090:
091:            /**
092:             * Removes all entities that have the same id used in the beans under test.
093:             */
094:            private void removeBean() {
095:                for (int i = 0; i < MAX_ENTITIES; i++) {
096:                    SimpleEntity simpleEntity = entityManager.find(
097:                            SimpleEntity.class, new Integer(i));
098:                    if (simpleEntity != null) {
099:                        entityManager.remove(simpleEntity);
100:                    }
101:                    entityManager.flush();
102:                }
103:            }
104:
105:            /**
106:             * Create the entity beans used during the test.
107:             */
108:            private void createBeanTest() {
109:
110:                for (int i = 0; i < MAX_ENTITIES; i++) {
111:                    SimpleEntity simpleEntity = new SimpleEntity();
112:                    simpleEntity.setId(i);
113:                    simpleEntity
114:                            .setName(ENTITY_NAME_ROOT + Integer.toString(i));
115:                    entityManager.persist(simpleEntity);
116:                    entityManager.flush();
117:                }
118:
119:            }
120:
121:            /**
122:             * Creates the beans used in the test and before clean the database.
123:             */
124:            public void startup() {
125:                removeBean();
126:                createBeanTest();
127:            }
128:
129:            /**
130:             * Calls an EJB QL named query defined by annotation.
131:             */
132:            public void callNamedQuery() {
133:                Query query = entityManager.createNamedQuery("findByName");
134:                query.setParameter("entityName", ENTITY_NAME_ROOT
135:                        + Integer.toString((MAX_ENTITIES - 1)));
136:                List simpleEntityResult = query.getResultList();
137:                assertEquals(simpleEntityResult.size(), 1,
138:                        "The query did not return any value.");
139:                for (int i = 0; i < simpleEntityResult.size(); i++) {
140:                    SimpleEntity simpleEntity = (SimpleEntity) simpleEntityResult
141:                            .get(i);
142:                    assertEquals(simpleEntity.getId(), MAX_ENTITIES - 1,
143:                            "The bean was not correctly returned");
144:                }
145:            }
146:
147:            /**
148:             * Calls a native query defined by annotation.
149:             */
150:            public void callNamedNativeQuery() {
151:                Query query = entityManager.createNamedQuery("findByAll");
152:                List simpleEntityResult = query.getResultList();
153:                // verifies if all entities were correctly returned.
154:                checkQueryResult(simpleEntityResult);
155:            }
156:
157:            /**
158:             * Uses the createQuery method of the EntityManager.
159:             */
160:            public void callCreateQuery() {
161:                Query query = entityManager.createQuery(EJBQL_QUERY);
162:                query.setParameter("entityname", ENTITY_NAME_ROOT + "%");
163:                List simpleEntityResult = query.getResultList();
164:
165:                // verifies if all entities were correctly returned.
166:                checkQueryResult(simpleEntityResult);
167:            }
168:
169:            /**
170:             * Calls the method createNative Query that has only the sqlQuery as
171:             * parameter.
172:             */
173:            public void callCreateNativeQuery00() {
174:                Query query = entityManager.createNativeQuery(NATIVE_QUERY);
175:                List simpleEntityResult = query.getResultList();
176:
177:                // verifies if the list returned has the same number of entities that
178:                // was created
179:                assertEquals(MAX_ENTITIES, simpleEntityResult.size(),
180:                        "The method did not returned all entities.");
181:
182:                for (int i = 0; i < simpleEntityResult.size(); i++) {
183:                    Integer intResult = (Integer) simpleEntityResult.get(i);
184:                    assertEquals(intResult.intValue(), i,
185:                            "The bean id was not correctly returned");
186:                }
187:            }
188:
189:            /**
190:             * Calls the method createNativeQuery that has the sqlQuery and the bean
191:             * class as parameters.
192:             */
193:            public void callCreateNativeQuery01() {
194:                Query query = entityManager.createNativeQuery(NATIVE_QUERY,
195:                        SimpleEntity.class);
196:                List simpleEntityResult = query.getResultList();
197:
198:                // verifies if all entities were correctly returned.
199:                checkQueryResult(simpleEntityResult);
200:            }
201:
202:            /**
203:             * Calls the method that has the sqlQuery and the resultSetMapping as
204:             * parameters.
205:             */
206:            public void callCreateNativeQuery02() {
207:                Query query = entityManager.createNativeQuery(NATIVE_QUERY_2,
208:                        "MappedSimpleEntity");
209:                query.setParameter(1, new Integer(0));
210:
211:                List simpleEntityResult = query.getResultList();
212:
213:                // verifies if all entities were correctly returned.
214:                checkQueryResult(simpleEntityResult);
215:            }
216:
217:        }
w___ww___.__jav_a__2s__.__c___o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.