use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLBasicTest method testModuloOperator.
/**
* Test for modulo operator in a Query.
*/
public void testModuloOperator() {
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();
// Run the test
tx.begin();
Query q = pm.newQuery(pm.getExtent(BasicTypeHolder.class, false), "longField % 2 == 1");
List c = (List) q.execute();
assertTrue("Number of items returned from modulo query was incorrect : should have been 3 but was " + c.size(), c.size() == 3);
for (int i = 0; i < c.size(); i++) {
long value = ((BasicTypeHolder) c.get(i)).getLongField();
assertTrue("Long value of returned object is incorrect : should have been 1, 3 or 5 but was " + value, value == 1 || value == 3 || value == 5);
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(BasicTypeHolder.class);
}
}
use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLBasicTest method testConcatStringAndNumbers.
/**
* Tests the concatenation of strings and numbers
*/
public void testConcatStringAndNumbers() {
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].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 + \"-\" + this.longField == \"1-1\" ");
Collection c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + 1 + \"-\" + 1");
c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + 1 + \"-\" + 1 + \"-\"");
c = (Collection) q.execute();
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == 1 + \"-\" + 1");
c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == 1 + \"-\" + 1 + \"-\"");
c = (Collection) q.execute();
Assert.assertEquals(0, c.size());
// test using parameters
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str ");
q.declareParameters("java.lang.String str");
c = (Collection) q.execute("1-1");
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.String str");
c = (Collection) q.execute("1");
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(new Integer(1));
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + str + \"-\" + str + \"-\"");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(new Integer(1));
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(new Integer(1));
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().next()));
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str + \"-\"");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(new Integer(1));
Assert.assertEquals(0, c.size());
// todo null
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str ");
q.declareParameters("java.lang.String str");
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.String str");
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "\"\" + this.longField + \"-\" + this.longField == \"\" + str + \"-\" + str + \"-\"");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(new Integer(1));
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "this.longField + \"-\" + this.longField == str + \"-\" + str + \"-\"");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(null);
Assert.assertEquals(0, c.size());
q = pm.newQuery(BasicTypeHolder.class, "null == str + \"-\" + str + \"-\" && this.longField == 1");
q.declareParameters("java.lang.Integer str");
c = (Collection) q.execute(null);
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[0], JDOHelper.getObjectId(c.iterator().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 JDOQLBasicTest method testAvg.
/**
* Tests the avg expression
*/
public void testAvg() {
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].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 == avg(p.longField)");
q.declareVariables("BasicTypeHolder p");
Collection c = (Collection) q.execute();
Assert.assertEquals(1, c.size());
Assert.assertEquals(ids[2], JDOHelper.getObjectId(c.iterator().next()));
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown executing JDOQL query with AVG", e);
fail("Exception thrown executing JDOQL query with AVG : " + e.getMessage());
} 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 testSetResultDistinct.
/**
* Test the use of setResult with the DISTINCT keyword.
*/
public void testSetResultDistinct() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Populate some data
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();
// Query the data
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("longField == 2 || longField == 3");
q.setResult("distinct this.longField");
q.setOrdering("this.longField ascending");
Collection c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
assertEquals(2, ((Long) it.next()).longValue());
assertEquals(3, ((Long) it.next()).longValue());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
tx.commit();
} 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 testSetResult.
/**
* Test the use of setResult().
*/
public void testSetResult() {
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("this.longField == 2 || this.longField == 3");
q.setResult("this.longField");
q.setOrdering("this.longField ascending");
Collection c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
try {
Iterator it = c.iterator();
assertEquals(2, ((Long) it.next()).longValue());
assertEquals(3, ((Long) it.next()).longValue());
} catch (Exception e) {
fail(e.getMessage());
}
q = pm.newQuery(BasicTypeHolder.class);
q.setResult("this.longField, this.shortField");
q.setFilter("this.longField == 2 || this.longField == 3");
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[0]).longValue());
assertEquals(12, ((Short) object[1]).shortValue());
object = (Object[]) it.next();
assertEquals(3, ((Long) object[0]).longValue());
assertEquals(13, ((Short) object[1]).shortValue());
} catch (Exception e) {
fail(e.getMessage());
}
q = pm.newQuery(BasicTypeHolder.class);
q.setResult("this.longField, this.shortField, this");
q.setFilter("this.longField == 2 || this.longField == 3");
q.setOrdering("this.longField ascending");
c = (Collection) q.execute();
try {
Iterator it = c.iterator();
Object[] object = (Object[]) it.next();
assertEquals(2, ((Long) object[0]).longValue());
assertEquals(12, ((Short) object[1]).shortValue());
assertEquals(ids[1], JDOHelper.getObjectId(object[2]));
object = (Object[]) it.next();
assertEquals(3, ((Long) object[0]).longValue());
assertEquals(13, ((Short) object[1]).shortValue());
assertEquals(ids[2], JDOHelper.getObjectId(object[2]));
} catch (Exception e) {
fail(e.getMessage());
}
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1 || this.longField == 2");
q.setResult("JDOHelper.getObjectId(this)");
c = (Collection) q.execute();
Assert.assertEquals(2, c.size());
Iterator it = c.iterator();
assertEquals(ids[0], it.next());
assertEquals(ids[1], it.next());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(BasicTypeHolder.class);
}
}
Aggregations