RefreshInstanceMerger { void mergeChanges(ObjectChangeSet changeSet) { final Object source = changeSet.getChangedObject(); final Object target = changeSet.getCloneObject(); for (ChangeRecord change : changeSet.getChanges()) { final FieldSpecification<?, ?> att = change.getAttribute(); final Object sourceValue = EntityPropertiesUtils.getAttributeValue(att, source); if (sourceValue instanceof IndirectCollection) { final IndirectCollection col = (IndirectCollection) sourceValue; final Object ic = indirectWrapperHelper .createIndirectWrapper(col.unwrap(), target, att.getJavaField()); merger.mergeValue(att, target, null, ic, null); } else { merger.mergeValue(att, target, null, sourceValue, null); } } } RefreshInstanceMerger(IndirectWrapperHelper indirectWrapperHelper);   }
@Test public void mergeChangesOverwritesSingularAttributeChangesWithSourceValues() throws Exception { final OWLClassA original = Generators.generateOwlClassAInstance(); final OWLClassA clone = new OWLClassA(original.getUri()); clone.setStringAttribute("changedString"); clone.setTypes(new HashSet<>(original.getTypes())); final ObjectChangeSet changeSet = ChangeSetFactory.createObjectChangeSet(original, clone, new EntityDescriptor()); final FieldSpecification fieldSpec = mock(FieldSpecification.class); when(fieldSpec.getJavaField()).thenReturn(OWLClassA.getStrAttField()); changeSet.addChangeRecord(new ChangeRecordImpl(fieldSpec, clone.getStringAttribute())); merger.mergeChanges(changeSet); assertEquals(original.getStringAttribute(), clone.getStringAttribute()); }
@Test public void mergeChangesReplacesCollectionWithNewOneWithSourceValues() throws Exception { final OWLClassA original = Generators.generateOwlClassAInstance(); original.setTypes(new IndirectSet<>(original, OWLClassA.getTypesField(), uowMock, original.getTypes())); final OWLClassA clone = new OWLClassA(original.getUri()); clone.setTypes(new HashSet<>(original.getTypes())); final ObjectChangeSet changeSet = ChangeSetFactory.createObjectChangeSet(original, clone, new EntityDescriptor()); final TypesSpecification fieldSpec = mock(TypesSpecification.class); when(fieldSpec.isCollection()).thenReturn(true); when(fieldSpec.getJavaField()).thenReturn(OWLClassA.getTypesField()); changeSet.addChangeRecord(new ChangeRecordImpl(fieldSpec, clone.getTypes())); merger.mergeChanges(changeSet); assertTrue(clone.getTypes() instanceof IndirectSet); assertEquals(original.getTypes(), clone.getTypes()); final Field ownerField = IndirectCollection.class.getDeclaredField("owner"); ownerField.setAccessible(true); assertEquals(clone, ownerField.get(clone.getTypes())); }
@Test public void mergeChangesReplacesObjectPropertyCollectionWithSourceValues() throws Exception { final OWLClassC original = new OWLClassC(Generators.createIndividualIdentifier()); final OWLClassC clone = new OWLClassC(original.getUri()); final List<OWLClassA> refList = IntStream.range(0, 5).mapToObj(i -> Generators.generateOwlClassAInstance()) .collect( Collectors.toList()); final List<OWLClassA> refListClone = new ArrayList<>(refList); original.setReferencedList(new IndirectList<>(original, OWLClassC.getRefListField(), uowMock, refList)); clone.setReferencedList(new IndirectList<>(clone, OWLClassC.getRefListField(), uowMock, refListClone)); clone.getReferencedList().add(Generators.generateOwlClassAInstance()); final Attribute att = mock(Attribute.class); when(att.isCollection()).thenReturn(true); when(att.getJavaField()).thenReturn(OWLClassC.getRefListField()); final ObjectChangeSet changeSet = ChangeSetFactory.createObjectChangeSet(original, clone, new EntityDescriptor()); changeSet.addChangeRecord(new ChangeRecordImpl(att, refListClone)); merger.mergeChanges(changeSet); assertEquals(refList.size(), clone.getReferencedList().size()); assertTrue(clone.getReferencedList() instanceof IndirectList); final Field ownerField = IndirectCollection.class.getDeclaredField("owner"); ownerField.setAccessible(true); assertEquals(clone, ownerField.get(clone.getReferencedList())); }