Adds new flag change events and fires them.

Updated click tests.

https://github.com/BentoBoxWorld/BentoBox/issues/753
This commit is contained in:
tastybento 2019-07-28 16:15:23 -07:00
parent a8578b5658
commit 0f5629b729
10 changed files with 344 additions and 153 deletions

View File

@ -1,54 +0,0 @@
package world.bentobox.bentobox.api.events.island;
import java.util.UUID;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island;
/**
* This event is fired when a player changes a flag on his island
* <p>
* Canceling this event will result in canceling the change.
*
* @author Poslovitch
*/
public class FlagChangeEvent extends IslandBaseEvent {
private final UUID player;
private final Flag editedFlag;
private final boolean setTo;
/**
* @param island - island
* @param player - the player
* @param editedFlag - flag edited
* @param setTo - new value
*/
public FlagChangeEvent(Island island, UUID player, Flag editedFlag, boolean setTo) {
super(island);
this.player = player;
this.editedFlag = editedFlag;
this.setTo = setTo;
}
/**
* @return the player
*/
public UUID getPlayer() {
return player;
}
/**
* @return the edited flag
*/
public Flag getFlag() {
return editedFlag;
}
/**
* @return enabled/disabled
*/
public boolean getSetTo() {
return setTo;
}
}

View File

@ -0,0 +1,62 @@
package world.bentobox.bentobox.api.events.island;
import java.util.UUID;
import world.bentobox.bentobox.api.events.BentoBoxEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
* @since 1.6.0
*/
public class FlagProtectionChangeEvent extends BentoBoxEvent {
private final Island island;
private final UUID player;
private final Flag editedFlag;
private final int setTo;
/**
* Event that fires when an island protection flag is changed
* @param island - island
* @param player - player changing the flag
* @param editedFlag - flag that has changed
* @param setTo - value it was set to
*/
public FlagProtectionChangeEvent(Island island, UUID player, Flag editedFlag, int setTo) {
this.island = island;
this.player = player;
this.editedFlag = editedFlag;
this.setTo = setTo;
}
/**
* @return the island
*/
public Island getIsland() {
return island;
}
/**
* @return the player
*/
public UUID getPlayer() {
return player;
}
/**
* @return the editedFlag
*/
public Flag getEditedFlag() {
return editedFlag;
}
/**
* @return the setTo
*/
public int getSetTo() {
return setTo;
}
}

View File

@ -0,0 +1,62 @@
package world.bentobox.bentobox.api.events.island;
import java.util.UUID;
import world.bentobox.bentobox.api.events.BentoBoxEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
* @since 1.6.0
*/
public class FlagSettingChangeEvent extends BentoBoxEvent {
private final Island island;
private final UUID player;
private final Flag editedFlag;
private final boolean setTo;
/**
* Event that fires when an island setting flag is changed
* @param island - island
* @param player - player changing the flag
* @param editedFlag - flag that has changed
* @param setTo - value it was set to
*/
public FlagSettingChangeEvent(Island island, UUID player, Flag editedFlag, boolean setTo) {
this.island = island;
this.player = player;
this.editedFlag = editedFlag;
this.setTo = setTo;
}
/**
* @return the island
*/
public Island getIsland() {
return island;
}
/**
* @return the player
*/
public UUID getPlayer() {
return player;
}
/**
* @return the editedFlag
*/
public Flag getEditedFlag() {
return editedFlag;
}
/**
* @return the setTo
*/
public boolean isSetTo() {
return setTo;
}
}

View File

@ -0,0 +1,64 @@
package world.bentobox.bentobox.api.events.island;
import java.util.UUID;
import org.bukkit.World;
import world.bentobox.bentobox.api.events.BentoBoxEvent;
import world.bentobox.bentobox.api.flags.Flag;
/**
* @author tastybento
* @since 1.6.0
*/
public class FlagWorldSettingChangeEvent extends BentoBoxEvent {
private final World world;
private final UUID player;
private final Flag editedFlag;
private final boolean setTo;
/**
* Event that fires when a world setting is changed
* @param world - world
* @param player - player changing the flag
* @param editedFlag - flag that has changed
* @param setTo - value it was set to
*/
public FlagWorldSettingChangeEvent(World world, UUID player, Flag editedFlag, boolean setTo) {
this.world = world;
this.player = player;
this.editedFlag = editedFlag;
this.setTo = setTo;
}
/**
* @return the world
*/
public World getWorld() {
return world;
}
/**
* @return the player
*/
public UUID getPlayer() {
return player;
}
/**
* @return the editedFlag
*/
public Flag getEditedFlag() {
return editedFlag;
}
/**
* @return the setTo
*/
public boolean isSetTo() {
return setTo;
}
}

View File

@ -1,10 +1,12 @@
package world.bentobox.bentobox.api.flags.clicklisteners;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.island.FlagProtectionChangeEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -86,6 +88,8 @@ public class CycleClick implements PanelItem.ClickHandler {
island.setFlag(flag, rm.getRankUpValue(currentRank));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_OFF, 1F, 1F);
// Fire event
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
} else if (click.equals(ClickType.RIGHT)) {
if (currentRank <= minRank) {
island.setFlag(flag, maxRank);
@ -93,6 +97,8 @@ public class CycleClick implements PanelItem.ClickHandler {
island.setFlag(flag, rm.getRankDownValue(currentRank));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
// Fire event
Bukkit.getPluginManager().callEvent(new FlagProtectionChangeEvent(island, user.getUniqueId(), flag, island.getFlag(flag)));
} else if (click.equals(ClickType.SHIFT_LEFT) && user.isOp()) {
if (!plugin.getIWM().getHiddenFlags(user.getWorld()).contains(flag.getID())) {
invisible = true;
@ -103,8 +109,7 @@ public class CycleClick implements PanelItem.ClickHandler {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1F, 1F);
}
// Save changes
plugin.getIWM().getAddon(user.getWorld()).ifPresent(GameModeAddon::saveWorldSettings);
}
plugin.getIWM().getAddon(user.getWorld()).ifPresent(GameModeAddon::saveWorldSettings); }
// Apply change to panel
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user, invisible).getItem());
});

View File

@ -1,10 +1,12 @@
package world.bentobox.bentobox.api.flags.clicklisteners;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.island.FlagSettingChangeEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
@ -72,6 +74,8 @@ public class IslandToggleClick implements ClickHandler {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
// Set cooldown
island.setCooldown(flag);
// Fire event
Bukkit.getPluginManager().callEvent(new FlagSettingChangeEvent(island, user.getUniqueId(), flag, island.isAllowed(flag)));
}
// Apply change to panel
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user, invisible).getItem());

View File

@ -1,10 +1,12 @@
package world.bentobox.bentobox.api.flags.clicklisteners;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.island.FlagWorldSettingChangeEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
@ -60,6 +62,8 @@ public class WorldToggleClick implements ClickHandler {
// Toggle flag
flag.setSetting(user.getWorld(), !flag.isSetForWorld(user.getWorld()));
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
// Fire event
Bukkit.getPluginManager().callEvent(new FlagWorldSettingChangeEvent(user.getWorld(), user.getUniqueId(), flag, flag.isSetForWorld(user.getWorld())));
}
// Apply change to panel
panel.getInventory().setItem(slot, flag.toPanelItem(plugin, user, invisible).getItem());

View File

@ -2,9 +2,13 @@ package world.bentobox.bentobox.api.flags.clicklisteners;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import java.util.Arrays;
import java.util.HashSet;
@ -18,10 +22,12 @@ import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -30,6 +36,7 @@ import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.events.island.FlagProtectionChangeEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -52,16 +59,27 @@ public class CycleClickTest {
private static final Integer X = 600;
private static final Integer Y = 120;
private static final Integer Z = 10000;
@Mock
private BentoBox plugin;
private UUID uuid;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private Flag flag;
@Mock
private Panel panel;
@Mock
private Inventory inv;
@Mock
private IslandWorldManager iwm;
@Mock
private RanksManager rm;
@Mock
private PluginManager pim;
/**
* @throws java.lang.Exception - exception
@ -70,7 +88,6 @@ public class CycleClickTest {
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// World
@ -83,7 +100,6 @@ public class CycleClickTest {
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
User.setPlugin(plugin);
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
@ -91,17 +107,16 @@ public class CycleClickTest {
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world);
when(user.hasPermission(Mockito.anyString())).thenReturn(true);
when(user.hasPermission(anyString())).thenReturn(true);
// No island for player to begin with (set it later in the tests)
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(false);
when(im.isOwner(Mockito.any(), Mockito.eq(uuid))).thenReturn(false);
when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
when(im.isOwner(any(), eq(uuid))).thenReturn(false);
when(plugin.getIslands()).thenReturn(im);
// Has team
PlayersManager pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
@ -119,9 +134,8 @@ public class CycleClickTest {
when(plugin.getNotifier()).thenReturn(notifier);
// Island Banned list initialization
island = mock(Island.class);
when(island.getBanned()).thenReturn(new HashSet<>());
when(island.isBanned(Mockito.any())).thenReturn(false);
when(island.isBanned(any())).thenReturn(false);
Location loc = mock(Location.class);
when(loc.getWorld()).thenReturn(world);
when(loc.getBlockX()).thenReturn(X);
@ -130,11 +144,11 @@ public class CycleClickTest {
when(island.getCenter()).thenReturn(loc);
when(island.getProtectionRange()).thenReturn(PROTECTION_RANGE);
// Island is not locked by default
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
when(island.isAllowed(any(), any())).thenReturn(true);
// Island owner is user by default
when(island.getOwner()).thenReturn(uuid);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
// Common from to's
Location outside = mock(Location.class);
@ -156,49 +170,48 @@ public class CycleClickTest {
when(inside.getBlockZ()).thenReturn(Z);
Optional<Island> opIsland = Optional.ofNullable(island);
when(im.getProtectedIslandAt(Mockito.eq(inside))).thenReturn(opIsland);
when(im.getProtectedIslandAt(Mockito.eq(inside2))).thenReturn(opIsland);
when(im.getProtectedIslandAt(Mockito.eq(outside))).thenReturn(Optional.empty());
when(im.getIslandAt(Mockito.any())).thenReturn(opIsland);
when(im.getProtectedIslandAt(eq(inside))).thenReturn(opIsland);
when(im.getProtectedIslandAt(eq(inside2))).thenReturn(opIsland);
when(im.getProtectedIslandAt(eq(outside))).thenReturn(Optional.empty());
when(im.getIslandAt(any())).thenReturn(opIsland);
PanelItem panelItem = mock(PanelItem.class);
flag = mock(Flag.class);
when(flag.toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false))).thenReturn(panelItem);
when(flag.toPanelItem(any(), any(), eq(false))).thenReturn(panelItem);
when(panelItem.getItem()).thenReturn(mock(ItemStack.class));
FlagsManager fm = mock(FlagsManager.class);
when(fm.getFlag(Mockito.anyString())).thenReturn(Optional.of(flag));
when(fm.getFlag(anyString())).thenReturn(Optional.of(flag));
when(plugin.getFlagsManager()).thenReturn(fm);
rm = mock(RanksManager.class);
// Ranks Manager
when(plugin.getRanksManager()).thenReturn(rm);
// Provide a current rank value - member
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.MEMBER_RANK);
// Set up up and down ranks
when(rm.getRankUpValue(Mockito.eq(RanksManager.VISITOR_RANK))).thenReturn(RanksManager.COOP_RANK);
when(rm.getRankUpValue(Mockito.eq(RanksManager.COOP_RANK))).thenReturn(RanksManager.TRUSTED_RANK);
when(rm.getRankUpValue(Mockito.eq(RanksManager.TRUSTED_RANK))).thenReturn(RanksManager.MEMBER_RANK);
when(rm.getRankUpValue(Mockito.eq(RanksManager.MEMBER_RANK))).thenReturn(RanksManager.OWNER_RANK);
when(rm.getRankDownValue(Mockito.eq(RanksManager.OWNER_RANK))).thenReturn(RanksManager.MEMBER_RANK);
when(rm.getRankDownValue(Mockito.eq(RanksManager.MEMBER_RANK))).thenReturn(RanksManager.TRUSTED_RANK);
when(rm.getRankDownValue(Mockito.eq(RanksManager.TRUSTED_RANK))).thenReturn(RanksManager.COOP_RANK);
when(rm.getRankDownValue(Mockito.eq(RanksManager.COOP_RANK))).thenReturn(RanksManager.VISITOR_RANK);
when(rm.getRankUpValue(eq(RanksManager.VISITOR_RANK))).thenReturn(RanksManager.COOP_RANK);
when(rm.getRankUpValue(eq(RanksManager.COOP_RANK))).thenReturn(RanksManager.TRUSTED_RANK);
when(rm.getRankUpValue(eq(RanksManager.TRUSTED_RANK))).thenReturn(RanksManager.MEMBER_RANK);
when(rm.getRankUpValue(eq(RanksManager.MEMBER_RANK))).thenReturn(RanksManager.OWNER_RANK);
when(rm.getRankDownValue(eq(RanksManager.OWNER_RANK))).thenReturn(RanksManager.MEMBER_RANK);
when(rm.getRankDownValue(eq(RanksManager.MEMBER_RANK))).thenReturn(RanksManager.TRUSTED_RANK);
when(rm.getRankDownValue(eq(RanksManager.TRUSTED_RANK))).thenReturn(RanksManager.COOP_RANK);
when(rm.getRankDownValue(eq(RanksManager.COOP_RANK))).thenReturn(RanksManager.VISITOR_RANK);
panel = mock(Panel.class);
inv = mock(Inventory.class);
// Panel
when(panel.getInventory()).thenReturn(inv);
// IslandWorldManager
iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock");
// Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
when(Util.getWorld(any())).thenReturn(world);
// Event
when(Bukkit.getPluginManager()).thenReturn(pim);
}
@ -208,15 +221,15 @@ public class CycleClickTest {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
CycleClick udc = new CycleClick("LOCK");
assertTrue(udc.onClick(panel, user, ClickType.LEFT, 5));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.wrong-world"));
verify(user).sendMessage(eq("general.errors.wrong-world"));
}
@Test
public void testNoPremission() {
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
when(user.hasPermission(anyString())).thenReturn(false);
CycleClick udc = new CycleClick("LOCK");
assertTrue(udc.onClick(panel, user, ClickType.LEFT, 5));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission"), Mockito.eq("[permission]"), Mockito.eq("bskyblock.settings.LOCK"));
verify(user).sendMessage(eq("general.errors.no-permission"), eq("[permission]"), eq("bskyblock.settings.LOCK"));
}
@Test
@ -232,37 +245,39 @@ public class CycleClickTest {
// Rank starts at member
// Click left
assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.OWNER_RANK));
Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.OWNER_RANK));
verify(flag).toPanelItem(any(), any(), eq(false));
verify(inv).setItem(eq(SLOT), any());
// Check rollover
// Clicking when Owner should go to Visitor
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.OWNER_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.OWNER_RANK);
assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.VISITOR_RANK));
Mockito.verify(flag, Mockito.times(2)).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv, Mockito.times(2)).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.VISITOR_RANK));
verify(flag, times(2)).toPanelItem(any(), any(), eq(false));
verify(inv, times(2)).setItem(eq(SLOT), any());
verify(pim, times(2)).callEvent(any(FlagProtectionChangeEvent.class));
}
@Test
public void testOnLeftClickSetMinMax() {
// Provide a current rank value - coop
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.COOP_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.COOP_RANK);
final int SLOT = 5;
CycleClick udc = new CycleClick("LOCK", RanksManager.COOP_RANK, RanksManager.MEMBER_RANK);
// Rank starts at member
// Click left
assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.TRUSTED_RANK));
Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.TRUSTED_RANK));
verify(flag).toPanelItem(any(), any(), eq(false));
verify(inv).setItem(eq(SLOT), any());
// Check rollover
// Clicking when Member should go to Coop
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.MEMBER_RANK);
assertTrue(udc.onClick(panel, user, ClickType.LEFT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.COOP_RANK));
Mockito.verify(flag, Mockito.times(2)).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv, Mockito.times(2)).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.COOP_RANK));
verify(flag, times(2)).toPanelItem(any(), any(), eq(false));
verify(inv, times(2)).setItem(eq(SLOT), any());
verify(pim, times(2)).callEvent(any(FlagProtectionChangeEvent.class));
}
@Test
@ -272,37 +287,39 @@ public class CycleClickTest {
// Rank starts at member
// Right click - down rank to Trusted
assertTrue(udc.onClick(panel, user, ClickType.RIGHT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.TRUSTED_RANK));
Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.TRUSTED_RANK));
verify(flag).toPanelItem(any(), any(), eq(false));
verify(inv).setItem(eq(SLOT), any());
// Check rollover
// Clicking when Visitor should go to Owner
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.VISITOR_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.VISITOR_RANK);
assertTrue(udc.onClick(panel, user, ClickType.RIGHT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.OWNER_RANK));
Mockito.verify(flag, Mockito.times(2)).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv, Mockito.times(2)).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.OWNER_RANK));
verify(flag, times(2)).toPanelItem(any(), any(), eq(false));
verify(inv, times(2)).setItem(eq(SLOT), any());
verify(pim, times(2)).callEvent(any(FlagProtectionChangeEvent.class));
}
@Test
public void testOnRightClickMinMaxSet() {
// Provide a current rank value - coop
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.TRUSTED_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.TRUSTED_RANK);
final int SLOT = 5;
CycleClick udc = new CycleClick("LOCK", RanksManager.COOP_RANK, RanksManager.MEMBER_RANK);
// Rank starts at member
// Right click
assertTrue(udc.onClick(panel, user, ClickType.RIGHT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.COOP_RANK));
Mockito.verify(flag).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.COOP_RANK));
verify(flag).toPanelItem(any(), any(), eq(false));
verify(inv).setItem(eq(SLOT), any());
// Check rollover
// Clicking when Coop should go to Member
when(island.getFlag(Mockito.any())).thenReturn(RanksManager.COOP_RANK);
when(island.getFlag(any())).thenReturn(RanksManager.COOP_RANK);
assertTrue(udc.onClick(panel, user, ClickType.RIGHT, SLOT));
Mockito.verify(island).setFlag(Mockito.eq(flag), Mockito.eq(RanksManager.MEMBER_RANK));
Mockito.verify(flag, Mockito.times(2)).toPanelItem(Mockito.any(), Mockito.any(), Mockito.eq(false));
Mockito.verify(inv, Mockito.times(2)).setItem(Mockito.eq(SLOT), Mockito.any());
verify(island).setFlag(eq(flag), eq(RanksManager.MEMBER_RANK));
verify(flag, times(2)).toPanelItem(any(), any(), eq(false));
verify(inv, times(2)).setItem(eq(SLOT), any());
verify(pim, times(2)).callEvent(any(FlagProtectionChangeEvent.class));
}
@Test
@ -310,6 +327,7 @@ public class CycleClickTest {
// Test all possible click types
CycleClick udc = new CycleClick("LOCK");
Arrays.asList(ClickType.values()).forEach(c -> assertTrue(udc.onClick(panel, user, c, 0)));
verify(pim, times(2)).callEvent(any(FlagProtectionChangeEvent.class));
}
@Test
@ -317,14 +335,14 @@ public class CycleClickTest {
UUID u = UUID.randomUUID();
when(island.getOwner()).thenReturn(u);
Mockito.verify(plugin, Mockito.never()).getRanksManager();
verify(plugin, Mockito.never()).getRanksManager();
}
@Test
public void testNullIsland() {
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(null);
Mockito.verify(plugin, Mockito.never()).getRanksManager();
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
verify(plugin, Mockito.never()).getRanksManager();
}
}

View File

@ -2,20 +2,25 @@ package world.bentobox.bentobox.api.flags.clicklisteners;
import static org.mockito.Matchers.any;
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.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -23,6 +28,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.island.FlagSettingChangeEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -34,17 +40,25 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class })
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class })
public class IslandToggleClickTest {
@Mock
private IslandWorldManager iwm;
private IslandToggleClick listener;
@Mock
private Panel panel;
@Mock
private User user;
@Mock
private Flag flag;
@Mock
private IslandsManager im;
@Mock
private Island island;
private UUID uuid;
@Mock
private PluginManager pim;
/**
* @throws java.lang.Exception
@ -59,12 +73,12 @@ public class IslandToggleClickTest {
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock");
when(plugin.getIWM()).thenReturn(iwm);
listener = new IslandToggleClick("test");
panel = mock(Panel.class);
// Panel
when(panel.getInventory()).thenReturn(mock(Inventory.class));
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
@ -75,28 +89,28 @@ public class IslandToggleClickTest {
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
when(Util.getWorld(any())).thenReturn(mock(World.class));
FlagsManager fm = mock(FlagsManager.class);
flag = mock(Flag.class);
when(flag.isSetForWorld(Mockito.any())).thenReturn(false);
when(flag.isSetForWorld(any())).thenReturn(false);
PanelItem item = mock(PanelItem.class);
when(item.getItem()).thenReturn(mock(ItemStack.class));
when(flag.toPanelItem(Mockito.any(), Mockito.eq(user), Mockito.eq(false))).thenReturn(item);
when(flag.toPanelItem(any(), Mockito.eq(user), Mockito.eq(false))).thenReturn(item);
when(fm.getFlag(Mockito.anyString())).thenReturn(Optional.of(flag));
when(plugin.getFlagsManager()).thenReturn(fm);
// Island Manager
im = mock(IslandsManager.class);
island = mock(Island.class);
when(island.getOwner()).thenReturn(uuid);
when(im.getIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(island);
when(im.getIsland(any(World.class), any(User.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Optional island
Optional<Island> opIsland = Optional.ofNullable(island);
when(im.getIslandAt(Mockito.any())).thenReturn(opIsland);
when(im.getIslandAt(any())).thenReturn(opIsland);
// Event
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
}
@Test
@ -104,28 +118,29 @@ public class IslandToggleClickTest {
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(iwm.inWorld(any(Location.class))).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.wrong-world");
verify(user).sendMessage("general.errors.wrong-world");
}
@Test
public void testOnClickNoPermission() {
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.settings.test");
verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.settings.test");
}
@Test
public void testOnClick() {
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(island).toggleFlag(flag);
verify(island).toggleFlag(flag);
verify(pim).callEvent(any(FlagSettingChangeEvent.class));
}
@Test
public void testOnClickNoIsland() {
when(im.getIslandAt(Mockito.any())).thenReturn(Optional.empty());
when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(null);
when(im.getIslandAt(any())).thenReturn(Optional.empty());
when(im.getIsland(any(), any(User.class))).thenReturn(null);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(island, Mockito.never()).toggleFlag(flag);
verify(island, never()).toggleFlag(flag);
}
@Test
@ -137,7 +152,7 @@ public class IslandToggleClickTest {
}
when(island.getOwner()).thenReturn(u);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(island, Mockito.never()).toggleFlag(flag);
verify(island, never()).toggleFlag(flag);
}
}

View File

@ -3,15 +3,18 @@ package world.bentobox.bentobox.api.flags.clicklisteners;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -24,6 +27,7 @@ import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.events.island.FlagWorldSettingChangeEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
@ -33,7 +37,7 @@ import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class })
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class })
public class WorldToggleClickTest {
@Mock
@ -46,6 +50,8 @@ public class WorldToggleClickTest {
private Flag flag;
@Mock
private GameModeAddon addon;
@Mock
private PluginManager pim;
/**
* @throws java.lang.Exception
@ -89,6 +95,10 @@ public class WorldToggleClickTest {
when(flag.toPanelItem(Mockito.any(), Mockito.eq(user), Mockito.eq(false))).thenReturn(item);
when(fm.getFlag(Mockito.anyString())).thenReturn(Optional.of(flag));
when(plugin.getFlagsManager()).thenReturn(fm);
// Event
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
}
@Test
@ -96,24 +106,25 @@ public class WorldToggleClickTest {
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(iwm.inWorld(any(Location.class))).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.wrong-world");
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
verify(user).sendMessage("general.errors.wrong-world");
verify(addon, Mockito.never()).saveWorldSettings();
}
@Test
public void testOnClickNoPermission() {
when(user.hasPermission(Mockito.anyString())).thenReturn(false);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test");
Mockito.verify(addon, Mockito.never()).saveWorldSettings();
verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test");
verify(addon, Mockito.never()).saveWorldSettings();
}
@Test
public void testOnClick() {
when(user.hasPermission(Mockito.anyString())).thenReturn(true);
listener.onClick(panel, user, ClickType.LEFT, 0);
Mockito.verify(flag).setSetting(Mockito.any(), Mockito.eq(true));
Mockito.verify(panel).getInventory();
Mockito.verify(addon).saveWorldSettings();
verify(flag).setSetting(Mockito.any(), Mockito.eq(true));
verify(panel).getInventory();
verify(addon).saveWorldSettings();
verify(pim).callEvent(any(FlagWorldSettingChangeEvent.class));
}
}