FlatFileDatabaseManagerTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.gmail.nossr50.database;
  2. import com.gmail.nossr50.TestUtil;
  3. import com.gmail.nossr50.datatypes.database.DatabaseType;
  4. import com.google.common.io.Files;
  5. import org.jetbrains.annotations.NotNull;
  6. import org.jetbrains.annotations.Nullable;
  7. import org.junit.After;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.powermock.modules.junit4.PowerMockRunner;
  12. import java.io.*;
  13. import java.util.List;
  14. import java.util.logging.Logger;
  15. import static org.junit.Assert.*;
  16. @RunWith(PowerMockRunner.class)
  17. public class FlatFileDatabaseManagerTest {
  18. public static final @NotNull String TEST_FILE_NAME = "test.mcmmo.users";
  19. public static final int HEALTHY_RETURN_CODE = 0;
  20. private static File tempDir;
  21. private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
  22. private final long PURGE_TIME = 2630000000L;
  23. private static @Nullable FlatFileDatabaseManager db;
  24. @Before
  25. public void init() {
  26. assertNull(db);
  27. tempDir = Files.createTempDir();
  28. db = new FlatFileDatabaseManager(tempDir.getPath() + File.separator + TEST_FILE_NAME, logger, PURGE_TIME, 0);
  29. }
  30. @After
  31. public void tearDown() {
  32. TestUtil.recursiveDelete(tempDir);
  33. db = null;
  34. }
  35. //Nothing wrong with this database
  36. private static final String[] normalDatabaseData = {
  37. "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:",
  38. "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:",
  39. "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:"
  40. };
  41. private static final String[] badUUIDDatabaseData = {
  42. "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:",
  43. "z750: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:3:5:1600906906:", //This one has an incorrect UUID representation
  44. "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:"
  45. };
  46. private static final String[] outdatedDatabaseData = {
  47. "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:",
  48. "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:",
  49. "electronicboy: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:" //This user is missing data added after UUID index
  50. };
  51. private static final String[] emptyLineDatabaseData = {
  52. "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:",
  53. "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:",
  54. "kashike: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:",
  55. "" //EMPTY LINE
  56. };
  57. private static final String[] emptyNameDatabaseData = {
  58. ":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:",
  59. "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:",
  60. "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:0:HEARTS:0:0:e0d07db8-f7e8-43c7-9ded-864dfc6f3b7c:5:1600906906:"
  61. };
  62. private static final String[] duplicateNameDatabaseData = {
  63. "mochi: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:",
  64. "mochi: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:",
  65. };
  66. private static final String[] duplicateUUIDDatabaseData = {
  67. "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:",
  68. "mrfloris: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:",
  69. };
  70. private static final String[] corruptDatabaseData = {
  71. "nossr50:1000:::0:100:0:0::1000:0:0:1593806053:HEARTS:1000:0:588fe472-1c82-4c4e-9aa1-7eefccb277e3:0:0:",
  72. "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:",
  73. "corruptdataboy:の:::ののの0:2452:0:1983:1937:1790:3042ののののの:1138:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617のののののの583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:d20c6e8d-5615-4284-b8d1-e20b92011530:5:1600906906:",
  74. "のjapaneseuserの:333:::0:2452:0:444: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:25870f0e-7558-4659-9f60-417e24cb3332:5:1600906906:",
  75. "sameUUIDasjapaneseuser:333:::0:442:0:544: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:25870f0e-7558-4659-9f60-417e24cb3332:5:1600906906:",
  76. };
  77. private static final String[] badDatabaseData = {
  78. //First entry here is missing some values
  79. "nossr50:1000: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:",
  80. //Second entry here has an integer value replaced by a string
  81. "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:"
  82. };
  83. @Test
  84. public void testPurgePowerlessUsers() {
  85. replaceDataInFile(db, normalDatabaseData);
  86. int purgeCount = db.purgePowerlessUsers();
  87. assertEquals(purgeCount, 1); //1 User should have been purged
  88. }
  89. @Test
  90. public void testCheckFileHealthAndStructure() {
  91. replaceDataInFile(db, badDatabaseData);
  92. List<FlatFileDataFlag> dataFlags = db.checkFileHealthAndStructure();
  93. assertNotNull(dataFlags);
  94. assertNotEquals(dataFlags.size(), 0);
  95. }
  96. @Test
  97. public void testFindFixableDuplicateNames() {
  98. overwriteDataAndCheckForFlag(db, duplicateNameDatabaseData, FlatFileDataFlag.DUPLICATE_NAME);
  99. }
  100. @Test
  101. public void testFindDuplicateUUIDs() {
  102. overwriteDataAndCheckForFlag(db, duplicateUUIDDatabaseData, FlatFileDataFlag.DUPLICATE_UUID);
  103. }
  104. @Test()
  105. public void findBadUUIDData() {
  106. overwriteDataAndCheckForFlag(db, badUUIDDatabaseData, FlatFileDataFlag.BAD_UUID_DATA);
  107. }
  108. @Test
  109. public void testFindCorruptData() {
  110. overwriteDataAndCheckForFlag(db, corruptDatabaseData, FlatFileDataFlag.CORRUPTED_OR_UNRECOGNIZABLE);
  111. }
  112. @Test
  113. public void testFindEmptyNames() {
  114. overwriteDataAndCheckForFlag(db, emptyNameDatabaseData, FlatFileDataFlag.MISSING_NAME);
  115. }
  116. @Test
  117. public void testFindBadValues() {
  118. overwriteDataAndCheckForFlag(db, badDatabaseData, FlatFileDataFlag.BAD_VALUES);
  119. }
  120. @Test
  121. public void testFindOutdatedData() {
  122. overwriteDataAndCheckForFlag(db, outdatedDatabaseData, FlatFileDataFlag.INCOMPLETE);
  123. }
  124. @Test
  125. public void testGetDatabaseType() {
  126. assertNotNull(db);
  127. assertEquals(db.getDatabaseType(), DatabaseType.FLATFILE);
  128. }
  129. private void replaceDataInFile(@NotNull FlatFileDatabaseManager flatFileDatabaseManager, @NotNull String[] dataEntries) {
  130. String filePath = flatFileDatabaseManager.getUsersFile().getAbsolutePath();
  131. BufferedReader in = null;
  132. FileWriter out = null;
  133. try {
  134. StringBuilder writer = new StringBuilder();
  135. for(String data : dataEntries) {
  136. writer.append(data).append("\r\n");
  137. }
  138. out = new FileWriter(filePath);
  139. out.write(writer.toString());
  140. } catch (FileNotFoundException e) {
  141. e.printStackTrace();
  142. System.out.println("File not found");
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. } finally {
  146. if (out != null) {
  147. try {
  148. out.close();
  149. }
  150. catch (IOException e) {
  151. // Ignore
  152. }
  153. }
  154. }
  155. try {
  156. System.out.println("Added the following lines to the FlatFileDatabase for the purposes of the test...");
  157. // Open the file
  158. in = new BufferedReader(new FileReader(filePath));
  159. String line;
  160. while ((line = in.readLine()) != null) {
  161. System.out.println(line);
  162. }
  163. } catch (IOException e) {
  164. e.printStackTrace();
  165. } finally {
  166. if (in != null) {
  167. try {
  168. in.close();
  169. }
  170. catch (IOException e) {
  171. // Ignore
  172. }
  173. }
  174. }
  175. }
  176. private void overwriteDataAndCheckForFlag(@NotNull FlatFileDatabaseManager targetDatabase, @NotNull String[] data, @NotNull FlatFileDataFlag flag) {
  177. replaceDataInFile(targetDatabase, data);
  178. List<FlatFileDataFlag> dataFlags = targetDatabase.checkFileHealthAndStructure();
  179. assertNotNull(dataFlags);
  180. assertTrue(dataFlags.contains(flag));
  181. }
  182. }