From 073bb2f0176db19fd41d18ee13d4b9db53266bd9 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 24 Oct 2019 18:21:07 -0700 Subject: [PATCH] Added Utils test class --- .../bentobox/challenges/utils/Utils.java | 2 +- .../bentobox/challenges/utils/UtilsTest.java | 199 ++++++++++++++++++ 2 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/test/java/world/bentobox/challenges/utils/UtilsTest.java diff --git a/src/main/java/world/bentobox/challenges/utils/Utils.java b/src/main/java/world/bentobox/challenges/utils/Utils.java index 45508d8..b67593a 100644 --- a/src/main/java/world/bentobox/challenges/utils/Utils.java +++ b/src/main/java/world/bentobox/challenges/utils/Utils.java @@ -26,7 +26,7 @@ public class Utils { List returnItems = new ArrayList<>(requiredItems.size()); - // Group all equal items in singe stack, as otherwise it will be too complicated to check if all + // Group all equal items in single stack, as otherwise it will be too complicated to check if all // items are in players inventory. for (ItemStack item : requiredItems) { diff --git a/src/test/java/world/bentobox/challenges/utils/UtilsTest.java b/src/test/java/world/bentobox/challenges/utils/UtilsTest.java new file mode 100644 index 0000000..a89873f --- /dev/null +++ b/src/test/java/world/bentobox/challenges/utils/UtilsTest.java @@ -0,0 +1,199 @@ +package world.bentobox.challenges.utils; + +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.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.ItemStack; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.addons.AddonDescription; +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.challenges.config.SettingsUtils.VisibilityMode; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class}) +public class UtilsTest { + + @Mock + private IslandWorldManager iwm; + @Mock + private GameModeAddon gameModeAddon; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Mock item factory (for itemstacks) + PowerMockito.mockStatic(Bukkit.class); + ItemFactory itemFactory = mock(ItemFactory.class); + when(Bukkit.getItemFactory()).thenReturn(itemFactory); + + // IWM getAddon + AddonDescription desc = new AddonDescription.Builder("main", "name", "1.0").build(); + when(gameModeAddon.getDescription()).thenReturn(desc); + Optional optionalAddon = Optional.of(gameModeAddon); + when(iwm.getAddon(any())).thenReturn(optionalAddon); + when(plugin.getIWM()).thenReturn(iwm); + + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}. + */ + @Test + public void testGroupEqualItemsEmpty() { + assertTrue(Utils.groupEqualItems(Collections.emptyList()).isEmpty()); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}. + */ + @Test + public void testGroupEqualItems() { + List requiredItems = new ArrayList<>(); + // First item + ItemStack is = mock(ItemStack.class); + when(is.getAmount()).thenReturn(1); + when(is.getType()).thenReturn(Material.ACACIA_FENCE); + when(is.getMaxStackSize()).thenReturn(64); + when(is.isSimilar(any())).thenReturn(true); + when(is.clone()).thenReturn(is); + requiredItems.add(is); + for (int i = 0; i < 9; i++) { + ItemStack is2 = mock(ItemStack.class); + when(is2.getAmount()).thenReturn(1); + when(is2.getType()).thenReturn(Material.ACACIA_FENCE); + when(is2.getMaxStackSize()).thenReturn(64); + when(is2.isSimilar(any())).thenReturn(true); + when(is2.clone()).thenReturn(is); + requiredItems.add(is2); + } + List list = Utils.groupEqualItems(requiredItems); + // Result should be two stacks stack of 64 doors and 36 doors + assertEquals(1, list.size()); + verify(is, times(9)).setAmount(2); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#groupEqualItems(java.util.List)}. + */ + @Test + public void testGroupEqualItemsUnique() { + List requiredItems = new ArrayList<>(); + // First item + ItemStack is = mock(ItemStack.class); + when(is.getAmount()).thenReturn(1); + when(is.getType()).thenReturn(Material.ACACIA_FENCE); + when(is.getMaxStackSize()).thenReturn(64); + when(is.isSimilar(any())).thenReturn(false); + when(is.clone()).thenReturn(is); + requiredItems.add(is); + for (int i = 0; i < 9; i++) { + ItemStack is2 = mock(ItemStack.class); + when(is2.getAmount()).thenReturn(1); + when(is2.getType()).thenReturn(Material.values()[i+20]); + when(is2.getMaxStackSize()).thenReturn(64); + when(is2.isSimilar(any())).thenReturn(false); + when(is2.clone()).thenReturn(is); + requiredItems.add(is2); + } + List list = Utils.groupEqualItems(requiredItems); + // Result should be two stacks stack of 64 doors and 36 doors + assertEquals(10, list.size()); + verify(is, never()).setAmount(2); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#canIgnoreMeta(org.bukkit.Material)}. + */ + @Test + public void testCanIgnoreMeta() { + assertTrue(Utils.canIgnoreMeta(Material.FIREWORK_ROCKET)); + assertTrue(Utils.canIgnoreMeta(Material.ENCHANTED_BOOK)); + assertTrue(Utils.canIgnoreMeta(Material.WRITTEN_BOOK)); + assertTrue(Utils.canIgnoreMeta(Material.FILLED_MAP)); + assertFalse(Utils.canIgnoreMeta(Material.CHISELED_RED_SANDSTONE)); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}. + */ + @Test + public void testGetGameModeNoGameMode() { + when(iwm.getAddon(any())).thenReturn(Optional.empty()); + assertNull(Utils.getGameMode(mock(World.class))); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#getGameMode(org.bukkit.World)}. + */ + @Test + public void testGetGameMode() { + assertEquals("name", Utils.getGameMode(mock(World.class))); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#getNextValue(T[], java.lang.Object)}. + */ + @Test + public void testGetNextValue() { + assertEquals(VisibilityMode.HIDDEN, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.VISIBLE)); + assertEquals(VisibilityMode.TOGGLEABLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.HIDDEN)); + assertEquals(VisibilityMode.VISIBLE, Utils.getNextValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE)); + } + + /** + * Test method for {@link world.bentobox.challenges.utils.Utils#getPreviousValue(T[], java.lang.Object)}. + */ + @Test + public void testGetPreviousValue() { + assertEquals(VisibilityMode.TOGGLEABLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.VISIBLE)); + assertEquals(VisibilityMode.VISIBLE, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.HIDDEN)); + assertEquals(VisibilityMode.HIDDEN, Utils.getPreviousValue(VisibilityMode.values(), VisibilityMode.TOGGLEABLE)); + } + +}