Search in sources :

Example 1 with Dog

use of org.datanucleus.samples.metadata.animal.Dog in project tests by datanucleus.

the class PersistenceManagerTest method testNonTransactionWriteNegative.

/**
 * Check for non active transactions and non-transactional write is false.
 */
public void testNonTransactionWriteNegative() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Dog rex = new Dog();
        rex.setName("rex");
        rex.setId("rex" + new Random().nextInt());
        rex.setColor("blue");
        Object id = null;
        try {
            tx.begin();
            pm.makePersistent(rex);
            id = pm.getObjectId(rex);
            tx.commit();
            tx.setNontransactionalWrite(false);
            // check updating field
            rex.setColor("yellow");
            fail("Expected TransactionNotWritableException");
        } catch (TransactionNotWritableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // check updating application id field
        try {
            tx.setNontransactionalWrite(false);
            Dog d = (Dog) pm.getObjectById(id, false);
            d.setId("newid");
            fail("Expected TransactionNotWritableException");
        } catch (TransactionNotWritableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalWrite(false);
            pm.newQuery(Dog.class).deletePersistentAll();
            fail("Expected TransactionNotActiveException");
        } catch (TransactionNotActiveException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalWrite(false);
            pm.newQuery(Dog.class).deletePersistentAll((Object[]) new String[] { "stupidarg" });
            fail("Expected TransactionNotActiveException");
        } catch (TransactionNotActiveException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalWrite(false);
            Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
            Dog d = (Dog) it.next();
            d.setId("newid");
            fail("Expected TransactionNotActiveException");
        } catch (TransactionNotActiveException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Dog.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Random(java.util.Random) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) Iterator(java.util.Iterator) Collection(java.util.Collection) Dog(org.datanucleus.samples.metadata.animal.Dog) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)

Example 2 with Dog

use of org.datanucleus.samples.metadata.animal.Dog in project tests by datanucleus.

the class PersistenceManagerTest method testNonTransactionReadNegative.

/**
 * Check for non active transactions and non-transactional read is false.
 */
public void testNonTransactionReadNegative() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Dog rex = new Dog();
        rex.setName("rex");
        rex.setId("rex" + new Random().nextInt());
        rex.setColor("blue");
        Object id = null;
        try {
            tx.begin();
            pm.makePersistent(rex);
            id = pm.getObjectId(rex);
            tx.commit();
            tx.setNontransactionalRead(false);
            rex.getColor();
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalRead(false);
            Dog d = (Dog) pm.getObjectById(id, false);
            d.getId();
        } catch (TransactionNotReadableException ex) {
            fail("Unexpected TransactionNotReadableException");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalRead(false);
            Dog d = (Dog) pm.getObjectById(id);
            d.getColor();
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalRead(false);
            pm.newQuery(Dog.class).execute();
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalRead(false);
            Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
            Dog d = (Dog) it.next();
            d.getColor();
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pmf.getDataStoreCache().evictAll();
        pm.evictAll();
        try {
            tx.setNontransactionalRead(false);
            // Validation so needs to go to datastore
            pm.getObjectById(id, true);
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.setNontransactionalRead(false);
            Iterator it = ((Collection) pm.newQuery(Dog.class).execute()).iterator();
            Dog d = (Dog) it.next();
            d.getColor();
            fail("Expected TransactionNotReadableException");
        } catch (TransactionNotReadableException ex) {
        // expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Dog.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Random(java.util.Random) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) Collection(java.util.Collection) Dog(org.datanucleus.samples.metadata.animal.Dog)

Aggregations

Collection (java.util.Collection)2 Iterator (java.util.Iterator)2 Random (java.util.Random)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 Dog (org.datanucleus.samples.metadata.animal.Dog)2 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)1 TransactionNotReadableException (org.datanucleus.api.jdo.exceptions.TransactionNotReadableException)1 TransactionNotWritableException (org.datanucleus.api.jdo.exceptions.TransactionNotWritableException)1