public void testSerializedObjectsRefreshWithProxy()
{
String prefix = "testSerializedObjectsRefreshWithProxy_" + System.currentTimeMillis() + "_";
ClassDescriptor cld = broker.getClassDescriptor(ObjectRepository.Component.class);
ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName("parentComponent");
boolean oldState = ord.isLazy();
try
{
ord.setLazy(true);
ObjectRepository.ComponentIF parent = new ObjectRepository.Component();
parent.setName(prefix + "main_component");
ObjectRepository.ComponentIF compSub1 = new ObjectRepository.Component();
compSub1.setName(prefix + "sub_1");
ObjectRepository.ComponentIF compSub2 = new ObjectRepository.Component();
compSub2.setName(prefix + "sub_2");
ObjectRepository.ComponentIF compSub3 = new ObjectRepository.Component();
compSub2.setName(prefix + "sub_3");
ObjectRepository.Group group = new ObjectRepository.Group();
group.setName(prefix + "test_group");
compSub1.setParentComponent(parent);
compSub2.setParentComponent(parent);
compSub3.setParentComponent(parent);
ArrayList list = new ArrayList();
list.add(compSub1);
list.add(compSub2);
list.add(compSub3);
parent.setChildComponents(list);
parent.setGroup(group);
broker.beginTransaction();
broker.store(parent);
broker.commitTransaction();
broker.clearCache();
Query query = QueryFactory.newQueryByIdentity(parent);
parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
Query groupQuery = QueryFactory.newQueryByIdentity(group);
ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
assertNotNull(parent);
assertNotNull(parent.getGroup());
assertNotNull(parent.getChildComponents());
assertNotNull(parent.getName());
assertEquals(3, parent.getChildComponents().size());
assertEquals(group.getName(), (parent.getGroup().getName()));
ObjectRepository.ComponentIF aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
assertNotNull(aChild);
assertNotNull(aChild.getParentComponent());
assertEquals(parent, aChild.getParentComponent());
assertNotNull(lookedUpGroup);
//*************************************
assertNotNull(parent);
assertNotNull(parent.getGroup());
parent = (ObjectRepository.ComponentIF) serializeDeserializeObject(parent);
broker.retrieveAllReferences(parent);
assertNotNull(parent);
/*
Now we have a problem! After serialization we can't find the anonymous keys
for parent object, because object identity has changed!!
This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
*/
assertNotNull(parent.getGroup());
//*************************************
assertNotNull(parent.getChildComponents());
assertNotNull(parent.getName());
assertEquals(3, parent.getChildComponents().size());
aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
assertNotNull(aChild);
assertNotNull(aChild.getParentComponent());
assertEquals(parent, aChild.getParentComponent());
broker.beginTransaction();
broker.store(parent);
broker.commitTransaction();
// now nothing should happen, because we don't make any changes
broker.clearCache();
query = QueryFactory.newQueryByIdentity(parent);
parent = (ObjectRepository.ComponentIF) broker.getObjectByQuery(query);
groupQuery = QueryFactory.newQueryByIdentity(group);
lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
assertNotNull(parent);
assertNotNull(parent.getGroup());
assertNotNull(parent.getChildComponents());
assertNotNull(parent.getName());
assertEquals(3, parent.getChildComponents().size());
assertEquals(group.getName(), (parent.getGroup().getName()));
aChild = (ObjectRepository.ComponentIF) parent.getChildComponents().iterator().next();
assertNotNull(aChild);
assertNotNull(aChild.getParentComponent());
assertEquals(parent, aChild.getParentComponent());
assertNotNull(lookedUpGroup);
}
finally
{
ord.setLazy(oldState);
}
}