From 60013c710bce88af76352eef64b8f07fd87c9ad3 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 12 Apr 2021 09:33:43 -0700 Subject: [PATCH] Add more tests to FlatFileDatabaseManagerTest --- .../nossr50/database/FlatFileDataFlag.java | 2 +- .../database/FlatFileDataProcessor.java | 22 ++++--- .../database/FlatFileDatabaseManagerTest.java | 64 +++++++++++++------ 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/FlatFileDataFlag.java b/src/main/java/com/gmail/nossr50/database/FlatFileDataFlag.java index 12dab5a84..deae836b3 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatFileDataFlag.java +++ b/src/main/java/com/gmail/nossr50/database/FlatFileDataFlag.java @@ -10,5 +10,5 @@ public enum FlatFileDataFlag { MISSING_OR_NULL_UUID, TOO_INCOMPLETE, JUNK, - EMPTY, + EMPTY_LINE, } diff --git a/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java b/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java index 6bac0bc1b..fe415fc79 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java +++ b/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java @@ -39,7 +39,7 @@ public class FlatFileDataProcessor { * Is the line empty? */ if (lineData.isEmpty()) { - registerData(builder.appendFlag(FlatFileDataFlag.EMPTY)); + registerData(builder.appendFlag(FlatFileDataFlag.EMPTY_LINE)); return; } @@ -61,6 +61,10 @@ public class FlatFileDataProcessor { corruptDataFound = true; } + //Flag as junk (corrupt) + builder.appendFlag(FlatFileDataFlag.JUNK); + + //TODO: This block here is probably pointless if(splitDataLine.length >= 10 //The value here is kind of arbitrary, it shouldn't be too low to avoid false positives, but also we aren't really going to correctly identify when player data has been corrupted or not with 100% accuracy ever && splitDataLine[0] != null && !splitDataLine[0].isEmpty()) { if(splitDataLine[0].length() <= 16 && splitDataLine[0].length() >= 3) { @@ -68,10 +72,10 @@ public class FlatFileDataProcessor { registerData(builder.appendFlag(FlatFileDataFlag.TOO_INCOMPLETE)); return; } - } else { - registerData(builder.appendFlag(FlatFileDataFlag.JUNK)); - return; } + + registerData(builder.appendFlag(FlatFileDataFlag.JUNK)); + return; } /* @@ -132,13 +136,13 @@ public class FlatFileDataProcessor { } } - names.add(name); + if(!name.isEmpty()) + names.add(name); //Make sure the data is up to date schema wise if(splitDataLine.length < DATA_ENTRY_COUNT) { - String[] correctSizeSplitData = Arrays.copyOf(splitDataLine, DATA_ENTRY_COUNT); - lineData = org.apache.commons.lang.StringUtils.join(correctSizeSplitData, ":") + ":"; - splitDataLine = lineData.split(":"); + splitDataLine = Arrays.copyOf(splitDataLine, DATA_ENTRY_COUNT+1); + lineData = org.apache.commons.lang.StringUtils.join(splitDataLine, ":") + ":"; builder.appendFlag(FlatFileDataFlag.INCOMPLETE); builder.setStringDataRepresentation(lineData); } @@ -180,7 +184,7 @@ public class FlatFileDataProcessor { if(getExpectedValueType(index) == ExpectedType.IGNORED) { return false; } else { - return data.isEmpty(); + return data == null || data.isEmpty(); } } diff --git a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java index 16ffb2106..f9ca5fbf9 100644 --- a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java +++ b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java @@ -42,26 +42,50 @@ public class FlatFileDatabaseManagerTest { } //Nothing wrong with this database - private static String[] normalDatabaseData = { + private static final String[] normalDatabaseData = { "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:", "powerless:0:::0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0::0:0:0:0:0:0:0:0:0::0:0:0:0:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:" }; - private static String[] duplicateNameDatabaseData = { + private static final String[] outdatedDatabaseData = { + "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", + "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:", + "powerless:0:::0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0::0:0:0:0:0:0:0:0:0::0:0:0:0:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:" //This user is missing data added after UUID index + }; + + private static final String[] emptyLineDatabaseData = { + "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", + "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:", + "powerless:0:::0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0::0:0:0:0:0:0:0:0:0::0:0:0:0:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:", + "" //EMPTY LINE + }; + + private static final String[] emptyNameDatabaseData = { + ":1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", + "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:", + "powerless:0:::0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0::0:0:0:0:0:0:0:0:0::0:0:0:0:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:" + }; + + private static final String[] duplicateNameDatabaseData = { "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:631e3896-da2a-4077-974b-d047859d76bc:0:0:", }; - private static String[] corruptDatabaseData = { + private static final String[] duplicateUUIDDatabaseData = { "nossr50:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", + "mrfloris:1000:::0:1000:640:1000:1000:1000:1000:1000:1000:1000:1000:16:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", + }; + + private static final String[] corruptDatabaseData = { + "nossr50:1000:::0:100:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:", "corruptdataboy:の:::ののの0:2452:0:1983:1937:1790:3042ののののの:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617のののののの583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:d20c6e8d-5615-4284-b8d1-e20b92011530:5:1600906906:", "のjapaneseuserの:333:::0:2452:0:444:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:25870f0e-7558-4659-9f60-417e24cb3332:5:1600906906:", "sameUUIDasjapaneseuser:333:::0:442:0:544:1937:1790:3042:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:25870f0e-7558-4659-9f60-417e24cb3332:5:1600906906:", }; - private static String[] badDatabaseData = { + private static final String[] badDatabaseData = { //First entry here is missing some values "nossr50:1000:0:500:0:0:0:0:0::1000:0:0:0:1593543012:0:0:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:", //Second entry here has an integer value replaced by a string @@ -70,7 +94,6 @@ public class FlatFileDatabaseManagerTest { @Test public void testPurgePowerlessUsers() { - assertNotNull(db); replaceDataInFile(db, normalDatabaseData); int purgeCount = db.purgePowerlessUsers(); assertEquals(purgeCount, 1); //1 User should have been purged @@ -78,8 +101,6 @@ public class FlatFileDatabaseManagerTest { @Test public void testCheckFileHealthAndStructure() { - assertNotNull(db); - replaceDataInFile(db, badDatabaseData); List dataFlags = db.checkFileHealthAndStructure(); @@ -89,38 +110,37 @@ public class FlatFileDatabaseManagerTest { @Test public void testFindDuplicateNames() { - assertNotNull(db); - - replaceDataInFile(db, duplicateNameDatabaseData); - - List dataFlags = db.checkFileHealthAndStructure(); - assertNotNull(dataFlags); - assertTrue(dataFlags.contains(FlatFileDataFlag.DUPLICATE_NAME_FIXABLE)); + addDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME_FIXABLE); } @Test public void testFindDuplicateUUIDs() { - + addDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID); } @Test public void testFindCorruptData() { - + addDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.JUNK); } @Test public void testFindEmptyNames() { + addDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME); + } + @Test + public void testFindEmptyLine() { + addDataAndCheckForFlag(db, emptyLineDatabaseData, FlatFileDataFlag.EMPTY_LINE); } @Test public void testFindBadValues() { - + addDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES); } @Test public void testFindOutdatedData() { - + addDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE); } @Test @@ -182,4 +202,12 @@ public class FlatFileDatabaseManagerTest { } } + + private void addDataAndCheckForFlag(@NotNull FlatFileDatabaseManager targetDatabase, @NotNull String[] data, @NotNull FlatFileDataFlag flag) { + replaceDataInFile(targetDatabase, data); + + List dataFlags = targetDatabase.checkFileHealthAndStructure(); + assertNotNull(dataFlags); + assertTrue(dataFlags.contains(flag)); + } } \ No newline at end of file