Search in sources :

Example 6 with Login

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 7 with Login

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) JDOException(javax.jdo.JDOException)

Example 8 with Login

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) SQLException(java.sql.SQLException) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Login(org.jpox.samples.one_one.unidir.Login) JDOException(javax.jdo.JDOException)

Aggregations

Login (org.jpox.samples.one_one.unidir.Login)8 LoginAccount (org.jpox.samples.one_one.unidir.LoginAccount)8 PersistenceManager (javax.jdo.PersistenceManager)7 Transaction (javax.jdo.Transaction)7 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 JDOException (javax.jdo.JDOException)2 SQLException (java.sql.SQLException)1 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)1 JDOUserException (javax.jdo.JDOUserException)1 NamingException (javax.naming.NamingException)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 UserTransaction (javax.transaction.UserTransaction)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusContext (org.datanucleus.NucleusContext)1 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1