Add loadFromFile test

This commit is contained in:
nossr50 2021-04-12 16:59:35 -07:00
parent 9f22cef175
commit 5b4af3f9ce
4 changed files with 130 additions and 14 deletions

10
pom.xml
View File

@ -65,6 +65,16 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>

View File

@ -76,14 +76,14 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
public static final int DATA_ENTRY_COUNT = COOLDOWN_CHIMAERA_WING + 1; //Update this everytime new data is added
protected FlatFileDatabaseManager(@NotNull String usersFilePath, @NotNull Logger logger, long purgeTime, int startingLevel) {
usersFile = new File(usersFilePath);
this.usersFilePath = usersFilePath;
protected FlatFileDatabaseManager(@NotNull File usersFile, @NotNull Logger logger, long purgeTime, int startingLevel, boolean testing) {
this.usersFile = usersFile;
this.usersFilePath = usersFile.getPath();
this.logger = logger;
this.purgeTime = purgeTime;
this.startingLevel = startingLevel;
checkFileHealthAndStructure();
if(!testing) {
List<FlatFileDataFlag> flatFileDataFlags = checkFileHealthAndStructure();
if(flatFileDataFlags != null) {
@ -91,10 +91,13 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
logger.info("Detected "+flatFileDataFlags.size() + " data entries which need correction.");
}
}
checkFileHealthAndStructure();
// updateLeaderboards();
}
}
protected FlatFileDatabaseManager(@NotNull String usersFilePath, @NotNull Logger logger, long purgeTime, int startingLevel) {
this(new File(usersFilePath), logger, purgeTime, startingLevel, false);
}
public int purgePowerlessUsers() {
int purgedUsers = 0;
@ -971,6 +974,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
* @return
*/
public @Nullable List<FlatFileDataFlag> checkFileHealthAndStructure() {
ArrayList<FlatFileDataFlag> flagsFound = null;
logger.info("(" + usersFile.getPath() + ") Validating database file..");
FlatFileDataProcessor dataProcessor = null;
if (usersFile.exists()) {
@ -996,6 +1001,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
//Only update the file if needed
if(dataProcessor.getFlatFileDataFlags().size() > 0) {
flagsFound = new ArrayList<>(dataProcessor.getFlatFileDataFlags());
logger.info("Saving the updated and or repaired FlatFile Database...");
fileWriter = new FileWriter(usersFilePath);
//Write data to file
@ -1009,10 +1015,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
}
}
if(dataProcessor == null || dataProcessor.getFlatFileDataFlags() == null) {
if(flagsFound == null || flagsFound.size() == 0) {
return null;
} else {
return dataProcessor.getFlatFileDataFlags();
return flagsFound;
}
}

View File

@ -9,10 +9,18 @@ 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.logging.Logger;
import static org.junit.Assert.*;
@ -23,6 +31,8 @@ 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:";
private static File tempDir;
private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private final long PURGE_TIME = 2630000000L;
@ -32,7 +42,7 @@ public class FlatFileDatabaseManagerTest {
public void init() {
assertNull(db);
tempDir = Files.createTempDir();
db = new FlatFileDatabaseManager(tempDir.getPath() + File.separator + TEST_FILE_NAME, logger, PURGE_TIME, 0);
db = new FlatFileDatabaseManager(new File(tempDir.getPath() + File.separator + TEST_FILE_NAME), logger, PURGE_TIME, 0, true);
}
@After
@ -155,6 +165,73 @@ public class FlatFileDatabaseManagerTest {
assertEquals(db.getDatabaseType(), DatabaseType.FLATFILE);
}
@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();
} catch (URISyntaxException e) {
e.printStackTrace();
}
assertNotNull(resourceFileURI);
File fromResourcesFile = new File(resourceFileURI);
assertNotNull(resourceFileURI);
File copyOfFile = new File(tempDir.getPath() + File.separator + "baddatafile.users");
if(copyOfFile.exists()) {
copyOfFile.delete();
}
assertTrue(fromResourcesFile.exists());
try {
Files.copy(fromResourcesFile, copyOfFile);
} catch (IOException e) {
e.printStackTrace();
}
assertNotNull(copyOfFile);
//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());
assertEquals(BAD_FILE_LINE_ONE.split(":"), dataFromFile.get(0));
assertEquals(dataFromFile.get(22)[0], "nossr51");
assertEquals(BAD_DATA_FILE_LINE_TWENTY_THREE.split(":"), dataFromFile.get(22));
FlatFileDatabaseManager db_a = new FlatFileDatabaseManager(copyOfFile, logger, PURGE_TIME, 0, true);
List<FlatFileDataFlag> flagsFound = db_a.checkFileHealthAndStructure();
assertNotNull(flagsFound);
assertTrue(flagsFound.contains(FlatFileDataFlag.BAD_VALUES));
}
private @NotNull ArrayList<String[]> getSplitDataFromFile(@NotNull File file) {
ArrayList<String[]> splitDataList = new ArrayList<>();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.isEmpty())
continue;
String[] splitData = line.split(":");
splitDataList.add(splitData);
}
} catch (Exception e) {
e.printStackTrace();
}
return splitDataList;
}
private void replaceDataInFile(@NotNull FlatFileDatabaseManager flatFileDatabaseManager, @NotNull String[] dataEntries) {
String filePath = flatFileDatabaseManager.getUsersFile().getAbsolutePath();
BufferedReader in = null;

View File

@ -0,0 +1,23 @@
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:
ender_vine:434:::33:93:0:115:115:389:216:88:287:130:468:380:944:150:90:0:0:1948:584::2:0:1584896276:1586477151:1585264062:0:1576456374:1534725135:1577535765::404:4207:0:1616024209:HEARTS:3:0:812c10b0-7bbf-49e5-ac53-3f0521eb504b:5:0:
electronicboy:225:::1876:16:0:99:137:148:308:109:418:193:430:0:854:0:0:0:0:260:2816::19:0:1604048398:1601216462:1563297488:1533836917:1571449594:1601219209:1601215461::406:4207:0:1617465739:HEARTS:7:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:0:
z750:1074:::21081:231:0:280:65:156:463:612:742:550:1060:1000:1751:3666:5834:4871:11278:1943:13642::25:460:1613103021:1610212680:1609278207:0:1610076033:1586744907:1610150046::399:7050:0:1613611193:HEARTS:11:0:1594aa76-6ce0-46f6-90b2-83896a829db8:5:0:
Aikar: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:1617623810:HEARTS:0:0:6c4ed41d-9936-45da-9f57-b0f4cfa451b4:0:
wizjany: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:1617624693:HEARTS:0:0:0609efd6-e9ae-4f6a-bcff-69a6c890192f:0:
SpottedLeaf:0:::0:0:0:0:0:0:0:0:0:0:21:0:0:0:0:0:0:0:231::0:0:0:0:0:0:0:0:0::0:0:0:1617628585:HEARTS:0:0:6c002484-c964-4627-8026-9955d0d30aaf:0:0:
Cheesy:0:::0:0:0:0:0:0:0:0:0:0:6:0:0:0:0:0:0:0:882::0:0:0:0:0:0:0:0:0::0:0:0:1617636874:HEARTS:0:0:0a5804b3-f127-4962-8b83-f5f3dfe6d8e3:0:0:
Mochi: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:1617642310:HEARTS:0:0:468fb276-444f-4c79-bb07-8a7a30f95606:0:
Futan: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:1617644058:HEARTS:0:0:98f9e3c2-75ce-448c-b30b-01f93b69f0ca:0:
Dave:0:::0:0:0:0:0:0:0:0:0:0:0:0:61:0:0:0:0:0:0::0:0:0:0:0:0:0:0:0::0:0:0:1617653877:HEARTS:0:0:23926471-655b-4117-8ee6-7c718677f2e1:0:0:
MomShroom:0:::0:0:0:0:0:0:0:0:0:0:1:0:0:0:0:0:0:0:231::0:0:0:0:0:0:0:0:0::0:0:0:1617700103:HEARTS:0:0:eda1c2d5-7cf6-461c-aced-86954675b905:0:0:
broccolai: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:1617736110:HEARTS:0:0:8a4f8f1c-8242-4c52-bd63-3a4f6fe85771:0:
gabizou:0:::0:0:0:0:0:0:0:0:0:0:6:0:0:0:0:0:692:897:231::0:0:0:0:0:0:0:0:0::0:302:0:1617744451:HEARTS:0:0:fd1ad842-8959-4433-9f09-406da1567f98:0:0:
Vera:18:::150:1:769:0:0:0:0:0:0:0:0:0:0:183:0:0:0:540:0::0:0:0:0:0:0:0:0:0::0:0:0:1617744730:HEARTS:0:0:0a95d72c-4316-4b25-a4ad-b0719843d723:0:0:
proxy:0:::189:1:0:1:1:0:0:0:0:1:0:36:96:0:0:0:0:181:0::1:0:0:0:0:0:0:0:0::0:0:0:1617792154:HEARTS:1:0:d8531191-e49b-4132-8223-e3b46f8450d2:0:0:
chew:4:::1011:0:0:0:0:0:2:0:0:0:0:0:61:0:432:0:0:0:651::0:0:0:0:0:0:0:0:0::0:0:0:1617794539:HEARTS:0:0:58cea4a2-1979-48e4-a171-eaa631376835:0:0:
t00thpick1: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:1617798657:HEARTS:0:0:1300f1de-0108-43c3-839f-d40d7e0c7027:0:
kashike:1:::756: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:1617826439:HEARTS:0:0:891825c2-2ceb-4b69-82c3-f2dfb47f9a6d:0:0:
camm: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:1617830574:HEARTS:0:0:5c34f924-f7f6-4547-9e36-4234133e2162:0:
brandonxp4000:0:::0:0:0:0:0:0:0:0:2:0:1:0:181:0:0:0:54:0:231::0:0:0:0:0:0:0:0:0::0:0:0:1617887039:HEARTS:0:0:cbf7122b-0271-4daa-aeaa-3bc44e6dae88:0:0:
Tapola:1:::111:0:0:1:0:0:0:0:3:2:0:111:193:0:0:0:1010:92:0::0:0:0:0:0:0:0:0:0::0:0:0:1617886681:HEARTS:0:0:4a21353a-8ca1-436f-9b5f-d2e522dcf79e:0:0:
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: