Add more tests, fix null name bug

This commit is contained in:
nossr50
2021-04-13 12:41:23 -07:00
parent 5b4af3f9ce
commit f7339277f8
25 changed files with 491 additions and 128 deletions

View File

@@ -2,6 +2,11 @@ package com.gmail.nossr50.database;
import com.gmail.nossr50.TestUtil;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.util.skills.SkillTools;
import com.google.common.io.Files;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -9,18 +14,14 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.logging.Logger;
import static org.junit.Assert.*;
@@ -30,17 +31,39 @@ import static org.junit.Assert.*;
public class FlatFileDatabaseManagerTest {
public static final @NotNull String TEST_FILE_NAME = "test.mcmmo.users";
public static final int HEALTHY_RETURN_CODE = 0;
public static final String BAD_FILE_LINE_ONE = "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:";
public static final String BAD_DATA_FILE_LINE_TWENTY_THREE = "nossr51:baddata:::baddata:baddata:640:baddata:1000:1000:1000:baddata:baddata:baddata:baddata:16:0:500:20273:0:0:0:0::1000:0:0:baddata:1593543012:0:0:0:0::1000:0:0:baddata:IGNORED:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1:0:";
public static final @NotNull String BAD_FILE_LINE_ONE = "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:";
public static final @NotNull String BAD_DATA_FILE_LINE_TWENTY_THREE = "nossr51:baddata:::baddata:baddata:640:baddata:1000:1000:1000:baddata:baddata:baddata:baddata:16:0:500:20273:0:0:0:0::1000:0:0:baddata:1593543012:0:0:0:0::1000:0:0:baddata:IGNORED:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1:0:";
public static final @NotNull String DB_BADDATA = "baddatadb.users";
public static final @NotNull String DB_HEALTHY = "healthydb.users";
public static final @NotNull String HEALTHY_DB_LINE_1 = "nossr50:1:IGNORED:IGNORED:10:2:20:3:4:5:6:7:8:9:10:30:40:50:60:70:80:90:100:IGNORED:11:110:111:222:333:444:555:666:777:IGNORED:12:120:888:2020:HEARTS:13:130:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1111:999:";
public static final @NotNull String HEALTHY_DB_LINE_ONE_UUID_STR = "588fe472-1c82-4c4e-9aa1-7eefccb277e3";
private static File tempDir;
private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private final long PURGE_TIME = 2630000000L;
private static @Nullable FlatFileDatabaseManager db;
//Making them all unique makes it easier on us to edit this stuff later
int expectedLvlMining = 1, expectedLvlWoodcutting = 2, expectedLvlRepair = 3,
expectedLvlUnarmed = 4, expectedLvlHerbalism = 5, expectedLvlExcavation = 6,
expectedLvlArchery = 7, expectedLvlSwords = 8, expectedLvlAxes = 9, expectedLvlAcrobatics = 10,
expectedLvlTaming = 11, expectedLvlFishing = 12, expectedLvlAlchemy = 13;
float expectedExpMining = 10, expectedExpWoodcutting = 20, expectedExpRepair = 30,
expectedExpUnarmed = 40, expectedExpHerbalism = 50, expectedExpExcavation = 60,
expectedExpArchery = 70, expectedExpSwords = 80, expectedExpAxes = 90, expectedExpAcrobatics = 100,
expectedExpTaming = 110, expectedExpFishing = 120, expectedExpAlchemy = 130;
long expectedBerserkCd = 111, expectedGigaDrillBreakerCd = 222, expectedTreeFellerCd = 333,
expectedGreenTerraCd = 444, expectedSerratedStrikesCd = 555, expectedSkullSplitterCd = 666,
expectedSuperBreakerCd = 777, expectedBlastMiningCd = 888, expectedChimaeraWingCd = 999;
int expectedScoreboardTips = 1111;
Long expectedLastLogin = 2020L;
@Before
public void init() {
assertNull(db);
//noinspection UnstableApiUsage
tempDir = Files.createTempDir();
db = new FlatFileDatabaseManager(new File(tempDir.getPath() + File.separator + TEST_FILE_NAME), logger, PURGE_TIME, 0, true);
}
@@ -108,6 +131,249 @@ public class FlatFileDatabaseManagerTest {
"mrfloris:2420:::0:2452:0:1983:1937:1790:3042:badvalue: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:"
};
@Test
public void testSaveUser() {
//Make a Profile to save and check to see if it worked
UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3");
String playerName = "nossr50";
PlayerProfile testProfile = new PlayerProfile(playerName, uuid);
//The above profile should be "zero" initialized
//Save the zero version and see if it looks correct
assertNotNull(db);
assertFalse(db.getUsersFile().exists());
db.checkFileHealthAndStructure();
assertTrue(db.getUsersFile().exists()); //Users file should have been created from the above com.gmail.nossr50.database.FlatFileDatabaseManager.checkFileHealthAndStructure
assertNotNull(db.getUsersFile());
//The DB is empty at this point, add our user
assertTrue(db.saveUser(testProfile)); //True means we saved the user
//Check for the empty profile
PlayerProfile retrievedFromData = db.loadPlayerProfile(playerName);
assertTrue(retrievedFromData.isLoaded()); //PlayerProfile::isLoaded returns true if the data was created from the file, false if it wasn't found and a dummy profile was returned
assertEquals(uuid, retrievedFromData.getUniqueId());
assertEquals(playerName, retrievedFromData.getPlayerName());
}
@Test
public void testLoadByName() {
}
@Test
public void testLoadByUUID() {
/*
* This test uses a file provided in test resources
*/
ClassLoader classLoader = getClass().getClassLoader();
URI resourceFileURI = null;
try {
resourceFileURI = classLoader.getResource(DB_HEALTHY).toURI();
} catch (URISyntaxException e) {
e.printStackTrace();
}
assertNotNull(resourceFileURI);
File fromResourcesFile = new File(resourceFileURI);
assertNotNull(resourceFileURI);
File copyOfFile = new File(tempDir.getPath() + File.separator + DB_HEALTHY);
if(copyOfFile.exists()) {
//noinspection ResultOfMethodCallIgnored
copyOfFile.delete();
}
assertTrue(fromResourcesFile.exists());
try {
//noinspection UnstableApiUsage
Files.copy(fromResourcesFile, copyOfFile);
} catch (IOException e) {
e.printStackTrace();
}
assertNotNull(copyOfFile);
/*
* We have established the files are in good order, so now for the actual testing
*/
//This makes sure our private method is working before the tests run afterwards
ArrayList<String[]> dataFromFile = getSplitDataFromFile(copyOfFile);
System.out.println("File Path: "+copyOfFile.getAbsolutePath());
assertArrayEquals(HEALTHY_DB_LINE_1.split(":"), dataFromFile.get(0));
assertEquals(dataFromFile.get(0)[FlatFileDatabaseManager.UUID_INDEX], HEALTHY_DB_LINE_ONE_UUID_STR);
UUID healthDBEntryOneUUID = UUID.fromString(HEALTHY_DB_LINE_ONE_UUID_STR);
FlatFileDatabaseManager db_a = new FlatFileDatabaseManager(copyOfFile, logger, PURGE_TIME, 0, true);
List<FlatFileDataFlag> flagsFound = db_a.checkFileHealthAndStructure();
assertNull(flagsFound); //No flags should be found
/*
* Once the DB looks fine load the profile
*/
String playerName = "nossr50";
UUID uuid = UUID.fromString("588fe472-1c82-4c4e-9aa1-7eefccb277e3");
PlayerProfile profile = db_a.loadPlayerProfile(uuid, null);
testHealthyDataProfileValues(db_a, playerName, uuid, profile);
}
private void testHealthyDataProfileValues(FlatFileDatabaseManager flatFileDatabaseManager, String playerName, UUID uuid, PlayerProfile playerProfile) {
PlayerProfile profile = flatFileDatabaseManager.loadPlayerProfile(uuid, null);
assertTrue(profile.isLoaded()); //PlayerProfile::isLoaded returns true if the data was created from the file, false if it wasn't found and a dummy profile was returned
assertEquals(uuid, profile.getUniqueId());
assertEquals(playerName, profile.getPlayerName());
/*
* Player is a match and data is loaded, check values
*/
for(PrimarySkillType primarySkillType : PrimarySkillType.values()) {
if(SkillTools.isChildSkill(primarySkillType))
continue;
// System.out.println("Checking expected values for: "+primarySkillType);
// System.out.println("Profile Level Value: "+profile.getSkillLevel(primarySkillType));
// System.out.println("Expected Lvl Value: "+getExpectedLevelHealthyDBEntryOne(primarySkillType));
// System.out.println("Profile Exp Value: "+profile.getSkillXpLevelRaw(primarySkillType));
// System.out.println("Expected Exp Value: "+getExpectedExperienceHealthyDBEntryOne(primarySkillType));
assertEquals(getExpectedLevelHealthyDBEntryOne(primarySkillType), profile.getSkillLevel(primarySkillType));
assertEquals(getExpectedExperienceHealthyDBEntryOne(primarySkillType), profile.getSkillXpLevelRaw(primarySkillType), 0);
}
//Check the other things
for(SuperAbilityType superAbilityType : SuperAbilityType.values()) {
assertEquals(getExpectedSuperAbilityDATS(superAbilityType), profile.getAbilityDATS(superAbilityType));
}
assertEquals(expectedChimaeraWingCd, profile.getUniqueData(UniqueDataType.CHIMAERA_WING_DATS));
assertEquals(expectedScoreboardTips, profile.getScoreboardTipsShown());
assertEquals(expectedLastLogin, profile.getLastLogin());
}
private long getExpectedSuperAbilityDATS(@NotNull SuperAbilityType superAbilityType) {
switch(superAbilityType) {
case BERSERK:
return expectedBerserkCd;
case SUPER_BREAKER:
return expectedSuperBreakerCd;
case GIGA_DRILL_BREAKER:
return expectedGigaDrillBreakerCd;
case GREEN_TERRA:
return expectedGreenTerraCd;
case SKULL_SPLITTER:
return expectedSkullSplitterCd;
case TREE_FELLER:
return expectedTreeFellerCd;
case SERRATED_STRIKES:
return expectedSerratedStrikesCd;
case BLAST_MINING:
return expectedBlastMiningCd;
}
return -1;
}
//TODO: Why is this stuff a float?
private float getExpectedExperienceHealthyDBEntryOne(@NotNull PrimarySkillType primarySkillType) {
switch(primarySkillType) {
case ACROBATICS:
return expectedExpAcrobatics;
case ALCHEMY:
return expectedExpAlchemy;
case ARCHERY:
return expectedExpArchery;
case AXES:
return expectedExpAxes;
case EXCAVATION:
return expectedExpExcavation;
case FISHING:
return expectedExpFishing;
case HERBALISM:
return expectedExpHerbalism;
case MINING:
return expectedExpMining;
case REPAIR:
return expectedExpRepair;
case SALVAGE:
case SMELTING:
return 0;
case SWORDS:
return expectedExpSwords;
case TAMING:
return expectedExpTaming;
case UNARMED:
return expectedExpUnarmed;
case WOODCUTTING:
return expectedExpWoodcutting;
}
return -1;
}
private int getExpectedLevelHealthyDBEntryOne(@NotNull PrimarySkillType primarySkillType) {
switch(primarySkillType) {
case ACROBATICS:
return expectedLvlAcrobatics;
case ALCHEMY:
return expectedLvlAlchemy;
case ARCHERY:
return expectedLvlArchery;
case AXES:
return expectedLvlAxes;
case EXCAVATION:
return expectedLvlExcavation;
case FISHING:
return expectedLvlFishing;
case HERBALISM:
return expectedLvlHerbalism;
case MINING:
return expectedLvlMining;
case REPAIR:
return expectedLvlRepair;
case SALVAGE:
case SMELTING:
return 0;
case SWORDS:
return expectedLvlSwords;
case TAMING:
return expectedLvlTaming;
case UNARMED:
return expectedLvlUnarmed;
case WOODCUTTING:
return expectedLvlWoodcutting;
}
return -1;
}
@Test
public void testOverwriteName() {
}
@Test
public void testDataNotFound() {
//Save the zero version and see if it looks correct
assertNotNull(db);
assertFalse(db.getUsersFile().exists());
db.checkFileHealthAndStructure();
assertTrue(db.getUsersFile().exists()); //Users file should have been created from the above com.gmail.nossr50.database.FlatFileDatabaseManager.checkFileHealthAndStructure
assertNotNull(db.getUsersFile());
//Check for the "unloaded" profile
PlayerProfile retrievedFromData = db.loadPlayerProfile("nossr50");
assertFalse(retrievedFromData.isLoaded()); //PlayerProfile::isLoaded returns false if data doesn't exist for the user
}
@Test
public void testPurgePowerlessUsers() {
replaceDataInFile(db, normalDatabaseData);
@@ -167,14 +433,11 @@ public class FlatFileDatabaseManagerTest {
@Test
public void testLoadFromFile() {
Path resourceDirectory = Paths.get("src","test","resources");
String absolutePath = resourceDirectory.toFile().getAbsolutePath();
ClassLoader classLoader = getClass().getClassLoader();
URI resourceFileURI = null;
try {
resourceFileURI = classLoader.getResource("baddatadb.users").toURI();
resourceFileURI = classLoader.getResource(DB_BADDATA).toURI();
} catch (URISyntaxException e) {
e.printStackTrace();
}
@@ -182,7 +445,7 @@ public class FlatFileDatabaseManagerTest {
assertNotNull(resourceFileURI);
File fromResourcesFile = new File(resourceFileURI);
assertNotNull(resourceFileURI);
File copyOfFile = new File(tempDir.getPath() + File.separator + "baddatafile.users");
File copyOfFile = new File(tempDir.getPath() + File.separator + DB_BADDATA);
if(copyOfFile.exists()) {
copyOfFile.delete();
@@ -282,7 +545,6 @@ public class FlatFileDatabaseManagerTest {
}
}
}
}
private void overwriteDataAndCheckForFlag(@NotNull FlatFileDatabaseManager targetDatabase, @NotNull String[] data, @NotNull FlatFileDataFlag flag) {

View File

@@ -0,0 +1,3 @@
nossr50:1:IGNORED:IGNORED:10:2:20:3:4:5:6:7:8:9:10:30:40:50:60:70:80:90:100:IGNORED:11:110:111:222:333:444:555:666:777:IGNORED:12:120:888:2020:HEARTS:13:130:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1111:999:
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: