test: broken sql connection + more adequate logging levels

This commit is contained in:
aro 2023-01-23 17:18:31 +01:00
parent 72c2c5ed12
commit d95a9e844a
5 changed files with 60 additions and 12 deletions

View file

@ -25,7 +25,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.plugin.java.JavaPluginLoader;
import java.io.File; import java.io.File;
import java.util.logging.Level;
public class NickoBukkit extends JavaPlugin { public class NickoBukkit extends JavaPlugin {
private static NickoBukkit plugin; private static NickoBukkit plugin;
@ -66,7 +65,7 @@ public class NickoBukkit extends JavaPlugin {
if (!dataStore.getStorage().getProvider().init()) { if (!dataStore.getStorage().getProvider().init()) {
dataStore.getStorage().setError(true); dataStore.getStorage().setError(true);
getLogger().warning("Failed to open persistence, data will NOT be saved!"); getLogger().severe("Failed to open persistence, data will NOT be saved!");
} }
} }
@ -78,7 +77,7 @@ public class NickoBukkit extends JavaPlugin {
getLogger().info("Loading internals..."); getLogger().info("Loading internals...");
if (getInternals() == null) { if (getInternals() == null) {
getLogger().log(Level.SEVERE, "Nicko could not find a valid implementation for this server version. Is your server supported?"); getLogger().severe("Nicko could not find a valid implementation for this server version. Is your server supported?");
dataStore.getStorage().setError(true); dataStore.getStorage().setError(true);
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
} }
@ -87,7 +86,7 @@ public class NickoBukkit extends JavaPlugin {
getLogger().info("Loading persistence..."); getLogger().info("Loading persistence...");
if (!dataStore.getStorage().getProvider().init()) { if (!dataStore.getStorage().getProvider().init()) {
dataStore.getStorage().setError(true); dataStore.getStorage().setError(true);
getLogger().warning("Failed to open persistence, data will NOT be saved!"); getLogger().severe("Failed to open persistence, data will NOT be saved!");
} }
mojangAPI = new MojangAPI(this); mojangAPI = new MojangAPI(this);
@ -97,7 +96,7 @@ public class NickoBukkit extends JavaPlugin {
if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) { if (localeFileManager.dumpFromLocale(Locale.ENGLISH)) {
getLogger().info("Successfully loaded custom language file."); getLogger().info("Successfully loaded custom language file.");
} else { } else {
getLogger().warning("Failed to load custom language file!"); getLogger().severe("Failed to load custom language file!");
} }
} }
@ -135,7 +134,7 @@ public class NickoBukkit extends JavaPlugin {
dataStore.removeAllNames(); dataStore.removeAllNames();
dataStore.saveAll(); dataStore.saveAll();
if (!dataStore.getStorage().getProvider().close()) { if (!dataStore.getStorage().getProvider().close()) {
getLogger().warning("Failed to close persistence!"); getLogger().severe("Failed to close persistence!");
} }
} }

View file

@ -15,10 +15,10 @@ public class BungeeCordSupport {
final Server server = instance.getServer(); final Server server = instance.getServer();
final YamlConfiguration config = server.spigot().getConfig(); final YamlConfiguration config = server.spigot().getConfig();
if (config.getConfigurationSection("settings").getBoolean("bungeecord") && instance.getNickoConfig().isBungeecordSupport()) { if (config.getConfigurationSection("settings").getBoolean("bungeecord") && instance.getNickoConfig().isBungeecordSupport()) {
instance.getLogger().severe("Hummm. Your server is hooked to BungeeCord, but it seems"); instance.getLogger().warning("Hummm. Your server is hooked to BungeeCord, but it seems");
instance.getLogger().severe("that BungeeCord support is not enabled inside Nicko."); instance.getLogger().warning("that BungeeCord support is not enabled inside Nicko.");
instance.getLogger().severe("If this is intentional, you can safely ignore this message."); instance.getLogger().warning("If this is intentional, you can safely ignore this message.");
instance.getLogger().severe("Otherwise, you can enable BungeeCord support inside Nicko's configuration file."); instance.getLogger().warning("Otherwise, you can enable BungeeCord support inside Nicko's configuration file.");
} }
} }

View file

@ -50,7 +50,7 @@ public class SQLStorage extends Storage {
statement.executeUpdate(); statement.executeUpdate();
return new ActionResult<>(); return new ActionResult<>();
} catch (SQLException e) { } catch (SQLException e) {
instance.getLogger().warning("Unable to store player. (%s)".formatted(e.getMessage())); instance.getLogger().warning("Couldn't send SQL Request: " + e.getMessage());
return new ActionResult<>(I18NDict.Error.SQL_ERROR); return new ActionResult<>(I18NDict.Error.SQL_ERROR);
} }
} }

View file

@ -40,7 +40,7 @@ public class SQLStorageProvider implements StorageProvider {
createTable(); createTable();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); instance.getLogger().severe("Couldn't establish a connection to the MySQL database: " + e.getMessage());
return false; return false;
} }
} }

View file

@ -0,0 +1,49 @@
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.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.Locale;
import org.junit.jupiter.api.*;
public class BrokenSQLTest {
private static ServerMock server;
private static NickoBukkit plugin;
@BeforeAll
public static void setup() {
final NickoConfiguration config = new NickoConfiguration(null);
config.setLocalStorage(false);
config.setBungeecordSupport(false);
config.setSQLAddress("127.0.0.1");
config.setSQLUsername("root");
config.setSQLPassword("INVALID_PASSWORD");
server = MockBukkit.mock();
plugin = MockBukkit.load(NickoBukkit.class, config);
}
@Test
@DisplayName("Fail to create Tables")
public void createSQLTables() {
Assertions.assertTrue(plugin.getDataStore().getStorage().isError());
}
@Test
@DisplayName("Store Player Via SQL")
public void storePlayer() {
final PlayerMock playerMock = server.addPlayer();
final NickoProfile profile = new NickoProfile("Notch", "Notch", Locale.ENGLISH, true);
final ActionResult<Void> storeAction = plugin.getDataStore().getStorage().store(playerMock.getUniqueId(), profile);
Assertions.assertFalse(storeAction.isError());
}
@AfterAll
public static void shutdown() {
MockBukkit.unmock();
}
}