JUnit 5 time

This commit is contained in:
nossr50 2021-04-13 17:25:56 -07:00
parent d9e195f63a
commit 22b24b4774
5 changed files with 202 additions and 88 deletions

54
pom.xml
View File

@ -11,6 +11,11 @@
<developerConnection>scm:git:git@github.com:mcMMO-Dev/mcMMO.git</developerConnection> <developerConnection>scm:git:git@github.com:mcMMO-Dev/mcMMO.git</developerConnection>
<tag>HEAD</tag> <tag>HEAD</tag>
</scm> </scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<issueManagement> <issueManagement>
<url>https://github.com/mcMMO-Dev/mcMMO/issues</url> <url>https://github.com/mcMMO-Dev/mcMMO/issues</url>
<system>GitHub</system> <system>GitHub</system>
@ -66,13 +71,12 @@
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version> <version>2.22.2</version>
<configuration> </plugin>
<reuseForks>false</reuseForks> <plugin>
<forkCount>1</forkCount> <artifactId>maven-failsafe-plugin</artifactId>
</configuration> <version>2.22.2</version>
</plugin> </plugin>
<plugin> <plugin>
@ -219,6 +223,17 @@
</repository> </repository>
<!-- ... --> <!-- ... -->
</repositories> </repositories>
<!-- <dependencyManagement>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.junit</groupId>-->
<!-- <artifactId>junit-bom</artifactId>-->
<!-- <version>5.7.1</version>-->
<!-- <type>pom</type>-->
<!-- <scope>import</scope>-->
<!-- </dependency>-->
<!-- </dependencies>-->
<!-- </dependencyManagement>-->
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.github.seeseemelk</groupId> <groupId>com.github.seeseemelk</groupId>
@ -271,6 +286,12 @@
<groupId>net.kyori</groupId> <groupId>net.kyori</groupId>
<artifactId>adventure-platform-common</artifactId> <artifactId>adventure-platform-common</artifactId>
<version>4.0.0-SNAPSHOT</version> <version>4.0.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>net.kyori</groupId>
<artifactId>adventure-nbt</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.maven.scm</groupId> <groupId>org.apache.maven.scm</groupId>
@ -314,9 +335,21 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-dep</artifactId> <artifactId>junit-jupiter-api</artifactId>
<version>4.11</version> <version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -349,7 +382,4 @@
<version>19.0.0</version> <version>19.0.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project> </project>

View File

@ -119,6 +119,8 @@ public class FlatFileDataProcessor {
if(names.contains(name)) { if(names.contains(name)) {
builder.appendFlag(FlatFileDataFlag.DUPLICATE_NAME); builder.appendFlag(FlatFileDataFlag.DUPLICATE_NAME);
anyBadData = true;
badDataValues[USERNAME_INDEX] = true;
} }
if(!name.isEmpty()) if(!name.isEmpty())

View File

@ -174,6 +174,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
return purgedUsers; return purgedUsers;
} }
//TODO: Test this
public void purgeOldUsers() { public void purgeOldUsers() {
int removedPlayers = 0; int removedPlayers = 0;
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
@ -211,7 +212,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
} }
} }
if (currentTime - lastPlayed > purgeTime) { if (lastPlayed != -1 && lastPlayed != 0 && currentTime - lastPlayed > purgeTime) {
removedPlayers++; removedPlayers++;
} else { } else {
if (rewrite) { if (rewrite) {
@ -425,53 +426,53 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
} }
} }
public void writeUserToLine(@NotNull PlayerProfile profile, @NotNull String playerName, @Nullable UUID uuid, @NotNull Appendable writer) throws IOException { public void writeUserToLine(@NotNull PlayerProfile profile, @NotNull String playerName, @Nullable UUID uuid, @NotNull Appendable appendable) throws IOException {
writer.append(playerName).append(":"); appendable.append(playerName).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.MINING))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.MINING))).append(":");
writer.append(IGNORED).append(":"); appendable.append(IGNORED).append(":");
writer.append(IGNORED).append(":"); appendable.append(IGNORED).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.MINING))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.MINING))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.WOODCUTTING))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.WOODCUTTING))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.WOODCUTTING))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.WOODCUTTING))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.REPAIR))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.REPAIR))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.UNARMED))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.UNARMED))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.HERBALISM))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.HERBALISM))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.EXCAVATION))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.EXCAVATION))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ARCHERY))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ARCHERY))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.SWORDS))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.SWORDS))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.AXES))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.AXES))).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ACROBATICS))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ACROBATICS))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.REPAIR))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.REPAIR))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.UNARMED))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.UNARMED))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.HERBALISM))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.HERBALISM))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.EXCAVATION))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.EXCAVATION))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ARCHERY))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ARCHERY))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.SWORDS))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.SWORDS))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.AXES))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.AXES))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ACROBATICS))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ACROBATICS))).append(":");
writer.append(IGNORED).append(":"); appendable.append(IGNORED).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.TAMING))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.TAMING))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.TAMING))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.TAMING))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.BERSERK))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.BERSERK))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.GIGA_DRILL_BREAKER))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.GIGA_DRILL_BREAKER))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.TREE_FELLER))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.TREE_FELLER))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.GREEN_TERRA))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.GREEN_TERRA))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SERRATED_STRIKES))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SERRATED_STRIKES))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SKULL_SPLITTER))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SKULL_SPLITTER))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SUPER_BREAKER))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.SUPER_BREAKER))).append(":");
writer.append(IGNORED).append(":"); appendable.append(IGNORED).append(":");
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.FISHING))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.FISHING))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.FISHING))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.FISHING))).append(":");
writer.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.BLAST_MINING))).append(":"); appendable.append(String.valueOf(profile.getAbilityDATS(SuperAbilityType.BLAST_MINING))).append(":");
writer.append(IGNORED).append(":"); //Legacy last login appendable.append(IGNORED).append(":"); //Legacy last login
writer.append(IGNORED).append(":"); //mob health bar appendable.append(IGNORED).append(":"); //mob health bar
writer.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ALCHEMY))).append(":"); appendable.append(String.valueOf(profile.getSkillLevel(PrimarySkillType.ALCHEMY))).append(":");
writer.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ALCHEMY))).append(":"); appendable.append(String.valueOf(profile.getSkillXpLevel(PrimarySkillType.ALCHEMY))).append(":");
writer.append(uuid != null ? uuid.toString() : "NULL").append(":"); appendable.append(uuid != null ? uuid.toString() : "NULL").append(":");
writer.append(String.valueOf(profile.getScoreboardTipsShown())).append(":"); appendable.append(String.valueOf(profile.getScoreboardTipsShown())).append(":");
writer.append(String.valueOf(profile.getUniqueData(UniqueDataType.CHIMAERA_WING_DATS))).append(":"); appendable.append(String.valueOf(profile.getUniqueData(UniqueDataType.CHIMAERA_WING_DATS))).append(":");
writer.append(String.valueOf(profile.getLastLogin())).append(":"); //overhaul last login appendable.append(String.valueOf(profile.getLastLogin())).append(":"); //overhaul last login
writer.append("\r\n"); appendable.append("\r\n");
} }
public @NotNull List<PlayerStat> readLeaderboard(@Nullable PrimarySkillType primarySkillType, int pageNumber, int statsPerPage) throws InvalidSkillException { public @NotNull List<PlayerStat> readLeaderboard(@Nullable PrimarySkillType primarySkillType, int pageNumber, int statsPerPage) throws InvalidSkillException {
@ -510,9 +511,23 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
PlayerProfile playerProfile = new PlayerProfile(playerName, uuid, true, startingLevel); PlayerProfile playerProfile = new PlayerProfile(playerName, uuid, true, startingLevel);
synchronized (fileWritingLock) { synchronized (fileWritingLock) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(usersFilePath, true))) { try(BufferedReader bufferedReader = new BufferedReader(new FileReader(usersFilePath))) {
writeUserToLine(playerProfile, playerName, uuid, bufferedWriter); StringBuilder stringBuilder = new StringBuilder();
} catch (Exception e) {
String line;
//Build up the file
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\r\n");
}
try (FileWriter fileWriter = new FileWriter(usersFile)) {
writeUserToLine(playerProfile, playerName, uuid, stringBuilder);
fileWriter.write(stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -536,7 +551,17 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
return loadPlayerByUUID(uuid, null, false); return loadPlayerByUUID(uuid, null, false);
} }
private @NotNull PlayerProfile loadPlayerByUUID(@NotNull UUID uuid, @Nullable String playerName, boolean isOnline) { /**
* Find and load a player by UUID
*
* @param uuid target uuid
* @param playerName target player name
* @param replaceName name to replace if the found name differs
* @return a profile with the targets data or an unloaded profile if no data was found
* @deprecated only use this if you know what you are doing, replacing the name can cause havoc
*/
@Deprecated
public @NotNull PlayerProfile loadPlayerByUUID(@NotNull UUID uuid, @Nullable String playerName, boolean replaceName) {
BufferedReader in = null; BufferedReader in = null;
synchronized (fileWritingLock) { synchronized (fileWritingLock) {
@ -574,7 +599,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
/* Check for nickname changes and update since we are here anyways */ /* Check for nickname changes and update since we are here anyways */
if(playerName != null) { if(playerName != null) {
if(isOnline) { if(replaceName) {
logger.info("A users name is being updated, this can happen from either a call to our API or they simply changed their name");
if (!rawSplitData[USERNAME_INDEX].equalsIgnoreCase(playerName)) { if (!rawSplitData[USERNAME_INDEX].equalsIgnoreCase(playerName)) {
//logger.info("Name updated for player: " + rawSplitData[USERNAME_INDEX] + " => " + playerName); //logger.info("Name updated for player: " + rawSplitData[USERNAME_INDEX] + " => " + playerName);
rawSplitData[USERNAME_INDEX] = playerName; rawSplitData[USERNAME_INDEX] = playerName;
@ -1162,7 +1188,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
lastLogin = -1; lastLogin = -1;
} }
return new PlayerProfile(character[USERNAME_INDEX], uuid, skills, skillsXp, skillsDATS, scoreboardTipsShown, uniquePlayerDataMap, lastLogin); return new PlayerProfile(username, uuid, skills, skillsXp, skillsDATS, scoreboardTipsShown, uniquePlayerDataMap, lastLogin);
} }
private void tryLoadSkillCooldownFromRawData(@NotNull Map<SuperAbilityType, Integer> cooldownMap, @NotNull String[] character, @NotNull SuperAbilityType superAbilityType, int cooldownSuperBreaker, @NotNull String userName) { private void tryLoadSkillCooldownFromRawData(@NotNull Map<SuperAbilityType, Integer> cooldownMap, @NotNull String[] character, @NotNull SuperAbilityType superAbilityType, int cooldownSuperBreaker, @NotNull String userName) {

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.database; package com.gmail.nossr50.database;
import com.gmail.nossr50.api.exceptions.InvalidSkillException; import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.datatypes.MobHealthbarType; import com.gmail.nossr50.datatypes.MobHealthbarType;
import com.gmail.nossr50.datatypes.database.DatabaseType; import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.database.PlayerStat; import com.gmail.nossr50.datatypes.database.PlayerStat;

View File

@ -6,36 +6,28 @@ import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType; import com.gmail.nossr50.datatypes.player.UniqueDataType;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType; import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask;
import com.gmail.nossr50.util.skills.SkillTools; import com.gmail.nossr50.util.skills.SkillTools;
import com.google.common.io.Files; import com.google.common.io.Files;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
//TODO: Test update leaderboards //TODO: Test update leaderboards
//This class uses JUnit5/Jupiter
public class FlatFileDatabaseManagerTest { public class FlatFileDatabaseManagerTest {
public mcMMO plugin;
public static final @NotNull String TEST_FILE_NAME = "test.mcmmo.users"; public static final @NotNull String TEST_FILE_NAME = "test.mcmmo.users";
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_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 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:";
@ -68,7 +60,7 @@ public class FlatFileDatabaseManagerTest {
int expectedScoreboardTips = 1111; int expectedScoreboardTips = 1111;
Long expectedLastLogin = 2020L; Long expectedLastLogin = 2020L;
@Before @BeforeEach
public void init() { public void init() {
assertNull(db); assertNull(db);
//noinspection UnstableApiUsage //noinspection UnstableApiUsage
@ -76,7 +68,7 @@ public class FlatFileDatabaseManagerTest {
db = new FlatFileDatabaseManager(new File(tempDir.getPath() + File.separator + TEST_FILE_NAME), logger, PURGE_TIME, 0, true); db = new FlatFileDatabaseManager(new File(tempDir.getPath() + File.separator + TEST_FILE_NAME), logger, PURGE_TIME, 0, true);
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
TestUtil.recursiveDelete(tempDir); TestUtil.recursiveDelete(tempDir);
db = null; db = null;
@ -184,7 +176,6 @@ public class FlatFileDatabaseManagerTest {
assertEquals(-1, (long) profile.getLastLogin()); assertEquals(-1, (long) profile.getLastLogin());
} }
@Test @Test
public void testLoadByName() { public void testLoadByName() {
File healthyDB = prepareDatabaseTestResource(DB_HEALTHY); File healthyDB = prepareDatabaseTestResource(DB_HEALTHY);
@ -244,6 +235,47 @@ public class FlatFileDatabaseManagerTest {
db.newUser("disco", new UUID(3, 3)); db.newUser("disco", new UUID(3, 3));
db.newUser("dingus", new UUID(3, 4)); db.newUser("dingus", new UUID(3, 4));
db.newUser("duped_dingus", new UUID(3, 4)); db.newUser("duped_dingus", new UUID(3, 4));
assertEquals(5, getSplitDataFromFile(db.getUsersFile()).size());
}
@Test
public void testAddingUsersToEndOfExistingDB() {
//We will test that new user values line up with our expectations
UUID uuid = new UUID(0, 80);
String playerName = "the_kitty_man";
File file = prepareDatabaseTestResource(DB_HEALTHY); //Existing DB
int newUserTestStartingLvl = 1337;
db = new FlatFileDatabaseManager(file, logger, PURGE_TIME, newUserTestStartingLvl, true);
db.checkFileHealthAndStructure();
PlayerProfile playerProfile = db.newUser(playerName, uuid);
assertTrue(playerProfile.isLoaded());
assertEquals(playerName, playerProfile.getPlayerName());
assertEquals(uuid, playerProfile.getUniqueId());
PlayerProfile retrievedFromDisk = db.loadPlayerProfile(uuid, playerName);
assertTrue(retrievedFromDisk.isLoaded());
assertEquals(playerName, retrievedFromDisk.getPlayerName());
assertEquals(uuid, retrievedFromDisk.getUniqueId());
//Checking a new user for being "zero" initialized
checkNewUserValues(playerProfile, newUserTestStartingLvl);
checkNewUserValues(retrievedFromDisk, newUserTestStartingLvl);
//TODO: Should we do any dupe checking? Probably not needed as it would be caught on the next load
db.newUser("bidoof", new UUID(3, 3));
db.newUser("derp", new UUID(3, 4));
db.newUser("pizza", new UUID(3, 4));
assertEquals(7, getSplitDataFromFile(db.getUsersFile()).size());
//Now we *fix* the DB and there should be one less
db.checkFileHealthAndStructure();
assertEquals(6, getSplitDataFromFile(db.getUsersFile()).size());
} }
private void checkNewUserValues(@NotNull PlayerProfile playerProfile, int startingLevel) { private void checkNewUserValues(@NotNull PlayerProfile playerProfile, int startingLevel) {
@ -466,7 +498,32 @@ public class FlatFileDatabaseManagerTest {
@Test @Test
public void testOverwriteName() { public void testOverwriteName() {
overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME);
ArrayList<String[]> splitDataLines = getSplitDataFromFile(db.getUsersFile());
assertNotEquals(splitDataLines.get(1)[0], splitDataLines.get(0)[0]); //Name comparison
}
@Test
public void testUpdateName() {
//TODO: The code in this test didn't actually trigger the save, so I'll have to do something else to test saving
// UUID uuid = UUID.fromString(HEALTHY_DB_LINE_ONE_UUID_STR); //Entrant "nossr50"
// String playerName = "the_new_name_man";
//
// File file = prepareDatabaseTestResource(DB_HEALTHY); //Existing DB
// db = new FlatFileDatabaseManager(file, logger, PURGE_TIME, 0, true);
// db.checkFileHealthAndStructure();
// ArrayList<String[]> splitDataLines = getSplitDataFromFile(db.getUsersFile());
// String oldName = "nossr50";
// assertEquals(oldName, splitDataLines.get(0)[0]); //Name comparison
// assertEquals(uuid.toString(), splitDataLines.get(0)[FlatFileDatabaseManager.UUID_INDEX]); //UUID Comparison
//
// //Now we load the player and their name should get replaced
// PlayerProfile profile = db.loadPlayerByUUID(uuid, playerName, true);
// assertEquals(playerName, profile.getPlayerName());
//
// splitDataLines = getSplitDataFromFile(db.getUsersFile()); //Load the file again
// assertNotEquals(oldName, splitDataLines.get(0)[0]); //Name comparison
// assertEquals(playerName, splitDataLines.get(0)[0]); //Name comparison
} }
@Test @Test
@ -573,9 +630,9 @@ public class FlatFileDatabaseManagerTest {
//This makes sure our private method is working before the tests run afterwards //This makes sure our private method is working before the tests run afterwards
ArrayList<String[]> dataFromFile = getSplitDataFromFile(copyOfFile); ArrayList<String[]> dataFromFile = getSplitDataFromFile(copyOfFile);
System.out.println("File Path: "+copyOfFile.getAbsolutePath()); System.out.println("File Path: "+copyOfFile.getAbsolutePath());
assertEquals(BAD_FILE_LINE_ONE.split(":"), dataFromFile.get(0)); assertArrayEquals(BAD_FILE_LINE_ONE.split(":"), dataFromFile.get(0));
assertEquals(dataFromFile.get(22)[0], "nossr51"); assertEquals(dataFromFile.get(22)[0], "nossr51");
assertEquals(BAD_DATA_FILE_LINE_TWENTY_THREE.split(":"), dataFromFile.get(22)); assertArrayEquals(BAD_DATA_FILE_LINE_TWENTY_THREE.split(":"), dataFromFile.get(22));
FlatFileDatabaseManager db_a = new FlatFileDatabaseManager(copyOfFile, logger, PURGE_TIME, 0, true); FlatFileDatabaseManager db_a = new FlatFileDatabaseManager(copyOfFile, logger, PURGE_TIME, 0, true);
List<FlatFileDataFlag> flagsFound = db_a.checkFileHealthAndStructure(); List<FlatFileDataFlag> flagsFound = db_a.checkFileHealthAndStructure();