Skip to content

Commit

Permalink
Add tests, and fix some failures using them
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-noey committed Sep 15, 2024
1 parent 7c7a5f6 commit 3f4d014
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions Movecraft/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3f4d014

Please sign in to comment.