[workspace model storage] add proper entities to change log in replaceBySource
authornik <Nikolay.Chashnikov@jetbrains.com>
Mon, 30 Dec 2019 14:39:16 +0000 (17:39 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 30 Dec 2019 15:37:06 +0000 (15:37 +0000)
Before the original instance of entity was added to change log, and this causes exceptions in code which tries to access references entities from EntityChange.Added::entity.

GitOrigin-RevId: 690f4d16d4f1b48fc3d19bfb8072ee84d230a0c4

platform/workspaceModel-core-tests/test/com/intellij/workspace/api/ReferencesInProxyBasedStorageTest.kt
platform/workspaceModel-core-tests/test/com/intellij/workspace/api/ReplaceBySourceTest.kt
platform/workspaceModel-core/src/ProxyBasedTypedEntityStore.kt

index 8dd7adc1d6d68963910377d5f72abebe07595ffc..5826f0580f7764ae8344dc9be1a2413390770c4c 100644 (file)
@@ -55,15 +55,17 @@ private interface ModifiableChildWithOptionalParentEntity : ChildWithOptionalPar
 }
 
 
-private fun TypedEntityStorageBuilder.addParentEntity(parentProperty: String = "parent") =
-  addEntity(ModifiableParentEntity::class.java, SampleEntitySource("test")) {
+internal fun TypedEntityStorageBuilder.addParentEntity(parentProperty: String = "parent",
+                                                       source: SampleEntitySource = SampleEntitySource("test")) =
+  addEntity(ModifiableParentEntity::class.java, source) {
     this.parentProperty = parentProperty
   }
 
-private fun TypedEntityStorageBuilder.addChildEntity(parentEntity: ParentEntity = addParentEntity(),
-                                                     childProperty: String = "child",
-                                                     dataClass: DataClass? = null) =
-  addEntity(ModifiableChildEntity::class.java, SampleEntitySource("test")) {
+internal fun TypedEntityStorageBuilder.addChildEntity(parentEntity: ParentEntity = addParentEntity(),
+                                                      childProperty: String = "child",
+                                                      dataClass: DataClass? = null,
+                                                      source: SampleEntitySource = SampleEntitySource("test")) =
+  addEntity(ModifiableChildEntity::class.java, source) {
     this.parent = parentEntity
     this.childProperty = childProperty
     this.dataClass = dataClass
@@ -75,9 +77,9 @@ private fun TypedEntityStorageBuilder.addChildChildEntity(parent1: ParentEntity,
     this.parent2 = parent2
   }
 
-internal fun TypedEntityStorageBuilder.addChildWithOptionalEntity(parentEntity: ParentEntity?,
-                                                                  childProperty: String = "child",
-                                                                  source: SampleEntitySource = SampleEntitySource("test")) =
+internal fun TypedEntityStorageBuilder.addChildWithOptionalParentEntity(parentEntity: ParentEntity?,
+                                                                        childProperty: String = "child",
+                                                                        source: SampleEntitySource = SampleEntitySource("test")) =
   addEntity(ModifiableChildWithOptionalParentEntity::class.java, source) {
     this.optionalParent = parentEntity
     this.childProperty = childProperty
@@ -228,7 +230,7 @@ class ReferencesInProxyBasedStorageTest {
   @Test
   fun `modify optional parent property`() {
     val builder = TypedEntityStorageBuilder.create()
-    val child = builder.addChildWithOptionalEntity(null)
+    val child = builder.addChildWithOptionalParentEntity(null)
     assertNull(child.optionalParent)
     val newParent = builder.addParentEntity()
     assertEquals(emptyList<ChildWithOptionalParentEntity>(), newParent.optionalChildren.toList())
index c32f09754f5297b395918f05bfadf1cf36c42034..5dbff96f0a3e5833da718b84b35655758e5f9b62 100644 (file)
@@ -16,11 +16,29 @@ class ReplaceBySourceTest {
   }
 
   @Test
+  fun `add parent and child entities`() {
+    val builder = TypedEntityStorageBuilder.create()
+    builder.addParentEntity("parent2", SampleEntitySource("2"))
+    val original = builder.toStorage()
+    val replacement = TypedEntityStorageBuilder.create()
+    val parent = replacement.addParentEntity("parent", SampleEntitySource("1"))
+    replacement.addChildWithOptionalParentEntity(parentEntity = parent, childProperty = "child", source = SampleEntitySource("1"))
+    builder.replaceBySource({it == SampleEntitySource("1")}, replacement)
+    builder.checkConsistency()
+    assertEquals("parent", builder.entities(ChildWithOptionalParentEntity::class.java).single().optionalParent!!.parentProperty)
+    val changes = builder.collectChanges(original)
+    @Suppress("UNCHECKED_CAST")
+    val childFromLog = (changes.getValue(ChildWithOptionalParentEntity::class.java).single() as EntityChange.Added<ChildWithOptionalParentEntity>).entity
+    assertEquals("child", childFromLog.childProperty)
+    assertEquals("parent", childFromLog.optionalParent!!.parentProperty)
+  }
+
+  @Test
   fun `modify entity with optional reference`() {
     val builder = TypedEntityStorageBuilder.create()
-    builder.addChildWithOptionalEntity(null, "hello", SampleEntitySource("1"))
+    builder.addChildWithOptionalParentEntity(null, "hello", SampleEntitySource("1"))
     val replacement = TypedEntityStorageBuilder.create()
-    replacement.addChildWithOptionalEntity(null, "hello2", SampleEntitySource("1"))
+    replacement.addChildWithOptionalParentEntity(null, "hello2", SampleEntitySource("1"))
     builder.replaceBySource({it == SampleEntitySource("1")}, replacement)
     builder.checkConsistency()
     assertEquals("hello2", builder.entities(ChildWithOptionalParentEntity::class.java).single().childProperty)
index 1d133a1e89ec035a5e8d882d2ba2bda8bedb87ea..863cd012ac51ffab22352540776fb5df071fcb59 100644 (file)
@@ -673,7 +673,7 @@ internal class TypedEntityStorageBuilderImpl(override val entitiesByType: Mutabl
     replaceMap[newData.id] = id
     copyEntityProperties(data, newData, replaceMap.inverse())
     addEntity(newData, null, handleReferrers = true)
-    updateChangeLog { it.add(ChangeEntry.AddEntity(data)) }
+    updateChangeLog { it.add(ChangeEntry.AddEntity(newData)) }
   }
 
   private fun copyEntityProperties(source: EntityData, dest: EntityData, replaceMap: Map<Long, Long>) {