use of org.datanucleus.query.expression.Literal in project tests by datanucleus.
the class JPQLCompilerTest method testUpdateSimple.
/**
* Test for use of update clause.
*/
public void testUpdateSimple() {
// Test use of UPDATE clause
try {
JPQLCompiler compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, null, null, null, null, null, null, null, "name = \"Sample Name\"");
QueryCompilation compilation = compiler.compile(null, null);
Expression[] updateExprs = compilation.getExprUpdate();
assertNotNull("Update clause is null but shouldnt be", updateExprs);
assertEquals("Number of update expressions is incorrect", 1, updateExprs.length);
assertTrue("Update expression is of incorrect type " + updateExprs[0].getClass().getName(), updateExprs[0] instanceof DyadicExpression);
DyadicExpression updateExpr = (DyadicExpression) updateExprs[0];
Expression left = updateExpr.getLeft();
Expression right = updateExpr.getRight();
Operator op = updateExpr.getOperator();
assertEquals("Operator in update expression is wrong", op, Expression.OP_EQ);
assertTrue("Left hand side in update is wrong", left instanceof PrimaryExpression);
assertTrue("Right hand side in update is wrong", right instanceof Literal);
PrimaryExpression primExpr = (PrimaryExpression) left;
assertEquals("Left hand side primary is wrong", "name", primExpr.getId());
Literal lit = (Literal) right;
assertEquals("Right hand side literal is wrong", "Sample Name", lit.getLiteral());
} catch (NucleusUserException ne) {
fail("Exception thrown in compile of update clause : " + ne.getMessage());
}
}
use of org.datanucleus.query.expression.Literal in project tests by datanucleus.
the class JPQLCompilerTest method testFilterComparison.
/**
* Tests for simple field-literal comparison in filter.
*/
public void testFilterComparison() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "budget = 2", null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception during compile", 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 Literal but isnt", dyExpr.getRight() instanceof Literal);
assertEquals("Operator between left and right is incorrect", Expression.OP_EQ, dyExpr.getOperator());
PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr.getLeft();
assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "budget", leftExpr1.getId());
Literal rightExpr1 = (Literal) dyExpr.getRight();
assertTrue("Compiled right expression literal is of incorrect type", rightExpr1.getLiteral() instanceof Long);
assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1.getLiteral()).longValue());
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "100.0 > budget", null, null, null, null, null, null, null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception during compile", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
compilation = compiler.compile(new HashMap(), null);
expr = compilation.getExprFilter();
assertTrue("Compiled expression should have been DyadicExpression but wasnt", expr instanceof DyadicExpression);
dyExpr = (DyadicExpression) expr;
assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr.getRight() instanceof PrimaryExpression);
assertTrue("Compiled left expression should be Literal but isnt", dyExpr.getLeft() instanceof Literal);
assertEquals("Operator between left and right is incorrect", Expression.OP_GT, dyExpr.getOperator());
PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr.getRight();
assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "budget", rightExpr2.getId());
Literal leftExpr2 = (Literal) dyExpr.getLeft();
// TODO Why BigDecimal and not Double??
assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof BigDecimal);
assertEquals("Compiled right expression literal has incorrect value", 100.0, ((BigDecimal) leftExpr2.getLiteral()).longValue(), 0.1);
}
use of org.datanucleus.query.expression.Literal in project tests by datanucleus.
the class JPQLCompilerTest method testFilterComparisonWithAnd.
/**
* Tests for simple field-literal comparison in filter and AND of another comparison.
*/
public void testFilterComparisonWithAnd() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "budget = 2 AND 'Sales' = name", null, null, null, null, null, null, null);
compilation = compiler.compile(new HashMap(), null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception during compile", 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_AND, dyExpr.getOperator());
assertTrue("Compiled left expression should be PrimaryExpression but isnt", dyExpr1.getLeft() instanceof PrimaryExpression);
assertTrue("Compiled right expression should be Literal but isnt", dyExpr1.getRight() instanceof Literal);
assertEquals("Operator between left (left and right) is incorrect", Expression.OP_EQ, dyExpr1.getOperator());
PrimaryExpression leftExpr1 = (PrimaryExpression) dyExpr1.getLeft();
assertEquals("Compiled left expression has incorrect number of tuples", 1, leftExpr1.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "budget", leftExpr1.getId());
Literal rightExpr1 = (Literal) dyExpr1.getRight();
assertTrue("Compiled right expression literal is of incorrect type", rightExpr1.getLiteral() instanceof Long);
assertEquals("Compiled right expression literal has incorrect value", 2, ((Long) rightExpr1.getLiteral()).longValue());
assertTrue("Compiled right expression should be PrimaryExpression but isnt", dyExpr2.getRight() instanceof PrimaryExpression);
assertTrue("Compiled left expression should be Literal but isnt", dyExpr2.getLeft() instanceof Literal);
assertEquals("Operator between right (left and right) is incorrect", Expression.OP_EQ, dyExpr2.getOperator());
PrimaryExpression rightExpr2 = (PrimaryExpression) dyExpr2.getRight();
assertEquals("Compiled left expression has incorrect number of tuples", 1, rightExpr2.getTuples().size());
assertEquals("Compiled left expression 'id' is incorrect", "name", rightExpr2.getId());
Literal leftExpr2 = (Literal) dyExpr2.getLeft();
assertTrue("Compiled right expression literal is of incorrect type", leftExpr2.getLiteral() instanceof String);
assertEquals("Compiled right expression literal has incorrect value", "Sales", ((String) leftExpr2.getLiteral()));
}
use of org.datanucleus.query.expression.Literal in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterCollectionContainsVariablePlusExtraJoin.
/**
* Tests for collection.contains(element) && elem.other.field == val.
*/
public void testFilterCollectionContainsVariablePlusExtraJoin() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Inventory.class, null, "products.contains(element) && element.guarantee.numberOfYears < 3", null, null, null, null, null, null, Product.class.getName() + " element", 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 DyadicExpression but wasnt", expr instanceof DyadicExpression);
DyadicExpression dyExpr = (DyadicExpression) expr;
LOG.info(">> expr=" + dyExpr);
// product.contains(element)
assertTrue("Left expression should have been InvokeExpression but wasnt", dyExpr.getLeft() instanceof InvokeExpression);
InvokeExpression leftExpr = (InvokeExpression) dyExpr.getLeft();
assertTrue("InvokeExpression should have been invoked on PrimaryExpression but wasnt", leftExpr.getLeft() instanceof PrimaryExpression);
assertEquals("Left expression : Name of field upon which we invoke the method was wrong", "products", ((PrimaryExpression) leftExpr.getLeft()).getId());
assertEquals("Left expression : Name of invoked method was wrong", "contains", leftExpr.getOperation());
assertEquals("Left expression : Number of parameters to contains() is wrong", 1, leftExpr.getArguments().size());
Object param1 = leftExpr.getArguments().get(0);
assertTrue("Left expression : Parameter1 to contains() is of wrong type", param1 instanceof VariableExpression);
VariableExpression vrExpr = (VariableExpression) param1;
assertEquals("Left expression : Name of variable to contains() is incorrect", "element", vrExpr.getId());
// element.guarantee.numberOfYears < 3
assertTrue("Right expression should have been DyadicExpression but wasnt", dyExpr.getRight() instanceof DyadicExpression);
DyadicExpression rightExpr = (DyadicExpression) dyExpr.getRight();
assertTrue("Right expression (left) should have been PrimaryExpression but wasnt", rightExpr.getLeft() instanceof PrimaryExpression);
PrimaryExpression rightExprLeft = (PrimaryExpression) rightExpr.getLeft();
assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
VariableExpression rightExprLeftLeft = (VariableExpression) rightExprLeft.getLeft();
assertTrue("Right expression (left).left is of incorrect type", rightExprLeft.getLeft() instanceof VariableExpression);
assertEquals("Right expression (left) part1 is incorrect", "element", rightExprLeftLeft.getId());
assertEquals("Right expression (left) has incorrect number of tuples", 2, rightExprLeft.getTuples().size());
assertEquals("Right expression (left) part2 is incorrect", "guarantee", rightExprLeft.getTuples().get(0));
assertEquals("Right expression (left) part2 is incorrect", "numberOfYears", rightExprLeft.getTuples().get(1));
assertEquals("Right expression : Operator between left and right is incorrect", Expression.OP_LT, rightExpr.getOperator());
assertTrue("Right expression (right) should have been Literal but wasnt", rightExpr.getRight() instanceof Literal);
Literal rightExprRight = (Literal) rightExpr.getRight();
assertEquals("Right expression (right) literal has incorrect value", 3, ((Long) rightExprRight.getLiteral()).longValue());
// Check symbols
SymbolTable symbols = compilation.getSymbolTable();
assertTrue("Symbol table doesnt have entry for 'element'", symbols.hasSymbol("element"));
assertTrue("Symbol table doesnt have entry for 'this'", symbols.hasSymbol("this"));
Symbol sy1 = symbols.getSymbol("element");
assertEquals("Type of symbol for 'element' is wrong", Product.class, sy1.getValueType());
Symbol sy2 = symbols.getSymbol("this");
assertEquals("Type of symbol for 'this' is wrong", Inventory.class, sy2.getValueType());
}
use of org.datanucleus.query.expression.Literal 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);
}
Aggregations