Use decorator pattern for Criteria Query tests

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@835961 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-11-13 19:29:48 +00:00
parent 6e630e827e
commit 6e37c5f34c
7 changed files with 179 additions and 36 deletions

View File

@ -23,6 +23,7 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
@ -103,7 +104,7 @@ public class TestDateStringConversion extends CriteriaTest {
Date earlier = new Date(now - 1000);
String dateString = createJDBCEscapeString(earlier);
final String dateString = createJDBCEscapeString(earlier);
String jpql = "select d from Dependent d where d.endDate >= :dateString ORDER BY d.endDate";
CriteriaQuery<Dependent> c = cb.createQuery(Dependent.class);
Root<Dependent> d = c.from(Dependent.class);
@ -112,7 +113,12 @@ public class TestDateStringConversion extends CriteriaTest {
.as(Date.class)));
c.orderBy(cb.asc(d.get(Dependent_.endDate)));
assertEquivalence(c, jpql, new String[]{"dateString"}, new Object[]{dateString});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("dateString", dateString);
}
}, c, jpql);
}
String createJDBCEscapeString(Object time) {

View File

@ -23,6 +23,7 @@ import java.util.Map;
import java.util.Set;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -1158,7 +1159,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
q.multiselect(e, e.get(Embed_Embed_.intVal1), e.get(Embed_Embed_.embed).get(Embed_.intVal2));
ParameterExpression<Embed_Embed> param1 = cb.parameter(Embed_Embed.class, "embed2");
q.where(cb.isMember(param1, a.get(EntityA_Coll_Embed_Embed_.embeds)));
assertEquivalence(q, jpql, new String[]{"embed2"}, new Object[]{new Embed_Embed()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("embed2", new Embed_Embed());
}
}, q, jpql);
}
@AllowFailure(message="JPQL parse error")
@ -1172,7 +1177,12 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
q.multiselect(e, e.get(Embed_Embed_.intVal1), e.get(Embed_Embed_.embed).get(Embed_.intVal2));
ParameterExpression<Embed_Embed> param1 = cb.parameter(Embed_Embed.class, "embed2");
q.where(cb.isMember(param1, a.get(EntityA_Coll_Embed_Embed_.embeds)));
assertEquivalence(q, jpql, new String[]{"embed2"}, new Object[]{new Embed_Embed()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("embed2", new Embed_Embed());
}
}, q, jpql);
}
@AllowFailure(message="JPQL parse error")
@ -1186,7 +1196,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
q.multiselect(e, e.get(Embed_Embed_.intVal1), e.get(Embed_Embed_.embed).get(Embed_.intVal2));
Parameter<Embed_Embed> param1 = cb.parameter(Embed_Embed.class);
//q.where(cb.equal(param1, e));
assertEquivalence(q, jpql, new String[]{"embed2"}, new Object[]{new Embed_Embed()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("embed2", new Embed_Embed());
}
}, q, jpql);
}
public void testEmbeddableQuery87() {
@ -1489,7 +1503,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
ParameterExpression<String> param1 = cb.parameter(String.class, "image");
q.where(cb.equal(param1, cb.any(sq)));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery112() {
@ -1505,7 +1523,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
ParameterExpression<String> param1 = cb.parameter(String.class, "image");
q.where(cb.equal(param1, cb.any(sq)));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery113() {
@ -1522,7 +1544,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(param1, e.key()));
q.where(cb.exists(sq));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery114() {
@ -1538,7 +1564,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
ParameterExpression<String> param1 = cb.parameter(String.class, "image");
q.where(cb.equal(param1, cb.any(sq)));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery115() {
@ -1555,7 +1585,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(param1, e.key()));
q.where(cb.exists(sq));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery116() {
@ -1571,7 +1605,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
ParameterExpression<String> param1 = cb.parameter(String.class, "image");
q.where(cb.equal(param1, cb.any(sq)));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery117() {
@ -1588,7 +1626,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(param1, e.key()));
q.where(cb.exists(sq));
q.orderBy(cb.asc(i));
assertEquivalence(q, jpql, new String[]{"image"}, new String[]{"my photo"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("image", "my photo");
}
}, q, jpql);
}
public void testEmbeddableQuery118() {
@ -1621,7 +1663,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(d.value().get(VicePresident_.id), 1));
q.where(cb.equal(param1, sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("division", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery122() {
@ -1638,7 +1684,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.select(d.key());
q.where(cb.exists(sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("division", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery123() {
@ -1655,7 +1705,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(d.key(), param1));
q.where(cb.exists(sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("division", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery124() {
@ -1672,7 +1726,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.where(cb.equal(d.value().get(VicePresident_.id), 3));
q.where(cb.equal(param1, sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"division"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("division", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery125() {
@ -1689,7 +1747,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.select(d.key());
q.where(cb.exists(sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"k"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("k", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery126() {
@ -1706,7 +1768,11 @@ public class TestEmbeddableCriteria extends EmbeddableDomainTestCase {
sq.select(d.value());
q.where(cb.exists(sq));
q.orderBy(cb.asc(c));
assertEquivalence(q, jpql, new String[]{"k"}, new Object[]{new Division()});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("k", new Division());
}
}, q, jpql);
}
public void testEmbeddableQuery127() {

View File

@ -34,6 +34,9 @@ import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
import org.apache.openjpa.persistence.criteria.AbstractCriteriaTestCase.QueryDecorator;
import org.apache.openjpa.persistence.embed.Division;
/**
* Tests type-strict version of Criteria API.
*
@ -1499,10 +1502,13 @@ public class TestJPQLSubquery extends CriteriaTest {
Predicate p4 = cb.lessThan(d.get(Dependent_.id).get(DependentId_.effDate), maxDate);
q.where(cb.and(cb.and(cb.and(p1, p2), p3), p4));
assertEquivalence(q, jpql,
new String[]{"empid", "minDate", "maxDate"},
new Object[]{101L, new Date(100), new Date(100000)});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("empid", 101L);
q.setParameter("minDate", new Date(100));
q.setParameter("maxDate", new Date(100000));
}
}, q, jpql);
}
public void testCorrelatedNestedSubquery1() {

View File

@ -22,6 +22,7 @@ import java.math.BigDecimal;
import java.util.List;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -334,7 +335,11 @@ public class TestMetaModelTypesafeCriteria extends CriteriaTest {
q.multiselect(c).where(cb.equal(
c.get(customer_.getSingularAttribute("status", Integer.class)), param));
assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
}
}, q, jpql);
}
public void testKeyExpressionInSelectList() {

View File

@ -21,6 +21,7 @@ package org.apache.openjpa.persistence.criteria;
import java.math.BigDecimal;
import java.util.Collection;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -34,6 +35,7 @@ import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
import org.apache.openjpa.persistence.criteria.AbstractCriteriaTestCase.QueryDecorator;
import org.apache.openjpa.persistence.test.AllowFailure;
public class TestStringCriteria extends CriteriaTest {
@ -215,7 +217,11 @@ public class TestStringCriteria extends CriteriaTest {
param.alias("stat");
q.select(c).where(cb.equal(c.get("status"), param));
assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
}
}, q, jpql);
}
public void testSelectList() {

View File

@ -37,6 +37,7 @@
package org.apache.openjpa.persistence.criteria;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -44,6 +45,7 @@ import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import org.apache.openjpa.persistence.criteria.AbstractCriteriaTestCase.QueryDecorator;
import org.apache.openjpa.persistence.test.DatabasePlatform;
/**
@ -163,7 +165,11 @@ public class TestTypeSafeCondExpression extends CriteriaTest {
cq.where(cb.like(c.get(CompUser_.name), param, '|'));
cq.select(c.get(CompUser_.name));
assertEquivalence(cq, query, new String[]{"name"}, new Object[] {"%|_%"});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("name", "%|_%");
}
}, cq, query);
}
public void testNullExpression() {
@ -501,8 +507,12 @@ public class TestTypeSafeCondExpression extends CriteriaTest {
cq.where(e.type().in(param1, param2));
cq.orderBy(cb.asc(e.get(CompUser_.name)));
assertEquivalence(cq, jpql, new String[]{"a","b"},
new Class[]{MaleUser.class,FemaleUser.class});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("a", MaleUser.class);
q.setParameter("b", FemaleUser.class);
}
}, cq, jpql);
}
public void testTypeExpression2() {
@ -516,7 +526,11 @@ public class TestTypeSafeCondExpression extends CriteriaTest {
q.multiselect(e.type());
q.where(cb.equal(e.type(), param1).not());
assertEquivalence(q, query, new String[]{"t"}, new Class[]{MaleUser.class});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("t", MaleUser.class);
}
}, q, query);
}
public void testTypeExpression3() {

View File

@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -33,6 +34,7 @@ import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.SetJoin;
@ -539,7 +541,11 @@ public class TestTypesafeCriteria extends CriteriaTest {
Parameter<Integer> param = cb.parameter(Integer.class, "stat");
q.select(c).where(cb.equal(c.get(Customer_.status), param));
assertEquivalence(q, jpql, new String[] { "stat" }, new Object[] { 1 });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
}
}, q, jpql);
}
public void testParameters2() {
@ -552,8 +558,12 @@ public class TestTypesafeCriteria extends CriteriaTest {
q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1),
cb.equal(c.get(Customer_.name), param2)));
assertEquivalence(q, jpql, new String[] { "stat", "name" },
new Object[] { 1, "test" });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
q.setParameter("name", "test");
}
}, q, jpql);
}
public void testParameters3() {
@ -564,7 +574,11 @@ public class TestTypesafeCriteria extends CriteriaTest {
Parameter<Integer> param = cb.parameter(Integer.class, "stat");
q.select(c).where(cb.equal(c.get(Customer_.status), param));
assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] { 1 });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
}
}, q, jpql);
}
public void testParameters4() {
@ -576,8 +590,12 @@ public class TestTypesafeCriteria extends CriteriaTest {
Parameter<String> param2 = cb.parameter(String.class, "name");
q.select(c).where(cb.and(cb.equal(c.get(Customer_.status), param1),
cb.equal(c.get(Customer_.name), param2)));
assertEquivalence(q, jpql, new String[]{"stat", "name"},
new Object[] { 1, "test" });
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("stat", 1);
q.setParameter("name", "test");
}
}, q, jpql);
}
public void testParameters5() {
@ -588,10 +606,14 @@ public class TestTypesafeCriteria extends CriteriaTest {
ParameterExpression<List> param1 = cb.parameter(List.class, "coll");
q.where(c.get(Customer_.status).in(param1));
q.select(c);
List vals = new ArrayList();
final List vals = new ArrayList();
vals.add(1);
vals.add(2);
assertEquivalence(q, jpql, new String[] {"coll"}, new Object[] {vals});
assertEquivalence(new QueryDecorator() {
public void decorate(Query q) {
q.setParameter("coll", vals);
}
}, q, jpql);
}
public void testSelectList1() {
@ -1333,5 +1355,23 @@ public class TestTypesafeCriteria extends CriteriaTest {
} catch (IllegalArgumentException e) {
}
}
public void testGroupByOnMaxResult() {
String jpql = "SELECT c.address.country, count(c) from Customer c GROUP BY c.address.country " +
"HAVING COUNT(c.address.country)>3";
CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
Root<Customer> customer = c.from(Customer.class);
Path<String> country = customer.get(Customer_.address).get(Address_.country);
c.multiselect(country, cb.count(customer))
.groupBy(country)
.having(cb.gt(cb.count(country), 3));
assertEquivalence(new QueryDecorator(){
public void decorate(Query q) {
q.setMaxResults(20);
}
}, c, jpql);
}
}