From 3f4d01420b68964ca181f89b31a4ad0e1f7be1f6 Mon Sep 17 00:00:00 2001 From: ohnoey Date: Sat, 14 Sep 2024 20:04:58 -0700 Subject: [PATCH] Add tests, and fix some failures using them --- Movecraft/build.gradle.kts | 1 + .../net/countercraft/movecraft/Startup.java | 2 +- .../movecraft/localisation/I18nSupport.java | 14 +++++++---- .../countercraft/movecraft/StartupTest.java | 23 +++++++++++++++++-- .../config/SettingsHostedService.java | 11 +++------ 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Movecraft/build.gradle.kts b/Movecraft/build.gradle.kts index bc6b3b9e6..14950e7b7 100644 --- a/Movecraft/build.gradle.kts +++ b/Movecraft/build.gradle.kts @@ -17,6 +17,7 @@ dependencies { testImplementation(libs.junit.junit) testImplementation(libs.org.hamcrest.hamcrest.library) testImplementation("org.mockito:mockito-core:5.13.0") + testImplementation("com.github.seeseemelk:MockBukkit-v1.18:2.85.2") } tasks.shadowJar { diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java index d59f3c6a0..d1f058b41 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/Startup.java @@ -32,12 +32,12 @@ public class Startup { .register(MapUpdateManager.class) .register(AsyncManager.class) .register(VersionProvider.class) + .register(SettingsHostedService.class) .register(SmoothTeleportFactory.class) .register(WorldHandlerFactory.class) .registerInstance(WorldManager.INSTANCE) .register(WreckManager.class) .register(I18nSupport.class) - .register(SettingsHostedService.class) .register(DataPackHostedService.class) .register(CraftManager.class) .register(InteractListener.class) diff --git a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java index a7931721d..2167bb794 100644 --- a/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java +++ b/Movecraft/src/main/java/net/countercraft/movecraft/localisation/I18nSupport.java @@ -33,6 +33,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Properties; import java.util.logging.Logger; @@ -65,15 +67,17 @@ public void start() { private void init() { languageFile = new Properties(); - File localisationDirectory = new File(plugin.getDataFolder().getAbsolutePath() + "/localisation"); + Path localisationDirectory = plugin.getDataFolder().toPath().resolve("localisation"); - if (!localisationDirectory.exists()) { - localisationDirectory.mkdirs(); + try { + Files.createDirectories(localisationDirectory); + } catch (IOException e) { + throw new IllegalStateException("Critical Error in Localisation System", e); } - InputStream inputStream = null; + InputStream inputStream; try { - inputStream = new FileInputStream(localisationDirectory.getAbsolutePath() + "/movecraftlang" + "_" + Settings.LOCALE + ".properties"); + inputStream = new FileInputStream(localisationDirectory.resolve("movecraftlang_" + Settings.LOCALE + ".properties").toFile()); } catch (FileNotFoundException e) { throw new IllegalStateException("Critical Error in Localisation System", e); } diff --git a/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java index 145a42f10..9c5cc7349 100644 --- a/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java +++ b/Movecraft/src/test/java/net/countercraft/movecraft/StartupTest.java @@ -1,12 +1,18 @@ package net.countercraft.movecraft; +import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock; +import io.papermc.paper.datapack.DatapackManager; import net.countercraft.movecraft.lifecycle.PluginBuilder; import org.bukkit.Server; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; -import org.bukkit.scheduler.BukkitScheduler; import org.junit.Test; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; import java.util.logging.Logger; import static org.mockito.Mockito.*; @@ -27,15 +33,28 @@ public void testStartStop(){ private static PluginBuilder.Application buildTestApplication(){ var pluginManager = mock(PluginManager.class); - var scheduler = mock(BukkitScheduler.class); + var scheduler = new BukkitSchedulerMock(); + var dataPackManager = mock(DatapackManager.class); var server = mock(Server.class); when(server.getScheduler()).thenReturn(scheduler); when(server.getPluginManager()).thenReturn(pluginManager); + when(server.getDatapackManager()).thenReturn(dataPackManager); + + Path directory; + try { + directory = Files.createTempDirectory(UUID.randomUUID().toString()); + directory.resolve("localisation").toFile().mkdir(); + directory.resolve("localisation").resolve("movecraftlang_null.properties").toFile().createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } var plugin = mock(Plugin.class); when(plugin.getLogger()).thenReturn(Logger.getLogger("movecraft-unit-test")); when(plugin.getServer()).thenReturn(server); + when(plugin.getDataFolder()).thenReturn(directory.toFile()); + when(plugin.getConfig()).thenReturn(mock(FileConfiguration.class)); // Create builder var builder = PluginBuilder.createFor(plugin); diff --git a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java index 20d8a9413..6ea4fbb8a 100644 --- a/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java +++ b/api/src/main/java/net/countercraft/movecraft/config/SettingsHostedService.java @@ -16,22 +16,17 @@ import java.util.logging.Logger; public class SettingsHostedService implements HostedService { - private final @Nullable Configuration configuration; + private final @NotNull Configuration configuration; private final @NotNull Logger logger; @Inject - public SettingsHostedService(@Nullable @Opt Plugin plugin, @NotNull Logger logger) { - this.configuration = plugin == null ? null : plugin.getConfig(); + public SettingsHostedService(@NotNull Plugin plugin, @NotNull Logger logger) { + this.configuration = plugin.getConfig(); this.logger = logger; } @Override public void start() { - // TODO: Abstract away configuration to not need plugin - if(configuration == null){ - return; - } - Settings.LOCALE = configuration.getString("Locale"); Settings.Debug = configuration.getBoolean("Debug", false); Settings.DisableNMSCompatibilityCheck = configuration.getBoolean("IReallyKnowWhatIAmDoing", false);