Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 16, 2022
1 parent 046c4ff commit 5ab89ac
Showing 1 changed file with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Chest;
import org.bukkit.block.Dispenser;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Dropper;
import org.bukkit.block.Furnace;
import org.bukkit.block.Hopper;
Expand All @@ -31,7 +38,6 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

Expand All @@ -48,12 +54,14 @@
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class InventoryListenerTest extends AbstractCommonSetup {

private final static List<Class<?>> HOLDERS = Arrays.asList(Horse.class, Chest.class,ShulkerBox.class, StorageMinecart.class,
private final static List<Class<?>> HOLDERS = Arrays.asList(Horse.class, Chest.class,
DoubleChest.class,
ShulkerBox.class, StorageMinecart.class,
Dispenser.class,
Dropper.class, Hopper.class, Furnace.class, BrewingStand.class,
Villager.class, WanderingTrader.class);

private InventoryListener l;
private Material type;

/**
* @throws java.lang.Exception
Expand All @@ -63,7 +71,13 @@ public class InventoryListenerTest extends AbstractCommonSetup {
public void setUp() throws Exception {
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);

// Block at chest location
type = Material.CHEST;
Block block = mock(Block.class);
when(block.getType()).thenReturn(type);
when(location.getBlock()).thenReturn(block);

// Listener
l = new InventoryListener();
Expand All @@ -82,6 +96,12 @@ public void testOnInventoryClickAllowed() {
HOLDERS.forEach(c -> {
Object holder = mock(c);
when(inv.getHolder()).thenReturn((InventoryHolder) holder);
if (c.equals(Chest.class)) {
when(((Chest)holder).getLocation()).thenReturn(location);
}
if (c.equals(DoubleChest.class)) {
when(((DoubleChest)holder).getLocation()).thenReturn(location);
}
when(view.getTopInventory()).thenReturn(inv);
when(inv.getLocation()).thenReturn(location);
when(view.getBottomInventory()).thenReturn(inv);
Expand All @@ -94,6 +114,15 @@ public void testOnInventoryClickAllowed() {

}

/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
@Test
public void testOnInventoryClickAllowedTrappedChest() {
type = Material.TRAPPED_CHEST;
testOnInventoryClickAllowed();
}

/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
Expand Down Expand Up @@ -141,14 +170,20 @@ public void testOnInventoryClickNotPlayer() {
*/
@Test
public void testOnInventoryClickNotAllowed() {
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
InventoryView view = mock(InventoryView.class);
when(view.getPlayer()).thenReturn(player);
Inventory inv = mock(Inventory.class);
when(inv.getLocation()).thenReturn(location);
when(inv.getSize()).thenReturn(9);
HOLDERS.forEach(c -> {
Object holder = mock(c);
if (c.equals(Chest.class)) {
when(((Chest)holder).getLocation()).thenReturn(location);
}
if (c.equals(DoubleChest.class)) {
when(((DoubleChest)holder).getLocation()).thenReturn(location);
}
when(inv.getHolder()).thenReturn((InventoryHolder) holder);
when(view.getTopInventory()).thenReturn(inv);
when(view.getBottomInventory()).thenReturn(inv);
Expand All @@ -158,9 +193,19 @@ public void testOnInventoryClickNotAllowed() {
l.onInventoryClick(e);
assertTrue(e.isCancelled());
});
Mockito.verify(notifier, Mockito.times(HOLDERS.size())).notify(Mockito.any(), Mockito.eq("protection.protected"));
verify(notifier, times(HOLDERS.size())).notify(any(), eq("protection.protected"));
}

/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
@Test
public void testOnInventoryClickNotAllowedTrappedChest() {
type = Material.TRAPPED_CHEST;
testOnInventoryClickNotAllowed();
}


/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/
Expand All @@ -187,7 +232,7 @@ public void testOnInventoryClickOtherHolderAllowed() {
*/
@Test
public void testOnInventoryClickOtherHolderNotAllowed() {
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
InventoryView view = mock(InventoryView.class);
when(view.getPlayer()).thenReturn(player);
Inventory inv = mock(Inventory.class);
Expand All @@ -209,7 +254,7 @@ public void testOnInventoryClickOtherHolderNotAllowed() {
*/
@Test
public void testOnInventoryClickOtherHolderPlayerNotAllowed() {
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
InventoryView view = mock(InventoryView.class);
when(view.getPlayer()).thenReturn(player);
Inventory inv = mock(Inventory.class);
Expand Down

0 comments on commit 5ab89ac

Please sign in to comment.