1 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 ; 30 31 import javax.ejb.Remote ; 32 import javax.ejb.Stateful ; 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 44 @Stateful 45 @Remote (ItfEntityManagerQueriesTester.class) 46 public class SFSBEntityManagerQueriesTester implements ItfEntityManagerQueriesTester { 47 48 51 @PersistenceContext 52 private EntityManager entityManager; 53 54 57 private static final String NATIVE_QUERY = "SELECT e.id FROM SIMPLE e ORDER BY e.id"; 58 59 62 private static final String 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 68 private static final String EJBQL_QUERY = "SELECT e FROM SimpleEntity e WHERE e.name LIKE :entityname ORDER BY e.id"; 69 70 74 private void checkQueryResult(final List queryResult) { 75 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 90 private void removeBean() { 91 for (int i = 0; i < MAX_ENTITIES; i++) { 92 SimpleEntity simpleEntity = entityManager.find(SimpleEntity.class, new Integer (i)); 93 if (simpleEntity != null) { 94 entityManager.remove(simpleEntity); 95 } 96 entityManager.flush(); 97 } 98 } 99 100 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 118 public void startup() { 119 removeBean(); 120 createBeanTest(); 121 } 122 123 126 public void callNamedQuery() { 127 Query query = entityManager.createNamedQuery("findByName"); 128 query.setParameter("entityName", ENTITY_NAME_ROOT + Integer.toString((MAX_ENTITIES - 1))); 129 List 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 140 public void callNamedNativeQuery() { 141 Query query = entityManager.createNamedQuery("findByAll"); 142 List simpleEntityResult = query.getResultList(); 143 checkQueryResult(simpleEntityResult); 145 } 146 147 150 public void callCreateQuery() { 151 Query query = entityManager.createQuery(EJBQL_QUERY); 152 query.setParameter("entityname", ENTITY_NAME_ROOT + "%"); 153 List simpleEntityResult = query.getResultList(); 154 155 checkQueryResult(simpleEntityResult); 157 } 158 159 163 public void callCreateNativeQuery00() { 164 Query query = entityManager.createNativeQuery(NATIVE_QUERY); 165 List simpleEntityResult = query.getResultList(); 166 167 assertEquals(MAX_ENTITIES, simpleEntityResult.size(), "The method did not returned all entities."); 170 171 for (int i = 0; i < simpleEntityResult.size(); i++) { 172 Integer intResult = (Integer ) simpleEntityResult.get(i); 173 assertEquals(intResult.intValue(), i, "The bean id was not correctly returned"); 174 } 175 } 176 177 181 public void callCreateNativeQuery01() { 182 Query query = entityManager.createNativeQuery(NATIVE_QUERY, SimpleEntity.class); 183 List simpleEntityResult = query.getResultList(); 184 185 checkQueryResult(simpleEntityResult); 187 } 188 189 193 public void callCreateNativeQuery02() { 194 Query query = entityManager.createNativeQuery(NATIVE_QUERY_2, "MappedSimpleEntity"); 195 query.setParameter(1, new Integer (0)); 196 197 List simpleEntityResult = query.getResultList(); 198 199 checkQueryResult(simpleEntityResult); 201 } 202 203 } 204 | Popular Tags |