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);
}
}
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);
}
}
Aggregations