Skip to content
Merged
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
25 changes: 18 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
plugins {
id("java")
java
`maven-publish`
alias(libs.plugins.run.paper)
alias(libs.plugins.plugin.yml)
alias(libs.plugins.shadow)

`maven-publish`
}

dependencies {
Expand All @@ -14,7 +13,14 @@ dependencies {
implementation(libs.adventurePlatformBukkit)
implementation(libs.paper)

testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testImplementation(libs.junit.platform.launcher)
testImplementation(libs.mockbukkit)

testRuntimeOnly(libs.junit.jupiter.engine)
}

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
Expand All @@ -29,14 +35,22 @@ tasks {
}

runServer {
minecraftVersion("1.20.6")
minecraftVersion("1.21.8")
jvmArgs("-Xmx2G", "-Dcom.mojang.eula.agree=true")
}

shadowJar {
archiveClassifier.set("")
archiveFileName.set("labyrinth.jar")
}

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

publishing {
Expand Down Expand Up @@ -89,7 +103,6 @@ publishing {
}

paper {

main = "net.onelitefeather.labyrinth.Labyrinth"
name = "Labyrinth"
description = "This is a prototype plugin for the Labyrinth of our Survival Server"
Expand Down Expand Up @@ -117,7 +130,5 @@ paper {
register("labyrinth.setup.deletezone") {
description = "This permission is needed to delete the zone."
}

}
}

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.2")
version("publishdata", "1.4.0")
version("shadow", "9.3.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,93 @@
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() {
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,36 @@
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,46 @@
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,47 @@
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());

}

}
Loading