use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JPQLCompilerTest 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 JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "(budget = 2 AND 'Sales' = name) OR (budget >= 50 AND name = 'Marketing')", null, null, null, null, null, null, null);
compilation = compiler.compile(null, 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_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 : budget == 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", "budget", 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 : "Sales" == name
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_EQ, 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", "name", rightExpr1b.getId());
Literal leftExpr1b = (Literal) dyExpr1b.getLeft();
assertTrue("Compiled right expression literal is of incorrect type", leftExpr1b.getLiteral() instanceof String);
assertEquals("Compiled right expression literal has incorrect value", "Sales", ((String) leftExpr1b.getLiteral()));
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 : budget >= 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", "budget", 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, ((Long) rightExpr2a.getLiteral()).longValue());
// 2b : name == "Marketing"
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_EQ, 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", "name", leftExpr2b.getId());
Literal rightExpr2b = (Literal) dyExpr2b.getRight();
assertTrue("Compiled right expression literal is of incorrect type", rightExpr2b.getLiteral() instanceof String);
assertEquals("Compiled right expression literal has incorrect value", "Marketing", ((String) rightExpr2b.getLiteral()));
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JPQLCompilerTest method testFilterWithStringEqualsLiteral.
/**
* Tests for "String = Literal" in filter.
*/
public void testFilterWithStringEqualsLiteral() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Project.class, null, "name = \"Kettle\"", 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("DyadicExpression should have left of PrimaryExpression but hasnt", dyExpr.getLeft() instanceof PrimaryExpression);
assertEquals("Name of field upon which we invoke the method was wrong", "name", ((PrimaryExpression) dyExpr.getLeft()).getId());
assertTrue("Name of invoked method was wrong", dyExpr.getOperator() == Expression.OP_EQ);
assertTrue("DyadicExpression should right of Literal but hasnt", dyExpr.getRight() instanceof Literal);
Literal paramLit = (Literal) dyExpr.getRight();
assertEquals("Parameter to equals() has wrong value", "Kettle", paramLit.getLiteral());
}
use of org.datanucleus.query.expression.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest 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 JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2 && 100.0 > price", 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 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", "statusId", 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_GT, 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", "price", rightExpr2.getId());
Literal leftExpr2 = (Literal) dyExpr2.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.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterComparison.
/**
* Tests for simple field-literal comparison in filter.
*/
public void testFilterComparison() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "statusId == 2", 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 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", "statusId", 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 JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "100.0 > price", null, null, null, null, null, null, null, null);
} catch (NucleusException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", 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", "price", 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.PrimaryExpression in project tests by datanucleus.
the class JDOQLCompilerTest method testFilterWithStringEqualsLiteral.
/**
* Tests for "String.equals(Literal)" in filter.
*/
public void testFilterWithStringEqualsLiteral() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, "name.equals(\"Kettle\")", 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", "equals", invExpr.getOperation());
assertEquals("Number of parameters is wrong", 1, invExpr.getArguments().size());
Object param = invExpr.getArguments().get(0);
assertTrue("Parameter to equals() is of wrong type", param instanceof Literal);
Literal paramLit = (Literal) param;
assertEquals("Parameter to equals() has wrong value", "Kettle", paramLit.getLiteral());
}
Aggregations