feat(test): unit testing support

This commit is contained in:
aro 2022-12-10 22:29:51 +01:00
parent 7b82126f40
commit 7272809044
7 changed files with 173 additions and 33 deletions

View file

@ -19,26 +19,41 @@ 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;
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(!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)) {
@ -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,12 +84,17 @@ public class NickoBukkit extends JavaPlugin {
new PlaceHolderHook(this).hook();
final ServerUtils serverUtils = new ServerUtils(this);
serverUtils.checkSpigotBungeeCordHook();
if (nickoConfiguration.isBungeecordEnabled()) {
if (serverUtils.checkBungeeCordHook()) {
getLogger().info("Enabling BungeeCord support...");
getServer().getMessenger().registerIncomingPluginChannel(this, NickoBungee.NICKO_PLUGIN_CHANNEL_UPDATE, new PluginMessageHandler());
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()) {
if (serverUtils.checkBungeeCordHook()) {
getLogger().info("Enabling BungeeCord support...");
getServer().getMessenger().registerIncomingPluginChannel(this, NickoBungee.NICKO_PLUGIN_CHANNEL_UPDATE, new PluginMessageHandler());
}
}
}
@ -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();
}

View file

@ -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();

View file

@ -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;

View file

@ -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 ]