Fixed random test failures.

Note: If using the User class in a test, you MUST set the mock plugin object using the User.setPlugin() method otherwise each test will use the wrong plugin object and verifications will not work.
This commit is contained in:
tastybento 2019-01-13 14:19:23 -08:00
parent 5f990b9103
commit 96639792b8
6 changed files with 35 additions and 30 deletions

View File

@ -7,13 +7,19 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.*; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Particle;
import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
/** /**

View File

@ -29,11 +29,11 @@ public class EnderChestListener extends FlagListener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEnderChestOpen(PlayerInteractEvent e) { public void onEnderChestOpen(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
e.setCancelled(checkEnderChest(e.getPlayer(), e.getClickedBlock().getType())); e.setCancelled(checkEnderChest(e.getPlayer(), e.getClickedBlock().getType()));
} }
} }
/** /**
* Prevents crafting of EnderChest unless the player has permission * Prevents crafting of EnderChest unless the player has permission
* *

View File

@ -65,6 +65,7 @@ public class IslandBanCommandTest {
// Set up plugin // Set up plugin
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);

View File

@ -65,6 +65,7 @@ public class IslandUnbanCommandTest {
// Set up plugin // Set up plugin
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);

View File

@ -27,6 +27,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe; import org.bukkit.inventory.Recipe;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -49,13 +50,16 @@ import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class, User.class }) @PrepareForTest({BentoBox.class, Util.class })
public class EnderChestListenerTest { public class EnderChestListenerTest {
private World world; private World world;
private Player player; private Player player;
private IslandWorldManager iwm; private IslandWorldManager iwm;
private Notifier notifier; private Notifier notifier;
private ItemStack item;
private Block clickedBlock;
private Action action;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -110,6 +114,8 @@ public class EnderChestListenerTest {
// No special perms // No special perms
when(player.hasPermission(Mockito.anyString())).thenReturn(false); when(player.hasPermission(Mockito.anyString())).thenReturn(false);
when(player.getWorld()).thenReturn(world); when(player.getWorld()).thenReturn(world);
User.setPlugin(plugin);
User.getInstance(player);
// Locales - this returns the string that was requested for translation // Locales - this returns the string that was requested for translation
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
@ -119,13 +125,22 @@ public class EnderChestListenerTest {
// Notifier // Notifier
notifier = mock(Notifier.class); notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier); when(plugin.getNotifier()).thenReturn(notifier);
// Acition, Item and clicked block
action = Action.RIGHT_CLICK_BLOCK;
item = mock(ItemStack.class);
clickedBlock = mock(Block.class);
}
@After
public void cleanUp() {
User.clearUsers();
} }
@Test @Test
public void testOnEnderChestOpenNotRightClick() { public void testOnEnderChestOpenNotRightClick() {
Action action = Action.LEFT_CLICK_AIR; action = Action.LEFT_CLICK_AIR;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
new EnderChestListener().onEnderChestOpen(e); new EnderChestListener().onEnderChestOpen(e);
@ -134,9 +149,6 @@ public class EnderChestListenerTest {
@Test @Test
public void testOnEnderChestOpenNotEnderChest() { public void testOnEnderChestOpenNotEnderChest() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.STONE); when(clickedBlock.getType()).thenReturn(Material.STONE);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
@ -146,9 +158,6 @@ public class EnderChestListenerTest {
@Test @Test
public void testOnEnderChestOpenEnderChestNotInWorld() { public void testOnEnderChestOpenEnderChestNotInWorld() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST); when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
@ -161,9 +170,6 @@ public class EnderChestListenerTest {
@Test @Test
public void testOnEnderChestOpenEnderChestOpPlayer() { public void testOnEnderChestOpenEnderChestOpPlayer() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST); when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
@ -175,9 +181,6 @@ public class EnderChestListenerTest {
@Test @Test
public void testOnEnderChestOpenEnderChestHasBypassPerm() { public void testOnEnderChestOpenEnderChestHasBypassPerm() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST); when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
@ -189,9 +192,6 @@ public class EnderChestListenerTest {
@Test @Test
public void testOnEnderChestOpenEnderChestOkay() { public void testOnEnderChestOpenEnderChestOkay() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST); when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
@ -199,21 +199,20 @@ public class EnderChestListenerTest {
Flags.ENDER_CHEST.setSetting(world, true); Flags.ENDER_CHEST.setSetting(world, true);
new EnderChestListener().onEnderChestOpen(e); new EnderChestListener().onEnderChestOpen(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.anyObject(), Mockito.anyString());
} }
@Test @Test
public void testOnEnderChestOpenEnderChestBlocked() { public void testOnEnderChestOpenEnderChestBlocked() {
Action action = Action.RIGHT_CLICK_BLOCK;
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST); when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST; BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace); PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Enderchest use is okay // Enderchest use is blocked
Flags.ENDER_CHEST.setSetting(world, false); Flags.ENDER_CHEST.setSetting(world, false);
new EnderChestListener().onEnderChestOpen(e); new EnderChestListener().onEnderChestOpen(e);
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
Mockito.verify(notifier).notify(Mockito.anyObject(), Mockito.eq("protection.protected")); Mockito.verify(notifier).notify(Mockito.any(User.class), Mockito.eq("protection.protected"));
} }
@Test @Test

View File

@ -136,8 +136,6 @@ public class PhysicalInteractionListenerTest {
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>()); Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// Users // Users
//User user = mock(User.class);
///user.setPlugin(plugin);
User.setPlugin(plugin); User.setPlugin(plugin);