This commit is contained in:
Timo Westkämper 2009-12-17 15:26:41 +00:00
parent 4696088013
commit 65579fa2fe
10 changed files with 87 additions and 77 deletions

View File

@ -13,7 +13,7 @@ public abstract class AbstractQueryTest implements Constants{
private HQLSerializer visitor = new HQLSerializer(new HQLTemplates());
protected void toString(String expected, Expr<?> expr) {
protected void assertToString(String expected, Expr<?> expr) {
assertEquals(expected, visitor.handle(expr).toString().replace("\n", " "));
// visitor.clear();
visitor = new HQLSerializer(new HQLTemplates());

View File

@ -13,13 +13,13 @@ public class AggregationTest extends AbstractQueryTest{
@Test
public void test(){
toString("max(cat.bodyWeight)", cat.bodyWeight.max());
toString("min(cat.bodyWeight)", cat.bodyWeight.min());
toString("avg(cat.bodyWeight)", cat.bodyWeight.avg());
assertToString("max(cat.bodyWeight)", cat.bodyWeight.max());
assertToString("min(cat.bodyWeight)", cat.bodyWeight.min());
assertToString("avg(cat.bodyWeight)", cat.bodyWeight.avg());
toString("count(*)", Expr.countAll());
toString("count(cat)", cat.count());
toString("count(distinct cat)", cat.countDistinct());
assertToString("count(*)", Expr.countAll());
assertToString("count(cat)", cat.count());
assertToString("count(distinct cat)", cat.countDistinct());
}
}

View File

@ -6,10 +6,10 @@ public class BooleanOperationsTest extends AbstractQueryTest {
@Test
public void testBooleanOperations() {
toString("cust is null or cat is null", cust.isNull().or(cat.isNull()));
toString("cust is null and cat is null", cust.isNull()
assertToString("cust is null or cat is null", cust.isNull().or(cat.isNull()));
assertToString("cust is null and cat is null", cust.isNull()
.and(cat.isNull()));
toString("not (cust is null)", cust.isNull().not());
assertToString("not (cust is null)", cust.isNull().not());
cat.name.eq(cust.name.firstName).and(
cat.bodyWeight.eq(kitten.bodyWeight));
cat.name.eq(cust.name.firstName).or(
@ -19,9 +19,9 @@ public class BooleanOperationsTest extends AbstractQueryTest {
@Test
public void testLogicalOperations() {
// logical operations and, or, not
toString("cat = kitten or kitten = cat", cat.eq(kitten).or(kitten.eq(cat)));
toString("cat = kitten and kitten = cat", cat.eq(kitten).and(kitten.eq(cat)));
toString("cat is null and (kitten is null or kitten.bodyWeight > :a1)",
assertToString("cat = kitten or kitten = cat", cat.eq(kitten).or(kitten.eq(cat)));
assertToString("cat = kitten and kitten = cat", cat.eq(kitten).and(kitten.eq(cat)));
assertToString("cat is null and (kitten is null or kitten.bodyWeight > :a1)",
cat.isNull().and(kitten.isNull().or(kitten.bodyWeight.gt(10))));
}
}

View File

@ -13,11 +13,11 @@ public class CollectionTest extends AbstractQueryTest{
@Test
public void test(){
toString(":a1 in elements(cat.kittensSet)", cat.kittensSet.contains(new Cat()));
toString(":a1 in elements(cat.kittens)", cat.kittens.contains(new Cat()));
assertToString(":a1 in elements(cat.kittensSet)", cat.kittensSet.contains(new Cat()));
assertToString(":a1 in elements(cat.kittens)", cat.kittens.contains(new Cat()));
toString("cat in elements(cat1.kittens)", cat.in(cat1.kittens));
toString("cat in elements(cat1.kittensSet)", cat.in(cat1.kittensSet));
assertToString("cat in elements(cat1.kittens)", cat.in(cat1.kittens));
assertToString("cat in elements(cat1.kittensSet)", cat.in(cat1.kittensSet));
}
}

View File

@ -12,12 +12,12 @@ public class ComparableTest extends AbstractQueryTest{
@Test
public void testBinaryComparisonOperations() {
// binary comparison operators =, >=, <=, <>, !=, like
toString("cat.bodyWeight = kitten.bodyWeight", cat.bodyWeight.eq(kitten.bodyWeight));
toString("cat.bodyWeight >= kitten.bodyWeight", cat.bodyWeight.goe(kitten.bodyWeight));
toString("cat.bodyWeight > kitten.bodyWeight", cat.bodyWeight.gt(kitten.bodyWeight));
toString("cat.bodyWeight <= kitten.bodyWeight", cat.bodyWeight.loe(kitten.bodyWeight));
toString("cat.bodyWeight < kitten.bodyWeight", cat.bodyWeight.lt(kitten.bodyWeight));
toString("cat.bodyWeight != kitten.bodyWeight", cat.bodyWeight.ne(kitten.bodyWeight));
assertToString("cat.bodyWeight = kitten.bodyWeight", cat.bodyWeight.eq(kitten.bodyWeight));
assertToString("cat.bodyWeight >= kitten.bodyWeight", cat.bodyWeight.goe(kitten.bodyWeight));
assertToString("cat.bodyWeight > kitten.bodyWeight", cat.bodyWeight.gt(kitten.bodyWeight));
assertToString("cat.bodyWeight <= kitten.bodyWeight", cat.bodyWeight.loe(kitten.bodyWeight));
assertToString("cat.bodyWeight < kitten.bodyWeight", cat.bodyWeight.lt(kitten.bodyWeight));
assertToString("cat.bodyWeight != kitten.bodyWeight", cat.bodyWeight.ne(kitten.bodyWeight));
// toString("cat.name like :a1", cat.name.like("Kitty"));
}

View File

@ -16,10 +16,10 @@ public class DateTimeTest extends AbstractQueryTest {
@Test
public void testDateOperations() {
// current_date(), current_time(), current_timestamp()
toString("current_date()", EDate.currentDate());
toString("current_date()", EDateTime.currentDate());
toString("current_time()", ETime.currentTime());
toString("current_timestamp()", EDateTime.currentTimestamp());
assertToString("current_date()", EDate.currentDate());
assertToString("current_date()", EDateTime.currentDate());
assertToString("current_time()", ETime.currentTime());
assertToString("current_timestamp()", EDateTime.currentTimestamp());
// second(...), minute(...), hour(...), day(...), month(...), year(...),
catalog.effectiveDate.second();
catalog.effectiveDate.minute();

View File

@ -25,9 +25,9 @@ public class EJBQLTest extends AbstractQueryTest{
// bit_length(),
// mod()
toString("trim(cat.name)", cat.name.trim());
toString("lower(cat.name)", cat.name.lower());
toString("upper(cat.name)", cat.name.upper());
assertToString("trim(cat.name)", cat.name.trim());
assertToString("lower(cat.name)", cat.name.lower());
assertToString("upper(cat.name)", cat.name.upper());
// cat.name.length();
}

View File

@ -53,7 +53,7 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testArgumentHandling() {
// Kitty is reused, so it should be used via one named parameter
toString(
assertToString(
"cat.name = :a1 or cust.name.firstName = :a2 or kitten.name = :a1",
cat.name.eq("Kitty").or(cust.name.firstName.eq("Hans")).or(
kitten.name.eq("Kitty")));
@ -63,10 +63,10 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testBasicOperations() {
toString("cat.bodyWeight = kitten.bodyWeight", cat.bodyWeight.eq(kitten.bodyWeight));
toString("cat.bodyWeight != kitten.bodyWeight", cat.bodyWeight.ne(kitten.bodyWeight));
assertToString("cat.bodyWeight = kitten.bodyWeight", cat.bodyWeight.eq(kitten.bodyWeight));
assertToString("cat.bodyWeight != kitten.bodyWeight", cat.bodyWeight.ne(kitten.bodyWeight));
toString("cat.bodyWeight + kitten.bodyWeight = kitten.bodyWeight",
assertToString("cat.bodyWeight + kitten.bodyWeight = kitten.bodyWeight",
cat.bodyWeight.add(kitten.bodyWeight).eq(kitten.bodyWeight));
}
@ -83,7 +83,7 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testCustomExpressions() {
toString("myCustom(cust,cat)", new MyCustomExpr(cust, cat));
assertToString("myCustom(cust,cat)", new MyCustomExpr(cust, cat));
}
@Test
@ -104,8 +104,8 @@ public class FeaturesTest extends AbstractQueryTest{
// maxelement(cat.kittens);
// minindex(cat.kittens);
// maxindex(cat.kittens);
toString("cat.kittens[0]", cat.kittens(0));
toString("cat.kittens[0]", cat.kittens.get(0));
assertToString("cat.kittens[0]", cat.kittens(0));
assertToString("cat.kittens[0]", cat.kittens.get(0));
// some, all, exists, any, in.
}
@ -117,17 +117,17 @@ public class FeaturesTest extends AbstractQueryTest{
com.mysema.query.hql.domain.Cat.class,
new Class[]{String.class},
cat.name);
toString("new " + com.mysema.query.hql.domain.Cat.class.getName()
assertToString("new " + com.mysema.query.hql.domain.Cat.class.getName()
+ "(cat.name)", c);
toString("new " + getClass().getName() + "$BookmarkDTO(cat.name)",
assertToString("new " + getClass().getName() + "$BookmarkDTO(cat.name)",
new _BookmarkDTO(cat.name));
}
@Test
public void testEqualsAndNotEqualsForAllExpressions() {
toString("cat.name = cust.name.firstName", cat.name
assertToString("cat.name = cust.name.firstName", cat.name
.eq(cust.name.firstName));
toString("cat.name != cust.name.firstName", cat.name
assertToString("cat.name != cust.name.firstName", cat.name
.ne(cust.name.firstName));
}
@ -161,7 +161,7 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testIsNullAndIsNotNullInFunctionalWay() {
toString("cat.bodyWeight is null", cat.bodyWeight.isNull());
assertToString("cat.bodyWeight is null", cat.bodyWeight.isNull());
}
@Test
@ -173,7 +173,7 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testStringConcatenations() {
// string concatenation ...||... or concat(...,...)
toString("cat.name || kitten.name", cat.name.concat(kitten.name));
assertToString("cat.name || kitten.name", cat.name.concat(kitten.name));
}
// coalesce() and nullif()
@ -181,37 +181,37 @@ public class FeaturesTest extends AbstractQueryTest{
@Test
public void testStringConversionOperations() {
// str() for converting numeric or temporal values to a readable string
toString("str(cat.bodyWeight)", cat.bodyWeight.stringValue());
assertToString("str(cat.bodyWeight)", cat.bodyWeight.stringValue());
}
@Test
public void testStringOperationsInFunctionalWay() {
toString("cat.name || cust.name.firstName", cat.name
assertToString("cat.name || cust.name.firstName", cat.name
.concat(cust.name.firstName));
// toString("cat.name like :a1", cat.name.like("A%"));
toString("lower(cat.name)", cat.name.lower());
assertToString("lower(cat.name)", cat.name.lower());
}
@Test
public void testToString() {
toString("cat", cat);
toString("cat.alive", cat.alive);
toString("cat.bodyWeight", cat.bodyWeight);
toString("cat.name", cat.name);
assertToString("cat", cat);
assertToString("cat.alive", cat.alive);
assertToString("cat.bodyWeight", cat.bodyWeight);
assertToString("cat.name", cat.name);
toString("cust.name", cust.name);
toString("cust.name.firstName = :a1", cust.name.firstName.eq("Martin"));
assertToString("cust.name", cust.name);
assertToString("cust.name.firstName = :a1", cust.name.firstName.eq("Martin"));
// toString("cat.kittens as kitten", cat.kittens.as(kitten));
toString("cat.bodyWeight + :a1", cat.bodyWeight.add(10));
toString("cat.bodyWeight - :a1", cat.bodyWeight.subtract(10));
toString("cat.bodyWeight * :a1", cat.bodyWeight.multiply(10));
toString("cat.bodyWeight / :a1", cat.bodyWeight.divide(10));
assertToString("cat.bodyWeight + :a1", cat.bodyWeight.add(10));
assertToString("cat.bodyWeight - :a1", cat.bodyWeight.subtract(10));
assertToString("cat.bodyWeight * :a1", cat.bodyWeight.multiply(10));
assertToString("cat.bodyWeight / :a1", cat.bodyWeight.divide(10));
// toString("cat.bodyWeight as bw", cat.bodyWeight.as("bw"));
toString("kitten in elements(cat.kittens)", kitten.in(cat.kittens));
assertToString("kitten in elements(cat.kittens)", kitten.in(cat.kittens));
// toString("distinct cat.bodyWeight", distinct(cat.bodyWeight));
}

View File

@ -15,32 +15,32 @@ public class MathTest extends AbstractQueryTest{
@Test
public void test(){
PNumber<Double> path = QCat.cat.bodyWeight;
toString("(cat.bodyWeight - sum(cat.bodyWeight)) * cat.bodyWeight", path.subtract(path.sum()).multiply(path));
assertToString("(cat.bodyWeight - sum(cat.bodyWeight)) * cat.bodyWeight", path.subtract(path.sum()).multiply(path));
}
@Test
public void testArithmeticOperationsInFunctionalWay() {
toString("cat.bodyWeight + :a1", cat.bodyWeight.add(10));
toString("cat.bodyWeight - :a1", cat.bodyWeight.subtract(10));
toString("cat.bodyWeight * :a1", cat.bodyWeight.multiply(10));
toString("cat.bodyWeight / :a1", cat.bodyWeight.divide(10));
assertToString("cat.bodyWeight + :a1", cat.bodyWeight.add(10));
assertToString("cat.bodyWeight - :a1", cat.bodyWeight.subtract(10));
assertToString("cat.bodyWeight * :a1", cat.bodyWeight.multiply(10));
assertToString("cat.bodyWeight / :a1", cat.bodyWeight.divide(10));
toString("cat.bodyWeight + :a1 < :a1", cat.bodyWeight.add(10.0).lt(10.0));
toString("cat.bodyWeight - :a1 < :a1", cat.bodyWeight.subtract(10.0).lt(10.0));
toString("cat.bodyWeight * :a1 < :a1", cat.bodyWeight.multiply(10.0).lt(10.0));
toString("cat.bodyWeight / :a1 < :a2", cat.bodyWeight.divide(10.0).lt(20.0));
assertToString("cat.bodyWeight + :a1 < :a1", cat.bodyWeight.add(10.0).lt(10.0));
assertToString("cat.bodyWeight - :a1 < :a1", cat.bodyWeight.subtract(10.0).lt(10.0));
assertToString("cat.bodyWeight * :a1 < :a1", cat.bodyWeight.multiply(10.0).lt(10.0));
assertToString("cat.bodyWeight / :a1 < :a2", cat.bodyWeight.divide(10.0).lt(20.0));
toString("(cat.bodyWeight + :a1) * :a2", cat.bodyWeight.add(10).multiply(20));
toString("(cat.bodyWeight - :a1) * :a2", cat.bodyWeight.subtract(10).multiply(20));
toString("cat.bodyWeight * :a1 + :a2", cat.bodyWeight.multiply(10).add(20));
toString("cat.bodyWeight * :a1 - :a2", cat.bodyWeight.multiply(10).subtract(20));
assertToString("(cat.bodyWeight + :a1) * :a2", cat.bodyWeight.add(10).multiply(20));
assertToString("(cat.bodyWeight - :a1) * :a2", cat.bodyWeight.subtract(10).multiply(20));
assertToString("cat.bodyWeight * :a1 + :a2", cat.bodyWeight.multiply(10).add(20));
assertToString("cat.bodyWeight * :a1 - :a2", cat.bodyWeight.multiply(10).subtract(20));
QCat c1 = new QCat("c1");
QCat c2 = new QCat("c2");
QCat c3 = new QCat("c3");
toString("c1.id + c2.id * c3.id", c1.id.add(c2.id.multiply(c3.id)));
toString("c1.id * (c2.id + c3.id)", c1.id.multiply(c2.id.add(c3.id)));
toString("(c1.id + c2.id) * c3.id", c1.id.add(c2.id).multiply(c3.id));
assertToString("c1.id + c2.id * c3.id", c1.id.add(c2.id.multiply(c3.id)));
assertToString("c1.id * (c2.id + c3.id)", c1.id.multiply(c2.id.add(c3.id)));
assertToString("(c1.id + c2.id) * c3.id", c1.id.add(c2.id).multiply(c3.id));
}

View File

@ -28,7 +28,17 @@ public class SubQueryTest extends AbstractQueryTest{
assertEquals("from Cat cat", query.toString());
query.from(fatcat);
assertEquals("from Cat cat, Cat fatcat", query.toString());
assertEquals("from Cat cat, Cat fatcat", query.toString());
}
@Test
public void uniqueProjection(){
assertToString("(select cat from Cat cat)", sub().from(cat).unique(cat));
}
@Test
public void listProjection(){
assertToString("(select cat from Cat cat)", sub().from(cat).list(cat));
}
@Test
@ -37,9 +47,9 @@ public class SubQueryTest extends AbstractQueryTest{
// toString("exists (select cat from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).exists());
// toString("exists (select cat from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).unique(cat).exists());
toString("exists (select 1 from Cat cat)", sub().from(cat).exists());
toString("exists (select 1 from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).exists());
toString("exists (select 1 from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).unique(cat).exists());
assertToString("exists (select 1 from Cat cat)", sub().from(cat).exists());
assertToString("exists (select 1 from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).exists());
assertToString("exists (select 1 from Cat cat where cat.weight < :a1)", sub().from(cat).where(cat.weight.lt(1)).unique(cat).exists());
}
}