Skip to content

Commit

Permalink
fix change recorder tests for uuid resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Wittler committed Nov 8, 2022
1 parent 667ff04 commit 752764e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,6 +31,8 @@ class EChangeIdManager {

def void setOrGenerateIds(EChange eChange) {
switch eChange {
CreateEObject<?>:
setOrGenerateCreatedEObjectId(eChange)
EObjectExistenceEChange<?>:
setAffectedEObjectId(eChange)
FeatureEChange<?,?>:
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
])
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -156,16 +156,10 @@ class ChangeRecorder implements AutoCloseable {
}

def private List<EChange> assignIds(List<EChange> 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<EChange> changes) {
if(changes.isEmpty) return changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends EObject> T wrapIntoRecordedResource(T object) {
val resource = resourceSet.createResource(URI.createURI('test://test.aet'))
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -833,6 +834,11 @@ class ChangeRecorderTest {
def private static hasNoChanges() {
new EChangeSequenceMatcher(emptyList)
}

def private <O extends EObject> withUuid(O eObject) {
uuidResolver.registerEObject(eObject)
eObject
}

@FinalFieldsConstructor
private static class EChangeSequenceMatcher extends TypeSafeMatcher<TransactionalChange> {
Expand Down

0 comments on commit 752764e

Please sign in to comment.