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
This commit is contained in:
Rsl1122 2018-12-27 18:39:36 +02:00
parent f2642f4079
commit 217867ab52
2 changed files with 22 additions and 34 deletions

View File

@ -77,7 +77,7 @@ public class WorldTimes {
*/ */
public void updateState(String worldName, String gameMode, long changeTime) { public void updateState(String worldName, String gameMode, long changeTime) {
GMTimes currentGMTimes = times.get(currentWorld); GMTimes currentGMTimes = times.get(currentWorld);
if (worldName.equals(currentWorld)) { if (currentWorld.equals(worldName)) {
currentGMTimes.changeState(gameMode, changeTime); currentGMTimes.changeState(gameMode, changeTime);
} else { } else {
GMTimes newGMTimes = times.get(worldName); GMTimes newGMTimes = times.get(worldName);

View File

@ -84,7 +84,6 @@ public abstract class CommonDBTest {
public Timeout globalTimeout = Timeout.seconds(5); public Timeout globalTimeout = Timeout.seconds(5);
static void handleSetup(String dbName) throws Exception { static void handleSetup(String dbName) throws Exception {
System.out.println("--- Test Class Setup ---");
system = component.getPlanSystem(); system = component.getPlanSystem();
system.getConfigSystem().getConfig().set(WebserverSettings.PORT, TEST_PORT_NUMBER); system.getConfigSystem().getConfig().set(WebserverSettings.PORT, TEST_PORT_NUMBER);
system.enable(); system.enable();
@ -93,7 +92,6 @@ public abstract class CommonDBTest {
db = (SQLDB) dbSystem.getActiveDatabaseByName(dbName); db = (SQLDB) dbSystem.getActiveDatabaseByName(dbName);
db.init(); db.init();
System.out.println("--- Class Setup Complete ---\n");
} }
@AfterClass @AfterClass
@ -103,12 +101,10 @@ public abstract class CommonDBTest {
@Before @Before
public void setUp() { public void setUp() {
System.out.println("\n-- Clearing Test Database --");
db.remove().everything(); db.remove().everything();
ServerTable serverTable = db.getServerTable(); ServerTable serverTable = db.getServerTable();
serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20)); serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20));
assertEquals(serverUUID, db.getServerUUIDSupplier().get()); assertEquals(serverUUID, db.getServerUUIDSupplier().get());
System.out.println("-- Clear Complete --\n");
} }
public void commitTest() throws DBInitException { public void commitTest() throws DBInitException {
@ -390,17 +386,6 @@ public abstract class CommonDBTest {
commitTest(); commitTest();
Map<UUID, List<Session>> sessions = sessionsTable.getSessions(playerUUID); Map<UUID, List<Session>> sessions = sessionsTable.getSessions(playerUUID);
for (Map.Entry<UUID, List<Session>> 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<Session> savedSessions = sessions.get(serverUUID); List<Session> savedSessions = sessions.get(serverUUID);
assertNotNull(savedSessions); assertNotNull(savedSessions);
@ -572,7 +557,6 @@ public abstract class CommonDBTest {
} }
private void saveAllData(SQLDB database) throws NoSuchAlgorithmException { private void saveAllData(SQLDB database) throws NoSuchAlgorithmException {
System.out.println("Saving all possible data to the Database..");
UserInfoTable userInfoTable = database.getUserInfoTable(); UserInfoTable userInfoTable = database.getUserInfoTable();
UsersTable usersTable = database.getUsersTable(); UsersTable usersTable = database.getUsersTable();
SessionsTable sessionsTable = database.getSessionsTable(); SessionsTable sessionsTable = database.getSessionsTable();
@ -631,7 +615,6 @@ public abstract class CommonDBTest {
)); ));
securityTable.addNewUser(new WebUser("Test", "RandomGarbageBlah", 0)); securityTable.addNewUser(new WebUser("Test", "RandomGarbageBlah", 0));
System.out.println("Done!\n");
} }
@Test @Test
@ -672,13 +655,14 @@ public abstract class CommonDBTest {
saveUserOne(); saveUserOne();
saveUserTwo(); saveUserTwo();
KillsTable killsTable = db.getKillsTable(); Session session = createSession();
List<PlayerKill> expected = createKills(); List<PlayerKill> expected = createKills();
killsTable.savePlayerKills(playerUUID, 1, expected); session.setPlayerKills(expected);
db.getSessionsTable().saveSession(playerUUID, session);
commitTest(); commitTest();
Map<UUID, List<PlayerKill>> playerKills = killsTable.getPlayerKills(); Map<UUID, List<PlayerKill>> playerKills = db.getKillsTable().getPlayerKills();
List<PlayerKill> kills = playerKills.get(playerUUID); List<PlayerKill> kills = playerKills.get(playerUUID);
assertFalse(playerKills.isEmpty()); assertFalse(playerKills.isEmpty());
assertNotNull(kills); assertNotNull(kills);
@ -686,18 +670,26 @@ public abstract class CommonDBTest {
assertEquals(expected, kills); 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 @Test
public void testBackupAndRestore() throws Exception { public void testBackupAndRestore() throws Exception {
System.out.println("- Creating Backup Database -");
H2DB backup = dbSystem.getH2Factory().usingFile(temporaryFolder.newFile("backup.db")); H2DB backup = dbSystem.getH2Factory().usingFile(temporaryFolder.newFile("backup.db"));
backup.init(); backup.init();
System.out.println("- Backup Database Created -");
saveAllData(db); saveAllData(db);
System.out.println("Running backup..");
db.backup().backup(backup); db.backup().backup(backup);
System.out.println("Backup Complete!");
UserInfoTable userInfoTable = backup.getUserInfoTable(); UserInfoTable userInfoTable = backup.getUserInfoTable();
UsersTable usersTable = backup.getUsersTable(); UsersTable usersTable = backup.getUsersTable();
@ -726,9 +718,11 @@ public abstract class CommonDBTest {
saveUserOne(); saveUserOne();
WorldTimes worldTimes = createWorldTimes(); WorldTimes worldTimes = createWorldTimes();
WorldTimesTable worldTimesTable = db.getWorldTimesTable(); WorldTimesTable worldTimesTable = db.getWorldTimesTable();
worldTimesTable.saveWorldTimes(playerUUID, 1, worldTimes);
Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0);
session.setWorldTimes(worldTimes);
db.getSessionsTable().saveSession(playerUUID, session);
Map<Integer, Session> sessions = new HashMap<>(); Map<Integer, Session> sessions = new HashMap<>();
sessions.put(1, session); sessions.put(1, session);
worldTimesTable.addWorldTimesToSessions(playerUUID, sessions); worldTimesTable.addWorldTimesToSessions(playerUUID, sessions);
@ -740,10 +734,11 @@ public abstract class CommonDBTest {
public void testSaveAllWorldTimes() { public void testSaveAllWorldTimes() {
saveUserOne(); saveUserOne();
WorldTimes worldTimes = createWorldTimes(); WorldTimes worldTimes = createWorldTimes();
System.out.println(worldTimes);
WorldTimesTable worldTimesTable = db.getWorldTimesTable(); WorldTimesTable worldTimesTable = db.getWorldTimesTable();
Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0);
session.setWorldTimes(worldTimes); session.setWorldTimes(worldTimes);
db.getSessionsTable().saveSession(playerUUID, session);
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>(); Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
Map<UUID, List<Session>> sessionMap = new HashMap<>(); Map<UUID, List<Session>> sessionMap = new HashMap<>();
@ -764,7 +759,6 @@ public abstract class CommonDBTest {
saveUserOne(); saveUserOne();
WorldTimes worldTimes = createWorldTimes(); WorldTimes worldTimes = createWorldTimes();
System.out.println(worldTimes);
Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0); Session session = new Session(1, playerUUID, serverUUID, 12345L, 23456L, 0, 0, 0);
session.setWorldTimes(worldTimes); session.setWorldTimes(worldTimes);
@ -813,11 +807,6 @@ public abstract class CommonDBTest {
assertEquals(new HashSet<>(worlds), worldNames); assertEquals(new HashSet<>(worlds), worldNames);
} }
@Test
@Ignore("Not yet re-implemented")
public void testSettingTransfer() {
}
@Test @Test
public void testGetNetworkGeolocations() { public void testGetNetworkGeolocations() {
GeoInfoTable geoInfoTable = db.getGeoInfoTable(); GeoInfoTable geoInfoTable = db.getGeoInfoTable();
@ -837,7 +826,6 @@ public abstract class CommonDBTest {
geoInfoTable.saveGeoInfo(thirdUuid, new GeoInfo("-", "Test4", 0, "3")); geoInfoTable.saveGeoInfo(thirdUuid, new GeoInfo("-", "Test4", 0, "3"));
List<String> geolocations = geoInfoTable.getNetworkGeolocations(); List<String> geolocations = geoInfoTable.getNetworkGeolocations();
System.out.println(geolocations);
assertNotNull(geolocations); assertNotNull(geolocations);
assertFalse(geolocations.isEmpty()); assertFalse(geolocations.isEmpty());