mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 14:38:53 +00:00
Update and add more unit tests
This commit is contained in:
1 parent
93dd15007a
commit
3a85e7fdf5
8 files changed
+253
-101
No files matched your search
@@ -41,6 +41,7 @@ allprojects {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
|
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.2'
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
|
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,55 +5,79 @@ import be.seeseemelk.mockbukkit.ServerMock;
|
|||||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||||
import de.themoep.minedown.adventure.MineDown;
|
import de.themoep.minedown.adventure.MineDown;
|
||||||
import net.william278.huskhomes.command.BukkitCommand;
|
import net.william278.huskhomes.command.BukkitCommand;
|
||||||
|
import net.william278.huskhomes.command.Command;
|
||||||
|
import net.william278.huskhomes.command.RtpCommand;
|
||||||
import net.william278.huskhomes.user.BukkitUser;
|
import net.william278.huskhomes.user.BukkitUser;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import net.william278.huskhomes.user.ConsoleUser;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.*;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses MockBukkit to test the plugin on a mock Spigot server implementing the Bukkit 1.16 API.
|
* Uses MockBukkit to test the plugin on a mock Spigot server implementing the Bukkit 1.16 API.
|
||||||
*/
|
*/
|
||||||
|
@DisplayName("Bukkit Plugin Tests")
|
||||||
public class BukkitPluginTests {
|
public class BukkitPluginTests {
|
||||||
|
|
||||||
public static ServerMock server;
|
private static ServerMock server;
|
||||||
public static BukkitHuskHomes plugin;
|
private static BukkitHuskHomes plugin;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void setUp() {
|
@DisplayName("Test Plugin Setup")
|
||||||
|
public static void setUpPlugin() {
|
||||||
server = MockBukkit.mock();
|
server = MockBukkit.mock();
|
||||||
|
server.addSimpleWorld("world");
|
||||||
plugin = MockBukkit.load(BukkitHuskHomes.class);
|
plugin = MockBukkit.load(BukkitHuskHomes.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterAll
|
@AfterAll
|
||||||
public static void tearDown() {
|
@DisplayName("Tear down Plugin")
|
||||||
|
public static void tearDownPlugin() {
|
||||||
MockBukkit.unmock();
|
MockBukkit.unmock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Plugin Initialization Tests")
|
||||||
|
public class ValidationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Plugin Enables")
|
||||||
public void testPluginEnables() {
|
public void testPluginEnables() {
|
||||||
Assertions.assertTrue(plugin.isEnabled());
|
Assertions.assertTrue(plugin.isEnabled());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Command Registration")
|
||||||
public void testCommandRegistration() {
|
public void testCommandRegistration() {
|
||||||
// Assert that all commands in BukkitCommandType are registered by the plugin
|
|
||||||
for (BukkitCommand.Type command : BukkitCommand.Type.values()) {
|
for (BukkitCommand.Type command : BukkitCommand.Type.values()) {
|
||||||
Assertions.assertNotNull(plugin.getCommand(command.getCommand().getName()));
|
Assertions.assertNotNull(plugin.getCommand(command.getCommand().getName()));
|
||||||
}
|
}
|
||||||
|
Assertions.assertEquals(BukkitCommand.Type.values().length, plugin.getCommands().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Player Adapter")
|
||||||
public void testPlayerAdaption() {
|
public void testPlayerAdaption() {
|
||||||
// Assert that the player adapter is working
|
|
||||||
PlayerMock player = server.addPlayer();
|
PlayerMock player = server.addPlayer();
|
||||||
Assertions.assertNotNull(BukkitUser.adapt(player));
|
Assertions.assertNotNull(BukkitUser.adapt(player));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Locale Formatting Tests")
|
||||||
|
public class LocaleFormattingTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Message Formatting")
|
||||||
public void testMessageFormatting() {
|
public void testMessageFormatting() {
|
||||||
PlayerMock player = server.addPlayer();
|
PlayerMock player = server.addPlayer();
|
||||||
|
|
||||||
@@ -67,6 +91,7 @@ public class BukkitPluginTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Message Dispatching")
|
||||||
public void testMessageDispatching() {
|
public void testMessageDispatching() {
|
||||||
PlayerMock player = server.addPlayer();
|
PlayerMock player = server.addPlayer();
|
||||||
|
|
||||||
@@ -80,6 +105,7 @@ public class BukkitPluginTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Locale Loading")
|
||||||
public void testLocalesLoading() {
|
public void testLocalesLoading() {
|
||||||
final Map<String, String> rawLocales = plugin.getLocales().rawLocales;
|
final Map<String, String> rawLocales = plugin.getLocales().rawLocales;
|
||||||
Assertions.assertTrue(rawLocales.size() > 0);
|
Assertions.assertTrue(rawLocales.size() > 0);
|
||||||
@@ -87,6 +113,7 @@ public class BukkitPluginTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Test Locale Parsing")
|
||||||
public void testLocaleParsing() {
|
public void testLocaleParsing() {
|
||||||
final Map<String, String> rawLocales = plugin.getLocales().rawLocales;
|
final Map<String, String> rawLocales = plugin.getLocales().rawLocales;
|
||||||
BukkitUser bukkitUser = BukkitUser.adapt(server.addPlayer());
|
BukkitUser bukkitUser = BukkitUser.adapt(server.addPlayer());
|
||||||
@@ -97,4 +124,51 @@ public class BukkitPluginTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Command Execution Tests")
|
||||||
|
public class CommandExecutionTests {
|
||||||
|
|
||||||
|
@DisplayName("Test User Execution")
|
||||||
|
@ParameterizedTest(name = "/{2} Command")
|
||||||
|
@MethodSource("provideUserExecutedCommands")
|
||||||
|
public void testUserExecution(@NotNull Command command, @NotNull OnlineUser user,
|
||||||
|
@NotNull @SuppressWarnings("unused") String commandName) {
|
||||||
|
Assertions.assertNotNull(command);
|
||||||
|
Assertions.assertNotNull(user);
|
||||||
|
command.execute(user, new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DisplayName("Test Console Execution")
|
||||||
|
@ParameterizedTest(name = "/{2} Command")
|
||||||
|
@MethodSource("provideConsoleExecutedCommands")
|
||||||
|
public void testUserExecution(@NotNull Command command, @NotNull ConsoleUser console,
|
||||||
|
@NotNull @SuppressWarnings("unused") String commandName) {
|
||||||
|
Assertions.assertNotNull(command);
|
||||||
|
Assertions.assertNotNull(console);
|
||||||
|
command.execute(console, new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideUserExecutedCommands() {
|
||||||
|
final List<Command> commands = plugin.getCommands();
|
||||||
|
final PlayerMock player = server.addPlayer();
|
||||||
|
player.setOp(true);
|
||||||
|
|
||||||
|
final BukkitUser playerUser = BukkitUser.adapt(player);
|
||||||
|
return commands.stream()
|
||||||
|
.filter(command -> !(command instanceof RtpCommand))
|
||||||
|
.flatMap(command -> Stream.of(Arguments.of(command, playerUser, command.getName())));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Stream<Arguments> provideConsoleExecutedCommands() {
|
||||||
|
final List<Command> commands = plugin.getCommands();
|
||||||
|
final ConsoleUser console = plugin.getConsole();
|
||||||
|
return commands.stream()
|
||||||
|
.filter(command -> !(command instanceof RtpCommand))
|
||||||
|
.flatMap(command -> Stream.of(Arguments.of(command, console, command.getName())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,31 +4,29 @@ import net.william278.huskhomes.HuskHomes;
|
|||||||
import net.william278.huskhomes.hook.EconomyHook;
|
import net.william278.huskhomes.hook.EconomyHook;
|
||||||
import net.william278.huskhomes.position.Position;
|
import net.william278.huskhomes.position.Position;
|
||||||
import net.william278.huskhomes.teleport.Teleport;
|
import net.william278.huskhomes.teleport.Teleport;
|
||||||
import net.william278.huskhomes.user.CommandUser;
|
|
||||||
import net.william278.huskhomes.user.OnlineUser;
|
import net.william278.huskhomes.user.OnlineUser;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class BackCommand extends Command {
|
public class BackCommand extends InGameCommand {
|
||||||
|
|
||||||
protected BackCommand(@NotNull HuskHomes plugin) {
|
protected BackCommand(@NotNull HuskHomes plugin) {
|
||||||
super("back", List.of(), "", plugin);
|
super("back", List.of(), "", plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(@NotNull CommandUser executor, @NotNull String[] args) {
|
public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
|
||||||
final OnlineUser player = (OnlineUser) executor;
|
final Optional<Position> lastPosition = plugin.getDatabase().getLastPosition(executor);
|
||||||
final Optional<Position> lastPosition = plugin.getDatabase().getLastPosition(player);
|
|
||||||
if (lastPosition.isEmpty()) {
|
if (lastPosition.isEmpty()) {
|
||||||
plugin.getLocales().getLocale("error_no_last_position")
|
plugin.getLocales().getLocale("error_no_last_position")
|
||||||
.ifPresent(player::sendMessage);
|
.ifPresent(executor::sendMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Teleport.builder(plugin)
|
Teleport.builder(plugin)
|
||||||
.teleporter(player)
|
.teleporter(executor)
|
||||||
.target(lastPosition.get())
|
.target(lastPosition.get())
|
||||||
.economyActions(EconomyHook.Action.BACK_COMMAND)
|
.economyActions(EconomyHook.Action.BACK_COMMAND)
|
||||||
.toTimedTeleport()
|
.toTimedTeleport()
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package net.william278.huskhomes.config;
|
package net.william278.huskhomes.config;
|
||||||
|
|
||||||
import net.william278.annotaml.Annotaml;
|
import net.william278.annotaml.Annotaml;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.*;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -11,30 +14,45 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@DisplayName("Locales Tests")
|
||||||
public class LocalesTests {
|
public class LocalesTests {
|
||||||
|
|
||||||
|
private static Locales englishLocales;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
@DisplayName("Test Loading English Locales")
|
||||||
@Test
|
@Test
|
||||||
public void testAllLocalesPresent() {
|
public void testLoadEnglishLocales() {
|
||||||
// Load locales/en-gb.yml as an InputStream
|
try (InputStream resource = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) {
|
||||||
try (InputStream localeStream = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) {
|
Assertions.assertNotNull(resource, "en-gb.yml is missing from the locales folder");
|
||||||
Assertions.assertNotNull(localeStream, "en-gb.yml is missing from the locales folder");
|
englishLocales = Annotaml.create(Locales.class, resource).get();
|
||||||
final Locales englishLocales = Annotaml.create(Locales.class, localeStream).get();
|
|
||||||
final Set<String> keys = englishLocales.rawLocales.keySet();
|
|
||||||
|
|
||||||
// Iterate through every locale file in the locales folder
|
|
||||||
URL url = LocalesTests.class.getClassLoader().getResource("locales");
|
|
||||||
Assertions.assertNotNull(url, "locales folder is missing");
|
|
||||||
|
|
||||||
for (File file : Objects.requireNonNull(new File(url.getPath()).listFiles(file -> file.getName().endsWith("yml")
|
|
||||||
&& !file.getName().equals("en-gb.yml")))) {
|
|
||||||
final Set<String> fileKeys = Annotaml.create(file, Locales.class).get().rawLocales.keySet();
|
|
||||||
keys.forEach(key -> Assertions.assertTrue(fileKeys.contains(key),
|
|
||||||
"Locale key " + key + " is missing from " + file.getName()));
|
|
||||||
}
|
|
||||||
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "{1} Locales")
|
||||||
|
@DisplayName("Test All Locale Keys Present")
|
||||||
|
@MethodSource("provideLocaleFiles")
|
||||||
|
public void testAllLocaleKeysPresent(@NotNull File file, @SuppressWarnings("unused") @NotNull String keyName) {
|
||||||
|
try {
|
||||||
|
final Set<String> fileKeys = Annotaml.create(file, Locales.class).get().rawLocales.keySet();
|
||||||
|
englishLocales.rawLocales.keySet()
|
||||||
|
.forEach(key -> Assertions.assertTrue(fileKeys.contains(key),
|
||||||
|
"Locale key " + key + " is missing from " + file.getName()));
|
||||||
|
} catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> provideLocaleFiles() {
|
||||||
|
URL url = LocalesTests.class.getClassLoader().getResource("locales");
|
||||||
|
Assertions.assertNotNull(url, "locales folder is missing");
|
||||||
|
return Stream.of(Objects.requireNonNull(new File(url.getPath())
|
||||||
|
.listFiles(file -> file.getName().endsWith("yml") && !file.getName().equals("en-gb.yml"))))
|
||||||
|
.map(file -> Arguments.of(file, file.getName().replace(".yml", "")));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package net.william278.huskhomes.hook;
|
package net.william278.huskhomes.hook;
|
||||||
|
|
||||||
import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
|
import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
@DisplayName("Plan Hook Tests")
|
||||||
public class PlanDataExtensionTests {
|
public class PlanDataExtensionTests {
|
||||||
|
|
||||||
// Throws IllegalArgumentException if there is an implementation error or warning.
|
|
||||||
@Test
|
@Test
|
||||||
public void testPlanDataExtension() {
|
@DisplayName("Test Plan Hook Implementation")
|
||||||
|
public void testPlanHookImplementation() {
|
||||||
new ExtensionExtractor(new PlanHook.PlanDataExtension()).validateAnnotations();
|
new ExtensionExtractor(new PlanHook.PlanDataExtension()).validateAnnotations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package net.william278.huskhomes.network;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import net.william278.huskhomes.position.Position;
|
||||||
|
import net.william278.huskhomes.position.World;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@DisplayName("Message Serialization Tests")
|
||||||
|
public class MessageSerializationTests {
|
||||||
|
|
||||||
|
private final static List<Message> TEST_MESSAGES = List.of(
|
||||||
|
Message.builder()
|
||||||
|
.type(Message.Type.REQUEST_PLAYER_LIST)
|
||||||
|
.target("TestTarget")
|
||||||
|
.payload(Payload.empty())
|
||||||
|
.build(),
|
||||||
|
Message.builder()
|
||||||
|
.type(Message.Type.TELEPORT_REQUEST)
|
||||||
|
.target("TestTarget")
|
||||||
|
.payload(Payload.withPosition(
|
||||||
|
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||||
|
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
||||||
|
.build(),
|
||||||
|
Message.builder()
|
||||||
|
.type(Message.Type.TELEPORT_TO_NETWORKED_USER)
|
||||||
|
.target("TestTarget")
|
||||||
|
.payload(Payload.withString("TestString"))
|
||||||
|
.build(),
|
||||||
|
Message.builder()
|
||||||
|
.type(Message.Type.PLAYER_LIST)
|
||||||
|
.target("TestTarget")
|
||||||
|
.payload(Payload.withStringList(List.of("TestString1", "TestString2", "TestString3")))
|
||||||
|
.build(),
|
||||||
|
Message.builder()
|
||||||
|
.type(Message.Type.TELEPORT_TO_POSITION)
|
||||||
|
.target("TestTarget")
|
||||||
|
.payload(Payload.withPosition(
|
||||||
|
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
||||||
|
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Parameterized test for Message serialization
|
||||||
|
@DisplayName("Test Message Serialization/Deserialization")
|
||||||
|
@ParameterizedTest(name = "{1} Message")
|
||||||
|
@MethodSource("provideMessages")
|
||||||
|
public void testMessageSerialization(@NotNull Message message, @SuppressWarnings("unused") String ignored) {
|
||||||
|
final Gson gson = createGson();
|
||||||
|
final String serializedMessage = gson.toJson(message);
|
||||||
|
Assertions.assertNotNull(serializedMessage);
|
||||||
|
final Message deserializedMessage = gson.fromJson(serializedMessage, Message.class);
|
||||||
|
Assertions.assertNotNull(deserializedMessage);
|
||||||
|
Assertions.assertEquals(message.getType(), deserializedMessage.getType());
|
||||||
|
Assertions.assertEquals(message.getScope(), deserializedMessage.getScope());
|
||||||
|
Assertions.assertEquals(message.getTarget(), deserializedMessage.getTarget());
|
||||||
|
Assertions.assertEquals(message.getPayload().getPosition().isPresent(), deserializedMessage.getPayload().getPosition().isPresent());
|
||||||
|
Assertions.assertEquals(message.getPayload().getString().isPresent(), deserializedMessage.getPayload().getString().isPresent());
|
||||||
|
Assertions.assertEquals(message.getPayload().getStringList().isPresent(), deserializedMessage.getPayload().getStringList().isPresent());
|
||||||
|
Assertions.assertEquals(message.getPayload().getStringList().isPresent(), deserializedMessage.getPayload().getStringList().isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static Gson createGson() {
|
||||||
|
return new GsonBuilder().create();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Stream<Arguments> provideMessages() {
|
||||||
|
return TEST_MESSAGES.stream().map(message -> Arguments.of(message, message.getType().name()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
package net.william278.huskhomes.network;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import net.william278.huskhomes.position.Position;
|
|
||||||
import net.william278.huskhomes.position.World;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class MessageTests {
|
|
||||||
|
|
||||||
private final static Gson GSON = new GsonBuilder().create();
|
|
||||||
private final static Message DUMMY_NO_PAYLOAD = Message.builder()
|
|
||||||
.type(Message.Type.TELEPORT_REQUEST)
|
|
||||||
.target("TestTarget")
|
|
||||||
.payload(Payload.empty())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
private final static Message DUMMY_PAYLOAD = Message.builder()
|
|
||||||
.type(Message.Type.TELEPORT_REQUEST)
|
|
||||||
.target("TestTarget")
|
|
||||||
.payload(Payload.withPosition(
|
|
||||||
new Position(63.25, 127.43, -32, 180f, -94.3f,
|
|
||||||
new World("TestWorld", UUID.randomUUID()), "TestServer")))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Test message serialization with empty payload")
|
|
||||||
public void testMessageSerializationWithEmptyPayload() {
|
|
||||||
Assertions.assertNotNull(GSON.toJson(DUMMY_NO_PAYLOAD));
|
|
||||||
|
|
||||||
final Message readMessage = GSON.fromJson(GSON.toJson(DUMMY_NO_PAYLOAD), Message.class);
|
|
||||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getUuid(), readMessage.getUuid());
|
|
||||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getTarget(), readMessage.getTarget());
|
|
||||||
Assertions.assertEquals(DUMMY_NO_PAYLOAD.getType(), readMessage.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Test message serialization with position payload")
|
|
||||||
public void testMessageSerializationWithPositionPayload() {
|
|
||||||
Assertions.assertNotNull(GSON.toJson(DUMMY_PAYLOAD));
|
|
||||||
|
|
||||||
final Message readMessage = GSON.fromJson(GSON.toJson(DUMMY_PAYLOAD), Message.class);
|
|
||||||
Assertions.assertEquals(DUMMY_PAYLOAD.getUuid(), readMessage.getUuid());
|
|
||||||
Assertions.assertNotNull(readMessage.getPayload().getPosition());
|
|
||||||
|
|
||||||
assert readMessage.getPayload().getPosition().isPresent() && DUMMY_PAYLOAD.getPayload().getPosition().isPresent();
|
|
||||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getX(),
|
|
||||||
readMessage.getPayload().getPosition().get().getX());
|
|
||||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getY(),
|
|
||||||
readMessage.getPayload().getPosition().get().getY());
|
|
||||||
Assertions.assertEquals(DUMMY_PAYLOAD.getPayload().getPosition().get().getZ(),
|
|
||||||
readMessage.getPayload().getPosition().get().getZ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,23 +4,53 @@ import com.github.plot.Plot;
|
|||||||
import net.william278.huskhomes.position.Location;
|
import net.william278.huskhomes.position.Location;
|
||||||
import net.william278.huskhomes.position.World;
|
import net.william278.huskhomes.position.World;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@DisplayName("Normal Distribution Tests")
|
||||||
public class NormalDistributionTests {
|
public class NormalDistributionTests {
|
||||||
|
|
||||||
|
private final static float MEAN = 0.75f;
|
||||||
|
private final static float STANDARD_DEVIATION = 2f;
|
||||||
|
private final static float SPAWN_RADIUS = 500f;
|
||||||
|
private final static float MAX_RADIUS = 3000f;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNormalDistributionRandomizer() {
|
@DisplayName("Test Point Generation")
|
||||||
|
public void testPointGenerator() {
|
||||||
Assertions.assertEquals(1000, generateLocations(1000).size());
|
Assertions.assertEquals(1000, generateLocations(1000).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plots location distribution
|
// Test points are within range
|
||||||
public static void generateRtpLocationDistributionImage() {
|
@Test
|
||||||
|
@DisplayName("Test Points Within Range")
|
||||||
|
public void testPointRange() {
|
||||||
|
final List<Location> locations = generateLocations(1000);
|
||||||
|
for (Location location : locations) {
|
||||||
|
Assertions.assertTrue(location.getX() >= -MAX_RADIUS && location.getX() <= MAX_RADIUS);
|
||||||
|
Assertions.assertTrue(location.getZ() >= -MAX_RADIUS && location.getZ() <= MAX_RADIUS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plots location distribution to an image for testing
|
||||||
|
*/
|
||||||
|
public static class DistributionModeller {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plot a demo of the normal distribution algorithm
|
||||||
|
*
|
||||||
|
* @param args file path to save image to
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public static void main(String[] args) {
|
||||||
final List<Location> locations = generateLocations(2000);
|
final List<Location> locations = generateLocations(2000);
|
||||||
final double[] xValues = new double[locations.size()];
|
final double[] xValues = new double[locations.size()];
|
||||||
final double[] zValues = new double[locations.size()];
|
final double[] zValues = new double[locations.size()];
|
||||||
@@ -39,19 +69,25 @@ public class NormalDistributionTests {
|
|||||||
.marker(Plot.Marker.CIRCLE)
|
.marker(Plot.Marker.CIRCLE)
|
||||||
.markerColor(new Color(0, 60, 255, 180))
|
.markerColor(new Color(0, 60, 255, 180))
|
||||||
.color(new Color(0, 0, 0, 0)));
|
.color(new Color(0, 0, 0, 0)));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
plot.save("images/rtp-location-distribution", "png");
|
final File output = args.length == 0 ? new File("target", "normal_distribution")
|
||||||
|
: new File(args[0], "normal_distribution");
|
||||||
|
|
||||||
|
plot.save(output.getAbsolutePath(), "png");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private static List<Location> generateLocations(int amount) {
|
private static List<Location> generateLocations(int amount) {
|
||||||
final List<Location> locations = new ArrayList<>();
|
final List<Location> locations = new ArrayList<>();
|
||||||
for (int i = 0; i < amount; i++) {
|
for (int i = 0; i < amount; i++) {
|
||||||
locations.add(NormalDistributionEngine.generateLocation(
|
locations.add(NormalDistributionEngine.generateLocation(
|
||||||
new Location(0, 0, 0, 0, 0, new World("TestWorld", UUID.randomUUID())),
|
new Location(0, 0, 0, 0, 0, new World("TestWorld", UUID.randomUUID())),
|
||||||
0.75f, 2f, 500f, 3000f));
|
MEAN, STANDARD_DEVIATION, SPAWN_RADIUS, MAX_RADIUS));
|
||||||
}
|
}
|
||||||
return locations;
|
return locations;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user