bentobox/src/test/java/world/bentobox/bentobox/listeners/flags/protection/FireListenerTest.java

359 lines
13 KiB
Java
Raw Normal View History

package world.bentobox.bentobox.listeners.flags.protection;
2018-02-20 23:06:29 +01:00
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
2018-02-20 23:06:29 +01:00
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
2018-02-20 23:06:29 +01:00
import java.util.HashSet;
import java.util.Map;
2018-02-20 23:06:29 +01:00
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.block.Block;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
2018-02-20 23:06:29 +01:00
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
2018-02-20 23:06:29 +01:00
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
2018-02-20 23:06:29 +01:00
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
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.PlayersManager;
import world.bentobox.bentobox.util.Util;
2018-02-20 23:06:29 +01:00
@RunWith(PowerMockRunner.class)
2019-03-10 04:30:49 +01:00
@PrepareForTest( {BentoBox.class, Bukkit.class, Flags.class, Util.class} )
2018-02-20 23:06:29 +01:00
public class FireListenerTest {
2019-03-10 04:30:49 +01:00
private Location location;
private BentoBox plugin;
@Mock
private World world;
private final Map<String, Boolean> worldFlags = new HashMap<>();
2018-02-20 23:06:29 +01:00
2019-03-10 04:30:49 +01:00
@Before
public void setUp() {
worldFlags.clear();
2019-03-10 04:30:49 +01:00
PowerMockito.mockStatic(Bukkit.class);
// Set up plugin
2018-07-29 22:21:46 +02:00
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
2018-02-20 23:06:29 +01:00
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);
when(Bukkit.getPluginManager()).thenReturn(pim);
2018-02-20 23:06:29 +01:00
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
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);
2018-02-20 23:06:29 +01:00
2018-06-01 03:52:05 +02:00
FlagsManager flagsManager = new FlagsManager(plugin);
2018-02-20 23:06:29 +01:00
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
2018-08-05 06:50:10 +02:00
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);
2018-02-20 23:06:29 +01:00
// Monsters and animals
2018-06-01 03:52:05 +02:00
Zombie zombie = mock(Zombie.class);
2018-02-20 23:06:29 +01:00
when(zombie.getLocation()).thenReturn(location);
2018-06-01 03:52:05 +02:00
Slime slime = mock(Slime.class);
2018-02-20 23:06:29 +01:00
when(slime.getLocation()).thenReturn(location);
2018-06-01 03:52:05 +02:00
Cow cow = mock(Cow.class);
2018-02-20 23:06:29 +01:00
when(cow.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
2018-08-05 06:50:10 +02:00
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<>());
2018-02-20 23:06:29 +01:00
// Users
User.setPlugin(plugin);
2018-02-20 23:06:29 +01:00
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation");
2018-05-26 02:58:07 +02:00
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(any())).thenReturn("tastybento");
2018-05-26 02:58:07 +02:00
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(any())).thenReturn(ws);
when(ws.getWorldFlags()).thenReturn(worldFlags);
GameModeAddon gma = mock(GameModeAddon.class);
Optional<GameModeAddon> opGma = Optional.of(gma );
when(iwm.getAddon(any())).thenReturn(opGma);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(mock(World.class));
}
2018-02-20 23:06:29 +01:00
@After
public void tearDown() {
User.clearUsers();
Mockito.framework().clearInlineMocks();
}
2018-02-20 23:06:29 +01:00
@Test
public void testCheckFire() {
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(any())).thenReturn(Optional.of(island));
2018-02-20 23:06:29 +01:00
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
BlockBurnEvent e = new BlockBurnEvent(block, block);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
2018-02-20 23:06:29 +01:00
// Disallow fire
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
Flags.FLINT_AND_STEEL.setDefaultSetting(false);
assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
Flags.FLINT_AND_STEEL.setDefaultSetting(true);
assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
2018-02-20 23:06:29 +01:00
// Allow fire
when(island.isAllowed(any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);
Flags.FLINT_AND_STEEL.setDefaultSetting(false);
assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
Flags.FLINT_AND_STEEL.setDefaultSetting(true);
assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
2018-02-20 23:06:29 +01:00
// Check with no island
when(im.getIslandAt(any())).thenReturn(Optional.empty());
2018-02-20 23:06:29 +01:00
// Fire is not allowed, so should be cancelled
Flags.FLINT_AND_STEEL.setDefaultSetting(false);
assertTrue(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
2018-02-20 23:06:29 +01:00
// Fire allowed
Flags.FLINT_AND_STEEL.setDefaultSetting(world, true);
assertFalse(listener.checkFire(e, location, Flags.FLINT_AND_STEEL));
2018-02-20 23:06:29 +01:00
}
@Test
public void testOnBlockBurn() {
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(any())).thenReturn(Optional.of(island));
2018-02-20 23:06:29 +01:00
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
BlockBurnEvent e = new BlockBurnEvent(block, block);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
2018-02-20 23:06:29 +01:00
// Disallow fire
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
Flags.FIRE_BURNING.setDefaultSetting(false);
listener.onBlockBurn(e);
assertTrue(e.isCancelled());
Flags.FIRE_BURNING.setDefaultSetting(true);
listener.onBlockBurn(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Allow fire
when(island.isAllowed(any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);
Flags.FIRE_BURNING.setDefaultSetting(false);
listener.onBlockBurn(e);
assertFalse(e.isCancelled());
Flags.FIRE_BURNING.setDefaultSetting(true);
listener.onBlockBurn(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Check with no island
e = new BlockBurnEvent(block, block);
when(im.getIslandAt(any())).thenReturn(Optional.empty());
2018-02-20 23:06:29 +01:00
// Fire is not allowed, so should be cancelled
Flags.FIRE_BURNING.setDefaultSetting(false);
listener.onBlockBurn(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Fire allowed
Flags.FIRE_BURNING.setDefaultSetting(true);
listener.onBlockBurn(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
}
@Test
public void testOnBlockSpread() {
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(any())).thenReturn(Optional.of(island));
2018-02-20 23:06:29 +01:00
// Block on fire spread
2018-02-20 23:06:29 +01:00
Block block = mock(Block.class);
Block fire = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(fire.getLocation()).thenReturn(location);
when(fire.getType()).thenReturn(Material.FIRE);
2018-02-20 23:06:29 +01:00
BlockSpreadEvent e = new BlockSpreadEvent(block, fire, null);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
2018-02-20 23:06:29 +01:00
// Disallow fire
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
Flags.FIRE_SPREAD.setDefaultSetting(false);
listener.onBlockSpread(e);
assertTrue(e.isCancelled());
Flags.FIRE_SPREAD.setDefaultSetting(true);
listener.onBlockSpread(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Allow fire spread
when(island.isAllowed(any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);
Flags.FIRE_SPREAD.setDefaultSetting(false);
listener.onBlockSpread(e);
assertFalse(e.isCancelled());
Flags.FIRE_SPREAD.setDefaultSetting(true);
listener.onBlockSpread(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Check with no island
when(im.getIslandAt(any())).thenReturn(Optional.empty());
2018-02-20 23:06:29 +01:00
// Fire spread is not allowed, so should be cancelled
Flags.FIRE_SPREAD.setDefaultSetting(false);
listener.onBlockSpread(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Fire allowed
Flags.FIRE_SPREAD.setDefaultSetting(true);
listener.onBlockSpread(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
}
@Test
public void testOnBlockIgnite() {
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(any())).thenReturn(Optional.of(island));
2018-02-20 23:06:29 +01:00
// Block on fire spread
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getType()).thenReturn(Material.OBSIDIAN);
BlockIgniteEvent e = new BlockIgniteEvent(block, null, block);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
2018-02-20 23:06:29 +01:00
// Obsidian is okay to ignite
listener.onBlockIgnite(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Now set to something flammable
2018-08-01 11:21:29 +02:00
when(block.getType()).thenReturn(Material.OAK_PLANKS);
2018-02-20 23:06:29 +01:00
// Disallow fire
when(island.isAllowed(any())).thenReturn(false);
when(island.isAllowed(any(), any())).thenReturn(false);
Flags.FIRE_IGNITE.setDefaultSetting(false);
listener.onBlockIgnite(e);
assertTrue(e.isCancelled());
Flags.FIRE_IGNITE.setDefaultSetting(true);
listener.onBlockIgnite(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Allow fire spread
when(island.isAllowed(any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);
Flags.FIRE_IGNITE.setDefaultSetting(false);
listener.onBlockIgnite(e);
assertFalse(e.isCancelled());
Flags.FIRE_IGNITE.setDefaultSetting(true);
listener.onBlockIgnite(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Check with no island
when(im.getIslandAt(any())).thenReturn(Optional.empty());
2018-02-20 23:06:29 +01:00
// Fire spread is not allowed, so should be cancelled
Flags.FIRE_IGNITE.setDefaultSetting(false);
listener.onBlockIgnite(e);
assertTrue(e.isCancelled());
2018-02-20 23:06:29 +01:00
// Fire allowed
Flags.FIRE_IGNITE.setDefaultSetting(true);
listener.onBlockIgnite(e);
assertFalse(e.isCancelled());
2018-02-20 23:06:29 +01:00
}
}