use of org.jpox.samples.types.basic.BasicTypeHolder in project tests by datanucleus.
the class JDOQLResultTest method testResultWithIfElse.
/**
* Test the use of result with "IF ... ELSE ...".
*/
public void testResultWithIfElse() {
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.setResult("IF (this.longField > 2) 1 ELSE 0");
Collection c = (Collection) q.execute();
Assert.assertEquals(5, c.size());
int numZeros = 0;
int numOnes = 0;
for (Object obj : c) {
Long l = Long.valueOf("" + obj);
if (l == 1) {
numOnes++;
} else if (l == 0) {
numZeros++;
}
}
assertEquals(3, numOnes);
assertEquals(2, numZeros);
tx.commit();
tx.begin();
try {
// Test construct with no final "ELSE expr"
q = pm.newQuery(BasicTypeHolder.class);
q.setResult("IF (this.longField > 2) 1 ELSE IF (this.logField < 0) 0");
q.compile();
fail("Should have thrown exception with missing ELSE on IF construct, but didnt");
} catch (JDOException jdoe) {
// Expected
}
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 testSetResultParameter.
/**
* Test the use of setResult() returning the parameter
*/
public void testSetResultParameter() {
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();
// obtain a primite persistent instance
Query q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1 || this.longField == 2");
q.setResult("this");
Collection c = (Collection) q.execute();
Iterator it = c.iterator();
BasicTypeHolder p = (BasicTypeHolder) it.next();
// test return String
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1 || this.longField == 2");
q.setResult("s");
q.declareParameters("String s");
c = (Collection) q.execute("result");
Assert.assertEquals(2, c.size());
it = c.iterator();
assertEquals("result", it.next());
assertEquals("result", it.next());
// test return Long
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1 || this.longField == 2");
q.setResult("l");
q.declareParameters("Long l");
c = (Collection) q.execute(new Long(10));
Assert.assertEquals(2, c.size());
it = c.iterator();
assertEquals(new Long(10), it.next());
assertEquals(new Long(10), it.next());
// test return Primitive
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1 || this.longField == 2");
q.setResult("p");
q.declareParameters("BasicTypeHolder p");
c = (Collection) q.execute(p);
Assert.assertEquals(2, c.size());
it = c.iterator();
assertEquals(p, it.next());
assertEquals(p, it.next());
// test return Primitive
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1");
q.setResult("p");
q.declareParameters("BasicTypeHolder p");
c = (Collection) q.execute(p);
Assert.assertEquals(1, c.size());
it = c.iterator();
assertEquals(p, it.next());
// test return null
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1");
q.setResult("p");
q.declareParameters("BasicTypeHolder p");
c = (Collection) q.execute(null);
Assert.assertEquals(1, c.size());
it = c.iterator();
assertNull(it.next());
// test return Primitive
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1");
q.setResult("this, p");
q.declareParameters("BasicTypeHolder p");
c = (Collection) q.execute(p);
Assert.assertEquals(1, c.size());
it = c.iterator();
assertEquals(p, ((Object[]) it.next())[1]);
// test return Long
q = pm.newQuery(BasicTypeHolder.class);
q.setFilter("this.longField == 1");
q.setResult("p.longField");
q.declareParameters("BasicTypeHolder p");
c = (Collection) q.execute(p);
Assert.assertEquals(1, c.size());
it = c.iterator();
assertEquals(1, p.getLongField());
assertEquals(new Long(1), it.next());
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 testCandidateCollection.
/**
* Test the use of setResult().
*/
public void testCandidateCollection() {
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]);
// Basic candidate test
Query q = pm.newQuery(BasicTypeHolder.class);
q.setCandidates(candidates);
Collection c = (Collection) q.execute();
assertEquals(5, c.size());
// Add duplicate of one candidate (so should get dup in result)
candidates.add(basics[4]);
q.setCandidates(candidates);
c = (Collection) q.execute();
assertEquals(6, c.size());
// Same thing but with result
q.setResult("this");
c = (Collection) q.execute();
assertEquals(6, c.size());
// Same thing but with distinct result so we eliminate the dup candidate
q.setResult("distinct this");
c = (Collection) q.execute();
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 JDOQLBasicTest method testDeleteByQuery.
/**
* Test for deletion by query.
*/
public void testDeleteByQuery() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some sample data (to delete)
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(BasicTypeHolder.class);
long numberDeleted = q.deletePersistentAll();
assertEquals("Number of BasicTypeHolder objects deleted by query was incorrect", 5, numberDeleted);
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 testClose.
public void testClose() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
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();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Assert.assertEquals(1, c.size());
q.closeAll();
tx.commit();
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
boolean success = false;
try {
success = false;
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
q.closeAll();
c.size();
tx.commit();
} catch (JDOUserException e) {
success = true;
} finally {
Assert.assertTrue("Query result has been closed and exception was expected", success);
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.closeAll();
Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
success = false;
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.closeAll();
iterator.next();
tx.commit();
} catch (NoSuchElementException e) {
success = true;
} finally {
Assert.assertTrue("Query result has been closed and exception was expected", success);
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.close(c);
Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
success = false;
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.close(c);
iterator.next();
tx.commit();
} catch (NoSuchElementException e) {
success = true;
} finally {
Assert.assertTrue("Query result has been closed and exception was expected", success);
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.close(new HashSet());
Assert.assertTrue("Query result has not been closed and iterator should not be closed", iterator.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.execute(new Long(1));
Iterator iterator = c.iterator();
q.close(new HashSet());
iterator.next();
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
Iterator iterator = c.iterator();
q.close(c);
Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
success = false;
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
Iterator iterator = c.iterator();
q.close(c);
iterator.next();
tx.commit();
} catch (NoSuchElementException e) {
success = true;
} finally {
Assert.assertTrue("Query result has been closed and exception was expected", success);
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)");
q.declareParameters("java.lang.Long longVar");
q.setCandidates(new HashSet());
Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
Iterator iterator = c.iterator();
q.close(c);
Assert.assertFalse("Query result has been closed and iterator should be closed", iterator.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
success = false;
tx.begin();
Query q = pm.newQuery(BasicTypeHolder.class, "(this.longField == longVar)").parameters("java.lang.Long longVar");
q.setCandidates(new HashSet());
Collection c = (Collection) q.executeWithArray(new Object[] { new Long(1) });
Iterator iterator = c.iterator();
q.close(c);
iterator.next();
tx.commit();
} catch (NoSuchElementException e) {
success = true;
} finally {
Assert.assertTrue("Query result has been closed and exception was expected", success);
if (tx.isActive()) {
tx.rollback();
}
}
} finally {
pm.close();
// Clean out our data
clean(BasicTypeHolder.class);
}
}
Aggregations