|
| 1 | +package world.bentobox.bentobox.listeners.flags.worldsettings; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.bukkit.Bukkit; |
| 12 | +import org.bukkit.block.Block; |
| 13 | +import org.bukkit.entity.Creeper; |
| 14 | +import org.bukkit.entity.Entity; |
| 15 | +import org.bukkit.entity.EntityType; |
| 16 | +import org.bukkit.event.entity.EntityExplodeEvent; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.mockito.Mockito; |
| 22 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 23 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 24 | + |
| 25 | +import world.bentobox.bentobox.BentoBox; |
| 26 | +import world.bentobox.bentobox.api.user.User; |
| 27 | +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; |
| 28 | +import world.bentobox.bentobox.lists.Flags; |
| 29 | +import world.bentobox.bentobox.util.Util; |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + */ |
| 34 | +@RunWith(PowerMockRunner.class) |
| 35 | +@PrepareForTest({ Bukkit.class, BentoBox.class, Flags.class, Util.class }) |
| 36 | +public class CreeperListenerTest extends AbstractCommonSetup { |
| 37 | + |
| 38 | + private CreeperListener cl; |
| 39 | + |
| 40 | + /** |
| 41 | + * @throws java.lang.Exception |
| 42 | + */ |
| 43 | + @Before |
| 44 | + public void setUp() throws Exception { |
| 45 | + super.setUp(); |
| 46 | + |
| 47 | + cl = new CreeperListener(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @throws java.lang.Exception |
| 52 | + */ |
| 53 | + @After |
| 54 | + public void tearDown() throws Exception { |
| 55 | + User.clearUsers(); |
| 56 | + Mockito.framework().clearInlineMocks(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. |
| 61 | + */ |
| 62 | + @Test |
| 63 | + public void testOnExplosionNotCreeper() { |
| 64 | + List<Block> list = new ArrayList<>(); |
| 65 | + Entity entity = mock(Entity.class); |
| 66 | + when(entity.getType()).thenReturn(EntityType.TNT); |
| 67 | + when(iwm.inWorld(location)).thenReturn(true); |
| 68 | + EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0); |
| 69 | + cl.onExplosion(event); |
| 70 | + assertFalse(event.isCancelled()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. |
| 75 | + */ |
| 76 | + @Test |
| 77 | + public void testOnExplosionNotInWorld() { |
| 78 | + List<Block> list = new ArrayList<>(); |
| 79 | + Entity entity = mock(Entity.class); |
| 80 | + when(entity.getLocation()).thenReturn(location); |
| 81 | + when(entity.getType()).thenReturn(EntityType.CREEPER); |
| 82 | + when(iwm.inWorld(location)).thenReturn(false); |
| 83 | + EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0); |
| 84 | + cl.onExplosion(event); |
| 85 | + assertFalse(event.isCancelled()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + public void testOnExplosionCreeperInWorldDamageOK() { |
| 93 | + List<Block> list = new ArrayList<>(); |
| 94 | + list.add(mock(Block.class)); |
| 95 | + list.add(mock(Block.class)); |
| 96 | + list.add(mock(Block.class)); |
| 97 | + Creeper entity = mock(Creeper.class); |
| 98 | + when(entity.getLocation()).thenReturn(location); |
| 99 | + when(entity.getType()).thenReturn(EntityType.CREEPER); |
| 100 | + when(iwm.inWorld(location)).thenReturn(true); |
| 101 | + EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0); |
| 102 | + cl.onExplosion(event); |
| 103 | + assertFalse(event.isCancelled()); |
| 104 | + assertFalse(event.blockList().isEmpty()); // No clearing of block list |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void testOnExplosionCreeperInWorldDamageNOK() { |
| 112 | + Flags.CREEPER_DAMAGE.setSetting(world, false); |
| 113 | + List<Block> list = new ArrayList<>(); |
| 114 | + list.add(mock(Block.class)); |
| 115 | + list.add(mock(Block.class)); |
| 116 | + list.add(mock(Block.class)); |
| 117 | + Creeper entity = mock(Creeper.class); |
| 118 | + when(location.getWorld()).thenReturn(world); |
| 119 | + when(entity.getLocation()).thenReturn(location); |
| 120 | + when(entity.getType()).thenReturn(EntityType.CREEPER); |
| 121 | + when(iwm.inWorld(location)).thenReturn(true); |
| 122 | + EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0); |
| 123 | + cl.onExplosion(event); |
| 124 | + assertFalse(event.isCancelled()); |
| 125 | + assertTrue(event.blockList().isEmpty()); // No clearing of block list |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.CreeperListener#onPlayerInteractEntity(org.bukkit.event.player.PlayerInteractEntityEvent)}. |
| 130 | + */ |
| 131 | + @Test |
| 132 | + public void testOnPlayerInteractEntity() { |
| 133 | + //TODO |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments