use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterExplicitParameter.
/**
* Test for use of an explicit parameter in the filter.
*/
public void testFilterExplicitParameter() {
// Test use of implicit variable in filter
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name == param1", null, null, null, null, null, "java.lang.String param1", null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
// TODO Debatable if this should throw a JDOUserException since the "notaField" is not bound, nor typed
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be ParameterExpression but isnt", dyExpr.getRight() instanceof ParameterExpression);
PrimaryExpression left = (PrimaryExpression) dyExpr.getLeft();
assertEquals("Primary expression name is wrong", left.getId(), "name");
ParameterExpression right = (ParameterExpression) dyExpr.getRight();
assertEquals("ParameterExpression has wrong value", "param1", right.getId());
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterComparisonWithAndOr.
/**
* Tests for filter with field-literal comparison AND another comparison, and ORed with
* another set of expressions.
*/
public void testFilterComparisonWithAndOr() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "(statusId == 2 && 100.0 > price) || (price >= 50 && price <= 95)", null, null, null, null, null, null, null, null);
compilation = compiler.compile(null, null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
assertTrue("Compiled left expression should be DyadicExpression but isnt", dyExpr.getLeft() instanceof DyadicExpression);
assertTrue("Compiled right expression should be DyadicExpression but isnt", dyExpr.getRight() instanceof DyadicExpression);
DyadicExpression dyExpr1 = (DyadicExpression) dyExpr.getLeft();
DyadicExpression dyExpr2 = (DyadicExpression) dyExpr.getRight();
assertEquals("Operator between left and right is incorrect", Expression.OP_OR, dyExpr.getOperator());
assertTrue("Compiled left(left) expression should be DyadicExpression but isnt", dyExpr1.getLeft() instanceof DyadicExpression);
assertTrue("Compiled left(right) expression should be DyadicExpression but isnt", dyExpr1.getRight() instanceof DyadicExpression);
DyadicExpression dyExpr1a = (DyadicExpression) dyExpr1.getLeft();
DyadicExpression dyExpr1b = (DyadicExpression) dyExpr1.getRight();
// 1a : statusId == 2
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1a.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be Literal but isnt", dyExpr1a.getRight() instanceof Literal);
assertEquals("Operator between left (left and right) is incorrect", Expression.OP_EQ, dyExpr1a.getOperator());
PrimaryExpression leftExpr1a = (PrimaryExpression) dyExpr1a.getLeft();
assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1a.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "statusId", leftExpr1a.getId());
Literal rightExpr1a = (Literal) dyExpr1a.getRight();
assertTrue("Compiled right expression literal is of incorrect type", rightExpr1a.getLiteral() instanceof Long);
assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1a.getLiteral()).longValue());
// 1b : 100.0 > price
assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr1b.getRight() instanceof PrimaryExpression);
assertTrue("Compiled left expression should be Literal but isnt", dyExpr1b.getLeft() instanceof Literal);
assertEquals("Operator between right (left and right) is incorrect", Expression.OP_GT, dyExpr1b.getOperator());
PrimaryExpression rightExpr1b = (PrimaryExpression) dyExpr1b.getRight();
assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr1b.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "price", rightExpr1b.getId());
Literal leftExpr1b = (Literal) dyExpr1b.getLeft();
// TODO Why BigDecimal and not Double??
assertTrue("Compiled right expression literal is of incorrect type", leftExpr1b.getLiteral() instanceof BigDecimal);
assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr1b.getLiteral()).longValue(), 0.1);
assertTrue("Compiled right(left) expression should be DyadicExpression but isnt", dyExpr2.getLeft() instanceof DyadicExpression);
assertTrue("Compiled right(right) expression should be DyadicExpression but isnt", dyExpr2.getRight() instanceof DyadicExpression);
DyadicExpression dyExpr2a = (DyadicExpression) dyExpr2.getLeft();
DyadicExpression dyExpr2b = (DyadicExpression) dyExpr2.getRight();
// 2a : price >= 50
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr2a.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be Literal but isnt", dyExpr2a.getRight() instanceof Literal);
assertEquals("Operator between right (left and right) is incorrect", Expression.OP_GTEQ, dyExpr2a.getOperator());
PrimaryExpression leftExpr2a = (PrimaryExpression) dyExpr2a.getLeft();
assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr2a.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "price", leftExpr2a.getId());
Literal rightExpr2a = (Literal) dyExpr2a.getRight();
assertTrue("Compiled right expression literal is of incorrect type " + rightExpr2a.getLiteral().getClass().getName(), rightExpr2a.getLiteral() instanceof Long);
assertEquals("Compiled right expression literal has incorrect value", 50.0, ((Long) rightExpr2a.getLiteral()).longValue(), 0.1);
// 2b : price >= 50
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr2b.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be Literal but isnt", dyExpr2b.getRight() instanceof Literal);
assertEquals("Operator between right (left and right) is incorrect", Expression.OP_LTEQ, dyExpr2b.getOperator());
PrimaryExpression leftExpr2b = (PrimaryExpression) dyExpr2b.getLeft();
assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr2b.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "price", leftExpr2b.getId());
Literal rightExpr2b = (Literal) dyExpr2b.getRight();
assertTrue("Compiled right expression literal is of incorrect type", rightExpr2b.getLiteral() instanceof Long);
assertEquals("Compiled right expression literal has incorrect value", 95.0, ((Long) rightExpr2b.getLiteral()).longValue(), 0.1);
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithStringIndexOfLiteral.
/**
* Tests for "String.indexOf(Literal, int)" in filter.
*/
public void testFilterWithStringIndexOfLiteral() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.indexOf(\"nd\", 3)", null, null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been InvokeExpression but wasnt", expr instanceof InvokeExpression);
InvokeExpression invExpr = (InvokeExpression) expr;
assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", invExpr.getLeft() instanceof PrimaryExpression);
assertEquals("Name of field upon which we invoke the method was wrong", "name", ((PrimaryExpression) invExpr.getLeft()).getId());
assertEquals("Name of invoked method was wrong", "indexOf", invExpr.getOperation());
assertEquals("Number of parameters is wrong", 2, invExpr.getArguments().size());
Object param1 = invExpr.getArguments().get(0);
assertTrue("Parameter1 to indexOf() is of wrong type", param1 instanceof Literal);
Literal param1Lit = (Literal) param1;
assertEquals("Parameter1 to indexOf() has wrong value", "nd", param1Lit.getLiteral());
Object param2 = invExpr.getArguments().get(1);
assertTrue("Parameter2 to indexOf() is of wrong type", param2 instanceof Literal);
Literal param2Lit = (Literal) param2;
assertEquals("Parameter2 to indexOf() has wrong value", new Long(3), param2Lit.getLiteral());
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithCast.
/**
* Tests for "(cast)expr" in filter.
*/
public void testFilterWithCast() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "((Book)this).author == 'Tolkien'", null, null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Filter should be DyadicExpression but is " + expr, expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
Expression leftExpr = dyExpr.getLeft();
assertTrue("Left side should be PrimaryExpression but is " + leftExpr, leftExpr instanceof PrimaryExpression);
PrimaryExpression primExpr = (PrimaryExpression) leftExpr;
assertTrue("PrimaryExpression should have left of CastExpression but is " + primExpr.getLeft(), primExpr.getLeft() instanceof DyadicExpression);
DyadicExpression castDyExpr = (DyadicExpression) primExpr.getLeft();
assertTrue("Cast DyadicExpression left should be PrimaryExpression", castDyExpr.getLeft() instanceof PrimaryExpression);
assertTrue("Cast DyadicExpression right should be PrimaryExpression", castDyExpr.getRight() instanceof Literal);
assertEquals("Cast class is incorrect", "Book", ((Literal) castDyExpr.getRight()).getLiteral());
PrimaryExpression castPrimExpr = (PrimaryExpression) castDyExpr.getLeft();
assertEquals("Expression being cast is incorrect", "this", castPrimExpr.getId());
assertEquals("PrimaryExpression off cast is incorrect", "author", primExpr.getId());
Expression rightExpr = dyExpr.getRight();
assertTrue("Right side should be Literal but is " + rightExpr, rightExpr instanceof Literal);
assertEquals("Right side literal value is incorrect", "Tolkien", ((Literal) rightExpr).getLiteral());
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithStringLiteralStartsWith.
/**
* Tests for "StringLiteral.startsWith(var)" in filter.
*/
public void testFilterWithStringLiteralStartsWith() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "\"SomeString\".startsWith(name)", null, null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression expr = compilation.getExprFilter();
assertTrue("Filter should be InvokeExpression but is " + expr, expr instanceof InvokeExpression);
InvokeExpression invExpr = (InvokeExpression) expr;
assertTrue("InvokeExpression should have been invoked on Literal but wasnt", invExpr.getLeft() instanceof Literal);
assertEquals("Value of literal is wrong", "SomeString", ((Literal) invExpr.getLeft()).getLiteral());
assertEquals("Name of invoked method was wrong", "startsWith", invExpr.getOperation());
assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
Object param1 = invExpr.getArguments().get(0);
assertTrue("Parameter1 to startsWith() is of wrong type : " + param1, param1 instanceof PrimaryExpression);
PrimaryExpression param1Expr = (PrimaryExpression) param1;
assertEquals("Parameter1 expression has incorrect number of tuples", 1, param1Expr.getTuples().size());
assertEquals("Parameter1 expression 'id' is incorrect", "name", param1Expr.getId());
}
Aggregations