Add more tests to FlatFileDatabaseManagerTest

This commit is contained in:
nossr50 2021-04-12 09:33:43 -07:00
parent aca1d6c0b1
commit 60013c710b
3 changed files with 60 additions and 28 deletions

View File

@ -10,5 +10,5 @@ public enum FlatFileDataFlag {
MISSING_OR_NULL_UUID, MISSING_OR_NULL_UUID,
TOO_INCOMPLETE, TOO_INCOMPLETE,
JUNK, JUNK,
EMPTY, EMPTY_LINE,
} }

View File

@ -39,7 +39,7 @@ public class FlatFileDataProcessor {
* Is the line empty? * Is the line empty?
*/ */
if (lineData.isEmpty()) { if (lineData.isEmpty()) {
registerData(builder.appendFlag(FlatFileDataFlag.EMPTY)); registerData(builder.appendFlag(FlatFileDataFlag.EMPTY_LINE));
return; return;
} }
@ -61,6 +61,10 @@ public class FlatFileDataProcessor {
corruptDataFound = true; 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 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()) { && splitDataLine[0] != null && !splitDataLine[0].isEmpty()) {
if(splitDataLine[0].length() <= 16 && splitDataLine[0].length() >= 3) { if(splitDataLine[0].length() <= 16 && splitDataLine[0].length() >= 3) {
@ -68,10 +72,10 @@ public class FlatFileDataProcessor {
registerData(builder.appendFlag(FlatFileDataFlag.TOO_INCOMPLETE)); registerData(builder.appendFlag(FlatFileDataFlag.TOO_INCOMPLETE));
return; 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 //Make sure the data is up to date schema wise
if(splitDataLine.length < DATA_ENTRY_COUNT) { if(splitDataLine.length < DATA_ENTRY_COUNT) {
String[] correctSizeSplitData = Arrays.copyOf(splitDataLine, DATA_ENTRY_COUNT); splitDataLine = Arrays.copyOf(splitDataLine, DATA_ENTRY_COUNT+1);
lineData = org.apache.commons.lang.StringUtils.join(correctSizeSplitData, ":") + ":"; lineData = org.apache.commons.lang.StringUtils.join(splitDataLine, ":") + ":";
splitDataLine = lineData.split(":");
builder.appendFlag(FlatFileDataFlag.INCOMPLETE); builder.appendFlag(FlatFileDataFlag.INCOMPLETE);
builder.setStringDataRepresentation(lineData); builder.setStringDataRepresentation(lineData);
} }
@ -180,7 +184,7 @@ public class FlatFileDataProcessor {
if(getExpectedValueType(index) == ExpectedType.IGNORED) { if(getExpectedValueType(index) == ExpectedType.IGNORED) {
return false; return false;
} else { } else {
return data.isEmpty(); return data == null || data.isEmpty();
} }
} }

View File

@ -42,26 +42,50 @@ public class FlatFileDatabaseManagerTest {
} }
//Nothing wrong with this database //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:", "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:", "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:" "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: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:", "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:", "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:", "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:", "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:", "の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:", "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 //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:", "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 //Second entry here has an integer value replaced by a string
@ -70,7 +94,6 @@ public class FlatFileDatabaseManagerTest {
@Test @Test
public void testPurgePowerlessUsers() { public void testPurgePowerlessUsers() {
assertNotNull(db);
replaceDataInFile(db, normalDatabaseData); replaceDataInFile(db, normalDatabaseData);
int purgeCount = db.purgePowerlessUsers(); int purgeCount = db.purgePowerlessUsers();
assertEquals(purgeCount, 1); //1 User should have been purged assertEquals(purgeCount, 1); //1 User should have been purged
@ -78,8 +101,6 @@ public class FlatFileDatabaseManagerTest {
@Test @Test
public void testCheckFileHealthAndStructure() { public void testCheckFileHealthAndStructure() {
assertNotNull(db);
replaceDataInFile(db, badDatabaseData); replaceDataInFile(db, badDatabaseData);
List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure(); List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure();
@ -89,38 +110,37 @@ public class FlatFileDatabaseManagerTest {
@Test @Test
public void testFindDuplicateNames() { public void testFindDuplicateNames() {
assertNotNull(db); addDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME_FIXABLE);
replaceDataInFile(db, duplicateNameDatabaseData);
List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure();
assertNotNull(dataFlags);
assertTrue(dataFlags.contains(FlatFileDataFlag.DUPLICATE_NAME_FIXABLE));
} }
@Test @Test
public void testFindDuplicateUUIDs() { public void testFindDuplicateUUIDs() {
addDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID);
} }
@Test @Test
public void testFindCorruptData() { public void testFindCorruptData() {
addDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.JUNK);
} }
@Test @Test
public void testFindEmptyNames() { public void testFindEmptyNames() {
addDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME);
}
@Test
public void testFindEmptyLine() {
addDataAndCheckForFlag(db, emptyLineDatabaseData, FlatFileDataFlag.EMPTY_LINE);
} }
@Test @Test
public void testFindBadValues() { public void testFindBadValues() {
addDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES);
} }
@Test @Test
public void testFindOutdatedData() { public void testFindOutdatedData() {
addDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE);
} }
@Test @Test
@ -182,4 +202,12 @@ public class FlatFileDatabaseManagerTest {
} }
} }
private void addDataAndCheckForFlag(@NotNull FlatFileDatabaseManager targetDatabase, @NotNull String[] data, @NotNull FlatFileDataFlag flag) {
replaceDataInFile(targetDatabase, data);
List<FlatFileDataFlag> dataFlags = targetDatabase.checkFileHealthAndStructure();
assertNotNull(dataFlags);
assertTrue(dataFlags.contains(flag));
}
} }