diff --git a/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java index 6164ff1b9..857670da0 100644 --- a/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java +++ b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java @@ -129,6 +129,7 @@ public void setUp() throws Exception { when(location.getBlockY()).thenReturn(0); when(location.getBlockZ()).thenReturn(0); when(location.toVector()).thenReturn(new Vector(0,0,0)); + when(location.clone()).thenReturn(location); // Paper // Players Manager and meta data PlayersManager pm = mock(PlayersManager.class); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java index e8fa95515..5a7e21a83 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java @@ -55,7 +55,6 @@ * @author tastybento * */ -@Ignore("Needs update to work with PaperAPI") @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class , ServerBuildInfo.class}) public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { @@ -227,6 +226,7 @@ public void testCanExecuteNoIsland() { * Test method for * {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}. */ + @Ignore("PaperAPI Material issue with Material.get") @Test public void testCanExecuteNoTarget() { assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList())); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java index 8b8199d8f..9df46bade 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListenerTest.java @@ -46,7 +46,6 @@ * @author tastybento * */ -@Ignore("Paper API update required") @RunWith(PowerMockRunner.class) @PrepareForTest({Bukkit.class, BentoBox.class, Util.class, ServerBuildInfo.class}) public class BlockInteractionListenerTest extends AbstractCommonSetup { @@ -130,8 +129,6 @@ private void setFlags() { } - /** - */ @Override @Before public void setUp() throws Exception { @@ -147,7 +144,9 @@ public void setUp() throws Exception { when(item.getType()).thenReturn(Material.AIR); when(mockPlayer.getInventory()).thenReturn(inv); when(inv.getItemInMainHand()).thenReturn(item); - when(inv.getItemInOffHand()).thenReturn(new ItemStack(Material.BUCKET)); + ItemStack mockBucket = mock(ItemStack.class); + when(mockBucket.getType()).thenReturn(Material.BUCKET); + when(inv.getItemInOffHand()).thenReturn(mockBucket); // FlagsManager setFlags(); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java index 5fc6e3d5e..bfb344324 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/EntityInteractListenerTest.java @@ -27,7 +27,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -45,7 +44,6 @@ * @author tastybento * */ -@Ignore("Needs redo for PaperAPI") @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, Util.class , ServerBuildInfo.class}) public class EntityInteractListenerTest extends AbstractCommonSetup { @@ -69,7 +67,9 @@ public void setUp() throws Exception { // Hand - main hand hand = EquipmentSlot.HAND; position = new Vector(10, 10, 10); - when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.NAME_TAG)); + ItemStack mockNameTag = mock(ItemStack.class); + when(mockNameTag.getType()).thenReturn(Material.NAME_TAG); + when(inv.getItemInMainHand()).thenReturn(mockNameTag); // Initialize the Flags class. This is a workaround to prevent weird errors when mocking // I think it's because the flag class needs to be initialized before use in argument matchers @@ -253,7 +253,9 @@ public void testOnPlayerInteractEntityWanderingTraderNoInteraction() { clickedEntity = mock(WanderingTrader.class); when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getType()).thenReturn(EntityType.WANDERING_TRADER); - when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.STONE)); + ItemStack mockStone = mock(ItemStack.class); + when(mockStone.getType()).thenReturn(Material.STONE); + when(inv.getItemInMainHand()).thenReturn(mockStone); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(mockPlayer, clickedEntity, hand); eil.onPlayerInteractEntity(e); verify(notifier, never()).notify(any(), eq("protection.protected")); @@ -319,7 +321,9 @@ public void testOnPlayerInteractEntitySheepAllowed() { clickedEntity = mock(Sheep.class); when(clickedEntity.getLocation()).thenReturn(location); when(clickedEntity.getType()).thenReturn(EntityType.SHEEP); - when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.AIR)); + ItemStack mockAir = mock(ItemStack.class); + when(mockAir.getType()).thenReturn(Material.AIR); + when(inv.getItemInMainHand()).thenReturn(mockAir); PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(mockPlayer, clickedEntity, hand); eil.onPlayerInteractEntity(e); verify(notifier, never()).notify(any(), eq("protection.protected")); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java index 0c78cf18d..533bb466b 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java @@ -30,7 +30,6 @@ import org.bukkit.scheduler.BukkitScheduler; 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; @@ -56,7 +55,6 @@ import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; -@Ignore("Needs PaperAPI update") @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class , ServerBuildInfo.class}) public class LockAndBanListenerTest { @@ -157,6 +155,7 @@ public void setUp() throws Exception { when(loc.getBlockY()).thenReturn(Y); when(loc.getBlockZ()).thenReturn(Z); when(island.getCenter()).thenReturn(loc); + when(loc.clone()).thenReturn(loc); when(island.getProtectionRange()).thenReturn(PROTECTION_RANGE); // Island is not locked by default when(island.isAllowed(any(), any())).thenReturn(true); @@ -171,16 +170,19 @@ public void setUp() throws Exception { when(outside.getBlockX()).thenReturn(X + PROTECTION_RANGE + 1); when(outside.getBlockY()).thenReturn(Y); when(outside.getBlockZ()).thenReturn(Z); + when(outside.clone()).thenReturn(outside); when(inside.getWorld()).thenReturn(world); when(inside.getBlockX()).thenReturn(X + PROTECTION_RANGE - 1); when(inside.getBlockY()).thenReturn(Y); when(inside.getBlockZ()).thenReturn(Z); + when(inside.clone()).thenReturn(inside); - when(inside.getWorld()).thenReturn(world); - when(inside.getBlockX()).thenReturn(X + PROTECTION_RANGE - 2); - when(inside.getBlockY()).thenReturn(Y); - when(inside.getBlockZ()).thenReturn(Z); + when(inside2.getWorld()).thenReturn(world); + when(inside2.getBlockX()).thenReturn(X + PROTECTION_RANGE - 2); + when(inside2.getBlockY()).thenReturn(Y); + when(inside2.getBlockZ()).thenReturn(Z); + when(inside2.clone()).thenReturn(inside2); Optional opIsland = Optional.ofNullable(island); when(im.getProtectedIslandAt(eq(inside))).thenReturn(opIsland); @@ -231,6 +233,7 @@ public void testTeleportToBannedIsland() { assertTrue(e.isCancelled()); } + @SuppressWarnings("deprecation") @Test public void testLoginToBannedIsland() { // Make player @@ -285,11 +288,13 @@ public void testVerticalVehicleMoveOnly() { when(from.getBlockX()).thenReturn(X); when(from.getBlockY()).thenReturn(50); when(from.getBlockZ()).thenReturn(Z); + when(from.clone()).thenReturn(from); Location to = mock(Location.class); when(to.getWorld()).thenReturn(world); when(to.getBlockX()).thenReturn(X); when(to.getBlockY()).thenReturn(55); when(to.getBlockZ()).thenReturn(Z); + when(to.clone()).thenReturn(to); // Create vehicle and put two players in it. Vehicle vehicle = mock(Vehicle.class); Player player2 = mock(Player.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java index af113d6eb..473583b93 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java @@ -12,7 +12,9 @@ import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import org.bukkit.Bukkit; @@ -21,6 +23,8 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.damage.DamageSource; +import org.bukkit.damage.DamageType; import org.bukkit.entity.Arrow; import org.bukkit.entity.Cow; import org.bukkit.entity.Entity; @@ -33,10 +37,12 @@ import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; +import org.bukkit.event.entity.EntityDamageEvent.DamageModifier; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; +import org.jetbrains.annotations.NotNull; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -46,6 +52,8 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import com.google.common.base.Function; + import io.papermc.paper.ServerBuildInfo; import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; @@ -54,7 +62,6 @@ import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.util.Util; -@Ignore("PaperAPI changes required to fix errors and failures") @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, Util.class, Bukkit.class, ServerBuildInfo.class }) public class TNTListenerTest extends AbstractCommonSetup { @@ -112,6 +119,7 @@ public void setUp() throws Exception { } @Test + @Ignore("PaperAPI error with Material isn't an item issue") public void testOnTNTPriming() { BlockFace clickedFace = BlockFace.DOWN; Block clickedBlock = mock(Block.class); @@ -324,8 +332,29 @@ public void testOnTNTDamageInWorldTNTProjectilePlayerFireArrowNotIslandNotAllowe @Test public void testOnEntityExplosion() { - EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION, null, - 20D); + /* + * org.bukkit.event.entity.EntityDamageByEntityEvent.EntityDamageByEntityEvent( + * @NotNull @NotNull Entity damager, + * @NotNull @NotNull Entity damagee, + * @NotNull @NotNull DamageCause cause, + * @NotNull @NotNull DamageSource damageSource, + * @NotNull @NotNull Map modifiers, + * @NotNull @NotNull Map modifierFunctions, + * boolean critical) + + Attempt to use newer event. This works but then other errors appear. Go figure. + + @NotNull + Map modifiers = new HashMap<>(); + modifiers.put(DamageModifier.BASE, 0.0D); + @NotNull + Map> modifier = new HashMap<>(); + modifier.put(DamageModifier.BASE, null); + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION, + DamageSource.builder(DamageType.EXPLOSION).build(), modifiers, modifier, false); + */ + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION, + null, 20D); listener.onExplosion(e); assertTrue(e.isCancelled()); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java index 6f5823161..66283680f 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java @@ -34,7 +34,6 @@ 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.Mockito; @@ -68,7 +67,6 @@ public class ChestDamageListenerTest extends AbstractCommonSetup { - private Location location; private BentoBox plugin; private World world; @@ -104,11 +102,7 @@ public void setUp() throws Exception { 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); @@ -180,28 +174,37 @@ public void tearDown() throws Exception { * Test method for {@link ChestDamageListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. */ @Test - @Ignore("Fixes required for failures PaperAPI") public void testOnExplosionChestDamageNotAllowed() { + // Srt the flag to not allow chest damage Flags.CHEST_DAMAGE.setSetting(world, false); + // Set the entity that is causing the damage (TNT) Entity entity = mock(Entity.class); when(entity.getType()).thenReturn(EntityType.TNT); + + // Create a list of blocks that will potentially be damaged by TNT List list = new ArrayList<>(); Block chest = mock(Block.class); - when(chest.getType()).thenReturn(Material.CHEST); + when(chest.getType()).thenReturn(Material.CHEST); // Regular chest when(chest.getLocation()).thenReturn(location); + Block trappedChest = mock(Block.class); - when(trappedChest.getType()).thenReturn(Material.TRAPPED_CHEST); + when(trappedChest.getType()).thenReturn(Material.TRAPPED_CHEST);// Trapped chest when(trappedChest.getLocation()).thenReturn(location); + Block stone = mock(Block.class); - when(stone.getType()).thenReturn(Material.STONE); + when(stone.getType()).thenReturn(Material.STONE); // Stone when(stone.getLocation()).thenReturn(location); list.add(chest); list.add(trappedChest); list.add(stone); + // Create the event EntityExplodeEvent e = getExplodeEvent(entity, location, list); + // Listener to test ChestDamageListener listener = new ChestDamageListener(); listener.setPlugin(plugin); listener.onExplosion(e); + + // Verify assertFalse(e.isCancelled()); assertEquals(1, e.blockList().size()); assertFalse(e.blockList().contains(chest)); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java index 500d57f54..9cf8a2dea 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java @@ -24,7 +24,6 @@ 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; @@ -52,8 +51,6 @@ public class WitherListenerTest extends AbstractCommonSetup { private WitherListener wl; @Mock - private Location location; - @Mock private Location location2; @Mock private World world; @@ -82,15 +79,11 @@ public void setUp() throws Exception { when(ws.getWorldFlags()).thenReturn(map); when(iwm.getWorldSettings(any())).thenReturn(ws); - when(location.getWorld()).thenReturn(world); - when(location.getBlockX()).thenReturn(0); - when(location.getBlockY()).thenReturn(0); - when(location.getBlockZ()).thenReturn(0); - when(location2.getWorld()).thenReturn(world2); when(location2.getBlockX()).thenReturn(0); when(location2.getBlockY()).thenReturn(0); when(location2.getBlockZ()).thenReturn(0); + when(location2.clone()).thenReturn(location2); // Paper blocks = new ArrayList<>(); for (int i = 0; i < 4; i++) { @@ -119,12 +112,12 @@ public void tearDown() { * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. */ @Test - @Ignore("Fixes required for failures PaperAPI") public void testOnExplosionWither() { Entity entity = mock(Entity.class); when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.WITHER); + when(location.clone()).thenReturn(location); EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertTrue(blocks.isEmpty()); @@ -165,12 +158,12 @@ public void testOnExplosionWitherAllowed() { * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. */ @Test - @Ignore("Fixes required for failures PaperAPI") public void testOnExplosionWitherSkull() { Entity entity = mock(Entity.class); when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.WITHER_SKULL); + when(location.clone()).thenReturn(location); EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertTrue(blocks.isEmpty());