From 1943a9f331a3bf3c115a99c15a977ac01b08db10 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Tue, 21 Nov 2023 14:18:01 +0100 Subject: [PATCH] profile manager tests --- .../kotlin/de/bixilon/minosoft/MinosoftSIT.kt | 14 +++++- .../bixilon/minosoft/config/profile/Boxed.kt | 22 +++++++++ .../delegate/types/RedirectDelegateTest.kt | 28 ++--------- .../storage/StorageProfileManagerTest.kt | 46 +++++++++++++++++++ .../config/profile/test/TestProfile.kt | 40 ++++++++++++++++ .../config/profile/test/TestProfileManager.kt | 32 +++++++++++++ .../config/profile/test/config/ConfigC.kt | 26 +++++++++++ .../delegate/types/RedirectDelegate.kt | 13 ++++++ .../profile/storage/StorageProfileManager.kt | 4 +- 9 files changed, 198 insertions(+), 27 deletions(-) create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/Boxed.kt create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/StorageProfileManagerTest.kt create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfile.kt create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfileManager.kt create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/config/ConfigC.kt diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt index e4c2dad5e..076adaac9 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/MinosoftSIT.kt @@ -14,6 +14,7 @@ package de.bixilon.minosoft import de.bixilon.kutil.latch.SimpleLatch +import de.bixilon.kutil.reflection.ReflectionUtil.forceSet import de.bixilon.minosoft.assets.meta.MinosoftMeta import de.bixilon.minosoft.assets.properties.version.AssetsVersionProperties import de.bixilon.minosoft.data.registries.fallback.FallbackRegistries @@ -28,15 +29,24 @@ import de.bixilon.minosoft.util.logging.Log import de.bixilon.minosoft.util.logging.LogLevels import de.bixilon.minosoft.util.logging.LogMessageType import org.testng.annotations.BeforeSuite +import java.nio.file.Path internal object MinosoftSIT { + private fun setupEnv() { + Log.ASYNC_LOGGING = false + RunConfiguration.VERBOSE_LOGGING = true + RunConfiguration.APPLICATION_NAME = "Minosoft it" + RunConfiguration::HOME_DIRECTORY.forceSet(Path.of(System.getProperty("java.io.tmpdir"), "minosoft")) + RunConfiguration::CONFIG_DIRECTORY.forceSet(Path.of(System.getProperty("java.io.tmpdir"), "minosoft").resolve("conf")) + RunConfiguration.PROFILES_HOT_RELOADING = false + } + @BeforeSuite fun setup() { - Log.ASYNC_LOGGING = false + setupEnv() Log.log(LogMessageType.OTHER, LogLevels.INFO) { "This is java version ${System.getProperty("java.version")}" } - RunConfiguration.VERBOSE_LOGGING = true KUtil.initBootClasses() KUtil.initPlayClasses() disableGC() diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/Boxed.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/Boxed.kt new file mode 100644 index 000000000..1c6c96bea --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/Boxed.kt @@ -0,0 +1,22 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.config.profile + +class Boxed(val value: Int, val assigned: Boolean) { + + override fun equals(other: Any?): Boolean { + if (other !is Boxed) return false + return other.value == this.value + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegateTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegateTest.kt index fd6b08e8b..833db95c8 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegateTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegateTest.kt @@ -16,11 +16,9 @@ package de.bixilon.minosoft.config.profile.delegate.types import com.fasterxml.jackson.databind.InjectableValues import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.module.kotlin.convertValue -import de.bixilon.kutil.concurrent.lock.Lock import de.bixilon.kutil.json.JsonObject -import de.bixilon.minosoft.config.profile.ProfileLock -import de.bixilon.minosoft.config.profile.profiles.Profile -import de.bixilon.minosoft.config.profile.storage.ProfileStorage +import de.bixilon.minosoft.config.profile.Boxed +import de.bixilon.minosoft.config.profile.test.TestProfile import de.bixilon.minosoft.util.json.Jackson import org.testng.Assert.assertEquals import org.testng.annotations.Test @@ -65,7 +63,8 @@ class RedirectDelegateTest { fun `update redirect property`() { val profile = create() assertEquals(profile.config.prop, null) - profile.update(mapOf("config" to mapOf("normal" to 12))) + + profile.update(mapOf("config" to mapOf("prop" to 12))) assertEquals(profile.config.prop, Boxed(12, false)) } @@ -82,23 +81,4 @@ class RedirectDelegateTest { } - class TestProfile( - override var storage: ProfileStorage? = null, - override val lock: Lock = ProfileLock(), - ) : Profile { - val config = TestConfig(this) - } - - class TestConfig(profile: Profile) { - var prop by RedirectDelegate(profile, { it?.value }, { it?.let { Boxed(it, false) } }) - var normal by StringDelegate(profile, "test") - } - - class Boxed(val value: Int, val unused: Boolean) { - - override fun equals(other: Any?): Boolean { - if (other !is Boxed) return false - return other.value == this.value - } - } } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/StorageProfileManagerTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/StorageProfileManagerTest.kt new file mode 100644 index 000000000..db4da81ea --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/StorageProfileManagerTest.kt @@ -0,0 +1,46 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.config.profile.storage + +import de.bixilon.minosoft.config.profile.test.TestProfileManager +import de.bixilon.minosoft.protocol.ProtocolUtil.encodeNetwork +import de.bixilon.minosoft.terminal.RunConfiguration +import org.testng.Assert.assertEquals +import org.testng.annotations.Test +import java.io.FileOutputStream + +@Test(groups = ["profiles"]) +class StorageProfileManagerTest { + private val base by lazy { RunConfiguration.CONFIG_DIRECTORY.resolve("minosoft").resolve("test") } + + + private fun dump(name: String, data: String) { + val path = base.resolve("$name.json") + path.parent.toFile().mkdirs() + val stream = FileOutputStream(path.toFile()) + stream.write(data.encodeNetwork()) + stream.close() + } + + fun `load dumped profiles`() { + val profile = """{"version": "1", "key_old": 123}""" + dump("Dumped", profile) + + val manager = TestProfileManager() + assertEquals(manager["Dumped"], null) + manager.load() + val dumped = manager["Dumped"]!! + assertEquals(dumped.key, 123) + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfile.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfile.kt new file mode 100644 index 000000000..6068d34e6 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfile.kt @@ -0,0 +1,40 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.config.profile.test + +import de.bixilon.kutil.concurrent.lock.Lock +import de.bixilon.minosoft.config.profile.ProfileLock +import de.bixilon.minosoft.config.profile.ProfileType +import de.bixilon.minosoft.config.profile.delegate.primitive.IntDelegate +import de.bixilon.minosoft.config.profile.profiles.Profile +import de.bixilon.minosoft.config.profile.storage.ProfileStorage +import de.bixilon.minosoft.config.profile.test.config.ConfigC +import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft + +class TestProfile( + override var storage: ProfileStorage? = null, + override val lock: Lock = ProfileLock(), +) : Profile { + val config = ConfigC(this) + + var key by IntDelegate(this, 1) + + + companion object : ProfileType { + override val identifier = minosoft("test") + override val clazz = TestProfile::class.java + + override fun create(storage: ProfileStorage?) = TestProfile(storage) + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfileManager.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfileManager.kt new file mode 100644 index 000000000..3c5ecc2bf --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/TestProfileManager.kt @@ -0,0 +1,32 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.config.profile.test + +import com.fasterxml.jackson.databind.node.ObjectNode +import de.bixilon.minosoft.config.profile.storage.StorageProfileManager + +class TestProfileManager : StorageProfileManager() { + override val type get() = TestProfile + override val latestVersion get() = 2 + + + override fun migrate(version: Int, data: ObjectNode) = when (version) { + 1 -> migrate1(data) + else -> Unit + } + + fun migrate1(data: ObjectNode) { + data.remove("key_old")?.asInt()?.let { data.put("key", it) } + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/config/ConfigC.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/config/ConfigC.kt new file mode 100644 index 000000000..d246f30a6 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/test/config/ConfigC.kt @@ -0,0 +1,26 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.config.profile.test.config + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import de.bixilon.minosoft.config.profile.Boxed +import de.bixilon.minosoft.config.profile.delegate.types.RedirectDelegate +import de.bixilon.minosoft.config.profile.delegate.types.StringDelegate +import de.bixilon.minosoft.config.profile.profiles.Profile + +class ConfigC(profile: Profile) { + @get:JsonDeserialize(using = RedirectDelegate.RedirectDeserializer::class) + var prop by RedirectDelegate(profile, { it?.value }, { it?.let { Boxed(it, false) } }) + var normal by StringDelegate(profile, "test") +} diff --git a/src/main/java/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegate.kt b/src/main/java/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegate.kt index 19561ac56..e60cb7272 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegate.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/delegate/types/RedirectDelegate.kt @@ -13,11 +13,16 @@ package de.bixilon.minosoft.config.profile.delegate.types +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.databind.deser.std.StdDeserializer import de.bixilon.kutil.observer.DataObserver import de.bixilon.minosoft.config.profile.delegate.AbstractProfileDelegate import de.bixilon.minosoft.config.profile.profiles.Profile import kotlin.reflect.KProperty +@JsonDeserialize(using = RedirectDelegate.RedirectDeserializer::class) class RedirectDelegate( override val profile: Profile, val serializer: (V?) -> S?, @@ -33,4 +38,12 @@ class RedirectDelegate( // fun setValue(thisRef: Any, property: KProperty<*>, value: S?) { // setValue(thisRef, property, lookup(value)) // } + + object RedirectDeserializer : StdDeserializer(Any::class.java) { + + override fun deserialize(parser: JsonParser, context: DeserializationContext?): Any { + return "test" + } + } + } diff --git a/src/main/java/de/bixilon/minosoft/config/profile/storage/StorageProfileManager.kt b/src/main/java/de/bixilon/minosoft/config/profile/storage/StorageProfileManager.kt index ee7ce96cc..69bec8d23 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/storage/StorageProfileManager.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/storage/StorageProfileManager.kt @@ -146,7 +146,9 @@ abstract class StorageProfileManager

: Iterable

, Identified { this.selected = this[selected] ?: create(selected) this::selected.observe(this) { ProfileIOManager.saveSelected(this) } - observe(root.toPath()) + if (RunConfiguration.PROFILES_HOT_RELOADING) { + observe(root.toPath()) + } } private fun observe(root: Path) {