use of org.datanucleus.query.compiler.Symbol in project tests by datanucleus.
the class JPQLCompilerTest method testFromInExpression.
/**
* Tests for from clause with an "IN(...) alias" expression.
*/
public void testFromInExpression() {
JavaQueryCompiler compiler = null;
QueryCompilation compilation = null;
try {
compiler = new JPQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), Department.class.getName() + " d, IN(d.projects) n", null, null, null, 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());
}
SymbolTable symtbl = compilation.getSymbolTable();
Expression[] exprs = compilation.getExprFrom();
assertEquals("Number of from expressions is incorrect", 2, exprs.length);
// Candidate clause
assertTrue("FROM candidate clause is of wrong type " + exprs[0].getClass().getName(), exprs[0] instanceof ClassExpression);
ClassExpression clsExpr0 = (ClassExpression) exprs[0];
assertEquals("FROM candidate clause alias is wrong", "d", clsExpr0.getAlias());
Symbol clsSym0 = symtbl.getSymbol(clsExpr0.getAlias());
assertEquals("FROM candidate clause class is wrong", Department.class, clsSym0.getValueType());
assertEquals("FROM candidate clause has incorrect left()", null, clsExpr0.getLeft());
assertEquals("FROM candidate clause has incorrect right()", null, clsExpr0.getRight());
// Candidate+IN clause
assertTrue("FROM candidate+IN clause is of wrong type " + exprs[1].getClass().getName(), exprs[1] instanceof ClassExpression);
ClassExpression clsExpr1 = (ClassExpression) exprs[1];
assertEquals("FROM candidate+IN clause alias is wrong", "d", clsExpr1.getAlias());
Symbol clsSym1 = symtbl.getSymbol(clsExpr1.getAlias());
assertEquals("FROM candidate+IN clause class is wrong", Department.class, clsSym1.getValueType());
assertEquals("FROM candidate+IN clause has incorrect left()", null, clsExpr1.getLeft());
assertTrue("FROM candidate+IN clause has incorrect right() - should be instanceof JoinExpression", clsExpr1.getRight() instanceof JoinExpression);
JoinExpression joinExpr1 = (JoinExpression) clsExpr1.getRight();
assertEquals("FROM candidate+IN clause join expression has incorrect alias", "n", joinExpr1.getAlias());
assertEquals("FROM candidate+IN clause join expression has incorrect join type", JoinType.JOIN_INNER, joinExpr1.getType());
PrimaryExpression joinPrimExpr1 = (PrimaryExpression) joinExpr1.getJoinedExpression();
assertEquals("FROM candidate+IN clause join primary expression is incorrect", "d.projects", joinPrimExpr1.getId());
}
use of org.datanucleus.query.compiler.Symbol 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());
}
Aggregations