diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestIndex.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestIndex.java index 91576db3d..143405192 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestIndex.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/expressions/TestIndex.java @@ -231,7 +231,6 @@ public class TestIndex extends SingleEMFTestCase { } // add the entities - log.trace("Adding " + newElements.size() + " of " + elementClassName + " to " + entityClassName); em = emf.createEntityManager(); em.getTransaction().begin(); for (INameEntity newElement : newElements) @@ -244,7 +243,6 @@ public class TestIndex extends SingleEMFTestCase { em.clear(); // verify the entity was stored - log.trace("Verifing the entity was stored"); IOrderedEntity findEntity = em.find(entityClass, entityId); assertNotNull("Found entity just created", findEntity); assertEquals("Verify entity id = " + entityId, entityId.intValue(), findEntity.getId()); @@ -275,15 +273,19 @@ public class TestIndex extends SingleEMFTestCase { String entityClassName = entityType.getEntityName(); String elementClassName = elementClass.getName().substring( elementClass.getName().lastIndexOf('.') + 1); - Integer entityId = 1; - // create the entity - IOrderedEntity newEntity = (IOrderedEntity)constructNewEntityObject(entityType); - newEntity.setId(entityId); - // persist the entity + // create the EM and transaction em = emf.createEntityManager(); em.getTransaction().begin(); - em.persist(newEntity); + + // create and persist the entities + List newEntities = new ArrayList(); + for (int i=0; i<3; i++) { + IOrderedEntity newEntity = (IOrderedEntity)constructNewEntityObject(entityType); + newEntity.setId(i); + em.persist(newEntity); + newEntities.add(newEntity); + } // create and persist the elements Constructor elementConstrctor = elementClass.getConstructor(String.class); @@ -291,27 +293,28 @@ public class TestIndex extends SingleEMFTestCase { IColumnEntity newElement; for (int i=0; i)elementClass); + verifyO2MEntities(entityType, (Class)elementClass); } else if (IColumnEntity.class.isAssignableFrom(elementClass)) { log.trace("** Verify IColumnEntity modifications on IOrderedEntity."); - verifyOrderedEntities(entityType, (Class)elementClass); + verifyM2MEntities(entityType, (Class)elementClass); } else { fail("verifyEntities(IOrderedEntity) - Unexpected elementClass=" + elementClass.getSimpleName()); } @@ -396,8 +399,10 @@ public class TestIndex extends SingleEMFTestCase { } } - private void verifyOrderedEntities(JPQLIndexEntityClasses entityType, Class elementClass) + private void verifyO2MEntities(JPQLIndexEntityClasses entityType, Class elementClass) { + EntityManager em = null; + try { Class entityClass = (Class)Class.forName(entityType.getEntityClassName()); String entityClassName = entityType.getEntityName(); @@ -409,7 +414,7 @@ public class TestIndex extends SingleEMFTestCase { + Arrays.toString(Element_Names)); } - EntityManager em = emf.createEntityManager(); + em = emf.createEntityManager(); em.clear(); int idx = 0; for (String expectedEntityName : Element_Names) { @@ -421,9 +426,8 @@ public class TestIndex extends SingleEMFTestCase { Object oo = res.get(0); assertEquals(" Verify element type is " + elementClass.getName(), elementClass.getName(), oo.getClass().getName()); - String name; try { - name = (String) elementClass.getMethod("getName").invoke(oo); + String name = (String) elementClass.getMethod("getName").invoke(oo); assertEquals(" Verify element value is '" + expectedEntityName + "'", expectedEntityName, name); } catch (Exception e) { @@ -436,11 +440,90 @@ public class TestIndex extends SingleEMFTestCase { } catch (Exception e) { log.error(e); throw new RuntimeException(e); + } finally { + if (em != null) { + em.close(); + em = null; + } + } + } + + private void verifyM2MEntities(JPQLIndexEntityClasses entityType, Class elementClass) + { + EntityManager em = null; + + try { + Class entityClass = (Class)Class.forName(entityType.getEntityClassName()); + String entityClassName = entityType.getEntityName(); + entityClassName = entityClassName.substring(entityClassName.lastIndexOf('.') + 1); + String elementClassName = entityType.getEntityName(); + elementClassName = elementClassName.substring(elementClassName.lastIndexOf('.') + 1); + + if (log.isTraceEnabled()) { + log.trace("Query " + entityClassName + " and verify 'entities' collection has " + + Element_Names.length + " elements in this order: " + + Arrays.toString(Element_Names)); + } + + em = emf.createEntityManager(); + em.clear(); + int idx = 0, idx2 = 0; + for (String expectedEntityName : Element_Names) { + Query q = em.createQuery("select w from " + entityClassName + + " o join o.entities w where index(w) = " + idx); + List res = (List)q.getResultList(); + assertEquals(" Verify query returns 1 element for index " + idx, 1, res.size()); + if (res.size() == 1) { + Object oo = res.get(0); + assertEquals(" Verify element type is " + elementClass.getName(), elementClass.getName(), + oo.getClass().getName()); + try { + String name = (String) elementClass.getMethod("getName").invoke(oo); + assertEquals(" Verify element value is '" + + expectedEntityName + "'", expectedEntityName, name); + + if (log.isTraceEnabled()) { + log.trace("Query " + elementClassName + " and verify 'entities' collection content"); + } + Query q2 = em.createQuery("select w from " + elementClassName + + " o join o.entities w where index(w) = " + idx2); + List res2 = (List)q.getResultList(); + assertEquals(" Verify query returns 1 entity for index " + idx2, 1, res2.size()); + if (res2.size() == 1) { + Object oo2 = res2.get(0); + assertEquals(" Verify entity type is " + entityClass.getName(), entityClass.getName(), + oo2.getClass().getName()); + try { + String name2 = (String) entityClass.getMethod("getName").invoke(oo2); + assertEquals(" Verify entity value is '" + + expectedEntityName + "'", expectedEntityName, name); + } catch (Exception e) { + log.error(" Caught unexpected exception:" + e.getMessage()); + throw new RuntimeException(e); + } + } + } catch (Exception e) { + log.error(" Caught unexpected exception:" + e.getMessage()); + throw new RuntimeException(e); + } + } + ++idx; + } + } catch (Exception e) { + log.error(e); + throw new RuntimeException(e); + } finally { + if (em != null) { + em.close(); + em = null; + } } } private void verifyOrderedElements(JPQLIndexEntityClasses entityType) { + EntityManager em = null; + try { Class entityClass = (Class)Class.forName(entityType.getEntityClassName()); String entityClassName = entityType.getEntityName(); @@ -452,7 +535,7 @@ public class TestIndex extends SingleEMFTestCase { + Arrays.toString(Element_Names)); } - EntityManager em = emf.createEntityManager(); + em = emf.createEntityManager(); em.clear(); int idx = 0; for (String expectedEntityName : Element_Names) { @@ -479,6 +562,11 @@ public class TestIndex extends SingleEMFTestCase { } catch (Exception e) { log.error(e); throw new RuntimeException(e); + } finally { + if (em != null) { + em.close(); + em = null; + } } }