use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLBasicTest method testUseOfParentheses.
/**
* Tests use of parentheses inside a JDOQL
*/
public void testUseOfParentheses() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
BasicTypeHolder[] basics = new BasicTypeHolder[5];
for (int i = 0; i < basics.length; i++) {
basics[i] = new BasicTypeHolder();
basics[i].setLongField(i + 1);
basics[i].setShortField((short) (i + 11));
basics[i].setCharField('0');
}
pm.makePersistentAll(basics);
tx.commit();
Object[] ids = new Object[5];
for (int i = 0; i < basics.length; i++) {
ids[i] = pm.getObjectId(basics[i]);
}
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 1 && this.shortField == 5) || (this.longField == 3 && this.shortField == 5)");
Collection c = (Collection) q.execute();
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 1 && this.shortField == 5) || (this.longField == 3 && this.shortField == 13)");
c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField == 3 && this.shortField == 5)");
c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[3], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField == 3 && this.shortField == 13)");
q.setOrdering("longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
Iterator it = c.iterator();
Assert.assertEquals(ids[2], JDOHelper.getObjectId(it.next()));
Assert.assertEquals(ids[3], JDOHelper.getObjectId(it.next()));
q = pm.newQuery(BasicTypeHolder.class, "(this.longField == 4 && this.shortField == 14) || (this.longField != 5 && this.shortField == 13)");
q.setOrdering("longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
it = c.iterator();
Assert.assertEquals(ids[2], JDOHelper.getObjectId(it.next()));
Assert.assertEquals(ids[3], JDOHelper.getObjectId(it.next()));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(BasicTypeHolder.class);
}
}
use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLResultTest method testCandidateCollectionWithResultNotSameTypeAsCandidates.
/**
* Test where the result class or result object instance is not of the same type as the candidates.
*/
public void testCandidateCollectionWithResultNotSameTypeAsCandidates() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
BasicTypeHolder[] basics = new BasicTypeHolder[5];
for (int i = 0; i < basics.length; i++) {
basics[i] = new BasicTypeHolder();
basics[i].setLongField(i + 1);
basics[i].setShortField((short) (i + 11));
basics[i].setCharField('0');
}
pm.makePersistentAll(basics);
List candidates = new ArrayList();
candidates.add(basics[0]);
candidates.add(basics[1]);
candidates.add(basics[2]);
candidates.add(basics[3]);
candidates.add(basics[4]);
candidates.add(basics[4]);
// Duplicate candidates, but with different result type
Query q = pm.newQuery(BasicTypeHolder.class);
q.setCandidates(candidates);
q.setResult("this, this");
Collection c = (Collection) q.execute();
// Not returning object of candidate type so ignore dups in candidates
assertEquals(5, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(BasicTypeHolder.class);
}
}
use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLResultTest method testSetResultWithAggregationAndNonAggregateExpression.
public void testSetResultWithAggregationAndNonAggregateExpression() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
BasicTypeHolder[] basics = new BasicTypeHolder[5];
for (int i = 0; i < basics.length; i++) {
basics[i] = new BasicTypeHolder();
basics[i].setLongField(i + 1);
basics[i].setShortField((short) (i + 11));
basics[i].setCharField('0');
}
pm.makePersistentAll(basics);
Object[] ids = new Object[5];
for (int i = 0; i < basics.length; i++) {
ids[i] = pm.getObjectId(basics[i]);
}
pm.flush();
// -----------
// COUNT + non aggregate expression
// -----------
Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this.longField),this.longField");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
Collection c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(2, ((Long) object[1]).longValue());
object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(3, ((Long) object[1]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// COUNT + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField,count(this.longField)");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(1, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(1, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// SUM + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField,sum(this.longField)");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(2, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(3, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// COUNT + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this.shortField),this.longField");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(2, ((Long) object[1]).longValue());
object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(3, ((Long) object[1]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// COUNT + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField,count(this.shortField)");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(1, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(1, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// SUM + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField, sum(this.shortField)");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(12, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(13, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
// -----------
// SUM(this.XXX) + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField, sum(this.shortField)");
q.setGrouping("this.longField");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
it = c.iterator();
object = (Object[]) it.next();
assertEquals(12, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(13, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
// -----------
// SUM(XXX) + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("this.longField, sum(shortField)");
q.setGrouping("this.longField");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
it = c.iterator();
object = (Object[]) it.next();
assertEquals(12, ((Long) object[1]).longValue());
assertEquals(2, ((Long) object[0]).longValue());
object = (Object[]) it.next();
assertEquals(13, ((Long) object[1]).longValue());
assertEquals(3, ((Long) object[0]).longValue());
// Dont persist the objects
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(BasicTypeHolder.class);
}
}
use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLResultTest method testSetResultWithAggregation1.
public void testSetResultWithAggregation1() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
BasicTypeHolder[] basics = new BasicTypeHolder[5];
for (int i = 0; i < basics.length; i++) {
basics[i] = new BasicTypeHolder();
basics[i].setLongField(i + 1);
basics[i].setShortField((short) (i + 11));
basics[i].setCharField('0');
}
pm.makePersistentAll(basics);
tx.commit();
Object[] ids = new Object[5];
for (int i = 0; i < basics.length; i++) {
ids[i] = pm.getObjectId(basics[i]);
}
tx.begin();
// -----------
// MAX
// -----------
Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("max(this.longField)");
Object result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 3, ((Long) result).longValue());
// -----------
// MIN
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("min(this.longField)");
result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 2, ((Long) result).longValue());
// -----------
// SUM
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("sum(this.longField)");
result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 5, ((Long) result).longValue());
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this.longField)");
result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 2, ((Long) result).longValue());
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this.intField)");
result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 2, ((Long) result).longValue());
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this)");
result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 2, ((Long) result).longValue());
// -----------
// COUNT + non aggregate expression
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 3");
q.setResult("count(this.longField),this.longField");
q.setGrouping("this.longField");
q.setOrdering("this.longField ascending");
Collection c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(2, ((Long) object[1]).longValue());
object = (Object[]) it.next();
assertEquals(1, ((Long) object[0]).longValue());
assertEquals(3, ((Long) object[1]).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
// -----------
// AVG
// -----------
q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false));
q.setFilter("this.longField == 2 || this.longField == 4");
q.setResult("avg(this.longField)");
result = q.execute();
assertEquals("Type of result object is incorrect", "java.lang.Double", result.getClass().getName());
assertEquals("Result is incorrect", 3, ((Double) result).longValue());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in query", e);
fail("Exception thrown by query " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(BasicTypeHolder.class);
}
}
use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLResultTest method testSetResultWithAggregationWithSubclasses.
public void testSetResultWithAggregationWithSubclasses() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
BasicTypeHolder[] basics = new BasicTypeHolder[5];
for (int i = 0; i < basics.length; i++) {
basics[i] = new BasicTypeHolder();
basics[i].setLongField(i + 1);
basics[i].setShortField((short) (i + 11));
basics[i].setCharField('0');
}
pm.makePersistentAll(basics);
tx.commit();
Object[] ids = new Object[5];
for (int i = 0; i < basics.length; i++) {
ids[i] = pm.getObjectId(basics[i]);
}
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("longField == 2 || longField == 3");
q.setResult("max(longField)");
Object result = q.execute();
assertEquals("Type of result object is incorrect", result.getClass().getName(), "java.lang.Long");
assertEquals("Result is incorrect", 3, ((Long) result).longValue());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(BasicTypeHolder.class);
}
}
Aggregations