Skip to content

Commit

Permalink
Added FlyFlagListenerTest class
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 3, 2019
1 parent 41352c8 commit 4b76300
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public void onFlagChange(FlagProtectionChangeEvent e) {

// Stream through all of the flying and not allowed users at
// the moment and warn them that their fly is about to turn off
e.getIsland().getPlayersOnIsland().parallelStream()
island.getPlayersOnIsland()
.stream()
//.parallelStream()
.filter(Player::isFlying)
.filter(p -> !(island.isAllowed(User.getInstance(p), IslandFlyAddon.ISLAND_FLY_PROTECTION) || p.isOp()))
.filter(p -> !p.isOp())
.filter(p -> !(island.isAllowed(User.getInstance(p), IslandFlyAddon.ISLAND_FLY_PROTECTION)))
.forEach(p -> startDisabling(p, island));
}

Expand All @@ -55,27 +58,28 @@ private void startDisabling(Player p, Island island) {
}

// Else disable fly with a delay
Bukkit.getScheduler().runTaskLater(this.addon.getPlugin(), () -> {
Bukkit.getScheduler().runTaskLater(this.addon.getPlugin(), () -> disable(p, user, island), 20L* flyTimeout);
}

// Verify that player is still online
if (!user.isOnline()) return;
void disable(Player p, User user, Island island) {

// Check if user was reallowed to fly in the meantime
if (!island.isAllowed(user,IslandFlyAddon.ISLAND_FLY_PROTECTION)) {
// Verify that player is still online
if (!user.isOnline()) return;

// Silent cancel fly if player changed island in the meantime
// It will be the job of Enter/Exit island event to turn fly off if required
if (!island.onIsland(p.getLocation()))
return;
// Check if user was reallowed to fly in the meantime
if (!island.isAllowed(user,IslandFlyAddon.ISLAND_FLY_PROTECTION)) {

p.setFlying(false);
p.setAllowFlight(false);
user.sendMessage("islandfly.disable-fly");
}
else {
user.sendMessage("islandfly.reallowed-fly");
}
// Silent cancel fly if player changed island in the meantime
// It will be the job of Enter/Exit island event to turn fly off if required
if (!island.onIsland(p.getLocation()))
return;

}, 20L* flyTimeout);
p.setFlying(false);
p.setAllowFlight(false);
user.sendMessage("islandfly.disable-fly");
}
else {
user.sendMessage("islandfly.reallowed-fly");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package world.bentobox.islandfly.listeners;

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.ArrayList;
import java.util.List;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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 world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.islandfly.IslandFlyAddon;
import world.bentobox.islandfly.config.Settings;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class})
public class FlyFlagListenerTest {

private FlyFlagListener ffl;
@Mock
private BentoBox plugin;
@Mock
private IslandFlyAddon addon;
@Mock
private Settings settings;
@Mock
private BukkitScheduler scheduler;
@Mock
private FlagProtectionChangeEvent e;
@Mock
private Player p1;
@Mock
private Player p2;
@Mock
private Player p3;
@Mock
private Player op;
@Mock
private Island island;

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
when(addon.getPlugin()).thenReturn(plugin);
// Locales
User.setPlugin(plugin);
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
PlaceholdersManager phm = mock(PlaceholdersManager.class);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));

// Settings
when(settings.getFlyTimeout()).thenReturn(5);
when(addon.getSettings()).thenReturn(settings);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(scheduler);

// Event
when(e.getEditedFlag()).thenReturn(IslandFlyAddon.ISLAND_FLY_PROTECTION);
when(e.getIsland()).thenReturn(island);
@NonNull
List<Player> list = new ArrayList<>();
when(p1.getUniqueId()).thenReturn(UUID.randomUUID());
User.getInstance(p1);
when(p1.isFlying()).thenReturn(true);
when(p2.getUniqueId()).thenReturn(UUID.randomUUID());
User.getInstance(p2);
when(p2.isFlying()).thenReturn(true);
when(p2.isOnline()).thenReturn(true);
when(p2.getLocation()).thenReturn(mock(Location.class));
when(p3.getUniqueId()).thenReturn(UUID.randomUUID());
User.getInstance(p3);
when(p3.isFlying()).thenReturn(false);
when(op.getUniqueId()).thenReturn(UUID.randomUUID());
User.getInstance(op);
when(op.isFlying()).thenReturn(true);
when(op.isOp()).thenReturn(true);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(op);
when(island.getPlayersOnIsland()).thenReturn(list);
// One player is allowed, others not
when(island.isAllowed(any(), any())).thenReturn(true, false);
when(island.onIsland(any())).thenReturn(true);

ffl = new FlyFlagListener(addon);
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
User.clearUsers();
}

/**
* Test method for {@link world.bentobox.islandfly.listeners.FlyFlagListener#onFlagChange(world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent)}.
*/
@Test
public void testOnFlagChangeOtherFlag() {
FlagProtectionChangeEvent e = mock(FlagProtectionChangeEvent.class);
Flag flag = mock(Flag.class);
when(e.getEditedFlag()).thenReturn(flag);
ffl.onFlagChange(e);
verify(e, never()).getIsland();
}

/**
* Test method for {@link world.bentobox.islandfly.listeners.FlyFlagListener#onFlagChange(world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent)}.
*/
@Test
public void testOnFlagChange() {
ffl.onFlagChange(e);
verify(p1, never()).sendMessage(anyString());
verify(p2).sendMessage(eq("islandfly.fly-turning-off-alert"));
verify(p3, never()).sendMessage(anyString());
verify(op, never()).sendMessage(anyString());
verify(scheduler).runTaskLater(eq(plugin), any(Runnable.class), eq(100L));
}

/**
* Test method for {@link world.bentobox.islandfly.listeners.FlyFlagListener#onFlagChange(world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent)}.
*/
@Test
public void testOnFlagChangeZeroTime() {
when(settings.getFlyTimeout()).thenReturn(0);
ffl.onFlagChange(e);
verify(p1, never()).sendMessage(anyString());
verify(p2).sendMessage(eq("islandfly.fly-turning-off-alert"));
verify(p3, never()).sendMessage(anyString());
verify(op, never()).sendMessage(anyString());

verify(p2).setFlying(false);
verify(p2).setAllowFlight(false);
verify(p2).sendMessage("islandfly.disable-fly");

}

/**
* Test method for {@link world.bentobox.islandfly.listeners.FlyFlagListener#disable(Player, User, Island)}.
*/
@Test
public void testDisableAllowedAgain() {
when(island.isAllowed(any(), any())).thenReturn(true);
ffl.disable(p2, User.getInstance(p2), island);
verify(p2).sendMessage(eq("islandfly.reallowed-fly"));
}

/**
* Test method for {@link world.bentobox.islandfly.listeners.FlyFlagListener#disable(Player, User, Island)}.
*/
@Test
public void testDisable() {
when(island.isAllowed(any(), any())).thenReturn(false);
ffl.disable(p2, User.getInstance(p2), island);
verify(p2).sendMessage(eq("islandfly.disable-fly"));
}
}

0 comments on commit 4b76300

Please sign in to comment.