KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > entitymanager > SFSBEntityManagerQueriesTester


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: SFSBEntityManagerQueriesTester.java 785 2006-06-27 13:05:00Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.entitymanager;
26
27 import static org.testng.Assert.assertEquals;
28
29 import java.util.List JavaDoc;
30
31 import javax.ejb.Remote JavaDoc;
32 import javax.ejb.Stateful JavaDoc;
33 import javax.persistence.EntityManager;
34 import javax.persistence.PersistenceContext;
35 import javax.persistence.Query;
36
37 import org.objectweb.easybeans.tests.common.ejbs.entity.simpleentity.SimpleEntity;
38
39 /**
40  * Verifies if the EntityManager methods that treats the queries work correctly.
41  * @author Gisele Pinheiro Souza
42  * @author Eduardo Studzinski Estima de Castro
43  */

44 @Stateful JavaDoc
45 @Remote JavaDoc(ItfEntityManagerQueriesTester.class)
46 public class SFSBEntityManagerQueriesTester implements ItfEntityManagerQueriesTester {
47
48     /**
49      * The persistence context used during the tests.
50      */

51     @PersistenceContext
52     private EntityManager entityManager;
53
54     /**
55      * The sql native query used during the tests.
56      */

57     private static final String JavaDoc NATIVE_QUERY = "SELECT e.id FROM SIMPLE e ORDER BY e.id";
58
59     /**
60      * Other sql native query. This query has a parameter.
61      */

62     private static final String JavaDoc NATIVE_QUERY_2 = "SELECT e.name AS entity_name, e.id AS entity_id "
63             + "FROM SIMPLE e WHERE e.id >= ? ORDER BY e.id";
64
65     /**
66      * The EJB QL query used during the test.
67      */

68     private static final String JavaDoc EJBQL_QUERY = "SELECT e FROM SimpleEntity e WHERE e.name LIKE :entityname ORDER BY e.id";
69
70     /**
71      * Verifies if the query returned all beans created.
72      * @param queryResult the result to be compared.
73      */

74     private void checkQueryResult(final List JavaDoc queryResult) {
75         // verifies if the list returned has the same number of entities that
76
// was created
77
assertEquals(MAX_ENTITIES, queryResult.size(), "The method did not returned all entities.");
78
79         for (int i = 0; i < queryResult.size(); i++) {
80             SimpleEntity simpleEntity = (SimpleEntity) queryResult.get(i);
81             assertEquals(simpleEntity.getId(), i, "The bean id was not correctly returned");
82             assertEquals(simpleEntity.getName(), ENTITY_NAME_ROOT + Integer.toString(i),
83                     "The bean name was not correctly returned");
84         }
85     }
86
87     /**
88      * Removes all entities that have the same id used in the beans under test.
89      */

90     private void removeBean() {
91         for (int i = 0; i < MAX_ENTITIES; i++) {
92             SimpleEntity simpleEntity = entityManager.find(SimpleEntity.class, new Integer JavaDoc(i));
93             if (simpleEntity != null) {
94                 entityManager.remove(simpleEntity);
95             }
96             entityManager.flush();
97         }
98     }
99
100     /**
101      * Create the entity beans used during the test.
102      */

103     private void createBeanTest() {
104
105         for (int i = 0; i < MAX_ENTITIES; i++) {
106             SimpleEntity simpleEntity = new SimpleEntity();
107             simpleEntity.setId(i);
108             simpleEntity.setName(ENTITY_NAME_ROOT + Integer.toString(i));
109             entityManager.persist(simpleEntity);
110             entityManager.flush();
111        }
112
113     }
114
115     /**
116      * Creates the beans used in the test and before clean the database.
117      */

118     public void startup() {
119         removeBean();
120         createBeanTest();
121     }
122
123     /**
124      * Calls an EJB QL named query defined by annotation.
125      */

126     public void callNamedQuery() {
127         Query query = entityManager.createNamedQuery("findByName");
128         query.setParameter("entityName", ENTITY_NAME_ROOT + Integer.toString((MAX_ENTITIES - 1)));
129         List JavaDoc simpleEntityResult = query.getResultList();
130         assertEquals(simpleEntityResult.size(), 1, "The query did not return any value.");
131         for (int i = 0; i < simpleEntityResult.size(); i++) {
132             SimpleEntity simpleEntity = (SimpleEntity) simpleEntityResult.get(i);
133             assertEquals(simpleEntity.getId(), MAX_ENTITIES - 1, "The bean was not correctly returned");
134          }
135     }
136
137     /**
138      * Calls a native query defined by annotation.
139      */

140     public void callNamedNativeQuery() {
141         Query query = entityManager.createNamedQuery("findByAll");
142         List JavaDoc simpleEntityResult = query.getResultList();
143         // verifies if all entities were correctly returned.
144
checkQueryResult(simpleEntityResult);
145     }
146
147     /**
148      * Uses the createQuery method of the EntityManager.
149      */

150     public void callCreateQuery() {
151         Query query = entityManager.createQuery(EJBQL_QUERY);
152         query.setParameter("entityname", ENTITY_NAME_ROOT + "%");
153         List JavaDoc simpleEntityResult = query.getResultList();
154
155         // verifies if all entities were correctly returned.
156
checkQueryResult(simpleEntityResult);
157     }
158
159     /**
160      * Calls the method createNative Query that has only the sqlQuery as
161      * parameter.
162      */

163     public void callCreateNativeQuery00() {
164         Query query = entityManager.createNativeQuery(NATIVE_QUERY);
165         List JavaDoc simpleEntityResult = query.getResultList();
166
167         // verifies if the list returned has the same number of entities that
168
// was created
169
assertEquals(MAX_ENTITIES, simpleEntityResult.size(), "The method did not returned all entities.");
170
171         for (int i = 0; i < simpleEntityResult.size(); i++) {
172             Integer JavaDoc intResult = (Integer JavaDoc) simpleEntityResult.get(i);
173             assertEquals(intResult.intValue(), i, "The bean id was not correctly returned");
174         }
175     }
176
177     /**
178      * Calls the method createNativeQuery that has the sqlQuery and the bean
179      * class as parameters.
180      */

181     public void callCreateNativeQuery01() {
182         Query query = entityManager.createNativeQuery(NATIVE_QUERY, SimpleEntity.class);
183         List JavaDoc simpleEntityResult = query.getResultList();
184
185         // verifies if all entities were correctly returned.
186
checkQueryResult(simpleEntityResult);
187     }
188
189     /**
190      * Calls the method that has the sqlQuery and the resultSetMapping as
191      * parameters.
192      */

193     public void callCreateNativeQuery02() {
194         Query query = entityManager.createNativeQuery(NATIVE_QUERY_2, "MappedSimpleEntity");
195         query.setParameter(1, new Integer JavaDoc(0));
196
197         List JavaDoc simpleEntityResult = query.getResultList();
198
199         // verifies if all entities were correctly returned.
200
checkQueryResult(simpleEntityResult);
201     }
202
203 }
204
Popular Tags