Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ dependencies {
implementation(libs.adventurePlatformBukkit)
implementation(libs.paper)

testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.platform.launcher)
testRuntimeOnly(libs.junit.jupiter.engine)
testImplementation(libs.mockbukkit)
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lines should be swapped to maintain a consistent definition of the test dependencies. The goal is to group all testImplementation calls together and do the same for testRuntimeOnly calls. This improves the readability and organization of the dependency definitions.

}
java {
sourceCompatibility = JavaVersion.VERSION_21
Expand All @@ -37,6 +42,14 @@ tasks {
archiveClassifier.set("")
archiveFileName.set("labyrinth.jar")
}

test {
useJUnitPlatform()
jvmArgs("-Dlabyrinth.insideTest=true")
testLogging {
events("passed", "skipped", "failed")
}
}
}

publishing {
Expand Down
10 changes: 9 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ dependencyResolutionManagement {
versionCatalogs {
create("libs") {

version("paper", "1.20.6-R0.1-SNAPSHOT")
version("paper", "1.21.8-R0.1-SNAPSHOT")
version("plugin.yml", "0.6.0")
version("run-paper", "3.0.0")
version("publishdata", "1.4.0")
version("shadow", "9.1.0")
version("junit-bom", "5.13.4")
version("mockbukit", "4.76.0")

plugin("plugin.yml", "net.minecrell.plugin-yml.paper").versionRef("plugin.yml")
plugin("run.paper", "xyz.jpenilla.run-paper").versionRef("run-paper")
Expand All @@ -22,6 +24,12 @@ dependencyResolutionManagement {

library("paper", "io.papermc.paper", "paper-api").versionRef("paper")

library("junit-bom", "org.junit", "junit-bom").versionRef("junit-bom")
library("junit-jupiter", "org.junit.jupiter", "junit-jupiter").withoutVersion()
library("junit-jupiter-engine", "org.junit.jupiter", "junit-jupiter-engine").withoutVersion()
library("junit.platform.launcher", "org.junit.platform", "junit-platform-launcher").withoutVersion()
library("mockbukkit", "org.mockbukkit.mockbukkit", "mockbukkit-v1.21").versionRef("mockbukit")

}
}
repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package net.onelitefeather.labyrinth.commands;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.onelitefeather.labyrinth.service.api.ValidationService;
import net.onelitefeather.labyrinth.utils.Constants;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.*;
import org.mockbukkit.mockbukkit.world.WorldMock;

class CenterCommandTest extends CommandPluginTestBase {

private CenterCommand command;
private MockValidationService validationService;

public static class MockValidationService implements ValidationService
{
private boolean isValid;

@Override
public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) {
return isValid;
}

public void setValid(boolean valid) {
isValid = valid;
}

public boolean isValid() {
return isValid;
}
}

@Override
@BeforeEach
void setUp()
{
super.setUp();
validationService = new MockValidationService();
command = new CenterCommand(plugin, validationService);
}


@DisplayName("Test if the player location is not zero")
@Test
void testIsYLocationFromPlayerNotZero()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project follows the standard Java code conventions. Therefore, each method declaration should include the opening brace on the same line as the method declaration.

Correct style:

void myTestMethod() {
}

var player = server.addPlayer();
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
player.setLocation(location);

command.centerCommand(player, "Test");
Assertions.assertNotEquals(0, location.getY());
}

@Test
void testValidationWrong()
{
var player = server.addPlayer();
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
player.setLocation(location);

validationService.setValid(false);
command.centerCommand(player, "Test");
var playerMessage = player.nextComponentMessage();
Assertions.assertNotNull(playerMessage);
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
Placeholder.component("prefix", Constants.PREFIX));
Assertions.assertEquals(expectedMessage, playerMessage);
Assertions.assertFalse(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted("Test")));
}

@Test
void testValidationTrue()
{
var player = server.addPlayer();
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CENTER_COMMAND_MESSAGE_SUCCESS,
Placeholder.unparsed("zone", zoneName),
Placeholder.component("prefix", Constants.PREFIX));

player.setLocation(location);
validationService.setValid(true);
command.centerCommand(player, zoneName);
location.setY(0);
var playerMessage = player.nextComponentMessage();

Assertions.assertNotNull(playerMessage);
Assertions.assertEquals(expectedMessage, playerMessage);
Assertions.assertTrue(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName)));
Assertions.assertEquals(location, plugin.getConfig().getLocation(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.onelitefeather.labyrinth.commands;

import net.onelitefeather.labyrinth.Labyrinth;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockbukkit.mockbukkit.MockBukkit;
import org.mockbukkit.mockbukkit.ServerMock;

public abstract class CommandPluginTestBase {
protected @NotNull ServerMock server;
protected Labyrinth plugin;

public static class MockLabyrinthPlugin extends Labyrinth {
@Override
public void onEnable() {

}

@Override
public void onDisable() {

}
}

@BeforeEach
void setUp()
{
server = MockBukkit.mock();
plugin = MockBukkit.load(CommandPluginTestBase.MockLabyrinthPlugin.class);
}

@AfterEach
void tearDown()
{
MockBukkit.unmock();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.onelitefeather.labyrinth.commands;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.onelitefeather.labyrinth.utils.Constants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CreateZoneCommandTest extends CommandPluginTestBase {

private CreateZoneCommand command;

@Override
@BeforeEach
void setUp()
{
super.setUp();
command = new CreateZoneCommand(plugin);
}

@Test
void testZoneNameNotMatchesPattern()
{
var player = server.addPlayer();
var zoneName = "Test%";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
Placeholder.component("prefix", Constants.PREFIX));
command.createZone(player, zoneName);

assertFalse(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName)));
assertEquals(expectedMessage, player.nextComponentMessage());
}

@Test
void testZoneCreated()
{
var player = server.addPlayer();
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CREATE_ZONE_MESSAGE_SUCCESS,
Placeholder.unparsed("zone", zoneName),
Placeholder.component("prefix", Constants.PREFIX));
command.createZone(player, zoneName);

assertTrue(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName)));
assertEquals(expectedMessage, player.nextComponentMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.onelitefeather.labyrinth.commands;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.onelitefeather.labyrinth.utils.Constants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class DeleteZoneCommandTest extends CommandPluginTestBase{

private DeleteZoneCommand command;

@Override
@BeforeEach
void setUp()
{
super.setUp();
command = new DeleteZoneCommand(plugin);
}

@Test
void testZoneDeleted()
{
var player = server.addPlayer();
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.DELETE_ZONE_MESSAGE_SUCCESS,
Placeholder.unparsed("zone", zoneName),
Placeholder.component("prefix", Constants.PREFIX));
var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName);
plugin.getConfig().createSection(configSectionName);
command.deleteZone(player, zoneName);
assertFalse(plugin.getConfig().isSet(configSectionName));
assertEquals(expectedMessage, player.nextComponentMessage());
}

@Test
void testZoneNotDeleted()
{
var player = server.addPlayer();
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
Placeholder.component("prefix", Constants.PREFIX));
command.deleteZone(player, zoneName);
assertEquals(expectedMessage, player.nextComponentMessage());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.onelitefeather.labyrinth.commands;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.onelitefeather.labyrinth.service.api.ValidationService;
import net.onelitefeather.labyrinth.utils.Constants;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockbukkit.mockbukkit.world.WorldMock;

import static org.junit.jupiter.api.Assertions.*;

class SetRadiusCommandTest extends CommandPluginTestBase{

private SetRadiusCommand command;
private SetRadiusCommandTest.MockValidationService validationService;

public static class MockValidationService implements ValidationService
{
private boolean isValid;

@Override
public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) {
return isValid;
}

public void setValid(boolean valid) {
isValid = valid;
}

public boolean isValid() {
return isValid;
}
}

@Override
@BeforeEach
void setUp()
{
super.setUp();
validationService = new SetRadiusCommandTest.MockValidationService();
command = new SetRadiusCommand(plugin, validationService);
}

@Test
void testValidationWrong()
{
var player = server.addPlayer();
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
Placeholder.component("prefix", Constants.PREFIX));
validationService.setValid(false);
command.setRadius(player, zoneName);
assertEquals(expectedMessage, player.nextComponentMessage());
}

@Test
void testNoLocationSet()
{
var player = server.addPlayer();
var zoneName = "Test";
validationService.setValid(true);
command.setRadius(player, zoneName);
assertNull(player.nextComponentMessage());
}

@Test
void testRadiusSet()
{
var player = server.addPlayer();
var zoneName = "Test";
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.SET_RADIUS_MESSAGE,
Placeholder.unparsed("zone", zoneName),
Placeholder.component("prefix", Constants.PREFIX));
var world = new WorldMock(Material.GRASS_BLOCK, 64);
var centerLocation = new Location(world, 120, 0, 120);
var playerLocation = new Location(world, 240, 64, 240);

validationService.setValid(true);
plugin.getConfig().set(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName), centerLocation);
player.setLocation(playerLocation);

command.setRadius(player,zoneName);

playerLocation.setY(0);
var expectedDistance = centerLocation.distance(playerLocation);
assertEquals(expectedDistance, plugin.getConfig().getDouble(Constants.CONFIG_ZONE_RADIUS_PATH.formatted(zoneName)));
assertEquals(expectedMessage, player.nextComponentMessage());
}

}
Loading