From aca1d6c0b15be0a8e17e1e9b975196884eda4b05 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 12 Apr 2021 09:02:50 -0700 Subject: [PATCH] Add test to find duplicate names - FlatFileDatabaseManagerTest --- .../nossr50/database/FlatFileDataProcessor.java | 15 ++++++++++++--- .../database/FlatFileDatabaseManager.java | 3 --- .../CategorizedFlatFileDataBuilder.java | 1 - .../database/FlatFileDatabaseManagerTest.java | 17 ++++++++++++++--- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java b/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java index 52f2fed51..6bac0bc1b 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java +++ b/src/main/java/com/gmail/nossr50/database/FlatFileDataProcessor.java @@ -113,6 +113,9 @@ public class FlatFileDataProcessor { return; } + uuids.add(uuid); + + if(names.contains(name)) { //Duplicate entry nameIsDupe = true; @@ -129,6 +132,8 @@ public class FlatFileDataProcessor { } } + 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); @@ -151,7 +156,7 @@ public class FlatFileDataProcessor { if(shouldNotBeEmpty(splitDataLine[i], i)) { badDataValues[i] = true; anyBadData = true; - reportBadDataLine("Data is empty when it should not be at index", "[EMPTY]", lineData); + reportBadDataLine("Data is empty when it should not be at index", "[index=" + i + "]", lineData); continue; } @@ -167,6 +172,8 @@ public class FlatFileDataProcessor { if(anyBadData) { builder.appendFlag(FlatFileDataFlag.BAD_VALUES); } + + registerData(builder); } public boolean shouldNotBeEmpty(String data, int index) { @@ -239,8 +246,10 @@ public class FlatFileDataProcessor { switch(dataIndex) { case USERNAME_INDEX: return ExpectedType.STRING; - case 2: //Used to be for something, no longer used - case 3: //Used to be for something, no longer used + case 2: //Assumption: Used to be for something, no longer used + case 3: //Assumption: Used to be for something, no longer used + case 23: //Assumption: Used to be used for something, no longer used + case 33: //Assumption: Used to be used for something, no longer used case HEALTHBAR: return ExpectedType.IGNORED; case SKILLS_MINING: diff --git a/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java index 33c88deab..41ee427d1 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java @@ -967,7 +967,6 @@ public final class FlatFileDatabaseManager implements DatabaseManager { public @Nullable List checkFileHealthAndStructure() { FlatFileDataProcessor dataProcessor = null; - int dataFlagCount = 0; if (usersFile.exists()) { BufferedReader bufferedReader = null; @@ -985,8 +984,6 @@ public final class FlatFileDatabaseManager implements DatabaseManager { } catch (IOException e) { e.printStackTrace(); } - - dataFlagCount = dataProcessor.getDataFlagCount(); } } diff --git a/src/main/java/com/gmail/nossr50/database/flatfile/CategorizedFlatFileDataBuilder.java b/src/main/java/com/gmail/nossr50/database/flatfile/CategorizedFlatFileDataBuilder.java index 205403a3e..0723ff3ef 100644 --- a/src/main/java/com/gmail/nossr50/database/flatfile/CategorizedFlatFileDataBuilder.java +++ b/src/main/java/com/gmail/nossr50/database/flatfile/CategorizedFlatFileDataBuilder.java @@ -22,7 +22,6 @@ public class CategorizedFlatFileDataBuilder { } public CategorizedFlatFileData build() { - assert dataFlags.size() > 0; return new CategorizedFlatFileData(uniqueProcessingId, dataFlags, stringDataRepresentation); } diff --git a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java index 2603a2aad..16ffb2106 100644 --- a/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java +++ b/src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java @@ -48,6 +48,11 @@ public class FlatFileDatabaseManagerTest { "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 = { + "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 = { "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:", @@ -66,7 +71,7 @@ public class FlatFileDatabaseManagerTest { @Test public void testPurgePowerlessUsers() { assertNotNull(db); - addDataToFile(db, normalDatabaseData); + replaceDataInFile(db, normalDatabaseData); int purgeCount = db.purgePowerlessUsers(); assertEquals(purgeCount, 1); //1 User should have been purged } @@ -75,7 +80,7 @@ public class FlatFileDatabaseManagerTest { public void testCheckFileHealthAndStructure() { assertNotNull(db); - addDataToFile(db, badDatabaseData); + replaceDataInFile(db, badDatabaseData); List dataFlags = db.checkFileHealthAndStructure(); assertNotNull(dataFlags); @@ -84,7 +89,13 @@ 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)); } @Test @@ -119,7 +130,7 @@ public class FlatFileDatabaseManagerTest { } - private void addDataToFile(@NotNull FlatFileDatabaseManager flatFileDatabaseManager, @NotNull String[] dataEntries) { + private void replaceDataInFile(@NotNull FlatFileDatabaseManager flatFileDatabaseManager, @NotNull String[] dataEntries) { String filePath = flatFileDatabaseManager.getUsersFile().getAbsolutePath(); BufferedReader in = null; FileWriter out = null;