use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SQLQueryTest method testNullQuery.
/**
* Test of a null query.
*/
public void testNullQuery() throws Exception {
// Try a null query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery("javax.jdo.query.SQL", null);
query.execute();
fail("Should have thrown an exception on a null query, but allowed execution");
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SQLQueryTest method testQueryWithTimeout.
/**
* Test of a query with a timeout.
*/
public void testQueryWithTimeout() throws Exception {
// Try a query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// TODO Change this to a query that will take a LONG time and check for it
String sqlText = "SELECT count(*) FROM PERSON";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.addExtension("org.jpox.query.timeout", "1");
query.execute();
tx.commit();
} catch (JDODataStoreException dse) {
fail("JDODataStoreException thrown when using query timeout : " + dse.getCause().getMessage());
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SQLQueryTest method testQueryFromJdoqueryFile.
/**
* Test of a query specified in a jdo query file.
*/
public void testQueryFromJdoqueryFile() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some inherited data to query and query using a ResultClass without constructor
tx = pm.currentTransaction();
tx.begin();
Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
pm.makePersistent(p3);
tx.commit();
// Run the query (specified in a jdoquery file)
tx.begin();
Query query = pm.newNamedQuery(null, "DeveloperSkills");
List results = (List) query.execute("jdo");
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
Object skill = resultsIter.next();
LOG.debug(">> Skill : " + skill);
}
} catch (JDOUserException ue) {
ue.printStackTrace();
LOG.error(ue);
fail("Exception thrown while persisting object and performing query : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Developer.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class ValueGeneratorTest method testTableGenerator.
/**
* Test the use of "table" generator. This is for all datastores.
*/
public void testTableGenerator() throws Exception {
try {
HashSet idSet = new HashSet();
Class idClass = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create a few objects.
TableGeneratorItem item = null;
item = new TableGeneratorItem("First item");
pm.makePersistent(item);
idSet.add(new Integer(item.getIdentifier()));
item = new TableGeneratorItem("Second item");
pm.makePersistent(item);
idSet.add(new Integer(item.getIdentifier()));
item = new TableGeneratorItem("Third item");
pm.makePersistent(item);
idSet.add(new Integer(item.getIdentifier()));
item = new TableGeneratorItem("Fourth item");
pm.makePersistent(item);
idSet.add(new Integer(item.getIdentifier()));
idClass = JDOHelper.getObjectId(item).getClass();
tx.commit();
} catch (JDOUserException e) {
fail("Exception thrown during insert of objects in \"table\" generator test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
HashSet idSetCopy = new HashSet(idSet);
// Retrieve the items
Query q = pm.newQuery(pm.getExtent(TableGeneratorItem.class, true));
Collection c = (Collection) q.execute();
// Check on the number of items
assertEquals("Number of TableGeneratorItem's retrieved is incorrect", 4, c.size());
Iterator iter = c.iterator();
while (iter.hasNext()) {
Object o = iter.next();
if (TableGeneratorItem.Oid.class.equals(idClass)) {
idSetCopy.remove(new Integer(((TableGeneratorItem) o).getIdentifier()));
}
}
tx.commit();
if (TableGeneratorItem.Oid.class.equals(idClass)) {
assertEquals("Wrong number of different IDs", 4, idSet.size());
assertTrue("Retrieved IDs did not match created IDs", 0 == idSetCopy.size());
}
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test " + ue.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out any created data
clean(TableGeneratorItem.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class ValueGeneratorTest method testAppIdentityGenerator.
/**
* Test the use of "identity" generator for application-identity.
*/
public void testAppIdentityGenerator() throws Exception {
try {
HashSet idSet = new HashSet();
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create a few objects.
for (int i = 0; i < 4; i++) {
AppIdStringGeneratorItem item = new AppIdStringGeneratorItem("First item");
pm.makePersistent(item);
pm.flush();
idSet.add(JDOHelper.getObjectId(item));
}
tx.commit();
assertEquals("Number of unique ids persisted is wrong", 4, idSet.size());
} catch (JDOUserException e) {
fail("Exception thrown during insert of objects with \"identity\" generator : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Iterator idIter = idSet.iterator();
while (idIter.hasNext()) {
Object id = idIter.next();
AppIdStringGeneratorItem obj = (AppIdStringGeneratorItem) pm.getObjectById(id);
assertEquals("Incorrect name", "First item", obj.getName());
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test " + ue.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
HashSet idSetCopy = new HashSet(idSet);
// Retrieve the items
Query q = pm.newQuery(pm.getExtent(AppIdStringGeneratorItem.class, true));
Collection c = (Collection) q.execute();
// Check on the number of items
assertTrue("Number of AppIdStringGeneratorItems retrieved is incorrect (" + c.size() + ") : should have been 4", c.size() == 4);
Iterator<AppIdStringGeneratorItem> iter = c.iterator();
while (iter.hasNext()) {
AppIdStringGeneratorItem item = iter.next();
Object id = JDOHelper.getObjectId(item);
if (idSetCopy.contains(id)) {
idSetCopy.remove(id);
}
}
tx.commit();
assertEquals("Wrong number of different IDs", 4, idSet.size());
assertTrue("Retrieved IDs did not match created IDs", 0 == idSetCopy.size());
} catch (JDOUserException ue) {
assertTrue("Exception thrown during test " + ue.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(AppIdStringGeneratorItem.class);
}
}
Aggregations