From 9e63f1ce7976fbcebc251d14dc938f106ce821c5 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Thu, 7 May 2020 19:56:45 +0200 Subject: [PATCH] Added Unit Tests for Categories --- .../Slimefun/Objects/Category.java | 4 + src/main/resources/wiki.json | 202 +++++++++--------- .../slimefun4/mocks/SlimefunMocks.java | 3 +- .../slimefun4/tests/items/TestCategories.java | 141 ++++++++++++ 4 files changed, 248 insertions(+), 102 deletions(-) create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java index 6c98d3c60..45e2f9b2e 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.apache.commons.lang.Validate; import org.bukkit.ChatColor; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; @@ -64,6 +65,9 @@ public class Category implements Keyed { * the {@link SlimefunGuide} */ public Category(NamespacedKey key, ItemStack item, int tier) { + Validate.notNull(key, "A Category's NamespacedKey must not be null!"); + Validate.notNull(item, "A Category's ItemStack must not be null!"); + this.item = item; this.key = key; diff --git a/src/main/resources/wiki.json b/src/main/resources/wiki.json index 2d8b1e456..3b598bcf9 100644 --- a/src/main/resources/wiki.json +++ b/src/main/resources/wiki.json @@ -1,103 +1,103 @@ { - "GOLD_PAN": "Gold-Pan", - "SIFTED_ORE": "Sifted-Ore", - "SMELTERY": "Smeltery", - "DIET_COOKIE": "Diet-Cookie", - "ENHANCED_CRAFTING_TABLE": "Enhanced-Crafting-Table", - "FORTUNE_COOKIE": "Fortune-Cookie", - "TABLE_SAW": "Table-Saw", - "APPLE_JUICE": "Juices", - "GOLDEN_APPLE_JUICE": "Juices", - "CARROT_JUICE": "Juices", - "MELON_JUICE": "Juices", - "PUMPKIN_JUICE": "Juices", - "SWEET_BERRY_JUICE": "Juices", - "MAGIC_SUGAR": "Magic-Sugar", - "MONSTER_JERKY": "Monster-Jerky", - "OUTPUT_CHEST": "Output-Chest", - "BEEF_JERKY": "Meat-Jerky", - "PORK_JERKY": "Meat-Jerky", - "CHICKEN_JERKY": "Meat-Jerky", - "MUTTON_JERKY": "Meat-Jerky", - "RABBIT_JERKY": "Meat-Jerky", - "FISH_JERKY": "Meat-Jerky", - "KELP_COOKIE": "Kelp-Cookie", - "ANCIENT_ALTAR": "Ancient-Altar", - "ANCIENT_PEDESTAL": "Ancient-Pedestal", - "BLADE_OF_VAMPIRES": "Blade-of-Vampires", - "BROKEN_SPAWNER": "Broken-Spawner", - "REPAIRED_SPAWNER": "Reinforced-Spawner", - "NETHER_GOLD_PAN": "Nether-Gold-Pan", - "PICKAXE_OF_CONTAINMENT": "Pickaxe-of-Containment", - "SEISMIC_AXE": "Seismic-Axe", - "SMELTERS_PICKAXE": "Smelter's-Pickaxe", - "MAGNET": "Magnet", - "BASIC_CIRCUIT_BOARD": "Circuit-Boards", - "ADVANCED_CIRCUIT_BOARD": "Circuit-Boards", - "BATTERY": "Battery", - "STEEL_THRUSTER": "Steel-Thruster", - "POWER_CRYSTAL": "Power-Crystal", - "SOLAR_PANEL": "Solar-Panel", - "ELECTRO_MAGNET": "Electromagnet", - "ELECTRIC_MOTOR": "Electric-Motor", - "HEATING_COIL": "Heating-Coil", - "COPPER_WIRE": "Copper-Wire", - "HARDENED_GLASS": "Hardened-Glass", - "COOLING_UNIT": "Cooling-Unit", - "WITHER_PROOF_OBSIDIAN": "Wither-Proof-Blocks", - "WITHER_PROOF_GLASS": "Wither-Proof-Blocks", - "REACTOR_COOLANT_CELL": "Coolant-Cells", - "NETHER_ICE_COOLANT_CELL": "Coolant-Cells", - "IRON_DUST": "Iron-Dust", - "GOLD_DUST": "Gold-Dust", - "GOLD_4K": "Gold-Ingot", - "GOLD_6K": "Gold-Ingot", - "GOLD_8K": "Gold-Ingot", - "GOLD_10K": "Gold-Ingot", - "GOLD_12K": "Gold-Ingot", - "GOLD_14K": "Gold-Ingot", - "GOLD_16K": "Gold-Ingot", - "GOLD_18K": "Gold-Ingot", - "GOLD_20K": "Gold-Ingot", - "GOLD_22K": "Gold-Ingot", - "GOLD_24K": "Gold-Ingot", - "ENERGY_REGULATOR": "Energy-Regulator", - "SMALL_CAPACITOR": "Energy-Capacitors", - "MEDIUM_CAPACITOR": "Energy-Capacitors", - "BIG_CAPACITOR": "Energy-Capacitors", - "LARGE_CAPACITOR": "Energy-Capacitors", - "CARBONADO_EDGED_CAPACITOR": "Energy-Capacitors", - "SOLAR_GENERATOR": "Solar-Generator", - "SOLAR_GENERATOR_2": "Solar-Generator", - "SOLAR_GENERATOR_3": "Solar-Generator", - "SOLAR_GENERATOR_4": "Solar-Generator", - "COAL_GENERATOR": "Coal-Generator", - "COAL_GENERATOR_2": "Coal-Generator", - "COBALT_PICKAXE": "Cobalt-Pickaxe", - "EXPLOSIVE_PICKAXE": "Explosive-Pickaxe", - "EXPLOSIVE_SHOVEL": "Explosive-Shovel", - "GRAPPLING_HOOK": "Grappling-Hook", - "HERCULES_PICKAXE": "Hercules'-Pickaxe", - "LUMBER_AXE": "Lumber-Axe", - "PICKAXE_OF_VEIN_MINING": "Pickaxe-of-Vein-Mining", - "PICKAXE_OF_THE_SEEKER": "Pickaxe-of-the-Seeker", - "CARGO_OUTPUT_ADVANCED": "Advanced-Output-Node", - "CARGO_MANAGER": "Cargo-Manager", - "CARGO_MOTOR": "Cargo-Motor", - "CARGO_NODE": "Connector-Node", - "CARGO_INPUT": "Input-Node", - "CARGO_OUTPUT": "Output-Node", - "TRASH_CAN": "Trash-Can", - "ORE_WASHER": "Ore-Washer", - "SCUBA_HELMET": "Hazmat-Suit", - "HAZMAT_CHESTPLATE": "Hazmat-Suit", - "HAZMAT_LEGGINGS": "Hazmat-Suit", - "RUBBER_BOOTS": "Hazmat-Suit", - "ARMOR_FORGE": "Armor-Forge", - "AUTOMATED_PANNING_MACHINE": "Automated-Panning-Machine", - "COMPRESSOR": "Compressor", - "ORE_CRUSHER": "Ore-Crusher", - "PRESSURE_CHAMBER": "Pressure-Chamber", - "GRIND_STONE": "Grind-Stone", - "MAGIC_WORKBENCH": "Magic-Workbench" + "GOLD_PAN" : "Gold-Pan", + "SIFTED_ORE" : "Sifted-Ore", + "SMELTERY" : "Smeltery", + "DIET_COOKIE" : "Diet-Cookie", + "ENHANCED_CRAFTING_TABLE" : "Enhanced-Crafting-Table", + "FORTUNE_COOKIE" : "Fortune-Cookie", + "TABLE_SAW" : "Table-Saw", + "APPLE_JUICE" : "Juices", + "GOLDEN_APPLE_JUICE" : "Juices", + "CARROT_JUICE" : "Juices", + "MELON_JUICE" : "Juices", + "PUMPKIN_JUICE" : "Juices", + "SWEET_BERRY_JUICE" : "Juices", + "MAGIC_SUGAR" : "Magic-Sugar", + "MONSTER_JERKY" : "Monster-Jerky", + "OUTPUT_CHEST" : "Output-Chest", + "BEEF_JERKY" : "Meat-Jerky", + "PORK_JERKY" : "Meat-Jerky", + "CHICKEN_JERKY" : "Meat-Jerky", + "MUTTON_JERKY" : "Meat-Jerky", + "RABBIT_JERKY" : "Meat-Jerky", + "FISH_JERKY" : "Meat-Jerky", + "KELP_COOKIE" : "Kelp-Cookie", + "ANCIENT_ALTAR" : "Ancient-Altar", + "ANCIENT_PEDESTAL" : "Ancient-Pedestal", + "BLADE_OF_VAMPIRES" : "Blade-of-Vampires", + "BROKEN_SPAWNER" : "Broken-Spawner", + "REPAIRED_SPAWNER" : "Reinforced-Spawner", + "NETHER_GOLD_PAN" : "Nether-Gold-Pan", + "PICKAXE_OF_CONTAINMENT" : "Pickaxe-of-Containment", + "SEISMIC_AXE" : "Seismic-Axe", + "SMELTERS_PICKAXE" : "Smelter's-Pickaxe", + "MAGNET" : "Magnet", + "BASIC_CIRCUIT_BOARD" : "Circuit-Boards", + "ADVANCED_CIRCUIT_BOARD" : "Circuit-Boards", + "BATTERY" : "Battery", + "STEEL_THRUSTER" : "Steel-Thruster", + "POWER_CRYSTAL" : "Power-Crystal", + "SOLAR_PANEL" : "Solar-Panel", + "ELECTRO_MAGNET" : "Electromagnet", + "ELECTRIC_MOTOR" : "Electric-Motor", + "HEATING_COIL" : "Heating-Coil", + "COPPER_WIRE" : "Copper-Wire", + "HARDENED_GLASS" : "Hardened-Glass", + "COOLING_UNIT" : "Cooling-Unit", + "WITHER_PROOF_OBSIDIAN" : "Wither-Proof-Blocks", + "WITHER_PROOF_GLASS" : "Wither-Proof-Blocks", + "REACTOR_COOLANT_CELL" : "Coolant-Cells", + "NETHER_ICE_COOLANT_CELL" : "Coolant-Cells", + "IRON_DUST" : "Iron-Dust", + "GOLD_DUST" : "Gold-Dust", + "GOLD_4K" : "Gold-Ingot", + "GOLD_6K" : "Gold-Ingot", + "GOLD_8K" : "Gold-Ingot", + "GOLD_10K" : "Gold-Ingot", + "GOLD_12K" : "Gold-Ingot", + "GOLD_14K" : "Gold-Ingot", + "GOLD_16K" : "Gold-Ingot", + "GOLD_18K" : "Gold-Ingot", + "GOLD_20K" : "Gold-Ingot", + "GOLD_22K" : "Gold-Ingot", + "GOLD_24K" : "Gold-Ingot", + "ENERGY_REGULATOR" : "Energy-Regulator", + "SMALL_CAPACITOR" : "Energy-Capacitors", + "MEDIUM_CAPACITOR" : "Energy-Capacitors", + "BIG_CAPACITOR" : "Energy-Capacitors", + "LARGE_CAPACITOR" : "Energy-Capacitors", + "CARBONADO_EDGED_CAPACITOR" : "Energy-Capacitors", + "SOLAR_GENERATOR" : "Solar-Generator", + "SOLAR_GENERATOR_2" : "Solar-Generator", + "SOLAR_GENERATOR_3" : "Solar-Generator", + "SOLAR_GENERATOR_4" : "Solar-Generator", + "COAL_GENERATOR" : "Coal-Generator", + "COAL_GENERATOR_2" : "Coal-Generator", + "COBALT_PICKAXE" : "Cobalt-Pickaxe", + "EXPLOSIVE_PICKAXE" : "Explosive-Pickaxe", + "EXPLOSIVE_SHOVEL" : "Explosive-Shovel", + "GRAPPLING_HOOK" : "Grappling-Hook", + "HERCULES_PICKAXE" : "Hercules'-Pickaxe", + "LUMBER_AXE" : "Lumber-Axe", + "PICKAXE_OF_VEIN_MINING" : "Pickaxe-of-Vein-Mining", + "PICKAXE_OF_THE_SEEKER" : "Pickaxe-of-the-Seeker", + "CARGO_OUTPUT_ADVANCED" : "Advanced-Output-Node", + "CARGO_MANAGER" : "Cargo-Manager", + "CARGO_MOTOR" : "Cargo-Motor", + "CARGO_NODE" : "Connector-Node", + "CARGO_INPUT" : "Input-Node", + "CARGO_OUTPUT" : "Output-Node", + "TRASH_CAN" : "Trash-Can", + "ORE_WASHER" : "Ore-Washer", + "SCUBA_HELMET" : "Hazmat-Suit", + "HAZMAT_CHESTPLATE" : "Hazmat-Suit", + "HAZMAT_LEGGINGS" : "Hazmat-Suit", + "RUBBER_BOOTS" : "Hazmat-Suit", + "ARMOR_FORGE" : "Armor-Forge", + "AUTOMATED_PANNING_MACHINE" : "Automated-Panning-Machine", + "COMPRESSOR" : "Compressor", + "ORE_CRUSHER" : "Ore-Crusher", + "PRESSURE_CHAMBER" : "Pressure-Chamber", + "GRIND_STONE" : "Grind-Stone", + "MAGIC_WORKBENCH" : "Magic-Workbench" } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java index 03d43a245..a1ecbb03e 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/mocks/SlimefunMocks.java @@ -9,6 +9,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.mockito.Mockito; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Objects.Category; @@ -16,7 +17,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; public final class SlimefunMocks { - private static final Category category = new Category(new NamespacedKey(SlimefunPlugin.instance, "test"), new ItemStack(Material.EMERALD)); + private static final Category category = new Category(new NamespacedKey(SlimefunPlugin.instance, "test"), new CustomItem(Material.EMERALD, "&4Test Category")); private SlimefunMocks() {} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java new file mode 100644 index 000000000..de4660f5b --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java @@ -0,0 +1,141 @@ +package io.github.thebusybiscuit.slimefun4.tests.items; + +import java.time.LocalDate; +import java.time.Month; + +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory; +import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; +import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.LockedCategory; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; + +public class TestCategories { + + private static ServerMock server; + private static SlimefunPlugin plugin; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(SlimefunPlugin.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + public void testCategoryGetters() { + Category category = SlimefunMocks.getCategory(); + + Assertions.assertEquals(3, category.getTier()); + Assertions.assertEquals(new NamespacedKey(SlimefunPlugin.instance, "test"), category.getKey()); + Assertions.assertEquals("Test Category", category.getUnlocalizedName()); + Assertions.assertEquals(0, category.getItems().size()); + + SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST_ITEM", new CustomItem(Material.BAMBOO, "&6Test Bamboo")); + item.register(plugin); + item.load(); + + Assertions.assertEquals(1, category.getItems().size()); + Assertions.assertTrue(category.getItems().contains(item)); + } + + @Test + public void testHidden() { + Category category = new Category(new NamespacedKey(plugin, "testCategory2"), new ItemStack(Material.BEACON)); + Player player = server.addPlayer(); + + // Empty Categories are also hidden + Assertions.assertTrue(category.isHidden(player)); + + VanillaItem vanillaItem = SlimefunMocks.mockVanillaItem(Material.BEETROOT, false); + vanillaItem.setCategory(category); + vanillaItem.register(plugin); + vanillaItem.load(); + + // A disabled Item should also make the Category hide + Assertions.assertTrue(category.isHidden(player)); + + SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_HIDDEN_TEST", new CustomItem(Material.BAMBOO, "&6Test Bamboo")); + item.setCategory(category); + item.setHidden(true); + item.register(plugin); + item.load(); + + // A hidden Item should also make the Category hide + Assertions.assertTrue(category.isHidden(player)); + + item.setHidden(false); + Assertions.assertFalse(category.isHidden(player)); + } + + @Test + public void testContains() { + Category category = SlimefunMocks.getCategory(); + SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST_ITEM_2", new CustomItem(Material.BOW, "&6Test Bow")); + item.register(plugin); + item.load(); + + Assertions.assertTrue(category.contains(item)); + Assertions.assertFalse(category.contains(null)); + Assertions.assertFalse(category.contains(SlimefunMocks.mockSlimefunItem("NULL", new ItemStack(Material.BEDROCK)))); + } + + @Test + public void testLockedCategories() { + Assertions.assertThrows(IllegalArgumentException.class, () -> new LockedCategory(new NamespacedKey(plugin, "locked"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), (NamespacedKey) null)); + + Category category = SlimefunMocks.getCategory(); + LockedCategory locked = new LockedCategory(new NamespacedKey(plugin, "locked"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), category.getKey()); + locked.register(); + + Assertions.assertTrue(locked.getParents().contains(category)); + + locked.removeParent(category); + Assertions.assertFalse(locked.getParents().contains(category)); + + Assertions.assertThrows(IllegalArgumentException.class, () -> locked.addParent(locked)); + Assertions.assertThrows(IllegalArgumentException.class, () -> locked.addParent(null)); + + locked.addParent(category); + Assertions.assertTrue(locked.getParents().contains(category)); + } + + @Test + public void testSeasonalCategories() { + // Category with current Month + Month month = LocalDate.now().getMonth(); + SeasonalCategory category = new SeasonalCategory(new NamespacedKey(plugin, "seasonal"), month, 1, new CustomItem(Material.NETHER_STAR, "&cSeasonal Test")); + SlimefunItem item = SlimefunMocks.mockSlimefunItem("SEASONAL_ITEM", new CustomItem(Material.NETHER_STAR, "&dSeasonal Test Star")); + item.setCategory(category); + item.register(plugin); + item.load(); + + Player player = server.addPlayer(); + + Assertions.assertEquals(month, category.getMonth()); + System.out.println(month); + System.out.println(category.getMonth()); + Assertions.assertFalse(category.isHidden(player)); + + // Category with future Month + SeasonalCategory category2 = new SeasonalCategory(category.getKey(), month.plus(6), 1, new CustomItem(Material.MILK_BUCKET, "&dSeasonal Test")); + Assertions.assertTrue(category2.isHidden(player)); + } +}