use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testAggregateOfDate.
/**
* Test the use of the Date in aggregates.
*/
public void testAggregateOfDate() {
try {
Date maxDate = null;
DateHolder prim1 = new DateHolder();
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_MONTH, 25);
cal1.set(Calendar.MONTH, 10);
cal1.set(Calendar.YEAR, 2000);
cal1.set(Calendar.HOUR_OF_DAY, 10);
cal1.set(Calendar.MINUTE, 55);
cal1.set(Calendar.SECOND, 5);
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
cal1.set(Calendar.MILLISECOND, 0);
}
prim1.setDateField(cal1.getTime());
maxDate = cal1.getTime();
DateHolder prim2 = new DateHolder();
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DAY_OF_MONTH, 5);
cal2.set(Calendar.MONTH, 3);
cal2.set(Calendar.YEAR, 1982);
cal2.set(Calendar.HOUR_OF_DAY, 4);
cal2.set(Calendar.MINUTE, 13);
cal2.set(Calendar.SECOND, 45);
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
cal2.set(Calendar.MILLISECOND, 0);
}
prim2.setDateField(cal2.getTime());
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id2 = null;
try {
tx.begin();
pm.makePersistent(prim1);
pm.makePersistent(prim2);
tx.commit();
id2 = JDOHelper.getObjectId(prim2);
tx.begin();
try {
Query q = pm.newQuery("SELECT MAX(dateField) FROM " + DateHolder.class.getName());
Date maxDateAgg = (Date) q.execute();
assertEquals("Max date is incorrect", maxDate, maxDateAgg);
q.closeAll();
q = pm.newQuery("SELECT FROM " + DateHolder.class.getName() + " WHERE dateField < MAX(dateField)");
List<DateHolder> results = (List) q.execute();
assertEquals("Received incorrect number of results", 1, results.size());
DateHolder prim = (DateHolder) results.iterator().next();
assertEquals("Retrieved object is incorrect", id2, JDOHelper.getObjectId(prim));
q.closeAll();
} catch (JDOUserException e) {
LOG.error("Exception during test", e);
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(DateHolder.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testQueryWithUnsetParameter.
/**
* test query with missing parameter value.
*/
public void testQueryWithUnsetParameter() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(homer);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(Manager.class);
q.setFilter("firstName != :myName");
q.execute();
fail("Should have thrown an exception for missing parameter but didn't");
} catch (JDOUserException ue) {
// Expected
}
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of javax.jdo.JDOUserException 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);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testEscapeSingleQuoteInString.
/**
* Test case for autoamtic escaping of single quote within a parameter String.
*/
public void testEscapeSingleQuoteInString() {
try {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "abc'def");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Persist some data to play with
tx.begin();
pm.makePersistent(woody);
tx.commit();
// Query for a parameter that contains a single-quote
tx.begin();
try {
Query q = pm.newQuery(pm.getExtent(Employee.class, false));
q.setFilter("serialNo == theSerial");
q.declareParameters("java.lang.String theSerial");
String serial = "abc'def";
List results = (List) q.execute(serial);
assertTrue("Test for a string containing a single-quote returned no results!", results != null && results.size() > 0);
assertTrue("Test for a string containing a single-quote returned incorrect number of objects - returned " + results.size() + " but should have been 1", results.size() == 1);
q.closeAll();
} catch (JDOUserException e) {
LOG.error(e);
fail(e.getMessage());
}
// Query for something that starts with something containing a single-quote
try {
Query q = pm.newQuery(pm.getExtent(Employee.class, false));
q.setFilter("serialNo.startsWith(theSerial)");
q.declareParameters("java.lang.String theSerial");
String serial = "abc'";
List results = (List) q.execute(serial);
assertTrue("Test for a string.startsWith() containing a single-quote returned no results!", results != null && results.size() > 0);
assertTrue("Test for a string.startsWith() containing a single-quote returned incorrect number of objects - returned " + results.size() + " but should have been 1", results.size() == 1);
q.closeAll();
} catch (JDOUserException e) {
LOG.error(e);
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class PersistenceManagerFactoryTest method testClose.
/**
* Test of the closure behaviour of the PMF.
*/
public void testClose() {
// Create 10 to give a reasonable chance that the active tx ones aren't "closed" first internally
PersistenceManager[] pms = new PersistenceManager[10];
for (int i = 0; i < 10; ++i) {
pms[i] = pmf.getPersistenceManager();
}
// Open and close a transaction on an arbitrary PM
pms[3].currentTransaction().begin();
pms[3].currentTransaction().commit();
// Open (and leave active) a transaction on 2 arbitrary PMs
pms[5].currentTransaction().begin();
pms[7].currentTransaction().begin();
// try to close PMF while has open tx
boolean success = false;
try {
pmf.close();
} catch (JDOException ex) {
Throwable[] nested = ex.getNestedExceptions();
assertNotNull("Expected nested exceptions", nested);
assertEquals("Expected exactly 2 nested exceptions", 2, nested.length);
for (PersistenceManager pm : pms) {
assertFalse("None of the PMs should have been closed", pm.isClosed());
}
success = true;
// Clean up the txns, PMs, and PMF
pms[5].currentTransaction().commit();
pms[7].currentTransaction().commit();
pmf.close();
} finally {
getPMF();
}
pmf.getPersistenceManager();
PersistenceManager pm3 = pmf.getPersistenceManager();
pm3.currentTransaction().begin();
pm3.currentTransaction().commit();
success = false;
try {
pmf.close();
success = true;
} finally {
getPMF();
}
assertTrue("should not have raised an exception", success);
// test close twice
pmf.close();
try {
pmf.close();
} catch (JDOUserException e) {
// JDO2.3 spec 11.4 allows redundant calls to close().
fail("should not have raised an exception when closing a closed PMF");
} finally {
getPMF();
}
// Try to access a PM from the closed PMF
pmf.close();
try {
pmf.getPersistenceManager();
fail("should have raised an exception when accessing PM from a closed PMF");
} catch (JDOUserException ue) {
// Expected, as per section 11.4 of the JDO2 spec
} catch (Exception e) {
LOG.error("Incorrect exception : expected JDOUserException ", e);
fail("wrong type of exception thrown when accessing PM from closed PMF " + e.getClass().getName());
} finally {
getPMF();
}
}
Aggregations