@Test
public void testPrimaryObjectWithProxiesDoesNotResolveThemOnSave() throws IOException
{
ResourceSet resourceSet = createResourceSet();
PrimaryObject primaryObject = ModelFactory.eINSTANCE.createPrimaryObject();
primaryObject.setName("junit");
{
TargetObject targetObject = ModelFactory.eINSTANCE.createTargetObject();
targetObject.setSingleAttribute("one");
saveObject(resourceSet, targetObject);
primaryObject.setSingleContainmentReferenceProxies(targetObject);
targetObject.eResource().unload();
}
{
TargetObject targetObject = ModelFactory.eINSTANCE.createTargetObject();
targetObject.setSingleAttribute("one");
saveObject(resourceSet, targetObject);
primaryObject.setSingleNonContainmentReference(targetObject);
targetObject.eResource().unload();
}
{
TargetObject targetObject = ModelFactory.eINSTANCE.createTargetObject();
targetObject.setSingleAttribute("one");
saveObject(resourceSet, targetObject);
primaryObject.getMultipleContainmentReferenceProxies().add(targetObject);
targetObject.eResource().unload();
}
{
TargetObject targetObject = ModelFactory.eINSTANCE.createTargetObject();
targetObject.setSingleAttribute("one");
saveObject(resourceSet, targetObject);
primaryObject.getMultipleNonContainmentReference().add(targetObject);
targetObject.eResource().unload();
}
{
PrimaryObject targetObject = ModelFactory.eINSTANCE.createPrimaryObject();
targetObject.setName("target");
saveObject(targetObject);
primaryObject.setContainmentReferenceSameCollectioin(targetObject);
targetObject.eResource().unload();
}
saveObject(resourceSet, primaryObject);
// Verify that proxies aren't resolved as a result of saving to Mongo DB.
//
assertTrue(((EObject) primaryObject.eGet(ModelPackage.Literals.PRIMARY_OBJECT__CONTAINMENT_REFERENCE_SAME_COLLECTIOIN, false)).eIsProxy());
assertTrue(((EObject) primaryObject.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_CONTAINMENT_REFERENCE_PROXIES, false)).eIsProxy());
assertTrue(((EObject) primaryObject.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_NON_CONTAINMENT_REFERENCE, false)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_CONTAINMENT_REFERENCE_PROXIES)).basicGet(0)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_NON_CONTAINMENT_REFERENCE)).basicGet(0)).eIsProxy());
ResourceSet resourceSet2 = createResourceSet();
resourceSet2.getLoadOptions().put(Options.OPTION_PROXY_ATTRIBUTES, Boolean.TRUE);
Resource primaryResource2 = resourceSet2.getResource(primaryObject.eResource().getURI(), true);
PrimaryObject primaryObject2 = (PrimaryObject) primaryResource2.getContents().get(0);
// Verify that proxies are created when loading from Mongo DB.
//
assertTrue(((EObject) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_CONTAINMENT_REFERENCE_PROXIES, false)).eIsProxy());
assertTrue(((EObject) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_NON_CONTAINMENT_REFERENCE, false)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_CONTAINMENT_REFERENCE_PROXIES)).basicGet(0)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_NON_CONTAINMENT_REFERENCE)).basicGet(0)).eIsProxy());
// Verify that those proxies have attributes populated as expected for OPTION_PROXY_ATTRIBUTES
//
assertThat(((TargetObject) ((EObject) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_CONTAINMENT_REFERENCE_PROXIES, false))).getSingleAttribute(), is("one"));
assertThat(((TargetObject) ((EObject) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_NON_CONTAINMENT_REFERENCE, false))).getSingleAttribute(), is("one"));
assertThat(((TargetObject) ((EObject) ((InternalEList<?>) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_CONTAINMENT_REFERENCE_PROXIES)).basicGet(0))).getSingleAttribute(),
is("one"));
assertThat(((TargetObject) ((EObject) ((InternalEList<?>) primaryObject2.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_NON_CONTAINMENT_REFERENCE)).basicGet(0))).getSingleAttribute(),
is("one"));
// Save it to XML, again with proxy attributes.
//
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_PROXY_ATTRIBUTES, Boolean.TRUE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
primaryResource2.save(out, options);
ResourceSet resourceSet3 = createResourceSet();
Resource primaryResource3 = resourceSet3.createResource(primaryObject.eResource().getURI());
primaryResource3.load(new ByteArrayInputStream(out.toByteArray()), null);
// Verify that proxies are created when loading from XML.
//
PrimaryObject primaryObject3 = (PrimaryObject) primaryResource3.getContents().get(0);
assertTrue(((EObject) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_CONTAINMENT_REFERENCE_PROXIES, false)).eIsProxy());
assertTrue(((EObject) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_NON_CONTAINMENT_REFERENCE, false)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_CONTAINMENT_REFERENCE_PROXIES)).basicGet(0)).eIsProxy());
assertTrue(((EObject) ((InternalEList<?>) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_NON_CONTAINMENT_REFERENCE)).basicGet(0)).eIsProxy());
// Verify that those proxies have attributes populated as expected for OPTION_PROXY_ATTRIBUTES.
//
assertThat(((TargetObject) ((EObject) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_CONTAINMENT_REFERENCE_PROXIES, false))).getSingleAttribute(), is("one"));
assertThat(((TargetObject) ((EObject) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__SINGLE_NON_CONTAINMENT_REFERENCE, false))).getSingleAttribute(), is("one"));
assertThat(((TargetObject) ((EObject) ((InternalEList<?>) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_CONTAINMENT_REFERENCE_PROXIES)).basicGet(0))).getSingleAttribute(),
is("one"));
assertThat(((TargetObject) ((EObject) ((InternalEList<?>) primaryObject3.eGet(ModelPackage.Literals.PRIMARY_OBJECT__MULTIPLE_NON_CONTAINMENT_REFERENCE)).basicGet(0))).getSingleAttribute(),
is("one"));
}