use of org.datanucleus.query.expression.Literal 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.Literal 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.Literal 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