diff --git a/build.gradle b/build.gradle index 60b06a1b..47e0d17a 100644 --- a/build.gradle +++ b/build.gradle @@ -41,6 +41,7 @@ allprojects { dependencies { 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' } diff --git a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java index facce167..6ba60810 100644 --- a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java +++ b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java @@ -5,96 +5,170 @@ import be.seeseemelk.mockbukkit.ServerMock; import be.seeseemelk.mockbukkit.entity.PlayerMock; import de.themoep.minedown.adventure.MineDown; 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 org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; +import net.william278.huskhomes.user.ConsoleUser; +import net.william278.huskhomes.user.OnlineUser; +import org.jetbrains.annotations.NotNull; +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.Optional; +import java.util.stream.Stream; /** * 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 static ServerMock server; - public static BukkitHuskHomes plugin; + private static ServerMock server; + private static BukkitHuskHomes plugin; @BeforeAll - public static void setUp() { + @DisplayName("Test Plugin Setup") + public static void setUpPlugin() { server = MockBukkit.mock(); + server.addSimpleWorld("world"); plugin = MockBukkit.load(BukkitHuskHomes.class); } @AfterAll - public static void tearDown() { + @DisplayName("Tear down Plugin") + public static void tearDownPlugin() { MockBukkit.unmock(); } - @Test - public void testPluginEnables() { - Assertions.assertTrue(plugin.isEnabled()); - } + @Nested + @DisplayName("Plugin Initialization Tests") + public class ValidationTests { - @Test - public void testCommandRegistration() { - // Assert that all commands in BukkitCommandType are registered by the plugin - for (BukkitCommand.Type command : BukkitCommand.Type.values()) { - Assertions.assertNotNull(plugin.getCommand(command.getCommand().getName())); + @Test + @DisplayName("Test Plugin Enables") + public void testPluginEnables() { + Assertions.assertTrue(plugin.isEnabled()); } + + @Test + @DisplayName("Test Command Registration") + public void testCommandRegistration() { + for (BukkitCommand.Type command : BukkitCommand.Type.values()) { + Assertions.assertNotNull(plugin.getCommand(command.getCommand().getName())); + } + Assertions.assertEquals(BukkitCommand.Type.values().length, plugin.getCommands().size()); + } + + @Test + @DisplayName("Test Player Adapter") + public void testPlayerAdaption() { + PlayerMock player = server.addPlayer(); + Assertions.assertNotNull(BukkitUser.adapt(player)); + } + } - @Test - public void testPlayerAdaption() { - // Assert that the player adapter is working - PlayerMock player = server.addPlayer(); - Assertions.assertNotNull(BukkitUser.adapt(player)); + @Nested + @DisplayName("Locale Formatting Tests") + public class LocaleFormattingTests { + + @Test + @DisplayName("Test Message Formatting") + public void testMessageFormatting() { + PlayerMock player = server.addPlayer(); + + final MineDown simpleLocale = plugin.getLocales() + .getLocale("error_in_game_only") + .orElseThrow(() -> new IllegalStateException("Failed to load locale")); + final String simpleLocaleText = plugin.getLocales().getRawLocale("error_in_game_only") + .orElseThrow(() -> new IllegalStateException("Failed to load raw locale")); + BukkitUser.adapt(player).sendMessage(simpleLocale); + player.assertSaid(simpleLocaleText); + } + + @Test + @DisplayName("Test Message Dispatching") + public void testMessageDispatching() { + PlayerMock player = server.addPlayer(); + + final MineDown locale = plugin.getLocales() + .getLocale("teleporting_action_bar_warmup", Integer.toString(3)) + .orElseThrow(() -> new IllegalStateException("Failed to load locale")); + BukkitUser.adapt(player).sendActionBar(locale); + BukkitUser.adapt(player).sendMessage(locale); + BukkitUser.adapt(player).sendTitle(locale, false); + BukkitUser.adapt(player).sendTitle(locale, true); + } + + @Test + @DisplayName("Test Locale Loading") + public void testLocalesLoading() { + final Map rawLocales = plugin.getLocales().rawLocales; + Assertions.assertTrue(rawLocales.size() > 0); + rawLocales.forEach((key, value) -> Assertions.assertNotNull(value)); + } + + @Test + @DisplayName("Test Locale Parsing") + public void testLocaleParsing() { + final Map rawLocales = plugin.getLocales().rawLocales; + BukkitUser bukkitUser = BukkitUser.adapt(server.addPlayer()); + rawLocales.forEach((key, value) -> { + Optional locale = plugin.getLocales().getLocale(key); + Assertions.assertTrue(locale.isPresent()); + bukkitUser.sendMessage(locale.get()); + }); + } + } - @Test - public void testMessageFormatting() { - PlayerMock player = server.addPlayer(); + @Nested + @DisplayName("Command Execution Tests") + public class CommandExecutionTests { - final MineDown simpleLocale = plugin.getLocales() - .getLocale("error_in_game_only") - .orElseThrow(() -> new IllegalStateException("Failed to load locale")); - final String simpleLocaleText = plugin.getLocales().getRawLocale("error_in_game_only") - .orElseThrow(() -> new IllegalStateException("Failed to load raw locale")); - BukkitUser.adapt(player).sendMessage(simpleLocale); - player.assertSaid(simpleLocaleText); - } + @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]); + } - @Test - public void testMessageDispatching() { - PlayerMock player = server.addPlayer(); + @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]); + } - final MineDown locale = plugin.getLocales() - .getLocale("teleporting_action_bar_warmup", Integer.toString(3)) - .orElseThrow(() -> new IllegalStateException("Failed to load locale")); - BukkitUser.adapt(player).sendActionBar(locale); - BukkitUser.adapt(player).sendMessage(locale); - BukkitUser.adapt(player).sendTitle(locale, false); - BukkitUser.adapt(player).sendTitle(locale, true); - } + public static Stream provideUserExecutedCommands() { + final List commands = plugin.getCommands(); + final PlayerMock player = server.addPlayer(); + player.setOp(true); - @Test - public void testLocalesLoading() { - final Map rawLocales = plugin.getLocales().rawLocales; - Assertions.assertTrue(rawLocales.size() > 0); - rawLocales.forEach((key, value) -> Assertions.assertNotNull(value)); - } + 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 provideConsoleExecutedCommands() { + final List 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()))); + } - @Test - public void testLocaleParsing() { - final Map rawLocales = plugin.getLocales().rawLocales; - BukkitUser bukkitUser = BukkitUser.adapt(server.addPlayer()); - rawLocales.forEach((key, value) -> { - Optional locale = plugin.getLocales().getLocale(key); - Assertions.assertTrue(locale.isPresent()); - bukkitUser.sendMessage(locale.get()); - }); } } diff --git a/common/src/main/java/net/william278/huskhomes/command/BackCommand.java b/common/src/main/java/net/william278/huskhomes/command/BackCommand.java index d8b02e10..ab77d418 100644 --- a/common/src/main/java/net/william278/huskhomes/command/BackCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/BackCommand.java @@ -4,31 +4,29 @@ import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.hook.EconomyHook; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.Teleport; -import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.OnlineUser; import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.Optional; -public class BackCommand extends Command { +public class BackCommand extends InGameCommand { protected BackCommand(@NotNull HuskHomes plugin) { super("back", List.of(), "", plugin); } @Override - public void execute(@NotNull CommandUser executor, @NotNull String[] args) { - final OnlineUser player = (OnlineUser) executor; - final Optional lastPosition = plugin.getDatabase().getLastPosition(player); + public void execute(@NotNull OnlineUser executor, @NotNull String[] args) { + final Optional lastPosition = plugin.getDatabase().getLastPosition(executor); if (lastPosition.isEmpty()) { plugin.getLocales().getLocale("error_no_last_position") - .ifPresent(player::sendMessage); + .ifPresent(executor::sendMessage); return; } Teleport.builder(plugin) - .teleporter(player) + .teleporter(executor) .target(lastPosition.get()) .economyActions(EconomyHook.Action.BACK_COMMAND) .toTimedTeleport() diff --git a/common/src/test/java/net/william278/huskhomes/config/LocalesTests.java b/common/src/test/java/net/william278/huskhomes/config/LocalesTests.java index 8a7830ef..b80c3260 100644 --- a/common/src/test/java/net/william278/huskhomes/config/LocalesTests.java +++ b/common/src/test/java/net/william278/huskhomes/config/LocalesTests.java @@ -1,8 +1,11 @@ package net.william278.huskhomes.config; import net.william278.annotaml.Annotaml; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.jetbrains.annotations.NotNull; +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.IOException; @@ -11,30 +14,45 @@ import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.util.Objects; import java.util.Set; +import java.util.stream.Stream; +@DisplayName("Locales Tests") public class LocalesTests { + private static Locales englishLocales; + + @BeforeEach + @DisplayName("Test Loading English Locales") @Test - public void testAllLocalesPresent() { - // Load locales/en-gb.yml as an InputStream - try (InputStream localeStream = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) { - Assertions.assertNotNull(localeStream, "en-gb.yml is missing from the locales folder"); - final Locales englishLocales = Annotaml.create(Locales.class, localeStream).get(); - final Set 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 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())); - } + public void testLoadEnglishLocales() { + try (InputStream resource = LocalesTests.class.getClassLoader().getResourceAsStream("locales/en-gb.yml")) { + Assertions.assertNotNull(resource, "en-gb.yml is missing from the locales folder"); + englishLocales = Annotaml.create(Locales.class, resource).get(); } catch (IOException | InvocationTargetException | InstantiationException | IllegalAccessException 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 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 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", ""))); + } + } diff --git a/common/src/test/java/net/william278/huskhomes/hook/PlanDataExtensionTests.java b/common/src/test/java/net/william278/huskhomes/hook/PlanDataExtensionTests.java index 18c9fe2d..2cfef0bf 100644 --- a/common/src/test/java/net/william278/huskhomes/hook/PlanDataExtensionTests.java +++ b/common/src/test/java/net/william278/huskhomes/hook/PlanDataExtensionTests.java @@ -1,13 +1,15 @@ package net.william278.huskhomes.hook; import com.djrapitops.plan.extension.extractor.ExtensionExtractor; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +@DisplayName("Plan Hook Tests") public class PlanDataExtensionTests { - // Throws IllegalArgumentException if there is an implementation error or warning. @Test - public void testPlanDataExtension() { + @DisplayName("Test Plan Hook Implementation") + public void testPlanHookImplementation() { new ExtensionExtractor(new PlanHook.PlanDataExtension()).validateAnnotations(); } diff --git a/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java new file mode 100644 index 00000000..221f72f1 --- /dev/null +++ b/common/src/test/java/net/william278/huskhomes/network/MessageSerializationTests.java @@ -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 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 provideMessages() { + return TEST_MESSAGES.stream().map(message -> Arguments.of(message, message.getType().name())); + } + +} \ No newline at end of file diff --git a/common/src/test/java/net/william278/huskhomes/network/MessageTests.java b/common/src/test/java/net/william278/huskhomes/network/MessageTests.java deleted file mode 100644 index 4ed55a5c..00000000 --- a/common/src/test/java/net/william278/huskhomes/network/MessageTests.java +++ /dev/null @@ -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()); - } -} diff --git a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java index 84eee6e2..da2a909b 100644 --- a/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java +++ b/common/src/test/java/net/william278/huskhomes/random/NormalDistributionTests.java @@ -4,46 +4,82 @@ import com.github.plot.Plot; import net.william278.huskhomes.position.Location; 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.awt.*; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; +@DisplayName("Normal Distribution Tests") 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 - public void testNormalDistributionRandomizer() { + @DisplayName("Test Point Generation") + public void testPointGenerator() { Assertions.assertEquals(1000, generateLocations(1000).size()); } - // Plots location distribution - public static void generateRtpLocationDistributionImage() { - final List locations = generateLocations(2000); - final double[] xValues = new double[locations.size()]; - final double[] zValues = new double[locations.size()]; - for (int i = 0; i < locations.size(); i++) { - xValues[i] = locations.get(i).getX(); - zValues[i] = locations.get(i).getZ(); + // Test points are within range + @Test + @DisplayName("Test Points Within Range") + public void testPointRange() { + final List 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); } - final Plot plot = Plot.plot(Plot.plotOpts() - .title("RTP Normal Distribution demo") - .legend(Plot.LegendFormat.BOTTOM)) - .xAxis("x", Plot.axisOpts().range(-3250, 3250)) - .yAxis("z", Plot.axisOpts().range(-3250, 3250)) - .series("Data", Plot.data() - .xy(xValues, zValues), - Plot.seriesOpts() - .marker(Plot.Marker.CIRCLE) - .markerColor(new Color(0, 60, 255, 180)) - .color(new Color(0, 0, 0, 0))); - try { - plot.save("images/rtp-location-distribution", "png"); - } catch (IOException e) { - e.printStackTrace(); + } + + /** + * 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 locations = generateLocations(2000); + final double[] xValues = new double[locations.size()]; + final double[] zValues = new double[locations.size()]; + for (int i = 0; i < locations.size(); i++) { + xValues[i] = locations.get(i).getX(); + zValues[i] = locations.get(i).getZ(); + } + final Plot plot = Plot.plot(Plot.plotOpts() + .title("RTP Normal Distribution demo") + .legend(Plot.LegendFormat.BOTTOM)) + .xAxis("x", Plot.axisOpts().range(-3250, 3250)) + .yAxis("z", Plot.axisOpts().range(-3250, 3250)) + .series("Data", Plot.data() + .xy(xValues, zValues), + Plot.seriesOpts() + .marker(Plot.Marker.CIRCLE) + .markerColor(new Color(0, 60, 255, 180)) + .color(new Color(0, 0, 0, 0))); + + try { + 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) { + e.printStackTrace(); + } } + } private static List generateLocations(int amount) { @@ -51,7 +87,7 @@ public class NormalDistributionTests { for (int i = 0; i < amount; i++) { locations.add(NormalDistributionEngine.generateLocation( 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; }