Fixes PanelListenerManagerTest

The InventoryView interface uses final methods so it cannot be mocked.
This test implements the interface so that the code can be tested.
This commit is contained in:
tastybento 2019-05-15 08:57:11 -07:00
parent 9437b916e0
commit 877046fe8c
2 changed files with 50 additions and 16 deletions

View File

@ -11,9 +11,8 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem; import world.bentobox.bentobox.api.panels.PanelItem;

View File

@ -12,17 +12,19 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.InventoryType.SlotType; import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -45,14 +47,12 @@ import world.bentobox.bentobox.util.Util;
* @author tastybento * @author tastybento
* *
*/ */
@Ignore("NPEs on setup")
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class }) @PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
public class PanelListenerManagerTest { public class PanelListenerManagerTest {
@Mock @Mock
private Player player; private Player player;
@Mock
private InventoryView view; private InventoryView view;
@Mock @Mock
private PanelListenerManager plm; private PanelListenerManager plm;
@ -89,11 +89,8 @@ public class PanelListenerManagerTest {
User.getInstance(player); User.getInstance(player);
// Inventory view // Inventory view
when(view.getPlayer()).thenReturn(player); view = new MyView("name");
when(view.getTitle()).thenReturn("name");
Inventory top = mock(Inventory.class);
when(top.getSize()).thenReturn(9);
when(view.getTopInventory()).thenReturn(top);
type = SlotType.CONTAINER; type = SlotType.CONTAINER;
click = ClickType.LEFT; click = ClickType.LEFT;
inv = InventoryAction.UNKNOWN; inv = InventoryAction.UNKNOWN;
@ -104,7 +101,7 @@ public class PanelListenerManagerTest {
// Panel // Panel
Optional<PanelListener> opl = Optional.of(pl); Optional<PanelListener> opl = Optional.of(pl);
when(panel.getListener()).thenReturn(opl); when(panel.getListener()).thenReturn(opl);
when(panel.getInventory()).thenReturn(top); when(panel.getInventory()).thenReturn(mock(Inventory.class));
when(panel.getName()).thenReturn("name"); when(panel.getName()).thenReturn("name");
Map<Integer, PanelItem> map = new HashMap<>(); Map<Integer, PanelItem> map = new HashMap<>();
PanelItem panelItem = mock(PanelItem.class); PanelItem panelItem = mock(PanelItem.class);
@ -115,8 +112,6 @@ public class PanelListenerManagerTest {
map.put(0, panelItem); map.put(0, panelItem);
when(panel.getItems()).thenReturn(map); when(panel.getItems()).thenReturn(map);
when(top.getHolder()).thenReturn(panel);
Panel wrongPanel = mock(Panel.class); Panel wrongPanel = mock(Panel.class);
when(wrongPanel.getName()).thenReturn("another_name"); when(wrongPanel.getName()).thenReturn("another_name");
when(wrongPanel.getInventory()).thenReturn(anotherInv); when(wrongPanel.getInventory()).thenReturn(anotherInv);
@ -125,6 +120,48 @@ public class PanelListenerManagerTest {
PanelListenerManager.getOpenPanels().clear(); PanelListenerManager.getOpenPanels().clear();
} }
class MyView extends InventoryView {
private Inventory top;
/**
* @param name
*/
@SuppressWarnings("deprecation")
public MyView(String name) {
top = mock(Inventory.class);
when(top.getSize()).thenReturn(9);
when(top.getHolder()).thenReturn(panel);
when(top.getTitle()).thenReturn(name);
}
@Override
public Inventory getTopInventory() {
return top;
}
@Override
public Inventory getBottomInventory() {
return null;
}
@Override
public HumanEntity getPlayer() {
return player;
}
@Override
public InventoryType getType() {
return InventoryType.PLAYER;
}
}
@After
public void cleanUp() {
PanelListenerManager.getOpenPanels().clear();
}
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.PanelListenerManager#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/ */
@ -166,9 +203,7 @@ public class PanelListenerManagerTest {
@Test @Test
public void testOnInventoryClickOpenPanelsWrongPanel() { public void testOnInventoryClickOpenPanelsWrongPanel() {
PanelListenerManager.getOpenPanels().put(uuid, panel); PanelListenerManager.getOpenPanels().put(uuid, panel);
when(view.getTopInventory()).thenReturn(anotherInv); InventoryClickEvent e = new InventoryClickEvent(new MyView("another"), type, 0, click, inv);
when(view.getTitle()).thenReturn("another title");
InventoryClickEvent e = new InventoryClickEvent(view, type, 0, click, inv);
plm.onInventoryClick(e); plm.onInventoryClick(e);
// Panel should be removed // Panel should be removed
assertTrue(PanelListenerManager.getOpenPanels().isEmpty()); assertTrue(PanelListenerManager.getOpenPanels().isEmpty());