use of org.jpox.samples.one_one.unidir.Login in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testPersistOneToOneUni.
/**
* Test of persist of 1-1 UNIDIR relation (PC field).
*/
public void testPersistOneToOneUni() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object acctId = null;
Object loginId = null;
try {
tx.begin();
LoginAccount acct = new LoginAccount("Mickey", "Mouse", "mickeym", "minnie");
acct.setId(1);
Login login = acct.getLogin();
login.setId(1);
pm.makePersistent(acct);
tx.commit();
acctId = pm.getObjectId(acct);
loginId = pm.getObjectId(login);
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
LoginAccount acct = (LoginAccount) pm.getObjectById(acctId);
Login login = (Login) pm.getObjectById(loginId);
assertEquals("Acct first name retrieved is incorrect", "Mickey", acct.getFirstName());
assertEquals("Acct last name retrieved is incorrect", "Mouse", acct.getLastName());
assertEquals("Login username retrieved is incorrect", "mickeym", login.getUserName());
assertEquals("Login password retrieved is incorrect", "minnie", login.getPassword());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getId(), acct.getLogin().getId());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getUserName(), acct.getLogin().getUserName());
assertEquals("Login of LoginAccount retrieved is incorrect", login.getPassword(), acct.getLogin().getPassword());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
clean(LoginAccount.class);
clean(Login.class);
}
}
use of org.jpox.samples.one_one.unidir.Login in project tests by datanucleus.
the class TransactionTest method testAutomaticRollback.
/**
* Test for proper functioning of automatic rollback upon failed commit
*/
public void testAutomaticRollback() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_FOREIGN_KEYS)) {
// No foreign keys with this datastore so cannot pass this test
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
Login login = acct.getLogin();
try {
tx.begin();
pm.makePersistent(acct);
tx.commit();
// provoke FK violation
tx.begin();
pm.deletePersistent(login);
boolean exceptionCaught = false;
try {
tx.commit();
assertTrue("Should have caught exception during commit due to FK violation", false);
} catch (JDOException e) {
// Expected
exceptionCaught = true;
}
assertTrue("No exception was thrown during commit so couldnt test autoRollback", exceptionCaught);
// now verify that we can still commit, i.e. tx was rolled back properly and is not active anymore
tx.begin();
LoginAccount acct2 = new LoginAccount("Wilma", "Flintstone", "wilma", "pebbles");
pm.makePersistent(acct2);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
} finally {
clean(LoginAccount.class);
}
}
use of org.jpox.samples.one_one.unidir.Login in project tests by datanucleus.
the class TransactionTest method testSqlExceptionIsAccessible.
/**
* Test that upon exception during commit, any SQLException that was the cause of the problem
* is provided as nested exception
*/
public void testSqlExceptionIsAccessible() {
if (vendorID == null) {
// Only applicable to RDBMS
return;
}
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setOptimistic(true);
LoginAccount acct = new LoginAccount("Fred", "Flintstone", "fred", "yabbadabbadoo");
Login login = acct.getLogin();
try {
tx.begin();
pm.makePersistent(acct);
tx.commit();
// provoke FK violation
tx.begin();
pm.deletePersistent(login);
boolean exceptionCaught = false;
try {
tx.commit();
assertTrue("Should have caught exception during commit due to FK violation", false);
} catch (JDOException e) {
Throwable nested = e.getCause();
if (nested != null && (nested instanceof SQLException)) {
exceptionCaught = true;
}
}
assertTrue("Did not catch JDOException with nested SQLException, although expected", exceptionCaught);
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
} finally {
clean(LoginAccount.class);
}
}
Aggregations