feat(test): unit testing support
This commit is contained in:
parent
7b82126f40
commit
7272809044
7 changed files with 173 additions and 33 deletions
|
@ -20,6 +20,10 @@
|
|||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>papermc</id>
|
||||
<url>https://repo.papermc.io/repository/maven-public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>xenondevs</id>
|
||||
<url>https://repo.xenondevs.xyz/releases</url>
|
||||
|
@ -88,11 +92,11 @@
|
|||
<version>31.1-jre</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Jupiter (Unit Tests) -->
|
||||
<!-- MockBukkit 1.19 (Bukkit Unit Tests) -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.9.0</version>
|
||||
<groupId>com.github.seeseemelk</groupId>
|
||||
<artifactId>MockBukkit-v1.19</artifactId>
|
||||
<version>2.29.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
|
|
@ -19,27 +19,42 @@ import net.artelnatif.nicko.storage.PlayerDataStore;
|
|||
import net.artelnatif.nicko.utils.ServerUtils;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class NickoBukkit extends JavaPlugin {
|
||||
private boolean unitTesting;
|
||||
private static NickoBukkit plugin;
|
||||
|
||||
private NickoConfiguration nickoConfiguration;
|
||||
private MojangAPI mojangAPI;
|
||||
private PlayerDataStore dataStore;
|
||||
|
||||
/**
|
||||
* Used by MockBukkit
|
||||
*/
|
||||
protected NickoBukkit(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) {
|
||||
super(loader, description, dataFolder, file);
|
||||
unitTesting = true;
|
||||
getLogger().info("Unit Testing Mode enabled.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
plugin = this;
|
||||
|
||||
if(!isUnitTesting()) {
|
||||
getLogger().info("Loading internals...");
|
||||
if (getInternals() == null) {
|
||||
getLogger().log(Level.SEVERE, "Nicko could not find a valid implementation for this server version. Is your server supported?");
|
||||
dataStore.getStorage().setError(true);
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
}
|
||||
|
||||
if (getServer().getPluginManager().isPluginEnabled(this)) {
|
||||
mojangAPI = new MojangAPI();
|
||||
|
@ -55,9 +70,6 @@ public class NickoBukkit extends JavaPlugin {
|
|||
command.setExecutor(new NickoCommand());
|
||||
}
|
||||
|
||||
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
|
||||
getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this);
|
||||
|
||||
Structure.addGlobalIngredient('#', new SimpleItem(new ItemBuilder(Material.BLACK_STAINED_GLASS_PANE)));
|
||||
Structure.addGlobalIngredient('%', new SimpleItem(new ItemBuilder(Material.ORANGE_STAINED_GLASS_PANE)));
|
||||
Structure.addGlobalIngredient('E', new ExitDoorItem());
|
||||
|
@ -72,6 +84,10 @@ public class NickoBukkit extends JavaPlugin {
|
|||
|
||||
new PlaceHolderHook(this).hook();
|
||||
|
||||
if(!isUnitTesting()) {
|
||||
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);
|
||||
getServer().getPluginManager().registerEvents(new PlayerQuitListener(), this);
|
||||
|
||||
final ServerUtils serverUtils = new ServerUtils(this);
|
||||
serverUtils.checkSpigotBungeeCordHook();
|
||||
if (nickoConfiguration.isBungeecordEnabled()) {
|
||||
|
@ -80,6 +96,7 @@ public class NickoBukkit extends JavaPlugin {
|
|||
getServer().getMessenger().registerIncomingPluginChannel(this, NickoBungee.NICKO_PLUGIN_CHANNEL_UPDATE, new PluginMessageHandler());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getLogger().info("Nicko (Bukkit) has been enabled.");
|
||||
}
|
||||
|
@ -116,6 +133,10 @@ public class NickoBukkit extends JavaPlugin {
|
|||
|
||||
public PlayerDataStore getDataStore() { return dataStore; }
|
||||
|
||||
public boolean isUnitTesting() {
|
||||
return unitTesting;
|
||||
}
|
||||
|
||||
public Internals getInternals() {
|
||||
return InternalsProvider.getInternals();
|
||||
}
|
||||
|
|
|
@ -6,32 +6,97 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||
|
||||
public class NickoConfiguration {
|
||||
private final NickoBukkit nicko;
|
||||
private String prefix;
|
||||
private String defaultLocale;
|
||||
|
||||
private Boolean bungeecordSupport;
|
||||
private Boolean localStorage;
|
||||
|
||||
private String sqlUsername, sqlPassword, sqlAddress;
|
||||
|
||||
public NickoConfiguration(NickoBukkit nicko) {
|
||||
this.nicko = nicko;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return getConfig().getString("prefix");
|
||||
}
|
||||
|
||||
public String getDefaultLocale() { return getConfig().getString("locale"); }
|
||||
|
||||
public ConfigurationSection getBungeecordSection() { return getConfig().getConfigurationSection("bungeecord"); }
|
||||
|
||||
public ConfigurationSection getStorageSction() { return getConfig().getConfigurationSection("storage"); }
|
||||
public ConfigurationSection getStorageSection() { return getConfig().getConfigurationSection("storage"); }
|
||||
|
||||
public ConfigurationSection getRedisSection() { return getBungeecordSection().getConfigurationSection("redis"); }
|
||||
|
||||
public boolean isLocalStorage() { return getStorageSction().getBoolean("local"); }
|
||||
public String getPrefix() {
|
||||
if (prefix == null) {
|
||||
return prefix = getConfig().getString("prefix");
|
||||
}
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public boolean isBungeecordEnabled() { return getBungeecordSection().getBoolean("enabled"); }
|
||||
public void setPrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getStorageUsername() { return getStorageSction().getString("username"); }
|
||||
public String getDefaultLocale() {
|
||||
if (defaultLocale == null) {
|
||||
return defaultLocale = getConfig().getString("locale");
|
||||
}
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
public String getStoragePassword() { return getStorageSction().getString("password"); }
|
||||
public void setDefaultLocale(String defaultLocale) {
|
||||
this.defaultLocale = defaultLocale;
|
||||
}
|
||||
|
||||
public String getStorageAddress() { return getStorageSction().getString("address"); }
|
||||
public boolean isBungeecordEnabled() {
|
||||
if (bungeecordSupport == null) {
|
||||
return bungeecordSupport = getBungeecordSection().getBoolean("enabled");
|
||||
}
|
||||
return bungeecordSupport;
|
||||
}
|
||||
|
||||
public void setBungeecordSupport(Boolean bungeecordSupport) {
|
||||
this.bungeecordSupport = bungeecordSupport;
|
||||
}
|
||||
|
||||
public boolean isLocalStorage() {
|
||||
if (localStorage == null) {
|
||||
return localStorage = getStorageSection().getBoolean("local");
|
||||
}
|
||||
return localStorage;
|
||||
}
|
||||
|
||||
public void setLocalStorage(Boolean localStorage) {
|
||||
this.localStorage = localStorage;
|
||||
}
|
||||
|
||||
public String getSQLUsername() {
|
||||
if (sqlUsername == null) {
|
||||
return sqlUsername = getStorageSection().getString("username");
|
||||
}
|
||||
return sqlUsername;
|
||||
}
|
||||
|
||||
public void setSQLUsername(String sqlUsername) {
|
||||
this.sqlUsername = sqlUsername;
|
||||
}
|
||||
|
||||
public String getSQLPassword() {
|
||||
return getStorageSection().getString("password");
|
||||
}
|
||||
|
||||
public void setSQLPassword(String sqlPassword) {
|
||||
this.sqlPassword = sqlPassword;
|
||||
}
|
||||
|
||||
public String getSQLAddress() {
|
||||
if (sqlAddress == null) {
|
||||
return sqlAddress = getStorageSection().getString("address");
|
||||
}
|
||||
return sqlAddress;
|
||||
}
|
||||
|
||||
public void setSQLAddress(String sqlAddress) {
|
||||
this.sqlAddress = sqlAddress;
|
||||
}
|
||||
|
||||
private FileConfiguration getConfig() {
|
||||
return nicko.getConfig();
|
||||
|
|
|
@ -18,11 +18,12 @@ public class SQLStorageProvider implements StorageProvider {
|
|||
public boolean init() {
|
||||
try {
|
||||
final NickoConfiguration config = instance.getNickoConfig();
|
||||
connection = DriverManager.getConnection("jdbc://" + config.getStorageAddress(), config.getStorageUsername(), config.getStoragePassword());
|
||||
connection = DriverManager.getConnection("jdbc://" + config.getSQLAddress(), config.getSQLUsername(), config.getSQLPassword());
|
||||
final boolean initialized = connection != null && !connection.isClosed();
|
||||
|
||||
if (initialized) {
|
||||
if (!doesTableExist()) {
|
||||
instance.getLogger().info("Creating SQL tables...");
|
||||
return createTables();
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: ${project.parent.name}
|
||||
name: Nicko
|
||||
main: net.artelnatif.nicko.NickoBukkit
|
||||
version: ${project.version}
|
||||
version: 1.0-SNAPSHOT
|
||||
author: Aro
|
||||
api-version: 1.19
|
||||
softdepend: [ PlaceholderAPI ]
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package net.artelnatif.nicko.test.mock;
|
||||
|
||||
import be.seeseemelk.mockbukkit.ServerMock;
|
||||
|
||||
public class NickoServerMock extends ServerMock {
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package net.artelnatif.nicko.test.storage;
|
||||
|
||||
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||
import be.seeseemelk.mockbukkit.ServerMock;
|
||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.NickoConfiguration;
|
||||
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||
import net.artelnatif.nicko.test.mock.NickoServerMock;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class SQLStorageTest {
|
||||
private static ServerMock server;
|
||||
private static NickoBukkit plugin;
|
||||
private static NickoConfiguration config;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
server = MockBukkit.mock(new NickoServerMock());
|
||||
plugin = MockBukkit.load(NickoBukkit.class);
|
||||
config = plugin.getNickoConfig();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Create SQL Tables")
|
||||
public void testSQLTables() {
|
||||
config.setSQLAddress("localhost");
|
||||
config.setSQLUsername("root");
|
||||
config.setSQLPassword("12345"); // https://howsecureismypassword.net/ "Your password would be cracked: Instantly"
|
||||
|
||||
final PlayerMock playerMock = server.addPlayer("Aro");
|
||||
final Optional<NickoProfile> data = plugin.getDataStore().getData(playerMock.getUniqueId());
|
||||
Assertions.assertTrue(data.isPresent());
|
||||
Assertions.assertNull(data.get().getSkin());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void shutdown() {
|
||||
MockBukkit.unmock();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue