From 217867ab52d7d47347db802a7a56179eb5a942a2 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 27 Dec 2018 18:39:36 +0200 Subject: [PATCH] Try to fix MySQL Tests The tests were failing due to missing Session in the database. The foreign key checks were not enforced on SQLite so the session was not being saved previously. - Session now saved in the 3 tests dependending on it - Removed empty ignored test method - Flipped an equals in WorldTimes --- .../djrapitops/plan/data/time/WorldTimes.java | 2 +- .../database/databases/CommonDBTest.java | 54 ++++++++----------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java b/Plan/common/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java index 0ae66019b..1ecc3b809 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/data/time/WorldTimes.java @@ -77,7 +77,7 @@ public class WorldTimes { */ public void updateState(String worldName, String gameMode, long changeTime) { GMTimes currentGMTimes = times.get(currentWorld); - if (worldName.equals(currentWorld)) { + if (currentWorld.equals(worldName)) { currentGMTimes.changeState(gameMode, changeTime); } else { GMTimes newGMTimes = times.get(worldName); diff --git a/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java b/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java index e455a829d..ad2830e0d 100644 --- a/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java +++ b/Plan/plugin/src/test/java/com/djrapitops/plan/system/database/databases/CommonDBTest.java @@ -84,7 +84,6 @@ public abstract class CommonDBTest { public Timeout globalTimeout = Timeout.seconds(5); static void handleSetup(String dbName) throws Exception { - System.out.println("--- Test Class Setup ---"); system = component.getPlanSystem(); system.getConfigSystem().getConfig().set(WebserverSettings.PORT, TEST_PORT_NUMBER); system.enable(); @@ -93,7 +92,6 @@ public abstract class CommonDBTest { db = (SQLDB) dbSystem.getActiveDatabaseByName(dbName); db.init(); - System.out.println("--- Class Setup Complete ---\n"); } @AfterClass @@ -103,12 +101,10 @@ public abstract class CommonDBTest { @Before public void setUp() { - System.out.println("\n-- Clearing Test Database --"); db.remove().everything(); ServerTable serverTable = db.getServerTable(); serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20)); assertEquals(serverUUID, db.getServerUUIDSupplier().get()); - System.out.println("-- Clear Complete --\n"); } public void commitTest() throws DBInitException { @@ -390,17 +386,6 @@ public abstract class CommonDBTest { commitTest(); Map> sessions = sessionsTable.getSessions(playerUUID); - - for (Map.Entry> entry : sessions.entrySet()) { - UUID key = entry.getKey(); - if (key == null) { - System.out.print("null"); - } else { - System.out.print(key); - } - System.out.println(" " + entry.getValue()); - } - List savedSessions = sessions.get(serverUUID); assertNotNull(savedSessions); @@ -572,7 +557,6 @@ public abstract class CommonDBTest { } private void saveAllData(SQLDB database) throws NoSuchAlgorithmException { - System.out.println("Saving all possible data to the Database.."); UserInfoTable userInfoTable = database.getUserInfoTable(); UsersTable usersTable = database.getUsersTable(); SessionsTable sessionsTable = database.getSessionsTable(); @@ -631,7 +615,6 @@ public abstract class CommonDBTest { )); securityTable.addNewUser(new WebUser("Test", "RandomGarbageBlah", 0)); - System.out.println("Done!\n"); } @Test @@ -672,13 +655,14 @@ public abstract class CommonDBTest { saveUserOne(); saveUserTwo(); - KillsTable killsTable = db.getKillsTable(); + Session session = createSession(); List expected = createKills(); - killsTable.savePlayerKills(playerUUID, 1, expected); + session.setPlayerKills(expected); + db.getSessionsTable().saveSession(playerUUID, session); commitTest(); - Map> playerKills = killsTable.getPlayerKills(); + Map> playerKills = db.getKillsTable().getPlayerKills(); List kills = playerKills.get(playerUUID); assertFalse(playerKills.isEmpty()); assertNotNull(kills); @@ -686,18 +670,26 @@ public abstract class CommonDBTest { assertEquals(expected, kills); } + private Session createSession() { + Session session = new Session( + playerUUID, + serverUUID, + System.currentTimeMillis(), + "world", + GMTimes.getGMKeyArray()[0] + ); + session.endSession(System.currentTimeMillis() + 1L); + return session; + } + @Test public void testBackupAndRestore() throws Exception { - System.out.println("- Creating Backup Database -"); H2DB backup = dbSystem.getH2Factory().usingFile(temporaryFolder.newFile("backup.db")); backup.init(); - System.out.println("- Backup Database Created -"); saveAllData(db); - System.out.println("Running backup.."); db.backup().backup(backup); - System.out.println("Backup Complete!"); UserInfoTable userInfoTable = backup.getUserInfoTable(); UsersTable usersTable = backup.getUsersTable(); @@ -726,9 +718,11 @@ public abstract class CommonDBTest { saveUserOne(); WorldTimes worldTimes = createWorldTimes(); WorldTimesTable worldTimesTable = db.getWorldTimesTable(); - worldTimesTable.saveWorldTimes(playerUUID, 1, worldTimes); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); + session.setWorldTimes(worldTimes); + db.getSessionsTable().saveSession(playerUUID, session); + Map sessions = new HashMap<>(); sessions.put(1, session); worldTimesTable.addWorldTimesToSessions(playerUUID, sessions); @@ -740,10 +734,11 @@ public abstract class CommonDBTest { public void testSaveAllWorldTimes() { saveUserOne(); WorldTimes worldTimes = createWorldTimes(); - System.out.println(worldTimes); + WorldTimesTable worldTimesTable = db.getWorldTimesTable(); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); session.setWorldTimes(worldTimes); + db.getSessionsTable().saveSession(playerUUID, session); Map>> map = new HashMap<>(); Map> sessionMap = new HashMap<>(); @@ -764,7 +759,6 @@ public abstract class CommonDBTest { saveUserOne(); WorldTimes worldTimes = createWorldTimes(); - System.out.println(worldTimes); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); session.setWorldTimes(worldTimes); @@ -813,11 +807,6 @@ public abstract class CommonDBTest { assertEquals(new HashSet<>(worlds), worldNames); } - @Test - @Ignore("Not yet re-implemented") - public void testSettingTransfer() { - } - @Test public void testGetNetworkGeolocations() { GeoInfoTable geoInfoTable = db.getGeoInfoTable(); @@ -837,7 +826,6 @@ public abstract class CommonDBTest { geoInfoTable.saveGeoInfo(thirdUuid, new GeoInfo("-", "Test4", 0, "3")); List geolocations = geoInfoTable.getNetworkGeolocations(); - System.out.println(geolocations); assertNotNull(geolocations); assertFalse(geolocations.isEmpty());