use of org.datanucleus.samples.detach.ClassWithNonPCCollection in project tests by datanucleus.
the class AttachDetachTest method testDetachCollectionWithNonPCElements.
/**
* Test of detach/attach with a Collection of non-PC objects.
* TODO Change the sample to a generic collection of non-PC to be used everywhere
*/
public void testDetachCollectionWithNonPCElements() {
try {
Object objectId = null;
ClassWithNonPCCollection obj1 = new ClassWithNonPCCollection();
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
// test detach and attach
tx.begin();
obj1.getElements().add("elem1");
obj1.getElements().add("elem2");
pm.makePersistent(obj1);
tx.commit();
objectId = pm.getObjectId(obj1);
// test detach and attach
tx.begin();
obj1 = (ClassWithNonPCCollection) pm.getObjectById(objectId, true);
pm.getFetchPlan().addGroup("detach");
obj1 = (ClassWithNonPCCollection) pm.detachCopy(obj1);
assertEquals("wrong number of detached non pc elements", 2, obj1.getElements().size());
assertEquals("wrong element of detached non pc element", "elem1", obj1.getElements().get(0));
assertEquals("wrong element of detached non pc element", "elem2", obj1.getElements().get(1));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
} finally {
clean(ClassWithNonPCCollection.class);
}
}
use of org.datanucleus.samples.detach.ClassWithNonPCCollection in project tests by datanucleus.
the class AttachDetachTest method testAttachCleanCollectionWithNonPCElements.
/**
* Test of attaching an object with a collection that has no changed elements and
* where the elements of the collection are non-PC.
*/
public void testAttachCleanCollectionWithNonPCElements() {
try {
Object objectId = null;
ClassWithNonPCCollection obj1 = new ClassWithNonPCCollection();
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
// test detach and attach
tx.begin();
obj1.getElements().add("elem1");
obj1.getElements().add("elem2");
pm.makePersistent(obj1);
tx.commit();
objectId = pm.getObjectId(obj1);
// detach
tx.begin();
obj1 = (ClassWithNonPCCollection) pm.getObjectById(objectId, true);
pm.getFetchPlan().addGroup("detach");
obj1 = (ClassWithNonPCCollection) pm.detachCopy(obj1);
tx.commit();
// add a storeLifeCycleListener to check what gets stored
final Collection<Object> storedElements = new ArrayList<>();
pm.addInstanceLifecycleListener(new StoreLifecycleListener() {
public void preStore(InstanceLifecycleEvent event) {
}
public void postStore(InstanceLifecycleEvent event) {
storedElements.add(event.getSource());
}
});
// attach the unchanged Object
tx.begin();
obj1 = (ClassWithNonPCCollection) pm.makePersistent(obj1);
tx.commit();
// test that nothing has been stored
assertTrue(storedElements.isEmpty());
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(ClassWithNonPCCollection.class);
}
}
Aggregations