use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterEqualityOperator.
/**
* Test of filter with == and != operators.
*/
public void testFilterEqualityOperator() {
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 == 'Mickey'");
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", "Mickey", p.getFirstName());
assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 101, p.getPersonNum());
// Compile the query
q = (JDOQuery) pm.newQuery(Person.class, "lastName != 'Mouse'");
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", "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();
}
}
use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.
the class JDOQLSingleStringParserTest method testWhereWithMultipleSpaces.
/**
* Test for the parse of a WHERE clause which has multiple spaces present.
*/
public void testWhereWithMultipleSpaces() {
PersistenceManager pm = pmf.getPersistenceManager();
Query q = pm.newQuery("JDOQL", null);
AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 == 'The value to compare against'";
JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
try {
parser.parse();
// Should preserve spaces in the WHERE
assertEquals("The WHERE clause was not correctly parsed", "field1 == 'The value to compare against'", query.getFilter());
} catch (NucleusUserException e) {
fail("Exception in parse : " + e.getMessage());
}
}
use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.
the class JDOQLSingleStringParserTest method testSubquery.
/**
* Test for parse and compile of single-string JDOQL subquery case.
*/
public void testSubquery() {
PersistenceManager pm = pmf.getPersistenceManager();
Query q = pm.newQuery("JDOQL", null);
AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
String str = "SELECT FROM org.jpox.samples.MyClass WHERE field1 > " + "(SELECT avg(f.field1) FROM org.jpox.samples.MyClass f)";
JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
try {
parser.parse();
SubqueryDefinition subq1 = query.getSubqueryForVariable("DN_SUBQUERY_1");
assertNotNull("Subquery #1 should be generated by single-string parser but isnt", subq1);
assertEquals(subq1.getQuery().toString(), "SELECT avg(f.field1) FROM org.jpox.samples.MyClass f");
SubqueryDefinition subq2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
assertNull("Subquery #2 shouldnt be generated by single-string parser but is", subq2);
} catch (NucleusUserException e) {
fail("Exception in parser : " + e.getMessage());
}
}
use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.
the class JDOQLSingleStringParserTest method testVariableBeforeParameter.
/**
* Test for the order of variable and parameter definitions.
*/
public void testVariableBeforeParameter() {
PersistenceManager pm = pmf.getPersistenceManager();
Query q = pm.newQuery("JDOQL", null);
AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
String str = "SELECT FROM org.jpox.samples.MyClass PARAMETERS String p1 VARIABLES int v1";
JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
try {
parser.parse();
fail("Query using VARIABLES after PARAMETERS should have thrown an exception but passed");
} catch (NucleusUserException e) {
// Expected since VARIABLES should be before PARAMETERS
}
}
use of org.datanucleus.api.jdo.JDOQuery in project tests by datanucleus.
the class JDOQLSingleStringParserTest method testSubquery2.
/**
* Test for parse and compile of single-string JDOQL subquery case.
*/
public void testSubquery2() {
PersistenceManager pm = pmf.getPersistenceManager();
Query q = pm.newQuery("JDOQL", null);
AbstractJDOQLQuery query = (AbstractJDOQLQuery) ((JDOQuery) q).getInternalQuery();
String str = "SELECT FROM org.jpox.samples.MyClass WHERE " + "field1 > (SELECT avg(f.field1) FROM org.jpox.samples.MyClass f) AND " + "field2 > (SELECT min(s.field1) FROM org.jpox.samples.OtherClass s)";
JDOQLSingleStringParser parser = new JDOQLSingleStringParser(query, str);
try {
parser.parse();
SubqueryDefinition subq1 = query.getSubqueryForVariable("DN_SUBQUERY_1");
assertNotNull("Subquery #1 should be generated by single-string parser but isnt", subq1);
assertEquals("SELECT avg(f.field1) FROM org.jpox.samples.MyClass f", subq1.getQuery().toString());
SubqueryDefinition subq2 = query.getSubqueryForVariable("DN_SUBQUERY_2");
assertNotNull("Subquery #2 should be generated by single-string parser but isnt", subq2);
assertEquals("SELECT min(s.field1) FROM org.jpox.samples.OtherClass s", subq2.getQuery().toString());
} catch (NucleusUserException e) {
fail("Exception in parser : " + e.getMessage());
}
}
Aggregations