[kpm] kgp-idea: Implement IdeaKpmProjectContainer

This container serves the purpose of transferring a already
serialized binary form of IdeaKpmProject to the IDE.

^KT-52568 In Progress
This commit is contained in:
sebastian.sellmair
2022-06-13 14:01:48 +02:00
committed by Space
parent 486c18bc21
commit 9a8832a241
2 changed files with 115 additions and 0 deletions
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("FunctionName")
package org.jetbrains.kotlin.gradle.kpm.idea
import java.io.Serializable
fun IdeaKpmProjectContainer(project: ByteArray): IdeaKpmProjectBinaryContainer {
return IdeaKpmProjectBinaryContainerImpl(project)
}
fun IdeaKpmProjectContainer(project: IdeaKpmProject): IdeaKpmProjectInstanceContainer {
return IdeaKpmProjectInstanceContainerImpl(project)
}
sealed interface IdeaKpmProjectContainer<T : Any> : Serializable {
val project: T
val binaryOrNull: ByteArray?
val instanceOrNull: IdeaKpmProject?
}
interface IdeaKpmProjectBinaryContainer : IdeaKpmProjectContainer<ByteArray> {
override val instanceOrNull: Nothing? get() = null
override val binaryOrNull: ByteArray get() = project
}
interface IdeaKpmProjectInstanceContainer : IdeaKpmProjectContainer<IdeaKpmProject> {
override val instanceOrNull: IdeaKpmProject get() = project
override val binaryOrNull: Nothing? get() = null
}
private data class IdeaKpmProjectBinaryContainerImpl(override val project: ByteArray) : IdeaKpmProjectBinaryContainer {
override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other !is IdeaKpmProjectBinaryContainer) return false
return other.project.contentEquals(this.project)
}
override fun hashCode(): Int {
return project.contentHashCode()
}
companion object {
const val serialVersionUID = 0L
}
}
private data class IdeaKpmProjectInstanceContainerImpl(
override val project: IdeaKpmProject
) : IdeaKpmProjectInstanceContainer {
companion object {
const val serialVersionUID = 0L
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.kpm.idea
import org.jetbrains.kotlin.gradle.kpm.idea.testFixtures.TestIdeaKpmInstances
import kotlin.test.*
class IdeaKpmProjectContainerTest {
@Test
fun `test - binary container - equality`() {
val container1 = IdeaKpmProjectContainer(byteArrayOf(1))
val container2 = IdeaKpmProjectContainer(byteArrayOf(1))
val container3 = IdeaKpmProjectContainer(byteArrayOf(1, 2))
assertEquals(container1, container2)
assertNotEquals(container2, container3)
}
@Test
fun `test - instance container - equality`() {
val container1 = IdeaKpmProjectContainer(TestIdeaKpmInstances.simpleProject)
val container2 = IdeaKpmProjectContainer(TestIdeaKpmInstances.simpleProject.copy())
val container3 = IdeaKpmProjectContainer(TestIdeaKpmInstances.simpleProject.copy(gradlePluginVersion = "some.other.version"))
assertEquals(container1, container2)
assertNotEquals(container2, container3)
}
@Test
fun `test - binary container - instanceOrNull`() {
assertNull(IdeaKpmProjectContainer(byteArrayOf()).instanceOrNull)
assertNotNull(IdeaKpmProjectBinaryContainer::class.java.getMethod("getInstanceOrNull"))
}
@Test
fun `test - instance container - instanceOrNull`() {
assertSame(TestIdeaKpmInstances.simpleProject, IdeaKpmProjectContainer(TestIdeaKpmInstances.simpleProject).instanceOrNull)
assertNotNull(IdeaKpmProjectInstanceContainer::class.java.getMethod("getInstanceOrNull"))
}
@Test
fun `test - binary container - binaryOrNull`() {
val binary = byteArrayOf()
assertEquals(binary, IdeaKpmProjectContainer(binary).binaryOrNull)
assertNotNull(IdeaKpmProjectBinaryContainer::class.java.getMethod("getBinaryOrNull"))
}
@Test
fun `test - instance container - binaryOrNull`() {
assertNull(IdeaKpmProjectContainer(TestIdeaKpmInstances.simpleProject).binaryOrNull)
assertNotNull(IdeaKpmProjectInstanceContainer::class.java.getMethod("getBinaryOrNull"))
}
}