use of javax.jdo.JDOException in project tests by datanucleus.
the class PersistenceManagerTest method testMakeTransientExceptions.
/**
* Tests attempts to call makeTransient must raise errors
*
* current state : new state/behaviour
* ---------------- --------------------------
* Transient : unchanged
* P-new : error (IN TEST)
* P-clean : Transient
* P-dirty : error (IN TEST)
* Hollow : Transient
* T-clean : unchanged
* T-dirty : unchanged
* P-new-del : error (IN TEST)
* P-del : error (IN TEST)
* P-nontrans : Transient
*/
public void testMakeTransientExceptions() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
int dataset = 0;
Person p = new Person(dataset, FIRSTNAME[dataset], LASTNAME[dataset], EMAIL[dataset]);
boolean success;
// -----------------------
// test P-new : error
// -----------------------
success = false;
tx.begin();
pm.makePersistent(p);
try {
pm.makeTransient(p);
success = false;
} catch (JDOException ex) {
success = true;
} finally {
tx.rollback();
}
if (!success) {
fail("persistent object in P-new state should have not been made transient");
}
// -----------------------
// P-new-del : error
// -----------------------
success = false;
tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(p);
pm.deletePersistent(p);
try {
pm.makeTransient(p);
success = false;
} catch (JDOException ex) {
success = true;
} finally {
tx.rollback();
}
if (!success) {
fail("persistent object in P-new-del state should have not been made transient");
}
// -----------------------
// prepare data for the next tests
// -----------------------
// create object in database
tx.begin();
pm.makePersistent(p);
tx.commit();
Object id = pm.getObjectId(p);
// -----------------------
// P-dirty : error
// -----------------------
success = false;
tx = pm.currentTransaction();
tx.begin();
p = (Person) pm.getObjectById(id, true);
p.setEmailAddress("new email");
try {
pm.makeTransient(p);
success = false;
} catch (JDOException ex) {
success = true;
} finally {
tx.rollback();
}
if (!success) {
fail("persistent object in P-dirty state should have not been made transient");
}
// -----------------------
// test P-del : error
// -----------------------
success = false;
tx = pm.currentTransaction();
tx.begin();
p = (Person) pm.getObjectById(id, true);
pm.deletePersistent(p);
try {
pm.makeTransient(p);
success = false;
} catch (JDOException ex) {
success = true;
} finally {
tx.commit();
}
if (!success) {
fail("persistent object in P-del state should have not been made transient");
}
} finally {
pm.close();
}
} finally {
// Clean out our data
clean(Person.class);
}
}
use of javax.jdo.JDOException in project tests by datanucleus.
the class JDOQLInMemoryTest method testInstanceOf.
/**
* Test use of instanceof operator.
*/
public void testInstanceOf() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find all Employee objects
Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE this instanceof Employee");
q.addExtension("datanucleus.query.evaluateInMemory", "true");
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees was incorrect", 2, results.size());
Iterator iter = results.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Returned object is not an Employee!", obj instanceof Employee);
}
tx.commit();
} catch (JDOException e) {
LOG.error("Exception in execution of query", e);
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of javax.jdo.JDOException in project tests by datanucleus.
the class JDOQLInMemoryTest method testCast.
/**
* Test use of a cast operator.
*/
public void testCast() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find the Employee with the specified serial number
Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName() + " WHERE this instanceof Employee && ((Employee)this).serialNo == \"10001\"");
q.addExtension("datanucleus.query.evaluateInMemory", "true");
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with serial number was incorrect", 1, results.size());
Object obj = results.get(0);
assertTrue("Object is not Employee", obj instanceof Employee);
Employee emp = (Employee) obj;
assertEquals("Employee first name is incorrect", "F1", emp.getFirstName());
assertEquals("Employee last name is incorrect", "S1", emp.getLastName());
assertEquals("Employee serial number is incorrect", "10001", emp.getSerialNo());
tx.commit();
} catch (JDOException e) {
LOG.error("Exception in execution of query", e);
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of javax.jdo.JDOException in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedQueryImpl method candidate.
/* (non-Javadoc)
* @see javax.jdo.JDOQLTypedQuery#candidate()
*/
public PersistableExpression candidate() {
assertIsOpen();
String candName = candidateCls.getName();
int pos = candName.lastIndexOf('.');
String qName = candName.substring(0, pos + 1) + getQueryClassNameForClassName(candName.substring(pos + 1));
try {
// Use the candidate() static method for access
Class qClass = ec.getClassLoaderResolver().classForName(qName);
Method method = qClass.getMethod("candidate", new Class[] {});
Object candObj = method.invoke(null, (Object[]) null);
if (candObj == null || !(candObj instanceof PersistableExpression)) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
}
return (PersistableExpression) candObj;
} catch (NoSuchMethodException nsfe) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
} catch (InvocationTargetException ite) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
} catch (IllegalAccessException iae) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
}
}
use of javax.jdo.JDOException in project datanucleus-api-jdo by datanucleus.
the class JDOQLTypedSubqueryImpl method candidate.
/* (non-Javadoc)
* @see org.datanucleus.query.typesafe.TypesafeSubquery#candidate()
*/
public PersistableExpression candidate() {
String candName = candidateCls.getName();
int pos = candName.lastIndexOf('.');
String qName = candName.substring(0, pos + 1) + JDOQLTypedQueryImpl.getQueryClassNameForClassName(candName.substring(pos + 1));
try {
// Access the "candidate" field of the query class
Class qClass = ec.getClassLoaderResolver().classForName(qName);
Constructor ctr = qClass.getConstructor(new Class[] { PersistableExpression.class, String.class });
Object candObj = ctr.newInstance(new Object[] { null, candidateAlias });
if (candObj == null || !(candObj instanceof PersistableExpression)) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
}
return (PersistableExpression) candObj;
} catch (NoSuchMethodException nsfe) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
} catch (InvocationTargetException ite) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
} catch (InstantiationException ie) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
} catch (IllegalAccessException iae) {
throw new JDOException("Class " + candidateCls.getName() + " has a Query class but the candidate is invalid");
}
}
Aggregations