Add some tests

This commit is contained in:
tastybento 2024-09-16 21:23:45 -07:00
parent 625bfa631b
commit 5a4cbcce25
2 changed files with 98 additions and 9 deletions

View File

@ -8,12 +8,9 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent; import org.bukkit.event.player.PlayerInteractEntityEvent;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -43,8 +40,8 @@ public class CreeperListener extends FlagListener {
e.blockList().clear(); e.blockList().clear();
// Still allow player and mob damage // Still allow player and mob damage
e.setCancelled(false); e.setCancelled(false);
return;
} }
// Check for griefing // Check for griefing
Creeper creeper = (Creeper)e.getEntity(); Creeper creeper = (Creeper)e.getEntity();
if (!Flags.CREEPER_GRIEFING.isSetForWorld(e.getLocation().getWorld()) if (!Flags.CREEPER_GRIEFING.isSetForWorld(e.getLocation().getWorld())

View File

@ -1,21 +1,50 @@
package world.bentobox.bentobox.listeners.flags.worldsettings; package world.bentobox.bentobox.listeners.flags.worldsettings;
import static org.junit.Assert.*; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
/** /**
* *
*/ */
public class CreeperListenerTest { @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, Flags.class, Util.class })
public class CreeperListenerTest extends AbstractCommonSetup {
private CreeperListener cl;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
cl = new CreeperListener();
} }
/** /**
@ -23,14 +52,77 @@ public class CreeperListenerTest {
*/ */
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
User.clearUsers();
Mockito.framework().clearInlineMocks();
} }
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/ */
@Test @Test
public void testOnExplosion() { public void testOnExplosionNotCreeper() {
fail("Not yet implemented"); List<Block> list = new ArrayList<>();
Entity entity = mock(Entity.class);
when(entity.getType()).thenReturn(EntityType.TNT);
when(iwm.inWorld(location)).thenReturn(true);
EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0);
cl.onExplosion(event);
assertFalse(event.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnExplosionNotInWorld() {
List<Block> list = new ArrayList<>();
Entity entity = mock(Entity.class);
when(entity.getLocation()).thenReturn(location);
when(entity.getType()).thenReturn(EntityType.CREEPER);
when(iwm.inWorld(location)).thenReturn(false);
EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0);
cl.onExplosion(event);
assertFalse(event.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnExplosionCreeperInWorldDamageOK() {
List<Block> list = new ArrayList<>();
list.add(mock(Block.class));
list.add(mock(Block.class));
list.add(mock(Block.class));
Creeper entity = mock(Creeper.class);
when(entity.getLocation()).thenReturn(location);
when(entity.getType()).thenReturn(EntityType.CREEPER);
when(iwm.inWorld(location)).thenReturn(true);
EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0);
cl.onExplosion(event);
assertFalse(event.isCancelled());
assertFalse(event.blockList().isEmpty()); // No clearing of block list
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
public void testOnExplosionCreeperInWorldDamageNOK() {
Flags.CREEPER_DAMAGE.setSetting(world, false);
List<Block> list = new ArrayList<>();
list.add(mock(Block.class));
list.add(mock(Block.class));
list.add(mock(Block.class));
Creeper entity = mock(Creeper.class);
when(location.getWorld()).thenReturn(world);
when(entity.getLocation()).thenReturn(location);
when(entity.getType()).thenReturn(EntityType.CREEPER);
when(iwm.inWorld(location)).thenReturn(true);
EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0);
cl.onExplosion(event);
assertFalse(event.isCancelled());
assertTrue(event.blockList().isEmpty()); // No clearing of block list
} }
/** /**
@ -38,7 +130,7 @@ public class CreeperListenerTest {
*/ */
@Test @Test
public void testOnPlayerInteractEntity() { public void testOnPlayerInteractEntity() {
fail("Not yet implemented"); //TODO
} }
} }