Skip to content

Commit d7dbd67

Browse files
authored
feat: add tests for listeners and commands (#25)
1 parent 48ae28b commit d7dbd67

10 files changed

Lines changed: 678 additions & 8 deletions

build.gradle.kts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
plugins {
2-
id("java")
2+
java
3+
`maven-publish`
34
alias(libs.plugins.run.paper)
45
alias(libs.plugins.plugin.yml)
56
alias(libs.plugins.shadow)
6-
7-
`maven-publish`
87
}
98

109
dependencies {
@@ -14,7 +13,14 @@ dependencies {
1413
implementation(libs.adventurePlatformBukkit)
1514
implementation(libs.paper)
1615

16+
testImplementation(platform(libs.junit.bom))
17+
testImplementation(libs.junit.jupiter)
18+
testImplementation(libs.junit.platform.launcher)
19+
testImplementation(libs.mockbukkit)
20+
21+
testRuntimeOnly(libs.junit.jupiter.engine)
1722
}
23+
1824
java {
1925
sourceCompatibility = JavaVersion.VERSION_21
2026
targetCompatibility = JavaVersion.VERSION_21
@@ -29,14 +35,22 @@ tasks {
2935
}
3036

3137
runServer {
32-
minecraftVersion("1.20.6")
38+
minecraftVersion("1.21.8")
3339
jvmArgs("-Xmx2G", "-Dcom.mojang.eula.agree=true")
3440
}
3541

3642
shadowJar {
3743
archiveClassifier.set("")
3844
archiveFileName.set("labyrinth.jar")
3945
}
46+
47+
test {
48+
useJUnitPlatform()
49+
jvmArgs("-Dlabyrinth.insideTest=true")
50+
testLogging {
51+
events("passed", "skipped", "failed")
52+
}
53+
}
4054
}
4155

4256
publishing {
@@ -89,7 +103,6 @@ publishing {
89103
}
90104

91105
paper {
92-
93106
main = "net.onelitefeather.labyrinth.Labyrinth"
94107
name = "Labyrinth"
95108
description = "This is a prototype plugin for the Labyrinth of our Survival Server"
@@ -117,7 +130,5 @@ paper {
117130
register("labyrinth.setup.deletezone") {
118131
description = "This permission is needed to delete the zone."
119132
}
120-
121133
}
122134
}
123-

settings.gradle.kts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ dependencyResolutionManagement {
44
versionCatalogs {
55
create("libs") {
66

7-
version("paper", "1.20.6-R0.1-SNAPSHOT")
7+
version("paper", "1.21.8-R0.1-SNAPSHOT")
88
version("plugin.yml", "0.6.0")
99
version("run-paper", "3.0.2")
1010
version("publishdata", "1.4.0")
1111
version("shadow", "9.3.0")
12+
version("junit-bom", "5.13.4")
13+
version("mockbukit", "4.76.0")
1214

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

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

27+
library("junit-bom", "org.junit", "junit-bom").versionRef("junit-bom")
28+
library("junit-jupiter", "org.junit.jupiter", "junit-jupiter").withoutVersion()
29+
library("junit-jupiter-engine", "org.junit.jupiter", "junit-jupiter-engine").withoutVersion()
30+
library("junit.platform.launcher", "org.junit.platform", "junit-platform-launcher").withoutVersion()
31+
library("mockbukkit", "org.mockbukkit.mockbukkit", "mockbukkit-v1.21").versionRef("mockbukit")
32+
2533
}
2634
}
2735
repositories {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package net.onelitefeather.labyrinth.commands;
2+
3+
import net.kyori.adventure.text.minimessage.MiniMessage;
4+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
5+
import net.onelitefeather.labyrinth.service.api.ValidationService;
6+
import net.onelitefeather.labyrinth.utils.Constants;
7+
import org.bukkit.Location;
8+
import org.bukkit.Material;
9+
import org.bukkit.entity.Player;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.junit.jupiter.api.*;
12+
import org.mockbukkit.mockbukkit.world.WorldMock;
13+
14+
class CenterCommandTest extends CommandPluginTestBase {
15+
16+
private CenterCommand command;
17+
private MockValidationService validationService;
18+
19+
public static class MockValidationService implements ValidationService {
20+
private boolean isValid;
21+
22+
@Override
23+
public boolean validateZoneInput(@NotNull Player player, @NotNull String zone) {
24+
return isValid;
25+
}
26+
27+
public void setValid(boolean valid) {
28+
isValid = valid;
29+
}
30+
31+
public boolean isValid() {
32+
return isValid;
33+
}
34+
}
35+
36+
@Override
37+
@BeforeEach
38+
void setUp() {
39+
super.setUp();
40+
validationService = new MockValidationService();
41+
command = new CenterCommand(plugin, validationService);
42+
}
43+
44+
45+
@DisplayName("Test if the player location is not zero")
46+
@Test
47+
void testIsYLocationFromPlayerNotZero() {
48+
var player = server.addPlayer();
49+
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
50+
player.setLocation(location);
51+
52+
command.centerCommand(player, "Test");
53+
Assertions.assertNotEquals(0, location.getY());
54+
}
55+
56+
@Test
57+
void testValidationWrong() {
58+
var player = server.addPlayer();
59+
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
60+
player.setLocation(location);
61+
62+
validationService.setValid(false);
63+
command.centerCommand(player, "Test");
64+
var playerMessage = player.nextComponentMessage();
65+
Assertions.assertNotNull(playerMessage);
66+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
67+
Placeholder.component("prefix", Constants.PREFIX));
68+
Assertions.assertEquals(expectedMessage, playerMessage);
69+
Assertions.assertFalse(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted("Test")));
70+
}
71+
72+
@Test
73+
void testValidationTrue() {
74+
var player = server.addPlayer();
75+
var location = new Location(new WorldMock(Material.GRASS_BLOCK, 64), 120, 64, 120);
76+
var zoneName = "Test";
77+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CENTER_COMMAND_MESSAGE_SUCCESS,
78+
Placeholder.unparsed("zone", zoneName),
79+
Placeholder.component("prefix", Constants.PREFIX));
80+
81+
player.setLocation(location);
82+
validationService.setValid(true);
83+
command.centerCommand(player, zoneName);
84+
location.setY(0);
85+
var playerMessage = player.nextComponentMessage();
86+
87+
Assertions.assertNotNull(playerMessage);
88+
Assertions.assertEquals(expectedMessage, playerMessage);
89+
Assertions.assertTrue(plugin.getConfig().contains(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName)));
90+
Assertions.assertEquals(location, plugin.getConfig().getLocation(Constants.CONFIG_ZONE_CENTER_PATH.formatted(zoneName)));
91+
}
92+
93+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.onelitefeather.labyrinth.commands;
2+
3+
import net.onelitefeather.labyrinth.Labyrinth;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.mockbukkit.mockbukkit.MockBukkit;
8+
import org.mockbukkit.mockbukkit.ServerMock;
9+
10+
public abstract class CommandPluginTestBase {
11+
protected @NotNull ServerMock server;
12+
protected Labyrinth plugin;
13+
14+
public static class MockLabyrinthPlugin extends Labyrinth {
15+
@Override
16+
public void onEnable() {
17+
18+
}
19+
20+
@Override
21+
public void onDisable() {
22+
23+
}
24+
}
25+
26+
@BeforeEach
27+
void setUp() {
28+
server = MockBukkit.mock();
29+
plugin = MockBukkit.load(CommandPluginTestBase.MockLabyrinthPlugin.class);
30+
}
31+
32+
@AfterEach
33+
void tearDown() {
34+
MockBukkit.unmock();
35+
}
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.onelitefeather.labyrinth.commands;
2+
3+
import net.kyori.adventure.text.minimessage.MiniMessage;
4+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
5+
import net.onelitefeather.labyrinth.utils.Constants;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class CreateZoneCommandTest extends CommandPluginTestBase {
12+
13+
private CreateZoneCommand command;
14+
15+
@Override
16+
@BeforeEach
17+
void setUp() {
18+
super.setUp();
19+
command = new CreateZoneCommand(plugin);
20+
}
21+
22+
@Test
23+
void testZoneNameNotMatchesPattern() {
24+
var player = server.addPlayer();
25+
var zoneName = "Test%";
26+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
27+
Placeholder.component("prefix", Constants.PREFIX));
28+
command.createZone(player, zoneName);
29+
30+
assertFalse(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName)));
31+
assertEquals(expectedMessage, player.nextComponentMessage());
32+
}
33+
34+
@Test
35+
void testZoneCreated() {
36+
var player = server.addPlayer();
37+
var zoneName = "Test";
38+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.CREATE_ZONE_MESSAGE_SUCCESS,
39+
Placeholder.unparsed("zone", zoneName),
40+
Placeholder.component("prefix", Constants.PREFIX));
41+
command.createZone(player, zoneName);
42+
43+
assertTrue(plugin.getConfig().isSet(Constants.CONFIG_ZONE_PATH.formatted(zoneName)));
44+
assertEquals(expectedMessage, player.nextComponentMessage());
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.onelitefeather.labyrinth.commands;
2+
3+
import net.kyori.adventure.text.minimessage.MiniMessage;
4+
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
5+
import net.onelitefeather.labyrinth.utils.Constants;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class DeleteZoneCommandTest extends CommandPluginTestBase {
12+
13+
private DeleteZoneCommand command;
14+
15+
@Override
16+
@BeforeEach
17+
void setUp() {
18+
super.setUp();
19+
command = new DeleteZoneCommand(plugin);
20+
}
21+
22+
@Test
23+
void testZoneDeleted() {
24+
var player = server.addPlayer();
25+
var zoneName = "Test";
26+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.DELETE_ZONE_MESSAGE_SUCCESS,
27+
Placeholder.unparsed("zone", zoneName),
28+
Placeholder.component("prefix", Constants.PREFIX));
29+
var configSectionName = Constants.CONFIG_ZONE_PATH.formatted(zoneName);
30+
plugin.getConfig().createSection(configSectionName);
31+
command.deleteZone(player, zoneName);
32+
assertFalse(plugin.getConfig().isSet(configSectionName));
33+
assertEquals(expectedMessage, player.nextComponentMessage());
34+
}
35+
36+
@Test
37+
void testZoneNotDeleted() {
38+
var player = server.addPlayer();
39+
var zoneName = "Test";
40+
var expectedMessage = MiniMessage.miniMessage().deserialize(Constants.ZONE_INVALID_MESSAGE,
41+
Placeholder.component("prefix", Constants.PREFIX));
42+
command.deleteZone(player, zoneName);
43+
assertEquals(expectedMessage, player.nextComponentMessage());
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)