use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.
the class PersistenceManagerTest method tearDown.
/**
* Invoked after each test is run
*/
public void tearDown() throws java.lang.Exception {
super.tearDown();
// TODO Remove all of this when each test cleans out its own data.
Extent ext = null;
java.util.Iterator it = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
// delete all InstanceCallbackContainer objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackContainer.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
pm.deletePersistent(owner);
}
pm.currentTransaction().commit();
// delete all InstanceCallbackTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
// necesaary to avoid exception from jdoPreDelete() for this class only
tester.setTransientValue("");
pm.deletePersistent(tester);
}
pm.currentTransaction().commit();
// delete all InversePrimitive objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
InversePrimitive ip = (InversePrimitive) it.next();
ip.setTester(null);
pm.deletePersistent(ip);
}
pm.currentTransaction().commit();
// delete all CollectionFieldTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(CollectionFieldTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
CollectionFieldTester t = (CollectionFieldTester) it.next();
if (t.getPrimitiveCollection() != null) {
t.getPrimitiveCollection().clear();
}
if (t.getInversePrimitiveCollection() != null) {
t.getInversePrimitiveCollection().clear();
}
pm.deletePersistent(t);
}
pm.currentTransaction().commit();
// delete all Primative objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
Primitive p = (Primitive) it.next();
pm.deletePersistent(p);
}
pm.currentTransaction().commit();
// disassociate all Employees and Departments from their Managers
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
if (mgr.getSubordinates() != null) {
mgr.getSubordinates().clear();
}
if (mgr.getDepartments() != null) {
mgr.getDepartments().clear();
}
}
pm.currentTransaction().commit();
// delete all Employee objects
pm.currentTransaction().begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
pm.deletePersistent(emp);
}
pm.currentTransaction().commit();
pm.currentTransaction().begin();
// dekete all Department objects
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
while (it.hasNext()) {
Department d = (Department) it.next();
pm.deletePersistent(d);
}
pm.currentTransaction().commit();
// delete all Manager objects
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
pm.deletePersistent(mgr);
}
pm.currentTransaction().commit();
// delete all Person objects
pm.currentTransaction().begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person person = (Person) it.next();
pm.deletePersistent(person);
}
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().commit();
pm.close();
}
}
use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.
the class PersistenceManagerTest method testNontransactionalUpdate.
/**
* Test for NontransactionalWrite updates.
*/
public void testNontransactionalUpdate() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Primitive p = null;
try {
BigDecimal bd = new BigDecimal("12345.12345");
BigInteger bi = new BigInteger("12345");
java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
tx.begin();
p = new Primitive();
setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
pm.makePersistent(p);
tx.setRetainValues(true);
tx.commit();
// Update the data without a transaction
tx.setNontransactionalRead(true);
tx.setNontransactionalWrite(true);
p.setIntObject(new Integer(1));
p.setShortObject(new Short((short) 2));
p = null;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
tx.setNontransactionalWrite(true);
try {
// Check read of persisted Primitive making sure that the previous write was persisted ok
Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
p = (Primitive) clnPrimitive.iterator().next();
assertNotNull(p);
assertEquals(1, p.getIntObject().intValue());
assertEquals(2, p.getShortObject().shortValue());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Primitive.class);
}
}
use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.
the class PersistenceManagerTest method testEvict.
/**
* Test for getEvict().
* @throws Exception
*/
public void testEvict() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
try {
// Test insert of native and native wrapper data types
BigDecimal bd = new BigDecimal("12345.12345");
BigInteger bi = new BigInteger("12345");
java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
tx.begin();
Primitive p = new Primitive();
setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
pm.makePersistent(p);
Object id = pm.getObjectId(p);
pm.evict(p);
tx.commit();
assertTrue(JDOHelper.isPersistent(p));
assertFalse(JDOHelper.isNew(p));
assertFalse(JDOHelper.isTransactional(p));
assertFalse(JDOHelper.isDirty(p));
Object o = pm.getObjectById(id, false);
assertTrue(p == o);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Primitive.class);
}
}
use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.
the class PersistenceManagerTest method testPCFieldAccess.
/**
* Test that transient fields are accessible both inside and outside transactions.
*/
public void testPCFieldAccess() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
Primitive p = new Primitive();
p.setTransient(100);
tx.begin();
p.setTransient(200);
pm.makePersistent(p);
p.setTransient(300);
tx.commit();
p.setTransient(400);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Primitive.class);
}
}
use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.
the class PersistenceManagerTest method testUpdatePersistentFields.
/**
* Simple test of transactional PC field updates.
*/
public void testUpdatePersistentFields() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
BigDecimal bd = new BigDecimal("12345.12345");
BigInteger bi = new BigInteger("12345");
java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
tx.begin();
Primitive p = new Primitive();
setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
pm.makePersistent(p);
tx.commit();
p = null;
// Retrieve the object and update some fields
tx.begin();
Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
p = (Primitive) clnPrimitive.iterator().next();
date1 = new java.util.Date(date1.getTime() + 10000);
date2 = java.sql.Date.valueOf("2001-01-02");
time3 = java.sql.Time.valueOf("10:01:59");
timestamp = java.sql.Timestamp.valueOf("2001-01-02 23:23:23.050500000");
setPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", "hugexyzabc", bd, bi, date1, date2, time3, timestamp);
tx.commit();
// test the results
tx.begin();
clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
p = (Primitive) clnPrimitive.iterator().next();
assertPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", "hugexyzabc", bd, bi, date1, date2, time3, timestamp);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Primitive.class);
}
}
Aggregations