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: }
|