mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-11-04 11:03:43 +01:00 
			
		
		
		
	Add lastlogin tests
This commit is contained in:
		@@ -19,14 +19,13 @@ import org.powermock.modules.junit4.PowerMockRunner;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.nio.file.Path;
 | 
			
		||||
import java.nio.file.Paths;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//TODO: Test update leaderboards
 | 
			
		||||
@RunWith(PowerMockRunner.class)
 | 
			
		||||
public class FlatFileDatabaseManagerTest {
 | 
			
		||||
 | 
			
		||||
@@ -35,8 +34,10 @@ public class FlatFileDatabaseManagerTest {
 | 
			
		||||
    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_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:IGNORED:HEARTS:13:130:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1111:999:2020:";
 | 
			
		||||
    public static final @NotNull String HEALTHY_DB_LINE_ONE_UUID_STR = "588fe472-1c82-4c4e-9aa1-7eefccb277e3";
 | 
			
		||||
    public static final String DB_MISSING_LAST_LOGIN = "missinglastlogin.users";
 | 
			
		||||
    public static final String LINE_TWO_FROM_MISSING_DB = "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:0:HEARTS:13:130:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1111:999:";
 | 
			
		||||
    private static File tempDir;
 | 
			
		||||
    private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
 | 
			
		||||
    private final long PURGE_TIME = 2630000000L;
 | 
			
		||||
@@ -157,21 +158,99 @@ public class FlatFileDatabaseManagerTest {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLoadByName() {
 | 
			
		||||
    public void testAddedMissingLastLoginValues() {
 | 
			
		||||
        File dbFile = prepareDatabaseTestResource(DB_MISSING_LAST_LOGIN);
 | 
			
		||||
 | 
			
		||||
        //This makes sure our private method is working before the tests run afterwards
 | 
			
		||||
        ArrayList<String[]> dataFromFile = getSplitDataFromFile(dbFile);
 | 
			
		||||
        System.out.println("File Path: "+ dbFile.getAbsolutePath());
 | 
			
		||||
        assertArrayEquals(LINE_TWO_FROM_MISSING_DB.split(":"), dataFromFile.get(1));
 | 
			
		||||
        assertEquals(dataFromFile.get(1)[FlatFileDatabaseManager.UUID_INDEX], HEALTHY_DB_LINE_ONE_UUID_STR);
 | 
			
		||||
 | 
			
		||||
        db = new FlatFileDatabaseManager(dbFile, logger, PURGE_TIME, 0, true);
 | 
			
		||||
        List<FlatFileDataFlag> flagsFound = db.checkFileHealthAndStructure();
 | 
			
		||||
        assertNotNull(flagsFound);
 | 
			
		||||
        assertTrue(flagsFound.contains(FlatFileDataFlag.LAST_LOGIN_SCHEMA_UPGRADE));
 | 
			
		||||
 | 
			
		||||
        //Check for the fixed value
 | 
			
		||||
        PlayerProfile profile = db.loadPlayerProfile("nossr50");
 | 
			
		||||
        assertEquals(-1, (long) profile.getLastLogin());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLoadByName() {
 | 
			
		||||
        File healthyDB = prepareDatabaseTestResource(DB_HEALTHY);
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
         * 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(healthyDB);
 | 
			
		||||
        System.out.println("File Path: "+healthyDB.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);
 | 
			
		||||
 | 
			
		||||
        db = new FlatFileDatabaseManager(healthyDB, logger, PURGE_TIME, 0, true);
 | 
			
		||||
        List<FlatFileDataFlag> flagsFound = db.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.loadPlayerProfile(playerName);
 | 
			
		||||
        testHealthyDataProfileValues(playerName, uuid, profile);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLoadByUUID() {
 | 
			
		||||
        File dbFile = prepareDatabaseTestResource(DB_HEALTHY);
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
         * This test uses a file provided in test resources
 | 
			
		||||
         * 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(dbFile);
 | 
			
		||||
        System.out.println("File Path: " + dbFile.getAbsolutePath());
 | 
			
		||||
        assertArrayEquals(HEALTHY_DB_LINE_1.split(":"), dataFromFile.get(0));
 | 
			
		||||
        assertEquals(dataFromFile.get(0)[FlatFileDatabaseManager.UUID_INDEX], HEALTHY_DB_LINE_ONE_UUID_STR);
 | 
			
		||||
 | 
			
		||||
        db = new FlatFileDatabaseManager(dbFile, logger, PURGE_TIME, 0, true);
 | 
			
		||||
        List<FlatFileDataFlag> flagsFound = db.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 profile1 = db.loadPlayerProfile(uuid, null);
 | 
			
		||||
        PlayerProfile profile2 = db.loadPlayerProfile(uuid, playerName);
 | 
			
		||||
        PlayerProfile profile3 = db.loadPlayerProfile(uuid, "incorrectName");
 | 
			
		||||
        PlayerProfile profile4 = db.loadPlayerProfile(new UUID(0, 1), "shouldBeUnloaded");
 | 
			
		||||
        assertFalse(profile4.isLoaded());
 | 
			
		||||
 | 
			
		||||
        //Three possible ways to load the thing
 | 
			
		||||
        testHealthyDataProfileValues(playerName, uuid, profile1);
 | 
			
		||||
        testHealthyDataProfileValues(playerName, uuid, profile2);
 | 
			
		||||
        testHealthyDataProfileValues(playerName, uuid, profile3);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private File prepareDatabaseTestResource(@NotNull String dbFileName) {
 | 
			
		||||
        ClassLoader classLoader = getClass().getClassLoader();
 | 
			
		||||
        URI resourceFileURI = null;
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            resourceFileURI = classLoader.getResource(DB_HEALTHY).toURI();
 | 
			
		||||
            resourceFileURI = classLoader.getResource(dbFileName).toURI();
 | 
			
		||||
        } catch (URISyntaxException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
@@ -179,7 +258,7 @@ public class FlatFileDatabaseManagerTest {
 | 
			
		||||
        assertNotNull(resourceFileURI);
 | 
			
		||||
        File fromResourcesFile = new File(resourceFileURI);
 | 
			
		||||
        assertNotNull(resourceFileURI);
 | 
			
		||||
        File copyOfFile = new File(tempDir.getPath() + File.separator + DB_HEALTHY);
 | 
			
		||||
        File copyOfFile = new File(tempDir.getPath() + File.separator + dbFileName);
 | 
			
		||||
 | 
			
		||||
        if(copyOfFile.exists()) {
 | 
			
		||||
            //noinspection ResultOfMethodCallIgnored
 | 
			
		||||
@@ -196,37 +275,10 @@ public class FlatFileDatabaseManagerTest {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        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);
 | 
			
		||||
        return copyOfFile;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void testHealthyDataProfileValues(FlatFileDatabaseManager flatFileDatabaseManager, String playerName, UUID uuid, PlayerProfile playerProfile) {
 | 
			
		||||
        PlayerProfile profile = flatFileDatabaseManager.loadPlayerProfile(uuid, null);
 | 
			
		||||
    private void testHealthyDataProfileValues(@NotNull String playerName, @NotNull UUID uuid, @NotNull PlayerProfile profile) {
 | 
			
		||||
        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());
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
 | 
			
		||||
public class FlatFileSaveDataProcessorTest {
 | 
			
		||||
public class FlatFileDataUtilTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void getPreparedSaveDataLine() {
 | 
			
		||||
@@ -22,6 +22,6 @@ public class FlatFileSaveDataProcessorTest {
 | 
			
		||||
    @Test(expected = AssertionError.class)
 | 
			
		||||
    public void testTooManyDataEntriesSplitString() {
 | 
			
		||||
        FlatFileDataContainer dataContainer = new CategorizedFlatFileData(0, new HashSet<>(), new String[FlatFileDatabaseManager.DATA_ENTRY_COUNT + 1]);
 | 
			
		||||
        FlatFileSaveDataProcessor.getPreparedSaveDataLine(dataContainer);
 | 
			
		||||
        FlatFileDataUtil.getPreparedSaveDataLine(dataContainer);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +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:
 | 
			
		||||
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:IGNORED:HEARTS:13:130:588fe472-1c82-4c4e-9aa1-7eefccb277e3:1111:999:2020:
 | 
			
		||||
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:3030:
 | 
			
		||||
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:1337:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:4040:
 | 
			
		||||
							
								
								
									
										4
									
								
								src/test/resources/missinglastlogin.users
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/test/resources/missinglastlogin.users
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
# A single comment line is sometimes at the top of the DB
 | 
			
		||||
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:0: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:1337:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:
 | 
			
		||||
		Reference in New Issue
	
	Block a user