public void complexDistinctCountTest() { oracle.toplink.essentials.ejb.cmp3.EntityManager em = (oracle.toplink.essentials.ejb.cmp3.EntityManager) createEntityManager(); ExpressionBuilder expbldr = new ExpressionBuilder(); ReportQuery rq = new ReportQuery(Employee.class, expbldr); Expression exp = expbldr.get("lastName").equal("Smith"); rq.setReferenceClass(Employee.class); rq.setSelectionCriteria(exp); rq.useDistinct(); rq.returnSingleAttribute(); rq.dontRetrievePrimaryKeys(); rq.addCount("COUNT", expbldr.get("lastName").distinct(), Long.class); Vector expectedResultVector = (Vector) em.getActiveSession().executeQuery(rq); Long expectedResult = (Long) expectedResultVector.get(0); String ejbqlString = "SELECT COUNT(DISTINCT emp.lastName) FROM Employee emp WHERE emp.lastName = \"Smith\""; Long result = (Long) em.createQuery(ejbqlString).getSingleResult(); Assert.assertEquals("Complex DISTINCT COUNT test failed", expectedResult, result); }
/* * test for gf675, using count, group by and having fails. This test is specific for a a use case * where DISTINCT is used with Count and group by */ public void complexCountDistinctWithGroupByTest2() { oracle.toplink.essentials.ejb.cmp3.EntityManager em = (oracle.toplink.essentials.ejb.cmp3.EntityManager) createEntityManager(); // need to set the class in the expressionbuilder, as the Count(Distinct) will cause the // query to change and be built around the Employee class instead of the Address class. ExpressionBuilder expbldr = new ExpressionBuilder(Address.class); ReportQuery rq = new ReportQuery(Address.class, expbldr); Expression exp = expbldr.anyOf("employees"); Expression exp2 = expbldr.get("city"); rq.addAttribute("city", exp2); rq.addCount("COUNT1", exp, Long.class); rq.addCount("COUNT2", exp.get("lastName").distinct(), Long.class); rq.addGrouping(exp2); Vector expectedResult = (Vector) em.getActiveSession().executeQuery(rq); String ejbqlString3 = "SELECT a.city, COUNT( e ), COUNT( DISTINCT e.lastName ) FROM Address a JOIN a.employees e GROUP BY a.city"; Query q = em.createQuery(ejbqlString3); List result = (List) q.getResultList(); Assert.assertTrue( "Complex COUNT(Distinct) with Group By test failed", comparer.compareObjects(result, expectedResult)); }
// shchool buses without drivers public static void test_schoolBusNullDriver() { ExpressionBuilder builder = new ExpressionBuilder(); Expression exp = builder .get("description") .equalsIgnoreCase("School bus") .and(builder.get("busDriver").isNull()); deleteAllQueryInternal_Deferred_Children(Bus.class, exp); }
// FueledVehicles owned by Companies that also own NonFueledVehicles public static void test_ownerOwnsNonFueledVehicle() { ExpressionBuilder builder = new ExpressionBuilder(); ExpressionBuilder subBuilder = new ExpressionBuilder(); ReportQuery rq = new ReportQuery(NonFueledVehicle.class, subBuilder); rq.addAttribute("id"); Expression subExpression = subBuilder.get("owner").equal(builder.get("owner")); rq.setSelectionCriteria(subExpression); Expression exp = builder.exists(rq); deleteAllQueryInternal_Deferred_Children(FueledVehicle.class, exp); }
/** Test for partial fix of GF 932. */ public void complexHavingWithAggregate() { oracle.toplink.essentials.ejb.cmp3.EntityManager em = (oracle.toplink.essentials.ejb.cmp3.EntityManager) createEntityManager(); // Test using the project id in COUNT, GROUP BY and HAVING ExpressionBuilder employeeBuilder = new ExpressionBuilder(Employee.class); ReportQuery rq = new ReportQuery(Employee.class, employeeBuilder); Expression projects = employeeBuilder.anyOf("projects"); Expression pid = projects.get("id"); Expression count = pid.count(); rq.addAttribute("id", pid); rq.addAttribute("COUNT", count, Long.class); rq.addGrouping(pid); rq.setHavingExpression(count.greaterThan(1)); rq.setShouldReturnWithoutReportQueryResult(true); Vector expectedResult = (Vector) em.getActiveSession().executeQuery(rq); String jpql = "SELECT p.id, COUNT(p.id) FROM Employee e JOIN e.projects p " + "GROUP BY p.id HAVING COUNT(p.id)>1"; List result = em.createQuery(jpql).getResultList(); Assert.assertTrue( "Complex HAVING with aggregate function failed", comparer.compareObjects(result, expectedResult)); // Test using the project itself in COUNT, GROUP BY and HAVING employeeBuilder = new ExpressionBuilder(Employee.class); rq = new ReportQuery(Employee.class, employeeBuilder); projects = employeeBuilder.anyOf("projects"); count = projects.count(); rq.addAttribute("projects", projects); rq.addAttribute("COUNT", count, Long.class); rq.addGrouping(projects); rq.setHavingExpression(count.greaterThan(1)); rq.setShouldReturnWithoutReportQueryResult(true); expectedResult = (Vector) em.getActiveSession().executeQuery(rq); jpql = "SELECT p, COUNT(p) FROM Employee e JOIN e.projects p " + "GROUP BY p HAVING COUNT(p)>1"; result = em.createQuery(jpql).getResultList(); Assert.assertTrue( "Complex HAVING with aggregate function failed", comparer.compareObjects(result, expectedResult)); }
public void complexSumTest() { oracle.toplink.essentials.ejb.cmp3.EntityManager em = (oracle.toplink.essentials.ejb.cmp3.EntityManager) createEntityManager(); ExpressionBuilder expbldr = new ExpressionBuilder(); ReportQuery rq = new ReportQuery(Employee.class, expbldr); rq.setReferenceClass(Employee.class); rq.returnSingleAttribute(); rq.dontRetrievePrimaryKeys(); rq.addAttribute("salary", expbldr.get("salary").distinct().sum(), Long.class); Vector expectedResultVector = (Vector) em.getActiveSession().executeQuery(rq); Long expectedResult = (Long) expectedResultVector.get(0); String ejbqlString = "SELECT SUM(DISTINCT emp.salary) FROM Employee emp"; Long result = (Long) em.createQuery(ejbqlString).getSingleResult(); Assert.assertEquals("Complex SUMtest failed", expectedResult, result); }
// FueledVehicles running on Petrol public static void test_fuelTypePetrol() { ExpressionBuilder builder = new ExpressionBuilder(); Expression exp = builder.get("fuelType").equalsIgnoreCase("Petrol"); deleteAllQueryInternal_Deferred_Children(FueledVehicle.class, exp); }
// Vehicles owned by TOP Company public static void test_ownerTOP() { ExpressionBuilder builder = new ExpressionBuilder(); Expression exp = builder.get("owner").get("name").equal("TOP"); deleteAllQueryInternal_Deferred_Children(Vehicle.class, exp); }