From 60036f7f9afa549dfffc750affed68201a7fc5c3 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Tue, 21 Nov 2023 14:36:47 +0100 Subject: [PATCH] StorageProfileManager test --- .../profile/storage/ProfileIOManagerTest.kt | 35 +++++++++++++++ .../storage/StorageProfileManagerTest.kt | 43 ++++++++++++++++--- .../profile/storage/ProfileIOManager.kt | 1 + 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/ProfileIOManagerTest.kt diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/ProfileIOManagerTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/ProfileIOManagerTest.kt new file mode 100644 index 000000000..080b07045 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/config/profile/storage/ProfileIOManagerTest.kt @@ -0,0 +1,35 @@ +/* + * 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.kutil.cast.CastUtil.unsafeCast +import de.bixilon.minosoft.config.profile.test.TestProfile + +class ProfileIOManagerTest { + + + companion object { + private val SAVE = ProfileIOManager::class.java.getDeclaredField("save").apply { isAccessible = true } + + fun ProfileIOManager.isSaveQueued(storage: FileStorage): Boolean { + val queue = SAVE.get(ProfileIOManager).unsafeCast>() + storage.invalid = false + return queue.remove(storage) + } + + fun TestProfile.isSaveQueued(): Boolean { + return ProfileIOManager.isSaveQueued(this.storage.unsafeCast()) + } + } +} 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 index db4da81ea..8f8696b2a 100644 --- 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 @@ -13,10 +13,11 @@ package de.bixilon.minosoft.config.profile.storage +import de.bixilon.minosoft.config.profile.storage.ProfileIOManagerTest.Companion.isSaveQueued 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.Assert.* import org.testng.annotations.Test import java.io.FileOutputStream @@ -33,14 +34,44 @@ class StorageProfileManagerTest { stream.close() } - fun `load dumped profiles`() { - val profile = """{"version": "1", "key_old": 123}""" - dump("Dumped", profile) + fun `load unmigrated profile`() { + val profile = """{"version": 2, "key": 123}""" + dump("Dumped1", profile) val manager = TestProfileManager() - assertEquals(manager["Dumped"], null) + assertEquals(manager["Dumped1"], null) manager.load() - val dumped = manager["Dumped"]!! + val dumped = manager["Dumped1"]!! + assertFalse(dumped.isSaveQueued()) assertEquals(dumped.key, 123) + base.toFile().deleteRecursively() + } + + fun `load migrated profile`() { + val profile = """{"version": 1, "key_old": 123}""" + dump("Dumped2", profile) + + val manager = TestProfileManager() + assertEquals(manager["Dumped2"], null) + manager.load() + val dumped = manager["Dumped2"]!! + assertTrue(dumped.isSaveQueued()) // profile was migrated + assertEquals(dumped.key, 123) + base.toFile().deleteRecursively() + } + + fun `create and verify save is queued`() { + val manager = TestProfileManager() + val profile = manager.create("Dumped3") + assertTrue(profile.isSaveQueued()) + } + + fun `queue save on change`() { + val manager = TestProfileManager() + val profile = manager.create("Dumped4") + assertTrue(profile.isSaveQueued()) + assertFalse(profile.isSaveQueued()) // save flag really removed + profile.key = 999 + assertTrue(profile.isSaveQueued()) } } diff --git a/src/main/java/de/bixilon/minosoft/config/profile/storage/ProfileIOManager.kt b/src/main/java/de/bixilon/minosoft/config/profile/storage/ProfileIOManager.kt index 4922760c1..77bde1319 100644 --- a/src/main/java/de/bixilon/minosoft/config/profile/storage/ProfileIOManager.kt +++ b/src/main/java/de/bixilon/minosoft/config/profile/storage/ProfileIOManager.kt @@ -43,6 +43,7 @@ object ProfileIOManager { ignoreAll { delete() } ignoreAll { save() } ignoreAll { reload() } + ignoreAll { selected() } lock.unlock()