From 752764e158300d45f6196faa64690d88dca3a02c Mon Sep 17 00:00:00 2001 From: Jan Wittler Date: Fri, 28 Oct 2022 15:50:02 +0200 Subject: [PATCH] fix change recorder tests for uuid resolver --- .../change/atomic/EChangeIdManager.xtend | 27 ++++++++++++++----- .../composite/recording/ChangeRecorder.xtend | 14 +++------- .../recording/ChangeRecorderTest.xtend | 14 +++++++--- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/bundles/tools.vitruv.change.atomic/src/tools/vitruv/change/atomic/EChangeIdManager.xtend b/bundles/tools.vitruv.change.atomic/src/tools/vitruv/change/atomic/EChangeIdManager.xtend index 4dc4012b..9dc8d6f4 100644 --- a/bundles/tools.vitruv.change.atomic/src/tools/vitruv/change/atomic/EChangeIdManager.xtend +++ b/bundles/tools.vitruv.change.atomic/src/tools/vitruv/change/atomic/EChangeIdManager.xtend @@ -9,6 +9,8 @@ import static com.google.common.base.Preconditions.checkArgument import static com.google.common.base.Preconditions.checkState import tools.vitruv.change.atomic.id.IdResolver import tools.vitruv.change.atomic.uuid.UuidResolver +import tools.vitruv.change.atomic.eobject.DeleteEObject +import tools.vitruv.change.atomic.eobject.CreateEObject /** * Provides logic for initializing the IDs within changes. @@ -29,6 +31,8 @@ class EChangeIdManager { def void setOrGenerateIds(EChange eChange) { switch eChange { + CreateEObject: + setOrGenerateCreatedEObjectId(eChange) EObjectExistenceEChange: setAffectedEObjectId(eChange) FeatureEChange: @@ -49,16 +53,21 @@ class EChangeIdManager { checkState(id !== null, "id must not be null") return id } + + private def String getOrGenerateId(EObject object) { + try { + getId(object) + } + catch (IllegalStateException e) { + uuidResolver.registerEObject(object) + } + } private def void setOrGenerateNewValueId(EObjectAddedEChange addedEChange) { if (addedEChange.newValue === null) { return; } - try { - addedEChange.newValueID = addedEChange.newValue.id - } catch (IllegalStateException e) { - addedEChange.newValueID = uuidResolver.generateUuid(addedEChange.newValue) - } + addedEChange.newValueID = addedEChange.newValue.getOrGenerateId } private def void setOldValueId(EObjectSubtractedEChange subtractedEChange) { @@ -71,7 +80,13 @@ class EChangeIdManager { private def void setAffectedEObjectId(EObjectExistenceEChange existenceChange) { val affectedEObject = existenceChange.affectedEObject checkArgument(affectedEObject !== null, "existence change must have an affected EObject: %s", existenceChange) - existenceChange.affectedEObjectID = affectedEObject.id + existenceChange.affectedEObjectID = affectedEObject.id + } + + private def void setOrGenerateCreatedEObjectId(CreateEObject existenceChange) { + val affectedEObject = existenceChange.affectedEObject + checkArgument(affectedEObject !== null, "existence change must have an affected EObject: %s", existenceChange) + existenceChange.affectedEObjectID = affectedEObject.getOrGenerateId } private def void setAffectedEObjectId(FeatureEChange featureChange) { diff --git a/bundles/tools.vitruv.change.composite/src/tools/vitruv/change/composite/recording/ChangeRecorder.xtend b/bundles/tools.vitruv.change.composite/src/tools/vitruv/change/composite/recording/ChangeRecorder.xtend index 29ba34d0..0533157e 100644 --- a/bundles/tools.vitruv.change.composite/src/tools/vitruv/change/composite/recording/ChangeRecorder.xtend +++ b/bundles/tools.vitruv.change.composite/src/tools/vitruv/change/composite/recording/ChangeRecorder.xtend @@ -63,8 +63,8 @@ class ChangeRecorder implements AutoCloseable { new(ResourceSet resourceSet, UuidResolver uuidResolver) { this.resourceSet = resourceSet - this.uuidResolver = uuidResolver; - this.eChangeIdManager = new EChangeIdManager(this.uuidResolver); + this.uuidResolver = uuidResolver + this.eChangeIdManager = new EChangeIdManager(this.uuidResolver) this.converter = new NotificationToEChangeConverter([ affectedObject, addedObject | isCreateChange(affectedObject, addedObject) ]) @@ -94,7 +94,7 @@ class ChangeRecorder implements AutoCloseable { checkNotDisposed() checkNotNull(notifier, "notifier") checkArgument(notifier.isInOurResourceSet, - "cannot record changes in a different resource set than that of our ID resolver!") + "cannot record changes in a different resource set than that of our UUID resolver!") if (rootObjects += notifier) { notifier.recursively [ @@ -156,16 +156,10 @@ class ChangeRecorder implements AutoCloseable { } def private List assignIds(List changes) { - changes.toList.reverseView.forEach[applyBackward] - changes.forEach[assignIds] + changes.forEach[eChangeIdManager.setOrGenerateIds(it)] changes } - def private void assignIds(EChange change) { - eChangeIdManager.setOrGenerateIds(change) - change.applyForward(uuidResolver) - } - def private postprocessRemovals(List changes) { if(changes.isEmpty) return changes diff --git a/tests/tools.vitruv.change.composite.tests/src/tools/vitruv/change/composite/recording/ChangeRecorderTest.xtend b/tests/tools.vitruv.change.composite.tests/src/tools/vitruv/change/composite/recording/ChangeRecorderTest.xtend index 09d0e24a..5ec20716 100644 --- a/tests/tools.vitruv.change.composite.tests/src/tools/vitruv/change/composite/recording/ChangeRecorderTest.xtend +++ b/tests/tools.vitruv.change.composite.tests/src/tools/vitruv/change/composite/recording/ChangeRecorderTest.xtend @@ -46,7 +46,8 @@ class ChangeRecorderTest { // this test only covers general behaviour of ChangeRecorder. Whether it always produces correct change sequences // is covered by other tests val ResourceSet resourceSet = new ResourceSetImpl().withGlobalFactories() - var ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet, UuidResolver.create(resourceSet)) + val uuidResolver = UuidResolver.create(resourceSet) + val ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet, uuidResolver) private def T wrapIntoRecordedResource(T object) { val resource = resourceSet.createResource(URI.createURI('test://test.aet')) @@ -80,7 +81,7 @@ class ChangeRecorderTest { @Test @DisplayName("records direct changes to an object") def void recordOnObject() { - val root = aet.Root + val root = aet.Root.withUuid changeRecorder.addToRecording(root) record [ root.id = 'test' @@ -361,8 +362,8 @@ class ChangeRecorderTest { def void recordsOnDeletedResource(@TestProject Path testDir) { changeRecorder.addToRecording(resourceSet) val resource = resourceSet.createResource(URI.createFileURI(testDir.resolve("test.aet").toString)) => [ - contents += aet.Root => [ - singleValuedContainmentEReference = aet.NonRoot + contents += aet.Root.withUuid => [ + singleValuedContainmentEReference = aet.NonRoot.withUuid ] ] record [ @@ -833,6 +834,11 @@ class ChangeRecorderTest { def private static hasNoChanges() { new EChangeSequenceMatcher(emptyList) } + + def private withUuid(O eObject) { + uuidResolver.registerEObject(eObject) + eObject + } @FinalFieldsConstructor private static class EChangeSequenceMatcher extends TypeSafeMatcher {