Changed AdminSwitchCommand to use player meta data

Fixed tests. Abstracted common test code to AbstractCommonSetup class.
This commit is contained in:
tastybento 2021-02-06 12:08:09 -08:00
parent bb7ed2b85c
commit 1da3539f02
20 changed files with 430 additions and 1909 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
/**
@ -12,8 +13,6 @@ import world.bentobox.bentobox.api.user.User;
*/
public class AdminSwitchCommand extends ConfirmableCommand {
private final String bypassPerm;
/**
* Switches bypass on and off
* @param parent - admin command
@ -21,7 +20,6 @@ public class AdminSwitchCommand extends ConfirmableCommand {
*/
public AdminSwitchCommand(CompositeCommand parent) {
super(parent, "switch");
bypassPerm = getPermissionPrefix() + "mod.bypassprotect";
}
@Override
@ -39,30 +37,22 @@ public class AdminSwitchCommand extends ConfirmableCommand {
showHelp(this, user);
return false;
}
if (user.isOp()) {
user.sendMessage("commands.admin.switch.op");
return false;
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
if (user.hasPermission(getPermissionPrefix() + "mod.switch")) {
if (user.hasPermission(bypassPerm)) {
user.sendMessage("commands.admin.switch.removing");
// Remove positive perm
if (user.removePerm(bypassPerm)) {
user.sendMessage("general.success");
}
} else {
user.sendMessage("commands.admin.switch.adding");
// Add positive permission
user.addPerm(bypassPerm);
if (user.hasPermission(bypassPerm)) {
user.sendMessage("general.success");
}
}
boolean switchState = user.getMetaData("AdminCommandSwitch").map(MetaDataValue::asBoolean).orElse(false);
if (switchState) {
// Turn off
user.putMetaData("AdminCommandSwitch", new MetaDataValue(false));
user.sendMessage("commands.admin.switch.adding"); // Adding protection bypass
user.sendMessage("general.success");
} else {
// Turn on
user.putMetaData("AdminCommandSwitch", new MetaDataValue(true));
user.sendMessage("commands.admin.switch.removing"); // Removing protection bypass
user.sendMessage("general.success");
}
return true;
}

View File

@ -14,6 +14,7 @@ import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.IslandWorldManager;
@ -151,9 +152,10 @@ public abstract class FlagListener implements Listener {
// Protection flag
// Ops or "bypass everywhere" moderators can do anything
if (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + "mod.bypassprotect")
|| user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + "mod.bypass." + flag.getID() + ".everywhere")) {
// Ops or "bypass everywhere" moderators can do anything unless they have switched it off
if (!user.getMetaData("AdminCommandSwitch").map(MetaDataValue::asBoolean).orElse(false)
&& (user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + "mod.bypassprotect")
|| user.hasPermission(getIWM().getPermissionPrefix(loc.getWorld()) + "mod.bypass." + flag.getID() + ".everywhere"))) {
if (user.isOp()) {
report(user, e, loc, flag, Why.OP);
} else {

View File

@ -642,7 +642,10 @@ public class User implements MetaDataAble {
*/
@Override
public Optional<Map<String, MetaDataValue>> getMetaData() {
return plugin.getPlayers().getPlayer(playerUUID).getMetaData();
return plugin
.getPlayers()
.getPlayer(playerUUID)
.getMetaData();
}
/**

View File

@ -0,0 +1,151 @@
package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class})
public class AdminSwitchCommandTest {
private AdminSwitchCommand asc;
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private Player p;
private UUID notUUID;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Player
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.isPlayer()).thenReturn(true);
User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getTopLabel()).thenReturn("bskyblock");
asc = new AdminSwitchCommand(ac);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSwitchCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("mod.switch", asc.getPermission());
assertTrue(asc.isOnlyPlayer());
assertEquals("commands.admin.switch.parameters", asc.getParameters());
assertEquals("commands.admin.switch.description", asc.getDescription());
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSwitchCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecute() {
assertFalse(asc.canExecute(user, "", Collections.singletonList("hello")));
verify(user).sendMessage("commands.help.header", TextVariables.LABEL, null);
assertTrue(asc.canExecute(user, "", Collections.emptyList()));
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSwitchCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoMetaData() {
when(user.getMetaData(eq("AdminCommandSwitch"))).thenReturn(Optional.empty());
asc.execute(user, "", Collections.emptyList());
verify(user).getMetaData("AdminCommandSwitch");
verify(user).sendMessage("commands.admin.switch.removing");
verify(user).sendMessage("general.success");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSwitchCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringMetaFalse() {
MetaDataValue md = new MetaDataValue(false);
when(user.getMetaData(eq("AdminCommandSwitch"))).thenReturn(Optional.of(md));
asc.execute(user, "", Collections.emptyList());
verify(user).getMetaData("AdminCommandSwitch");
verify(user).sendMessage("commands.admin.switch.removing");
verify(user).sendMessage("general.success");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminSwitchCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringMetaTrue() {
MetaDataValue md = new MetaDataValue(true);
when(user.getMetaData(eq("AdminCommandSwitch"))).thenReturn(Optional.of(md));
asc.execute(user, "", Collections.emptyList());
verify(user).getMetaData("AdminCommandSwitch");
verify(user).sendMessage("commands.admin.switch.adding");
verify(user).sendMessage("general.success");
}
}

View File

@ -0,0 +1,168 @@
package world.bentobox.bentobox.listeners.flags;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.listeners.flags.protection.TestWorldSettings;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
public abstract class AbstractCommonSetup {
protected UUID uuid = UUID.randomUUID();
@Mock
protected Player player;
@Mock
protected PluginManager pim;
@Mock
protected ItemFactory itemFactory;
@Mock
protected Location location;
@Mock
protected World world;
@Mock
protected IslandWorldManager iwm;
@Mock
protected IslandsManager im;
@Mock
protected Island island;
@Mock
protected BentoBox plugin;
@Mock
protected PlayerInventory inv;
@Mock
protected Notifier notifier;
@Mock
protected FlagsManager fm;
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
// Players Manager and meta data
PlayersManager pm = mock(PlayersManager.class);
when(plugin.getPlayers()).thenReturn(pm);
Players players = mock(Players.class);
when(players.getMetaData()).thenReturn(Optional.empty());
when(pm.getPlayer(any(UUID.class))).thenReturn(players);
// Player
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
when(player.getWorld()).thenReturn(world);
when(player.getName()).thenReturn("tastybento");
when(player.getInventory()).thenReturn(inv);
User.setPlugin(plugin);
User.getInstance(player);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
@Nullable
WorldSettings worldSet = new TestWorldSettings();
when(iwm.getWorldSettings(any())).thenReturn(worldSet);
// Island Manager
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optionalIsland = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optionalIsland);
// Island - nothing is allowed by default
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
when(island.getOwner()).thenReturn(uuid);
// Enable reporting from Flags class
MetadataValue mdv = new FixedMetadataValue(plugin, "_why_debug");
when(player.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv));
// Locales & Placeholders
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Fake players
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
}

View File

@ -4,98 +4,49 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
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.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class BlockInteractionListenerTest {
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
public class BlockInteractionListenerTest extends AbstractCommonSetup {
@Mock
private Player player;
@Mock
private PluginManager pim;
@Mock
private ItemFactory itemFactory;
@Mock
private Location location;
@Mock
private World world;
private UUID uuid = UUID.randomUUID();
@Mock
private IslandWorldManager iwm;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private BentoBox plugin;
@Mock
private PlayerInventory inv;
@Mock
private Notifier notifier;
private EquipmentSlot hand;
@ -104,8 +55,6 @@ public class BlockInteractionListenerTest {
private ItemStack item;
@Mock
private Block clickedBlock;
@Mock
private FlagsManager fm;
private Map<Material, Flag> inHandItems = new EnumMap<>(Material.class);
@ -169,69 +118,21 @@ public class BlockInteractionListenerTest {
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
super.setUp();
// Clicked block
when(clickedBlock.getLocation()).thenReturn(location);
when(clickedBlock.getType()).thenReturn(Material.ITEM_FRAME);
// Player
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
when(player.getWorld()).thenReturn(world);
User.setPlugin(plugin);
User.getInstance(player);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
@Nullable
WorldSettings worldSet = new TestWorldSettings();
when(iwm.getWorldSettings(any())).thenReturn(worldSet);
// Island Manager
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optionalIsland = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optionalIsland);
// Island - nothing is allowed by default
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
// Inventory
hand = EquipmentSlot.HAND;
// Nothing in hand right now
when(item.getType()).thenReturn(Material.AIR);
when(player.getInventory()).thenReturn(inv);
// Enable reporting from Flags class
MetadataValue mdv = new FixedMetadataValue(plugin, "_why_debug");
when(player.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv));
// Locales & Placeholders
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// FlagsManager
setFlags();
@ -239,15 +140,6 @@ public class BlockInteractionListenerTest {
bil = new BlockInteractionListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -12,19 +11,9 @@ 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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.ArmorStand;
@ -35,7 +24,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Vehicle;
@ -48,34 +36,16 @@ import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
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.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -84,130 +54,26 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class BreakBlocksListenerTest {
@Mock
private Location location;
@Mock
private BentoBox plugin;
@Mock
private Notifier notifier;
public class BreakBlocksListenerTest extends AbstractCommonSetup {
private BreakBlocksListener bbl;
@Mock
private Player player;
@Mock
private World world;
@Mock
private Island island;
@Mock
private IslandWorldManager iwm;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Island World Manager
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
super.setUp();
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Player
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Listener
bbl = new BreakBlocksListener();
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.BreakBlocksListener#onBlockBreak(org.bukkit.event.block.BlockBreakEvent)}.
*/

View File

@ -3,63 +3,37 @@ package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.framework;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -68,63 +42,24 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {Bukkit.class, BentoBox.class, Flags.class, Util.class} )
public class BreedingListenerTest {
public class BreedingListenerTest extends AbstractCommonSetup {
private Location location;
private BentoBox plugin;
private Notifier notifier;
private Player player;
private PlayerInventory inventory;
private ItemStack itemInMainHand;
private ItemStack itemInOffHand;
private IslandWorldManager iwm;
private static final EntityType ENTITY_TYPE = EntityType.COW;
private static final Material BREEDABLE_WITH = Material.WHEAT;
private static final Material NOT_BREEDABLE_WITH = Material.SEAGRASS;
@Override
@Before
public void setUp() {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
public void setUp() throws Exception {
super.setUp();
SkullMeta skullMeta = mock(SkullMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Monsters and animals
Zombie zombie = mock(Zombie.class);
@ -134,70 +69,14 @@ public class BreedingListenerTest {
Cow cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// Player and player inventory. Start with nothing in hands
player = mock(Player.class);
inventory = mock(PlayerInventory.class);
itemInMainHand = mock(ItemStack.class);
when(itemInMainHand.getType()).thenReturn(Material.AIR);
itemInOffHand = mock(ItemStack.class);
when(itemInOffHand.getType()).thenReturn(Material.AIR);
when(inventory.getItemInMainHand()).thenReturn(itemInMainHand);
when(inventory.getItemInOffHand()).thenReturn(itemInOffHand);
when(player.getInventory()).thenReturn(inventory);
User.setPlugin(plugin);
when(inv.getItemInMainHand()).thenReturn(itemInMainHand);
when(inv.getItemInOffHand()).thenReturn(itemInOffHand);
when(player.getInventory()).thenReturn(inv);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(any())).thenReturn("tastybento");
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
}
@After
public void tearDown() {
User.clearUsers();
framework().clearInlineMocks();
}
/**

View File

@ -3,62 +3,31 @@ package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.entity.TropicalFish;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
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.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -67,130 +36,27 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class BucketListenerTest {
public class BucketListenerTest extends AbstractCommonSetup {
@Mock
private Location location;
@Mock
private BentoBox plugin;
@Mock
private Notifier notifier;
private BucketListener l;
@Mock
private Player player;
@Mock
private World world;
@Mock
private Island island;
@Mock
private IslandWorldManager iwm;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
super.setUp();
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
// Default is that everything is allowed
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
// Player
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
when(island.isAllowed(any(), any())).thenReturn(true);
// Listener
l = new BucketListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.BucketListener#onBucketEmpty(org.bukkit.event.player.PlayerBucketEmptyEvent)}.
*/
@ -283,7 +149,7 @@ public class BucketListenerTest {
verify(notifier, times(4)).notify(any(), eq("protection.protected"));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.BucketListener#onBucketFill(org.bukkit.event.player.PlayerBucketFillEvent)}.
*/

View File

@ -9,49 +9,19 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Egg;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerEggThrowEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
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.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -60,131 +30,24 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class EggListenerTest {
@Mock
private Location location;
@Mock
private BentoBox plugin;
@Mock
private Notifier notifier;
public class EggListenerTest extends AbstractCommonSetup {
private EggListener el;
@Mock
private Player player;
@Mock
private World world;
@Mock
private Island island;
@Mock
private IslandWorldManager iwm;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Player
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Listener
el = new EggListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EggListener#onEggThrow(org.bukkit.event.player.PlayerEggThrowEvent)}.
*/

View File

@ -5,146 +5,51 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
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.stubbing.Answer;
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.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class ElytraListenerTest {
@Mock
private BentoBox plugin;
@Mock
private Player player;
@Mock
private Location location;
@Mock
private World world;
private UUID uuid = UUID.randomUUID();
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
public class ElytraListenerTest extends AbstractCommonSetup {
private ElytraListener el;
@Mock
private IslandWorldManager iwm;
@Mock
private IslandsManager im;
private Island island;
@Mock
private PluginManager pim;
@Mock
private ItemFactory itemFactory;
@Mock
private Notifier notifier;
@Mock
private PlayerInventory inv;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
super.setUp();
// Player
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
when(player.getWorld()).thenReturn(world);
when(player.isGliding()).thenReturn(true);
User.setPlugin(plugin);
User.getInstance(player);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Island manager
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Class under test
el = new ElytraListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.ElytraListener#onGlide(org.bukkit.event.entity.EntityToggleGlideEvent)}.
*/
@ -155,7 +60,7 @@ public class ElytraListenerTest {
assertFalse(e.isCancelled());
verify(notifier, never()).notify(any(), anyString());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.ElytraListener#onGlide(org.bukkit.event.entity.EntityToggleGlideEvent)}.
*/
@ -178,7 +83,7 @@ public class ElytraListenerTest {
verify(notifier, never()).notify(any(), anyString());
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.ElytraListener#onGliding(org.bukkit.event.player.PlayerTeleportEvent)}.
*/

View File

@ -3,7 +3,6 @@ package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -11,20 +10,13 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
@ -32,148 +24,54 @@ import org.bukkit.entity.minecart.RideableMinecart;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector;
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.stubbing.Answer;
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.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class EntityInteractListenerTest {
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
public class EntityInteractListenerTest extends AbstractCommonSetup {
private EntityInteractListener eil;
@Mock
private Player player;
@Mock
private Entity clickedEntity;
private Vector position;
private EquipmentSlot hand;
@Mock
private PluginManager pim;
@Mock
private ItemFactory itemFactory;
@Mock
private Location location;
@Mock
private World world;
private UUID uuid = UUID.randomUUID();
@Mock
private IslandWorldManager iwm;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private BentoBox plugin;
@Mock
private PlayerInventory inv;
@Mock
private Notifier notifier;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
// Location
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
super.setUp();
// Clicked block
when(clickedEntity.getLocation()).thenReturn(location);
// Player
when(player.getUniqueId()).thenReturn(uuid);
when(player.getLocation()).thenReturn(location);
when(player.getWorld()).thenReturn(world);
User.setPlugin(plugin);
User.getInstance(player);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
// Island Manager
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optionalIsland = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optionalIsland);
// Island - nothing is allowed by default
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
// Hand - main hand
hand = EquipmentSlot.HAND;
position = new Vector(10,10,10);
when(player.getInventory()).thenReturn(inv);
when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.NAME_TAG));
// Enable reporting from Flags class
MetadataValue mdv = new FixedMetadataValue(plugin, "_why_debug");
when(player.getMetadata(anyString())).thenReturn(Collections.singletonList(mdv));
// Locales & Placeholders
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(phm.replacePlaceholders(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Class under test
eil = new EntityInteractListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener#onPlayerInteractAtEntity(org.bukkit.event.player.PlayerInteractAtEntityEvent)}.
*/

View File

@ -1,54 +1,32 @@
/**
*
*/
package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -58,104 +36,29 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {Bukkit.class, BentoBox.class, Flags.class, Util.class} )
public class ExperiencePickupListenerTest {
public class ExperiencePickupListenerTest extends AbstractCommonSetup {
private EntityTargetLivingEntityEvent e;
private ExperiencePickupListener epl;
private World world;
private IslandWorldManager iwm;
private Notifier notifier;
private LivingEntity targetEntity;
private Island island;
private Entity entity;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// World
world = mock(World.class);
//FlagsManager flagsManager = new FlagsManager(plugin);
//when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// Location
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
super.setUp();
when(island.isAllowed(any(), any())).thenReturn(true);
// Set up as valid
entity = mock(ExperienceOrb.class);
targetEntity = mock(Player.class);
when(targetEntity.getLocation()).thenReturn(location);
when(entity.getLocation()).thenReturn(location);
TargetReason reason = TargetReason.CLOSEST_PLAYER;
e = new EntityTargetLivingEntityEvent(entity, targetEntity, reason);
e = new EntityTargetLivingEntityEvent(entity, player, reason);
epl = new ExperiencePickupListener();
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
@ -164,10 +67,10 @@ public class ExperiencePickupListenerTest {
@Test
public void testOnExperienceOrbTargetPlayerNotAllowed() {
// Not allowed
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
epl.onExperienceOrbTargetPlayer(e);
assertNull(e.getTarget());
Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq("protection.protected"));
verify(notifier).notify(any(), eq("protection.protected"));
}
/**
@ -177,7 +80,7 @@ public class ExperiencePickupListenerTest {
public void testOnExperienceOrbTargetPlayerAllowed() {
epl.onExperienceOrbTargetPlayer(e);
assertNotNull(e.getTarget());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
verify(notifier, never()).notify(any(), anyString());
}
/**
@ -185,10 +88,11 @@ public class ExperiencePickupListenerTest {
*/
@Test
public void testOnExperienceOrbTargetNotPlayer() {
targetEntity = mock(Zombie.class);
LivingEntity zombie = mock(Zombie.class);
e = new EntityTargetLivingEntityEvent(entity, zombie, TargetReason.CLOSEST_ENTITY);
epl.onExperienceOrbTargetPlayer(e);
assertNotNull(e.getTarget());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
verify(notifier, never()).notify(any(), anyString());
}
/**
@ -199,7 +103,7 @@ public class ExperiencePickupListenerTest {
entity = mock(ArmorStand.class);
epl.onExperienceOrbTargetPlayer(e);
assertNotNull(e.getTarget());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
verify(notifier, never()).notify(any(), anyString());
}
}

View File

@ -3,64 +3,37 @@ package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Animals;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Villager;
import org.bukkit.entity.WanderingTrader;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerFishEvent.State;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -69,23 +42,11 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class HurtingListenerTest {
public class HurtingListenerTest extends AbstractCommonSetup {
@Mock
private Enderman enderman;
@Mock
private LocalesManager lm;
@Mock
private Notifier notifier;
@Mock
private Island island;
@Mock
private Player player;
@Mock
private Location location;
@Mock
private World world;
@Mock
private FishHook hookEntity;
private User user;
@ -93,44 +54,10 @@ public class HurtingListenerTest {
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
SkullMeta skullMeta = mock(SkullMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
super.setUp();
// Monsters and animals
when(enderman.getLocation()).thenReturn(location);
@ -139,60 +66,14 @@ public class HurtingListenerTest {
Slime slime = mock(Slime.class);
when(slime.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Locales
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> invocation.getArgument(1, String.class);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Utils
when(Util.isPassiveEntity(any())).thenCallRealMethod();
when(Util.isHostileEntity(any())).thenCallRealMethod();
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// User & player
when(player.getLocation()).thenReturn(location);
user = User.getInstance(player);
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link HurtingListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/

View File

@ -2,24 +2,13 @@ package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Chest;
import org.bukkit.block.Dispenser;
@ -39,32 +28,16 @@ import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -73,135 +46,29 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class InventoryListenerTest {
public class InventoryListenerTest extends AbstractCommonSetup {
private final static List<Class<?>> HOLDERS = Arrays.asList(Horse.class, Chest.class,ShulkerBox.class, StorageMinecart.class,
Dispenser.class,
Dropper.class, Hopper.class, Furnace.class, BrewingStand.class,
Villager.class, WanderingTrader.class);
private Location location;
private BentoBox plugin;
private Notifier notifier;
private InventoryListener l;
private Player player;
private World world;
private Island island;
private IslandWorldManager iwm;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
// Player
player = mock(Player.class);
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Listener
l = new InventoryListener();
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.protection.InventoryListener#onInventoryClick(org.bukkit.event.inventory.InventoryClickEvent)}.
*/

View File

@ -11,25 +11,15 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
@ -37,35 +27,17 @@ import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.projectiles.ProjectileSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -74,56 +46,15 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class PhysicalInteractionListenerTest {
public class PhysicalInteractionListenerTest extends AbstractCommonSetup {
private Location location;
private BentoBox plugin;
private Notifier notifier;
private Player player;
private ItemStack item;
private Block clickedBlock;
@Override
@Before
public void setUp() {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
SkullMeta skullMeta = mock(SkullMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
public void setUp() throws Exception {
super.setUp();
// Monsters and animals
Zombie zombie = mock(Zombie.class);
@ -133,80 +64,15 @@ public class PhysicalInteractionListenerTest {
Cow cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// Users
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(any())).thenReturn("tastybento");
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Player setup
player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
User.getInstance(player);
// Item and clicked block
item = mock(ItemStack.class);
clickedBlock = mock(Block.class);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Tags
when(Tag.PRESSURE_PLATES.isTagged(any(Material.class))).thenReturn(true);
when(Tag.WOODEN_BUTTONS.isTagged(any(Material.class))).thenReturn(true);
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -12,20 +11,9 @@ 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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
@ -33,7 +21,6 @@ import org.bukkit.entity.Creeper;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
@ -41,33 +28,16 @@ import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
@ -76,125 +46,22 @@ import world.bentobox.bentobox.util.Util;
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class, Tag.class} )
public class PlaceBlocksListenerTest {
private Location location;
private BentoBox plugin;
private Notifier notifier;
public class PlaceBlocksListenerTest extends AbstractCommonSetup {
private PlaceBlocksListener pbl;
private Player player;
private World world;
private Island island;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(any(), any())).thenReturn(true);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Player
player = mock(Player.class);
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Listener
pbl = new PlaceBlocksListener();
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -12,24 +11,18 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.WitherSkeleton;
import org.bukkit.entity.Zombie;
@ -41,80 +34,35 @@ import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
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.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Util.class, Bukkit.class} )
public class TNTListenerTest {
public class TNTListenerTest extends AbstractCommonSetup {
@Mock
private Location location;
@Mock
private BentoBox plugin;
@Mock
private Notifier notifier;
@Mock
private Block block;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private Player player;
@Mock
private IslandWorldManager iwm;
@Mock
private World world;
@Mock
private Entity entity;
private Map<String, Boolean> worldFlags;
// Class under test
private TNTListener listener;
@Override
@Before
public void setUp() {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
public void setUp() throws Exception {
super.setUp();
// Monsters and animals
Zombie zombie = mock(Zombie.class);
@ -124,54 +72,6 @@ public class TNTListenerTest {
Cow cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
// Users
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(any())).thenReturn("tastybento");
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(any())).thenReturn(optional);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
// Block
when(block.getLocation()).thenReturn(location);
when(block.getWorld()).thenReturn(world);
@ -181,24 +81,11 @@ public class TNTListenerTest {
when(entity.getWorld()).thenReturn(world);
when(entity.getLocation()).thenReturn(location);
// Player
when(player.getLocation()).thenReturn(location);
// In world
when(iwm.inWorld(any(Location.class))).thenReturn(true);
listener = new TNTListener();
listener.setPlugin(plugin);
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
@Test
public void testOnTNTPriming() {
BlockFace clickedFace = BlockFace.DOWN;
@ -207,7 +94,6 @@ public class TNTListenerTest {
when(clickedBlock.getLocation()).thenReturn(location);
ItemStack item = new ItemStack(Material.FLINT_AND_STEEL);
Action action = Action.RIGHT_CLICK_BLOCK;
Player player = mock(Player.class);
PlayerInteractEvent e = new PlayerInteractEvent(player , action, item, clickedBlock, clickedFace);
listener.onTNTPriming(e);

View File

@ -1,184 +1,49 @@
/**
*
*/
package world.bentobox.bentobox.listeners.flags.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.entity.ThrownPotion;
import org.bukkit.entity.Witch;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class ThrowingListenerTest {
private Location location;
private BentoBox plugin;
private Notifier notifier;
public class ThrowingListenerTest extends AbstractCommonSetup {
private ThrowingListener tl;
private Player player;
private World world;
private Island island;
/**
* @throws java.lang.Exception
*/
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Server server = mock(Server.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pim = mock(PluginManager.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getPluginManager()).thenReturn(pim);
ItemMeta meta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(meta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
User.setPlugin(plugin);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> (String)Arrays.asList(invocation.getArguments()).get(1);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
super.setUp();
// Default is that everything is allowed
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
// Thrown listener
tl = new ThrowingListener();
// Player
player = mock(Player.class);
when(player.getLocation()).thenReturn(location);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(player.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
/**

View File

@ -10,18 +10,12 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.ClickType;
@ -33,138 +27,46 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
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.stubbing.Answer;
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.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Util.class, Bukkit.class })
public class EnderChestListenerTest {
public class EnderChestListenerTest extends AbstractCommonSetup {
@Mock
private World world;
@Mock
private Player player;
@Mock
private IslandWorldManager iwm;
@Mock
private Notifier notifier;
@Mock
private ItemStack item;
@Mock
private Block clickedBlock;
private Action action;
@Override
@Before
public void setUp() throws Exception {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Owner
UUID uuid1 = UUID.randomUUID();
// Island initialization
Island island = mock(Island.class);
when(island.getOwner()).thenReturn(uuid1);
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
Location inside = mock(Location.class);
when(inside.getWorld()).thenReturn(world);
Optional<Island> opIsland = Optional.ofNullable(island);
when(im.getProtectedIslandAt(eq(inside))).thenReturn(opIsland);
// On island
when(im.locationIsOnIsland(any(), any())).thenReturn(true);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world);
// World Settings
when(plugin.getIWM()).thenReturn(iwm);
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// By default everything is in world
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getAddon(any())).thenReturn(Optional.empty());
super.setUp();
// Ender chest use is not allowed by default
Flags.ENDER_CHEST.setSetting(world, false);
// Sometimes use Mockito.withSettings().verboseLogging()
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
when(player.isOp()).thenReturn(false);
// No special perms
when(player.hasPermission(anyString())).thenReturn(false);
when(player.getWorld()).thenReturn(world);
User.setPlugin(plugin);
User.getInstance(player);
// Locales - this returns the string that was requested for translation
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = invocation -> invocation.getArgument(1, String.class);
when(lm.get(any(), any())).thenAnswer(answer);
// Placeholders
PlaceholdersManager placeholdersManager = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(placeholdersManager);
when(placeholdersManager.replacePlaceholders(any(), any())).thenAnswer(answer);
// Notifier
when(plugin.getNotifier()).thenReturn(notifier);
// Action, Item and clicked block
action = Action.RIGHT_CLICK_BLOCK;
when(item.getType()).thenReturn(Material.ENDER_CHEST);
when(clickedBlock.getLocation()).thenReturn(inside);
when(clickedBlock.getLocation()).thenReturn(location);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
// Addon
when(iwm.getAddon(any())).thenReturn(Optional.empty());
Settings settings = mock(Settings.class);
// Fake players
when(plugin.getSettings()).thenReturn(settings);
// Util strip spaces
when(Util.stripSpaceAfterColorCodes(anyString())).thenCallRealMethod();
}
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
@Test