use of org.datanucleus.query.compiler.QueryCompilation 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());
}
use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.
the class JDOQLCompilerTest method testOrderNulls.
/**
* Test for order clause.
*/
public void testOrderNulls() {
// 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, null, null, "name ASC", null, null, null, null, null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression[] orderExprs1 = compilation.getExprOrdering();
assertEquals(1, orderExprs1.length);
assertTrue(orderExprs1[0] instanceof OrderExpression);
OrderExpression orderExpr1 = (OrderExpression) orderExprs1[0];
assertEquals("name", ((PrimaryExpression) orderExpr1.getLeft()).getId());
assertEquals("ascending", orderExpr1.getSortOrder());
assertNull(orderExpr1.getNullOrder());
// Test use of NULLS
try {
compiler = new JDOQLCompiler(nucCtx, nucCtx.getClassLoaderResolver(null), null, Product.class, null, null, null, "name ASC NULLS FIRST", null, null, null, null, null, null);
compilation = compiler.compile(null, null);
} catch (NucleusUserException ne) {
NucleusLogger.QUERY.error("Exception thrown during compilation", ne);
fail("compilation of filter with valid field threw exception : " + ne.getMessage());
}
Expression[] orderExprs2 = compilation.getExprOrdering();
assertEquals(1, orderExprs2.length);
assertTrue(orderExprs2[0] instanceof OrderExpression);
OrderExpression orderExpr2 = (OrderExpression) orderExprs2[0];
assertEquals("name", ((PrimaryExpression) orderExpr2.getLeft()).getId());
assertEquals("ascending", orderExpr2.getSortOrder());
assertEquals(NullOrderingType.NULLS_FIRST, orderExpr2.getNullOrder());
}
use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterCollectionContains.
/**
* Test of filter with collectionField.contains(element).
*/
public void testFilterCollectionContains() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<UserGroup> instances = new ArrayList<>();
UserGroup grp1 = new UserGroup(101, "First Group");
UserGroup grp2 = new UserGroup(102, "Second Group");
UserGroup grp3 = new UserGroup(103, "Third Group");
GroupMember mem1 = new GroupMember(201, "Donald Duck");
GroupMember mem2 = new GroupMember(202, "Mickey Mouse");
GroupMember mem3 = new GroupMember(203, "Minnie Mouse");
grp1.getMembers().add(mem1);
grp2.getMembers().add(mem1);
grp2.getMembers().add(mem2);
grp3.getMembers().add(mem2);
grp3.getMembers().add(mem3);
instances.add(grp1);
instances.add(grp2);
instances.add(grp3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(UserGroup.class, "members.contains(el) && el.name == 'Minnie Mouse'");
q.declareVariables(GroupMember.class.getName() + " el");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
UserGroup grp = (UserGroup) results.get(0);
assertEquals("Result instance has wrong name", "Third Group", grp.getName());
tx.commit();
} catch (Exception e) {
LOG.info(">> Unexpected exception thrown during test", e);
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterGreaterThanLessThan.
/**
* Test of filter with ">", "<" and "+".
*/
public void testFilterGreaterThanLessThan() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
p1.setAge(34);
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
p2.setAge(38);
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
p3.setAge(31);
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "age > 34");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
assertEquals("Age of result instance is wrong", 38, p.getAge());
// Compile the query
q = (JDOQuery) pm.newQuery(Person.class, "age+2 < 35");
query = q.getInternalQuery();
clr = query.getExecutionContext().getClassLoaderResolver();
compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
compilation = compiler.compile(new HashMap(), null);
// Execute the query
eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Minnie", p.getFirstName());
assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
assertEquals("Age of result instance is wrong", 31, p.getAge());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.query.compiler.QueryCompilation in project tests by datanucleus.
the class JDOQLEvaluatorTest method testImplicitParametersPositionalRepeated.
/**
* Test use of input params, positional, with one repeated before the other appears
*/
public void testImplicitParametersPositionalRepeated() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
Person p2 = new Person(102, "Donald", "Duck", "donald.duck@warnerbros.com");
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "firstName.endsWith(:param1) && !lastName.endsWith(:param1)" + " && lastName.length() == :param2");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(null, null);
// Execute the query
Map<Integer, Object> paramValues = new HashMap<>();
// Simulate what org.datanucleus.store.query.Query does
paramValues.put(new Integer(0), "ald");
paramValues.put(new Integer(1), 4);
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, paramValues, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Donald", p.getFirstName());
assertEquals("Result instance has wrong last name", "Duck", p.getLastName());
assertEquals("Person number of result instance is wrong", 102, p.getPersonNum());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations