Start adding unit tests via MockBukkit

This commit is contained in:
William committed 2022-08-15 16:28:21 +01:00
1 parent c31229c19e
commit eddae1051c
15 files changed
+147 -28

No files matched your search

+11 -1
View File
@@ -22,17 +22,27 @@ allprojects {
compileJava.options.encoding = 'UTF-8'
javadoc.options.encoding = 'UTF-8'
compileJava.options.release.set 16
compileJava.options.release.set 17
repositories {
mavenLocal()
mavenCentral()
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://repo.papermc.io/repository/maven-public/' }
maven { url 'https://repo.minebench.de/' }
maven { url 'https://repo.alessiodp.com/releases/' }
maven { url 'https://jitpack.io' }
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {
useJUnitPlatform()
}
processResources {
filter ReplaceTokens as Class, beginToken: '${', endToken: '}',
tokens: rootProject.ext.properties
+7 -1
View File
@@ -3,14 +3,20 @@ dependencies {
implementation 'org.bstats:bstats-bukkit:3.0.0'
implementation 'io.papermc:paperlib:1.0.7'
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT'
compileOnly 'redis.clients:jedis:' + jedis_version
compileOnly 'commons-io:commons-io:2.11.0'
compileOnly 'com.google.guava:guava:31.1-jre'
compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT'
compileOnly 'org.jetbrains:annotations:23.0.0'
compileOnly 'net.william278:Annotaml:1.0.1'
testImplementation 'com.github.seeseemelk:MockBukkit-v1.19:2.117.0'
testImplementation 'redis.clients:jedis:' + jedis_version
testImplementation 'org.xerial:sqlite-jdbc:' + sqlite_driver_version
testImplementation 'mysql:mysql-connector-java:8.0.30'
testImplementation 'org.apache.commons:commons-text:1.9'
}
shadowJar {
@@ -29,7 +29,9 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.PluginCommand;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -182,7 +184,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
try {
new Metrics(this, METRICS_ID);
} catch (final Exception e) {
getLoggingAdapter().log(Level.WARNING, "Skipped bStats metrics initialization due to an exception.");
getLoggingAdapter().log(Level.WARNING, "Skipped bStats metrics initialization.");
}
// Check for updates
@@ -320,6 +322,19 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
});
}
// Default constructor
public BukkitHuskHomes()
{
super();
}
// Super constructor for unit testing
protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description,
@NotNull File dataFolder, @NotNull File file)
{
super(loader, description, dataFolder, file);
}
public static final class BukkitAdapter {
public static Optional<Location> adaptLocation(net.william278.huskhomes.position.Location location) {
@@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
/**
* Bukkit executor that implements and executes {@link CommandBase}s
@@ -38,7 +37,6 @@ public class BukkitCommand implements CommandExecutor, TabExecutor {
*/
public void register(@NotNull PluginCommand pluginCommand) {
try {
plugin.getLoggingAdapter().log(Level.INFO, "Registering executor for command " + pluginCommand.getName() + "...");
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this);
pluginCommand.setPermission(command.permission);
@@ -7,7 +7,7 @@ import net.william278.huskhomes.BukkitHuskHomes;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
import net.william278.huskhomes.util.EconomyUnsupportedException;
import net.william278.huskhomes.EconomyUnsupportedException;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
+2 -2
View File
@@ -6,8 +6,8 @@ version: '${version}'
api-version: 1.16
libraries:
- 'redis.clients:jedis:${jedis_version}'
- 'mysql:mysql-connector-java:8.0.29'
- 'org.xerial:sqlite-jdbc:3.36.0.3'
- 'mysql:mysql-connector-java:8.0.30'
- 'org.xerial:sqlite-jdbc:${sqlite_driver_version}'
- 'org.apache.commons:commons-text:1.9'
commands:
@@ -0,0 +1,54 @@
package net.william278.huskhomes;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import net.william278.huskhomes.command.BukkitCommandType;
import net.william278.huskhomes.player.BukkitPlayer;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.PositionMeta;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* Uses MockBukkit to test the plugin on a mock Paper server implementing the Bukkit 1.19 API.
*/
public class BukkitPluginTests {
public static ServerMock server;
public static BukkitHuskHomes plugin;
@BeforeAll
public static void setUp() {
server = MockBukkit.mock();
plugin = MockBukkit.load(BukkitHuskHomes.class);
}
@AfterAll
public static void tearDown() {
MockBukkit.unmock();
}
@Test
public void testPluginEnables() {
Assertions.assertTrue(plugin.isEnabled());
}
@Test
public void testCommandRegistration() {
// Assert that all commands in BukkitCommandType are registered by the plugin
for (BukkitCommandType command : BukkitCommandType.values()) {
Assertions.assertNotNull(plugin.getCommand(command.commandBase.command));
}
}
@Test
public void testPlayerAdaption() {
// Assert that the player adapter is working
PlayerMock player = server.addPlayer();
Assertions.assertNotNull(BukkitPlayer.adapt(player));
}
}
@@ -0,0 +1,12 @@
package net.william278.huskhomes;
/**
* Indicates that economy features of the plugin have tried to be accessed when they are not enabled.
*/
public class EconomyUnsupportedException extends HuskHomesException {
public EconomyUnsupportedException(String message) {
super(message);
}
}
@@ -0,0 +1,18 @@
package net.william278.huskhomes;
import org.jetbrains.annotations.NotNull;
/**
* Indicates an exception caused by HuskHomes' plugin logic occurred
*/
public class HuskHomesException extends RuntimeException {
public HuskHomesException(@NotNull String message) {
super(message);
}
public HuskHomesException(@NotNull String message, @NotNull Throwable cause) {
super(message, cause);
}
}
@@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Indicates an exception occurred while initialising the HuskHomes plugin
*/
public class HuskHomesInitializationException extends RuntimeException {
public class HuskHomesInitializationException extends HuskHomesException {
public HuskHomesInitializationException(@NotNull String message) {
super(message);
}
@@ -127,7 +127,13 @@ public class SqLiteDatabase extends Database {
statement.setString(8, position.server.name);
statement.executeUpdate();
return statement.getGeneratedKeys().getInt(1);
// Return the ID of the newly inserted row
try (ResultSet resultSet = statement.getGeneratedKeys()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
throw new SQLException("No generated key found");
}
}
@@ -170,7 +176,13 @@ public class SqLiteDatabase extends Database {
statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli()));
statement.executeUpdate();
return statement.getGeneratedKeys().getInt(1);
// Return the ID of the newly inserted row
try (ResultSet resultSet = statement.getGeneratedKeys()) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
}
throw new SQLException("No generated key found");
}
}
@@ -884,6 +896,7 @@ public class SqLiteDatabase extends Database {
statement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
getLogger().log(Level.SEVERE,
"Failed to set a home to the database for " + home.owner.username, e);
}
@@ -4,7 +4,7 @@ import de.themoep.minedown.MineDown;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportResult;
import net.william278.huskhomes.util.EconomyUnsupportedException;
import net.william278.huskhomes.EconomyUnsupportedException;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@@ -50,7 +50,7 @@ public class SavedPositionManager {
cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>());
cache.homes.get(home.owner.uuid).add(home.meta.name);
cache.positionLists.clear();
return database.setHome(home).thenApply(ignored ->
return database.setHome(home).thenApply(value ->
new SetResult(SetResult.ResultType.SUCCESS, home)).join();
}
return new SetResult(SetResult.ResultType.FAILED_DUPLICATE, null);
@@ -188,7 +188,12 @@ public class SavedPositionManager {
/**
* The position was not set; the description uses illegal characters
*/
FAILED_DESCRIPTION_CHARACTERS(false);
FAILED_DESCRIPTION_CHARACTERS(false),
/**
* The position was not set; the position is not in a valid world
*/
FAILED_ERROR(false);
public final boolean successful;
@@ -1,12 +0,0 @@
package net.william278.huskhomes.util;
/**
* Signals that economy features of the plugin have tried to be accessed when they are not enabled.
*/
public class EconomyUnsupportedException extends IllegalStateException {
public EconomyUnsupportedException(String message) {
super(message);
}
}
+2 -2
View File
@@ -1,10 +1,10 @@
org.gradle.jvmargs='-Dfile.encoding=UTF-8'
org.gradle.daemon=true
javaVersion=16
javaVersion=17
plugin_version=3.0
plugin_archive=huskhomes
jedis_version=4.2.3
sqlite_driver_version=3.36.0.3
sqlite_driver_version=3.39.2.0