From ee5a2c920b8b893584a39c0b00f268caa17cb231 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 9 Mar 2019 18:43:27 -0800 Subject: [PATCH] Added Panel Test class. Fixed bugs with head requesting. --- .../bentobox/bentobox/api/panels/Panel.java | 20 +- .../bentobox/util/heads/HeadRequester.java | 5 +- .../bentobox/api/panels/PanelTest.java | 343 ++++++++++++++++++ 3 files changed, 361 insertions(+), 7 deletions(-) create mode 100644 src/test/java/world/bentobox/bentobox/api/panels/PanelTest.java diff --git a/src/main/java/world/bentobox/bentobox/api/panels/Panel.java b/src/main/java/world/bentobox/bentobox/api/panels/Panel.java index 40738c9dd..ce3e7efa1 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/Panel.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/Panel.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.panels; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -41,10 +42,12 @@ public class Panel implements HeadRequester { // Fill the inventory and return for (Map.Entry en: items.entrySet()) { //TODO allow multi-paging - if (en.getKey() < 54) inventory.setItem(en.getKey(), en.getValue().getItem()); - // Get player head async - if (en.getValue().isPlayerHead()) { - HeadGetter.getHead(en.getValue(), this); + if (en.getKey() < 54) { + inventory.setItem(en.getKey(), en.getValue().getItem()); + // Get player head async + if (en.getValue().isPlayerHead()) { + HeadGetter.getHead(en.getValue(), this); + } } } } else { @@ -124,16 +127,23 @@ public class Panel implements HeadRequester { this.user = user; } + /* (non-Javadoc) + * @see world.bentobox.bentobox.util.heads.HeadRequester#setHead(world.bentobox.bentobox.api.panels.PanelItem) + */ @Override public void setHead(PanelItem item) { // Update the panel item - items.values().stream().filter(i -> i.getName().equals(item.getName())).forEach(i -> i = item); + // Replace the item in the item list if the name is the same + items = items.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> (item.getName().equals(e.getValue().getName()) ? item : e.getValue()))); + // Replace the inventory slot item for (int i = 0; i < inventory.getSize(); i++) { ItemStack it = inventory.getItem(i); if (it != null && it.getType().equals(Material.PLAYER_HEAD)) { ItemMeta meta = it.getItemMeta(); if (item.getName().equals(meta.getLocalizedName())) { inventory.setItem(i, item.getItem()); + // If one is found, we are done return; } } diff --git a/src/main/java/world/bentobox/bentobox/util/heads/HeadRequester.java b/src/main/java/world/bentobox/bentobox/util/heads/HeadRequester.java index a76fe50cd..c5eb25cc5 100644 --- a/src/main/java/world/bentobox/bentobox/util/heads/HeadRequester.java +++ b/src/main/java/world/bentobox/bentobox/util/heads/HeadRequester.java @@ -3,9 +3,10 @@ package world.bentobox.bentobox.util.heads; import world.bentobox.bentobox.api.panels.PanelItem; public interface HeadRequester { - + /** - * @param item - panel item + * Replaces the head in an open inventory panel with the supplied panel item + * @param item - panel item, must be a player head */ void setHead(PanelItem item); } diff --git a/src/test/java/world/bentobox/bentobox/api/panels/PanelTest.java b/src/test/java/world/bentobox/bentobox/api/panels/PanelTest.java new file mode 100644 index 000000000..947ceeaa4 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/panels/PanelTest.java @@ -0,0 +1,343 @@ +/** + * + */ +package world.bentobox.bentobox.api.panels; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.internal.verification.VerificationModeFactory; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.heads.HeadGetter; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({ Bukkit.class, HeadGetter.class }) +public class PanelTest { + + private String name; + private Map items; + @Mock + private User user; + @Mock + private PanelListener listener; + @Mock + private Player player; + @Mock + private Inventory inv; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Server & Bukkit + Server server = mock(Server.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getServer()).thenReturn(server); + when(Bukkit.createInventory(Mockito.any(), Mockito.anyInt(), Mockito.anyString())).thenReturn(inv); + + name = "panel"; + items = Collections.emptyMap(); + when(user.getPlayer()).thenReturn(player); + when(user.getUniqueId()).thenReturn(UUID.randomUUID()); + + // Head getter + PowerMockito.mockStatic(HeadGetter.class); + + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanel() { + // Panel + new Panel(name, items, 10, user, listener); + + // The next two lines have to be paired together to verify the static call + PowerMockito.verifyStatic(Bukkit.class, VerificationModeFactory.times(1)); + Bukkit.createInventory(Mockito.eq(null), Mockito.eq(18), Mockito.eq(name)); + + Mockito.verify(listener).setup(); + Mockito.verify(player).openInventory(Mockito.any(Inventory.class)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanelZeroSize() { + // Panel + new Panel(name, items, 0, user, listener); + + // The next two lines have to be paired together to verify the static call + PowerMockito.verifyStatic(Bukkit.class, VerificationModeFactory.times(1)); + Bukkit.createInventory(Mockito.eq(null), Mockito.eq(9), Mockito.eq(name)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanelTooBig() { + // Panel + new Panel(name, items, 100, user, listener); + + // The next two lines have to be paired together to verify the static call + PowerMockito.verifyStatic(Bukkit.class, VerificationModeFactory.times(1)); + Bukkit.createInventory(Mockito.eq(null), Mockito.eq(54), Mockito.eq(name)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanelNullUser() { + // Panel + new Panel(name, items, 10, null, listener); + Mockito.verify(player, Mockito.never()).openInventory(Mockito.any(Inventory.class)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanelWithItems() { + // Items + ItemStack itemStack = mock(ItemStack.class); + PanelItem item = mock(PanelItem.class); + when(item.getItem()).thenReturn(itemStack); + + items = new HashMap<>(); + for (int i = 0; i<100; i++) { + items.put(i, item); + } + // Panel + new Panel(name, items, 0, user, listener); + + // The next two lines have to be paired together to verify the static call + PowerMockito.verifyStatic(Bukkit.class, VerificationModeFactory.times(1)); + Bukkit.createInventory(Mockito.eq(null), Mockito.eq(54), Mockito.eq(name)); + + Mockito.verify(inv, Mockito.times(54)).setItem(Mockito.anyInt(), Mockito.eq(itemStack)); + Mockito.verify(player).openInventory(Mockito.any(Inventory.class)); + + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#Panel(java.lang.String, java.util.Map, int, world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testPanelWithHeads() { + // Items + ItemStack itemStack = mock(ItemStack.class); + PanelItem item = mock(PanelItem.class); + when(item.getItem()).thenReturn(itemStack); + when(item.isPlayerHead()).thenReturn(true); + + items = new HashMap<>(); + for (int i = 0; i<100; i++) { + items.put(i, item); + } + // Panel + Panel p = new Panel(name, items, 0, user, listener); + + // The next two lines have to be paired together to verify the static call + PowerMockito.verifyStatic(HeadGetter.class, VerificationModeFactory.times(54)); + HeadGetter.getHead(Mockito.eq(item), Mockito.eq(p)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#getInventory()}. + */ + @Test + public void testGetInventory() { + Panel p = new Panel(name, items, 10, null, listener); + assertEquals(inv, p.getInventory()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#getItems()}. + */ + @Test + public void testGetItems() { + Panel p = new Panel(name, items, 10, null, listener); + assertEquals(items, p.getItems()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#getListener()}. + */ + @Test + public void testGetListener() { + Panel p = new Panel(name, items, 10, null, listener); + assertEquals(listener, p.getListener().get()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#getUser()}. + */ + @Test + public void testGetUser() { + Panel p = new Panel(name, items, 10, user, listener); + assertEquals(user, p.getUser().get()); + + p = new Panel(name, items, 10, null, listener); + assertEquals(Optional.empty(), p.getUser()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#open(org.bukkit.entity.Player[])}. + */ + @Test + public void testOpenPlayerArray() { + Panel p = new Panel(name, items, 10, user, listener); + p.open(player, player, player); + Mockito.verify(player, Mockito.times(4)).openInventory(inv); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#open(world.bentobox.bentobox.api.user.User[])}. + */ + @Test + public void testOpenUserArray() { + Panel p = new Panel(name, items, 10, user, listener); + p.open(user, user, user); + Mockito.verify(player, Mockito.times(4)).openInventory(inv); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#setInventory(org.bukkit.inventory.Inventory)}. + */ + @Test + public void testSetInventory() { + Panel p = new Panel(name, items, 10, user, listener); + Inventory inventory = mock(Inventory.class); + p.setInventory(inventory); + assertEquals(inventory, p.getInventory()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#setItems(java.util.Map)}. + */ + @Test + public void testSetItems() { + Panel p = new Panel(name, items, 10, user, listener); + Map newMap = new HashMap<>(); + newMap.put(23, mock(PanelItem.class)); + p.setItems(newMap); + assertEquals(newMap, p.getItems()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#setListener(world.bentobox.bentobox.api.panels.PanelListener)}. + */ + @Test + public void testSetListener() { + Panel p = new Panel(name, items, 10, user, null); + assertEquals(Optional.empty(), p.getListener()); + p.setListener(listener); + assertEquals(listener, p.getListener().get()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#setUser(world.bentobox.bentobox.api.user.User)}. + */ + @Test + public void testSetUser() { + Panel p = new Panel(name, items, 10, null, listener); + assertEquals(Optional.empty(), p.getUser()); + p.setUser(user); + assertEquals(user, p.getUser().get()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#setHead(world.bentobox.bentobox.api.panels.PanelItem)}. + */ + @Test + public void testSetHead() { + // Items + ItemStack itemStack = mock(ItemStack.class); + when(itemStack.getType()).thenReturn(Material.PLAYER_HEAD); + ItemMeta im = mock(ItemMeta.class); + when(im.getLocalizedName()).thenReturn("tastybento"); + when(itemStack.getItemMeta()).thenReturn(im); + PanelItem item = mock(PanelItem.class); + when(item.getItem()).thenReturn(itemStack); + when(item.isPlayerHead()).thenReturn(true); + when(item.getName()).thenReturn("tastybento"); + + items = new HashMap<>(); + for (int i = 0; i<10; i++) { + items.put(i, item); + } + // Inv + when(inv.getSize()).thenReturn(18); + when(inv.getItem(Mockito.anyInt())).thenReturn(itemStack); + + // Panel + Panel p = new Panel(name, items, 0, user, listener); + + ItemStack itemStack2 = mock(ItemStack.class); + when(itemStack2.getType()).thenReturn(Material.PLAYER_HEAD); + ItemMeta im2 = mock(ItemMeta.class); + when(im2.getLocalizedName()).thenReturn("tastybento"); + when(itemStack2.getItemMeta()).thenReturn(im2); + + PanelItem newItem = mock(PanelItem.class); + when(itemStack.getType()).thenReturn(Material.PLAYER_HEAD); + when(newItem.getItem()).thenReturn(itemStack2); + when(newItem.isPlayerHead()).thenReturn(true); + when(newItem.getName()).thenReturn("tastybento"); + + p.setHead(newItem); + + assertEquals(newItem, p.getItems().get(0)); + Mockito.verify(inv).setItem(Mockito.anyInt(), Mockito.eq(itemStack2)); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.panels.Panel#getName()}. + */ + @Test + public void testGetName() { + Panel p = new Panel(name, items, 10, null, listener); + assertEquals(name, p.getName()); + } + +}