From 3f6e45534e57142673dac601b5f9ea817482d1cd Mon Sep 17 00:00:00 2001 From: Alex Plate Date: Tue, 2 Jun 2020 20:10:31 +0300 Subject: [PATCH] [Workspace Model] Add new tests for soft links GitOrigin-RevId: a919775d70020d2d6fb60e5550e22b9fe4e1d823 --- .../storage/ReplaceBySourceTest.kt | 23 +++++-- .../workspaceModel/storage/SoftLinksTest.kt | 60 +++++++++++++++++-- .../storage/entities/EntitiesWithSoftLinks.kt | 56 +++++++++++++++-- 3 files changed, 125 insertions(+), 14 deletions(-) diff --git a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/ReplaceBySourceTest.kt b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/ReplaceBySourceTest.kt index 852e0072f860..f6e076dfa18f 100644 --- a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/ReplaceBySourceTest.kt +++ b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/ReplaceBySourceTest.kt @@ -3,12 +3,8 @@ package com.intellij.workspaceModel.storage import com.intellij.testFramework.UsefulTestCase.assertEmpty import com.intellij.testFramework.UsefulTestCase.assertOneElement -import com.intellij.workspaceModel.storage.impl.WorkspaceEntityStorageBuilderImpl import com.intellij.workspaceModel.storage.entities.* -import com.intellij.workspaceModel.storage.entities.ModifiableNamedEntity -import com.intellij.workspaceModel.storage.entities.ModifiableWithListSoftLinksEntity -import com.intellij.workspaceModel.storage.entities.NamedEntity -import com.intellij.workspaceModel.storage.entities.WithSoftLinkEntity +import com.intellij.workspaceModel.storage.impl.WorkspaceEntityStorageBuilderImpl import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test @@ -320,6 +316,23 @@ class ReplaceBySourceTest { builder.assertConsistency() } + @Test + fun `replace by source with composite id`() { + val builder = WorkspaceEntityStorageBuilderImpl.create() + val replacement = WorkspaceEntityStorageBuilderImpl.create() + val namedEntity = replacement.addNamedEntity("MyName") + val composedEntity = replacement.addComposedIdSoftRefEntity("AnotherName", namedEntity.persistentId()) + replacement.addWithSoftLinkEntity(composedEntity.persistentId()) + + replacement.assertConsistency() + builder.replaceBySource({ true }, replacement) + builder.assertConsistency() + + assertOneElement(builder.entities(NamedEntity::class.java).toList()) + assertOneElement(builder.entities(ComposedIdSoftRefEntity::class.java).toList()) + assertOneElement(builder.entities(WithSoftLinkEntity::class.java).toList()) + } + /* Not sure if this should work this way. We can track persistentId changes in builder, but what if we perform replaceBySource with storage? @Test diff --git a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/SoftLinksTest.kt b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/SoftLinksTest.kt index 15e321ccc7aa..ded3a9a261d3 100644 --- a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/SoftLinksTest.kt +++ b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/SoftLinksTest.kt @@ -2,14 +2,17 @@ package com.intellij.workspaceModel.storage import com.intellij.testFramework.UsefulTestCase.assertOneElement +import com.intellij.workspaceModel.storage.entities.* import com.intellij.workspaceModel.storage.impl.WorkspaceEntityStorageBuilderImpl -import com.intellij.workspaceModel.storage.entities.ModifiableNamedEntity -import com.intellij.workspaceModel.storage.entities.ModifiableWithSoftLinkEntity -import com.intellij.workspaceModel.storage.entities.MySource -import com.intellij.workspaceModel.storage.entities.NameId +import junit.framework.Assert.assertEquals import junit.framework.Assert.assertNotNull import org.junit.Test +/** + * Soft reference + * Persistent id via soft reference + * Persistent id via strong reference + */ class SoftLinksTest { @Test fun `test add diff with soft links`() { @@ -82,4 +85,53 @@ class SoftLinksTest { assertNotNull(builder.resolve(NameId(id))) assertOneElement(builder.indexes.softLinks.getValues(NameId(id))) } + + @Test + fun `change persistent id part`() { + val builder = WorkspaceEntityStorageBuilderImpl.create() + val entity = builder.addNamedEntity("Name") + builder.addWithSoftLinkEntity(entity.persistentId()) + + builder.modifyEntity(ModifiableNamedEntity::class.java, entity) { + this.name = "newName" + } + + builder.assertConsistency() + + assertEquals("newName", builder.entities(WithSoftLinkEntity::class.java).single().link.presentableName) + } + + @Test + fun `change persistent id part of composed id entity`() { + val builder = WorkspaceEntityStorageBuilderImpl.create() + val entity = builder.addNamedEntity("Name") + builder.addComposedIdSoftRefEntity("AnotherName", entity.persistentId()) + + builder.modifyEntity(ModifiableNamedEntity::class.java, entity) { + this.name = "newName" + } + + builder.assertConsistency() + + val updatedPersistentId = builder.entities(ComposedIdSoftRefEntity::class.java).single().persistentId() + assertEquals("newName", updatedPersistentId.link.presentableName) + } + + @Test + fun `change persistent id part of composed id entity and with linked entity`() { + val builder = WorkspaceEntityStorageBuilderImpl.create() + val entity = builder.addNamedEntity("Name") + val composedIdEntity = builder.addComposedIdSoftRefEntity("AnotherName", entity.persistentId()) + builder.addWithSoftLinkEntity(composedIdEntity.persistentId()) + + builder.modifyEntity(ModifiableNamedEntity::class.java, entity) { + this.name = "newName" + } + + builder.assertConsistency() + + val updatedPersistentId = builder.entities(ComposedIdSoftRefEntity::class.java).single().persistentId() + assertEquals("newName", updatedPersistentId.link.presentableName) + assertEquals("newName", (builder.entities(WithSoftLinkEntity::class.java).single().link as ComposedId).link.presentableName) + } } \ No newline at end of file diff --git a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/entities/EntitiesWithSoftLinks.kt b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/entities/EntitiesWithSoftLinks.kt index 476a812dd068..2598ec5ac14a 100644 --- a/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/entities/EntitiesWithSoftLinks.kt +++ b/platform/workspaceModel/storage/tests/testSrc/com/intellij/workspaceModel/storage/entities/EntitiesWithSoftLinks.kt @@ -29,6 +29,13 @@ internal data class AnotherNameId(private val name: String) : PersistentEntityId override fun toString(): String = name } +internal data class ComposedId(internal val name: String, internal val link: NameId) : PersistentEntityId() { + override val parentId: PersistentEntityId<*>? + get() = null + override val presentableName: String + get() = "$name - ${link.presentableName}" +} + // ------------------------------ Entity With Persistent Id ------------------ internal class NamedEntityData : WorkspaceEntityData.WithCalculablePersistentId() { @@ -59,7 +66,7 @@ internal fun WorkspaceEntityStorageBuilderImpl.addNamedEntity(name: String, sour internal class WithSoftLinkEntityData : WorkspaceEntityData(), SoftLinkable { - lateinit var link: NameId + lateinit var link: PersistentEntityId<*> override fun createEntity(snapshot: WorkspaceEntityStorage): WithSoftLinkEntity { return WithSoftLinkEntity(link).also { addMetaData(it, snapshot) } @@ -70,18 +77,18 @@ internal class WithSoftLinkEntityData : WorkspaceEntityData( override fun updateLink(oldLink: PersistentEntityId<*>, newLink: PersistentEntityId<*>, affectedIds: MutableList, PersistentEntityId<*>>>): Boolean { - this.link = newLink as NameId + this.link = newLink return true } } -internal class WithSoftLinkEntity(val link: NameId) : WorkspaceEntityBase() +internal class WithSoftLinkEntity(val link: PersistentEntityId<*>) : WorkspaceEntityBase() internal class ModifiableWithSoftLinkEntity : ModifiableWorkspaceEntityBase() { - var link: NameId by EntityDataDelegation() + var link: PersistentEntityId<*> by EntityDataDelegation() } -internal fun WorkspaceEntityStorageBuilderImpl.addWithSoftLinkEntity(link: NameId, source: EntitySource = MySource) = +internal fun WorkspaceEntityStorageBuilderImpl.addWithSoftLinkEntity(link: PersistentEntityId<*>, source: EntitySource = MySource) = addEntity(ModifiableWithSoftLinkEntity::class.java, source) { this.link = link } @@ -124,3 +131,42 @@ internal fun WorkspaceEntityStorageBuilderImpl.addWithListSoftLinksEntity(name: this.name = name this.links = links } + +// --------------------------- Entity with composed persistent id via soft reference ------------------ + +internal class ComposedIdSoftRefEntityData: WorkspaceEntityData.WithCalculablePersistentId(), SoftLinkable { + lateinit var name: String + lateinit var link: NameId + + override fun createEntity(snapshot: WorkspaceEntityStorage): ComposedIdSoftRefEntity { + return ComposedIdSoftRefEntity(name, link).also { addMetaData(it, snapshot) } + } + + override fun getLinks(): Set> = setOf(link) + + override fun updateLink(oldLink: PersistentEntityId<*>, + newLink: PersistentEntityId<*>, + affectedIds: MutableList, PersistentEntityId<*>>>): Boolean { + if (oldLink != link) return false + this.link = newLink as NameId + return true + } + + override fun persistentId() = ComposedId(name, link) +} + +internal class ComposedIdSoftRefEntity(val name: String, val link: NameId) : WorkspaceEntityBase(), WorkspaceEntityWithPersistentId { + override fun persistentId(): ComposedId = ComposedId(name, link) +} + +internal class ModifiableComposedIdSoftRefEntity : ModifiableWorkspaceEntityBase() { + var name: String by EntityDataDelegation() + var link: NameId by EntityDataDelegation() +} + +internal fun WorkspaceEntityStorageBuilderImpl.addComposedIdSoftRefEntity(name: String, link: NameId, source: EntitySource = MySource): ComposedIdSoftRefEntity { + return addEntity(ModifiableComposedIdSoftRefEntity::class.java, source) { + this.name = name + this.link = link + } +}