use of org.jpox.samples.objects.ObjectHolder in project tests by datanucleus.
the class ObjectsTest method testOneToOneRelation.
/**
* Test for 1-1 relations where 1 side is marked as an Object, though is
* really PersistenceCapable.
*/
public void testOneToOneRelation() {
try {
Object holderId = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects.
tx.begin();
ObjectImpl1 impl1 = new ObjectImpl1("First implementation");
ObjectHolder holder = new ObjectHolder("First Holder");
holder.setObject3(impl1);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of java.lang.Object objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the holder and check its contents
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
assertTrue("Holder was not retrieved correctly", holder != null);
assertTrue("Holder nonserialised object is null!", holder.getObject3() != null);
assertEquals("Holder nonserialised object was of incorrect type", holder.getObject3().getClass().getName(), "org.jpox.samples.objects.ObjectImpl1");
assertEquals("Holder nonserialised object has incorrect name!", ((ObjectImpl1) holder.getObject3()).getName(), "First implementation");
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of java.lang.Object objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the object field to use a different object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
holder.setObject3(new ObjectImpl1("Second implementation"));
tx.commit();
} catch (JDOUserException ue) {
fail("Exception thrown during update of object container with new object reference : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the container and check its contents
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
assertTrue("Holder was not retrieved correctly", holder != null);
assertTrue("Holder nonserialised object is null!", holder.getObject3() != null);
assertEquals("Holder nonserialised object was of incorrect type", holder.getObject3().getClass().getName(), "org.jpox.samples.objects.ObjectImpl1");
assertEquals("Holder nonserialised object has incorrect name!", ((ObjectImpl1) holder.getObject3()).getName(), "Second implementation");
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of java.lang.Object objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ObjectHolder.class);
clean(ObjectImpl1.class);
}
}
use of org.jpox.samples.objects.ObjectHolder in project tests by datanucleus.
the class ObjectsTest method testOneToManyRelationWithJoinTable.
/**
* Test for 1-N relations the collection is of Object (not embedded) using a Join table.
*/
public void testOneToManyRelationWithJoinTable() {
try {
Object holderId = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects.
tx.begin();
ObjectImpl1 impl1 = new ObjectImpl1("Second implementation");
ObjectHolder holder = new ObjectHolder("First Holder");
holder.getSet1().add(impl1);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of Collection of java.lang.Object objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ObjectHolder holder = (ObjectHolder) pm.getObjectById(holderId);
assertTrue("Holder was not retrieved correctly", holder != null);
assertEquals("Holder has incorrect number of objects", holder.getSet1().size(), 1);
Object obj = holder.getSet1().iterator().next();
assertEquals("Object contained in container is of incorrect type", obj.getClass(), org.jpox.samples.objects.ObjectImpl1.class);
ObjectImpl1 obj1 = (ObjectImpl1) obj;
assertEquals("Object container in container has incorrect name", obj1.getName(), "Second implementation");
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of container of java.lang.Object objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ObjectHolder.class);
clean(ObjectImpl1.class);
}
}
Aggregations