From fd7e3e4154a37cfab218d2d03910e9ff99d45ac3 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 12 Mar 2019 20:35:33 -0700 Subject: [PATCH] Changed approach to support test concurrency --- .../yaml/YamlDatabaseHandlerTest.java | 310 +++++++++--------- 1 file changed, 149 insertions(+), 161 deletions(-) diff --git a/src/test/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandlerTest.java b/src/test/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandlerTest.java index 35f6d9490..fe52cfe20 100644 --- a/src/test/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandlerTest.java +++ b/src/test/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandlerTest.java @@ -5,7 +5,6 @@ package world.bentobox.bentobox.database.yaml; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -14,6 +13,8 @@ import java.beans.IntrospectionException; import java.io.File; import java.lang.reflect.InvocationTargetException; import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -27,7 +28,7 @@ import org.bukkit.World; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitTask; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,7 +53,6 @@ import world.bentobox.bentobox.managers.FlagsManager; @PrepareForTest( {BentoBox.class, Bukkit.class} ) public class YamlDatabaseHandlerTest { - private static final String UNIQUE_ID = "BSkyBlock6fb2d177-b186-4cac-84b9-27203ce5c6e1"; @Mock private BentoBox plugin; @Mock @@ -71,11 +71,11 @@ public class YamlDatabaseHandlerTest { private YamlDatabaseHandler handler; // File system - private File database; + private static File database; private File islandTable; private File record; private File record2; - + private UUID uuid; /** @@ -99,10 +99,12 @@ public class YamlDatabaseHandlerTest { when(Bukkit.getServer()).thenReturn(server); // A YAML file representing island + uuid = UUID.randomUUID(); + UUID uuid2 = UUID.randomUUID(); YamlConfiguration config = new YamlConfiguration(); - config.loadFromString(YAML); + config.loadFromString(getYaml(uuid)); YamlConfiguration config2 = new YamlConfiguration(); - config2.loadFromString(YAML2); + config2.loadFromString(getYaml2(uuid2)); when(dbConnector.loadYamlFile(Mockito.anyString(), Mockito.anyString())).thenReturn(config, config2); // Flags Manager @@ -111,20 +113,16 @@ public class YamlDatabaseHandlerTest { when(plugin.getFlagsManager()).thenReturn(fm); // Island - when(island.getUniqueId()).thenReturn(UNIQUE_ID); + when(island.getUniqueId()).thenReturn(uuid.toString()); // File system - database = new File("Database"); + database = new File("database"); islandTable = new File(database, "Island"); islandTable.mkdirs(); - record = new File(islandTable, UNIQUE_ID + ".yml"); - record2 = new File(islandTable, "BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d.yml"); - YamlConfiguration c = new YamlConfiguration(); - c.loadFromString(YAML); - c.save(record); - c = new YamlConfiguration(); - c.loadFromString(YAML2); - c.save(record2); + record = new File(islandTable, uuid.toString() + ".yml"); + record2 = new File(islandTable, uuid2.toString() + ".yml"); + config.save(record); + config2.save(record2); // Handler handler = new YamlDatabaseHandler(plugin, Island.class, dbConnector); @@ -133,21 +131,13 @@ public class YamlDatabaseHandlerTest { /** * @throws java.lang.Exception */ - @After - public void tearDown() throws Exception { + @AfterClass + public static void tearDown() throws Exception { // Clean up file system - if (record.exists()) { - Files.delete(record.toPath()); - } - if (record2.exists()) { - Files.delete(record2.toPath()); - } - if (islandTable.exists()) { - Files.delete(islandTable.toPath()); - } - if (database.exists()) { - Files.delete(database.toPath()); - } + Files.walk(database.toPath()) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); } /** @@ -156,18 +146,12 @@ public class YamlDatabaseHandlerTest { @Test public void testLoadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { List list = handler.loadObjects(); - assertTrue(list.size() == 2); - Island is = list.get(0); - assertEquals(UNIQUE_ID, is.getUniqueId()); - assertEquals("5988eecd-1dcd-4080-a843-785b62419abb", is.getOwner().toString()); - assertEquals(1552264678424L, is.getCreatedDate()); - assertEquals((Integer)1000, is.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb"))); - - is = list.get(1); - assertEquals("BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d", is.getUniqueId()); - assertNull(is.getOwner()); - assertEquals(1552264640164L, is.getCreatedDate()); - assertTrue(is.getMembers().isEmpty()); + assertFalse(list.isEmpty()); + // Check at least one contains correct info + assertTrue(list.stream().anyMatch(i -> i.getOwner().toString().equals("5988eecd-1dcd-4080-a843-785b62419abb"))); + assertTrue(list.stream().anyMatch(i -> i.getUniqueId().equals(uuid.toString()))); + assertTrue(list.stream().anyMatch(i -> i.getCreatedDate() == 1552264678424L)); + assertTrue(list.stream().anyMatch(i -> i.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb")) == 1000)); } /** @@ -183,7 +167,7 @@ public class YamlDatabaseHandlerTest { public void testLoadObject() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { String name = UUID.randomUUID().toString(); Island is = handler.loadObject(name); - assertEquals(UNIQUE_ID, is.getUniqueId()); + assertEquals(uuid.toString(), is.getUniqueId()); assertEquals("5988eecd-1dcd-4080-a843-785b62419abb", is.getOwner().toString()); assertEquals(1552264678424L, is.getCreatedDate()); assertEquals((Integer)1000, is.getMembers().get(UUID.fromString("5988eecd-1dcd-4080-a843-785b62419abb"))); @@ -276,8 +260,8 @@ public class YamlDatabaseHandlerTest { */ @Test public void testObjectExists() { - when(dbConnector.uniqueIdExists(Mockito.eq(Island.class.getSimpleName()), Mockito.eq(UNIQUE_ID))).thenReturn(true); - assertTrue(handler.objectExists(UNIQUE_ID)); + when(dbConnector.uniqueIdExists(Mockito.eq(Island.class.getSimpleName()), Mockito.eq(uuid.toString()))).thenReturn(true); + assertTrue(handler.objectExists(uuid.toString())); assertFalse(handler.objectExists("nope")); } @@ -286,7 +270,7 @@ public class YamlDatabaseHandlerTest { */ @Test public void testDeleteID() { - handler.deleteID(UNIQUE_ID); + handler.deleteID(uuid.toString()); // Handled by queue assertTrue(record.exists()); } @@ -297,7 +281,7 @@ public class YamlDatabaseHandlerTest { @Test public void testDeleteIDPluginNotEnabled() { when(plugin.isEnabled()).thenReturn(false); - handler.deleteID(UNIQUE_ID); + handler.deleteID(uuid.toString()); assertFalse(record.exists()); } @@ -307,7 +291,7 @@ public class YamlDatabaseHandlerTest { @Test public void testDeleteIDNotEnabledWithYML() { when(plugin.isEnabled()).thenReturn(false); - handler.deleteID(UNIQUE_ID + ".yml"); + handler.deleteID(uuid.toString() + ".yml"); assertFalse(record.exists()); } @@ -326,117 +310,121 @@ public class YamlDatabaseHandlerTest { } // YAML - private final static String YAML = "deleted: false\n" + - "uniqueId: BSkyBlock6fb2d177-b186-4cac-84b9-27203ce5c6e1\n" + - "center: cleanroom:384:100:-768:0:0\n" + - "range: 192\n" + - "protectionRange: 100\n" + - "maxEverProtectionRange: 100\n" + - "world: cleanroom\n" + - "name: 'null'\n" + - "createdDate: 1552264678424\n" + - "updatedDate: 1552264678424\n" + - "owner: 5988eecd-1dcd-4080-a843-785b62419abb\n" + - "members:\n" + - " 5988eecd-1dcd-4080-a843-785b62419abb: 1000\n" + - "spawn: false\n" + - "purgeProtected: false\n" + - "flags:\n" + - " HURT_ANIMALS: 500\n" + - " DRAGON_EGG: 500\n" + - " REDSTONE: 500\n" + - " BUCKET: 500\n" + - " LOCK: 0\n" + - " ENDER_PEARL: 500\n" + - " DOOR: 500\n" + - " FURNACE: 500\n" + - " MINECART: 500\n" + - " ANVIL: 500\n" + - " FISH_SCOOPING: 500\n" + - " FIRE_IGNITE: 500\n" + - " END_PORTAL: 500\n" + - " BREEDING: 500\n" + - " TNT: 500\n" + - " HURT_VILLAGERS: 500\n" + - " FROST_WALKER: 500\n" + - " TURTLE_EGGS: 500\n" + - " CHALLENGES_ISLAND_PROTECTION: 0\n" + - " LEAF_DECAY: 500\n" + - " COLLECT_LAVA: 500\n" + - " LEVER: 500\n" + - " RIDING: 500\n" + - " HURT_MONSTERS: 500\n" + - " ARMOR_STAND: 500\n" + - " NAME_TAG: 500\n" + - " FIRE_SPREAD: 500\n" + - " TRADING: 500\n" + - " EGGS: 500\n" + - " ITEM_DROP: 500\n" + - " PVP_OVERWORLD: -1\n" + - " NOTE_BLOCK: 500\n" + - " FLINT_AND_STEEL: 500\n" + - " NETHER_PORTAL: 500\n" + - " CROP_TRAMPLE: 500\n" + - " ITEM_PICKUP: 500\n" + - " DROPPER: 500\n" + - " BREWING: 500\n" + - " PVP_END: -1\n" + - " COLLECT_WATER: 500\n" + - " GREENHOUSE: 500\n" + - " BUTTON: 500\n" + - " FIRE_EXTINGUISH: 500\n" + - " BEACON: 500\n" + - " TRAPDOOR: 500\n" + - " PRESSURE_PLATE: 500\n" + - " EXPERIENCE_BOTTLE_THROWING: 500\n" + - " ITEM_FRAME: 500\n" + - " PLACE_BLOCKS: 500\n" + - " CRAFTING: 500\n" + - " ENCHANTING: 500\n" + - " SHEARING: 500\n" + - " BOAT: 500\n" + - " SPAWN_EGGS: 500\n" + - " BED: 500\n" + - " PVP_NETHER: -1\n" + - " MILKING: 500\n" + - " MONSTER_SPAWN: 500\n" + - " DISPENSER: 500\n" + - " GATE: 500\n" + - " FIRE_BURNING: 500\n" + - " EXPERIENCE_PICKUP: 500\n" + - " HOPPER: 500\n" + - " ANIMAL_SPAWN: 500\n" + - " LEASH: 500\n" + - " BREAK_BLOCKS: 500\n" + - " MOUNT_INVENTORY: 500\n" + - " CHORUS_FRUIT: 500\n" + - " CONTAINER: 500\n" + - " POTION_THROWING: 500\n" + - " JUKEBOX: 500\n" + - "history: []\n" + - "levelHandicap: 0\n" + - "spawnPoint:\n" + - " THE_END: cleanroom_the_end:383:106:-769:1134395392:1106247680\n" + - " NORMAL: cleanroom:384:105:-766:0:1106247680\n" + - "doNotLoad: false\n"; + private String getYaml(UUID uuid) { + return "deleted: false\n" + + "uniqueId: " + uuid.toString() + "\n" + + "center: cleanroom:384:100:-768:0:0\n" + + "range: 192\n" + + "protectionRange: 100\n" + + "maxEverProtectionRange: 100\n" + + "world: cleanroom\n" + + "name: 'null'\n" + + "createdDate: 1552264678424\n" + + "updatedDate: 1552264678424\n" + + "owner: 5988eecd-1dcd-4080-a843-785b62419abb\n" + + "members:\n" + + " 5988eecd-1dcd-4080-a843-785b62419abb: 1000\n" + + "spawn: false\n" + + "purgeProtected: false\n" + + "flags:\n" + + " HURT_ANIMALS: 500\n" + + " DRAGON_EGG: 500\n" + + " REDSTONE: 500\n" + + " BUCKET: 500\n" + + " LOCK: 0\n" + + " ENDER_PEARL: 500\n" + + " DOOR: 500\n" + + " FURNACE: 500\n" + + " MINECART: 500\n" + + " ANVIL: 500\n" + + " FISH_SCOOPING: 500\n" + + " FIRE_IGNITE: 500\n" + + " END_PORTAL: 500\n" + + " BREEDING: 500\n" + + " TNT: 500\n" + + " HURT_VILLAGERS: 500\n" + + " FROST_WALKER: 500\n" + + " TURTLE_EGGS: 500\n" + + " CHALLENGES_ISLAND_PROTECTION: 0\n" + + " LEAF_DECAY: 500\n" + + " COLLECT_LAVA: 500\n" + + " LEVER: 500\n" + + " RIDING: 500\n" + + " HURT_MONSTERS: 500\n" + + " ARMOR_STAND: 500\n" + + " NAME_TAG: 500\n" + + " FIRE_SPREAD: 500\n" + + " TRADING: 500\n" + + " EGGS: 500\n" + + " ITEM_DROP: 500\n" + + " PVP_OVERWORLD: -1\n" + + " NOTE_BLOCK: 500\n" + + " FLINT_AND_STEEL: 500\n" + + " NETHER_PORTAL: 500\n" + + " CROP_TRAMPLE: 500\n" + + " ITEM_PICKUP: 500\n" + + " DROPPER: 500\n" + + " BREWING: 500\n" + + " PVP_END: -1\n" + + " COLLECT_WATER: 500\n" + + " GREENHOUSE: 500\n" + + " BUTTON: 500\n" + + " FIRE_EXTINGUISH: 500\n" + + " BEACON: 500\n" + + " TRAPDOOR: 500\n" + + " PRESSURE_PLATE: 500\n" + + " EXPERIENCE_BOTTLE_THROWING: 500\n" + + " ITEM_FRAME: 500\n" + + " PLACE_BLOCKS: 500\n" + + " CRAFTING: 500\n" + + " ENCHANTING: 500\n" + + " SHEARING: 500\n" + + " BOAT: 500\n" + + " SPAWN_EGGS: 500\n" + + " BED: 500\n" + + " PVP_NETHER: -1\n" + + " MILKING: 500\n" + + " MONSTER_SPAWN: 500\n" + + " DISPENSER: 500\n" + + " GATE: 500\n" + + " FIRE_BURNING: 500\n" + + " EXPERIENCE_PICKUP: 500\n" + + " HOPPER: 500\n" + + " ANIMAL_SPAWN: 500\n" + + " LEASH: 500\n" + + " BREAK_BLOCKS: 500\n" + + " MOUNT_INVENTORY: 500\n" + + " CHORUS_FRUIT: 500\n" + + " CONTAINER: 500\n" + + " POTION_THROWING: 500\n" + + " JUKEBOX: 500\n" + + "history: []\n" + + "levelHandicap: 0\n" + + "spawnPoint:\n" + + " THE_END: cleanroom_the_end:383:106:-769:1134395392:1106247680\n" + + " NORMAL: cleanroom:384:105:-766:0:1106247680\n" + + "doNotLoad: false\n"; + } - private final static String YAML2 = "deleted: false\n" + - "uniqueId: BSkyBlock56ffb3d6-67bf-4951-8b7b-67b91cfec40d\n" + - "center: cleanroom:0:100:0:0:0\n" + - "range: 192\n" + - "protectionRange: 100\n" + - "maxEverProtectionRange: 100\n" + - "world: cleanroom\n" + - "name: 'null'\n" + - "createdDate: 1552264640164\n" + - "updatedDate: 1552264640164\n" + - "owner: 'null'\n" + - "members: {}\n" + - "spawn: false\n" + - "purgeProtected: false\n" + - "flags: {}\n" + - "history: []\n" + - "levelHandicap: 0\n" + - "spawnPoint: {}\n" + - "doNotLoad: false\n"; + private String getYaml2(UUID uuid) { + return "deleted: false\n" + + "uniqueId: " + uuid.toString() + "\n" + + "center: cleanroom:0:100:0:0:0\n" + + "range: 192\n" + + "protectionRange: 100\n" + + "maxEverProtectionRange: 100\n" + + "world: cleanroom\n" + + "name: 'null'\n" + + "createdDate: 1552264640164\n" + + "updatedDate: 1552264640164\n" + + "owner: 'null'\n" + + "members: {}\n" + + "spawn: false\n" + + "purgeProtected: false\n" + + "flags: {}\n" + + "history: []\n" + + "levelHandicap: 0\n" + + "spawnPoint: {}\n" + + "doNotLoad: false\n"; + } }