Added Warp Signs Listener test class

Cleaned up code.
This commit is contained in:
tastybento 2018-07-31 22:48:13 -04:00
parent 90103bd0a7
commit 4980122a53
5 changed files with 374 additions and 47 deletions

View File

@ -37,10 +37,12 @@ public class Warp extends Addon {
private Set<World> registeredWorlds;
private PluginConfig settings;
@Override
public void onEnable() {
// Load the plugin's config
new PluginConfig(this);
settings = new PluginConfig(this);
// Get the BSkyBlock plugin. This will be available because this plugin depends on it in plugin.yml.
plugin = this.getPlugin();
// Check if it is enabled - it might be loaded, but not enabled.
@ -55,7 +57,7 @@ public class Warp extends Addon {
warpSignsManager = new WarpSignsManager(this, plugin);
warpPanelManager = new WarpPanelManager(this);
// Load the listener
getServer().getPluginManager().registerEvents(new WarpSignsListener(this, plugin), plugin);
getServer().getPluginManager().registerEvents(new WarpSignsListener(this), plugin);
// Register commands
getServer().getScheduler().runTask(getPlugin(), () -> {
// BSkyBlock hook in
@ -124,4 +126,11 @@ public class Warp extends Addon {
return registeredWorlds.contains(Util.getWorld(world));
}
/**
* @return the settings
*/
public PluginConfig getSettings() {
return settings;
}
}

View File

@ -1,5 +1,8 @@
package bskyblock.addon.warps;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@ -13,7 +16,6 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import bskyblock.addon.level.Level;
import bskyblock.addon.warps.config.Settings;
import bskyblock.addon.warps.event.WarpRemoveEvent;
import world.bentobox.bbox.BentoBox;
import world.bentobox.bbox.api.user.User;
@ -31,11 +33,10 @@ public class WarpSignsListener implements Listener {
/**
* @param addon - addon
* @param plugin - BSB plugin
*/
public WarpSignsListener(Warp addon, BentoBox plugin) {
public WarpSignsListener(Warp addon) {
this.addon = addon;
this.plugin = plugin;
this.plugin = addon.getPlugin();
}
/**
@ -60,14 +61,13 @@ public class WarpSignsListener implements Listener {
if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getConfig().getString("welcomeLine"))) {
// Do a quick check to see if this sign location is in
// the list of warp signs
if (addon.getWarpSignsManager().getWarpList(b.getWorld()).containsValue(s.getLocation())) {
Map<UUID, Location> list = addon.getWarpSignsManager().getWarpList(b.getWorld());
if (list.containsValue(s.getLocation())) {
// Welcome sign detected - check to see if it is
System.out.println("DEBIG:");
// this player's sign
if ((addon.getWarpSignsManager().getWarpList(b.getWorld()).containsKey(user.getUniqueId()) && addon.getWarpSignsManager().getWarpList(b.getWorld()).get(user.getUniqueId()).equals(s.getLocation()))) {
// Player removed sign
addon.getWarpSignsManager().removeWarp(s.getLocation());
Bukkit.getPluginManager().callEvent(new WarpRemoveEvent(addon, s.getLocation(), user.getUniqueId()));
} else if (user.isOp() || user.hasPermission(addon.getPermPrefix(e.getBlock().getWorld()) + "mod.removesign")) {
if ((list.containsKey(user.getUniqueId()) && list.get(user.getUniqueId()).equals(s.getLocation()))
|| user.isOp() || user.hasPermission(addon.getPermPrefix(e.getBlock().getWorld()) + "mod.removesign")) {
// Op or mod removed sign
user.sendMessage("warps.removed");
addon.getWarpSignsManager().removeWarp(s.getLocation());
@ -89,10 +89,6 @@ public class WarpSignsListener implements Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
public void onSignWarpCreate(SignChangeEvent e) {
Block b = e.getBlock();
// Signs only
if (!b.getType().equals(Material.SIGN) && !b.getType().equals(Material.WALL_SIGN)) {
return;
}
if (!addon.inRegisteredWorld(b.getWorld())) {
return;
}
@ -108,11 +104,11 @@ public class WarpSignsListener implements Listener {
}
if (addon.getLevelAddon().isPresent()) {
Level lev = (Level) addon.getLevelAddon().get();
if (lev.getIslandLevel(b.getWorld(), user.getUniqueId()) < Settings.warpLevelRestriction) {
if (lev.getIslandLevel(b.getWorld(), user.getUniqueId()) < addon.getSettings().getWarpLevelRestriction()) {
user.sendMessage("warps.error.not-enough-level");
user.sendMessage("warps.error.your-level-is",
"[level]", String.valueOf(lev.getIslandLevel(b.getWorld(), user.getUniqueId())),
"[required]", String.valueOf(Settings.warpLevelRestriction));
"[required]", String.valueOf(addon.getSettings().getWarpLevelRestriction()));
return;
}
}
@ -127,22 +123,9 @@ public class WarpSignsListener implements Listener {
// Check if the player already has a sign
final Location oldSignLoc = addon.getWarpSignsManager().getWarp(b.getWorld(), user.getUniqueId());
if (oldSignLoc == null) {
//plugin.getLogger().info("DEBUG: Player does not have a sign already");
// First time the sign has been placed or this is a new
// sign
if (addon.getWarpSignsManager().addWarp(user.getUniqueId(), b.getLocation())) {
user.sendMessage("warps.success");
e.setLine(0, ChatColor.GREEN + addon.getConfig().getString("welcomeLine"));
for (int i = 1; i<4; i++) {
e.setLine(i, ChatColor.translateAlternateColorCodes('&', e.getLine(i)));
}
} else {
user.sendMessage("warps.error.duplicate");
e.setLine(0, ChatColor.RED + addon.getConfig().getString("welcomeLine"));
for (int i = 1; i<4; i++) {
e.setLine(i, ChatColor.translateAlternateColorCodes('&', e.getLine(i)));
}
}
addSign(e, user, b);
} else {
// A sign already exists. Check if it still there and if
// so,
@ -161,17 +144,27 @@ public class WarpSignsListener implements Listener {
}
}
}
// Set up the warp
if (addon.getWarpSignsManager().addWarp(user.getUniqueId(), b.getLocation())) {
user.sendMessage("warps.error.success");
e.setLine(0, ChatColor.GREEN + addon.getConfig().getString("welcomeLine"));
} else {
user.sendMessage("warps.error.duplicate");
e.setLine(0, ChatColor.RED + addon.getConfig().getString("welcomeLine"));
}
// Set up the new warp sign
addSign(e, user, b);
}
}
}
private void addSign(SignChangeEvent e, User user, Block b) {
if (addon.getWarpSignsManager().addWarp(user.getUniqueId(), b.getLocation())) {
user.sendMessage("warps.success");
e.setLine(0, ChatColor.GREEN + addon.getConfig().getString("welcomeLine"));
for (int i = 1; i<4; i++) {
e.setLine(i, ChatColor.translateAlternateColorCodes('&', e.getLine(i)));
}
} else {
user.sendMessage("warps.error.duplicate");
e.setLine(0, ChatColor.RED + addon.getConfig().getString("welcomeLine"));
for (int i = 1; i<4; i++) {
e.setLine(i, ChatColor.translateAlternateColorCodes('&', e.getLine(i)));
}
}
}
}

View File

@ -4,12 +4,21 @@ import bskyblock.addon.warps.Warp;
public class PluginConfig {
private int warpLevelRestriction;
/**
* Loads the various settings from the config.yml file into the plugin
*/
public PluginConfig(Warp plugin) {
plugin.saveDefaultConfig();
Settings.warpLevelRestriction = plugin.getConfig().getInt("warplevelrestriction",10);
warpLevelRestriction = plugin.getConfig().getInt("warplevelrestriction",10);
// All done
}
/**
* @return the warpLevelRestriction
*/
public int getWarpLevelRestriction() {
return warpLevelRestriction;
}
}

View File

@ -1,6 +0,0 @@
package bskyblock.addon.warps.config;
public class Settings {
public static int warpLevelRestriction;
}

View File

@ -0,0 +1,322 @@
package bskyblock.addon.warps;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
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 bskyblock.addon.level.Level;
import bskyblock.addon.warps.config.PluginConfig;
import world.bentobox.bbox.BentoBox;
import world.bentobox.bbox.api.addons.Addon;
import world.bentobox.bbox.api.user.User;
import world.bentobox.bbox.managers.IslandsManager;
import world.bentobox.bbox.managers.LocalesManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class})
public class WarpSignsListenerTest {
private Warp addon;
private Block block;
private Player player;
private World world;
private Sign s;
private WarpSignsManager wsm;
private PluginManager pm;
private UUID uuid;
private String[] lines;
private FileConfiguration config;
private PluginConfig settings;
private Level level_addon;
private IslandsManager im;
@Before
public void setUp() throws Exception {
addon = mock(Warp.class);
when(addon.inRegisteredWorld(Mockito.any())).thenReturn(true);
config = mock(FileConfiguration.class);
when(config.getString(Mockito.anyString())).thenReturn("[WELCOME]");
when(addon.getConfig()).thenReturn(config);
block = mock(Block.class);
when(block.getType()).thenReturn(Material.WALL_SIGN);
world = mock(World.class);
when(block.getWorld()).thenReturn(world);
player = mock(Player.class);
when(player.hasPermission(Mockito.anyString())).thenReturn(false);
uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
s = mock(Sign.class);
when(s.getLine(Mockito.anyInt())).thenReturn(ChatColor.GREEN + "[WELCOME]");
when(block.getState()).thenReturn((BlockState)s);
wsm = mock(WarpSignsManager.class);
when(addon.getWarpSignsManager()).thenReturn(wsm);
Map<UUID, Location> list = new HashMap<>();
Location location = mock(Location.class);
when(location.getBlock()).thenReturn(block);
when(s.getLocation()).thenReturn(location);
list.put(uuid, location);
// Player is in world
when(wsm.getWarpList(Mockito.eq(world))).thenReturn(list);
//Player has a warp sign already here
when(wsm.getWarp(Mockito.any(), Mockito.any())).thenReturn(location);
// Unique spot
when(wsm.addWarp(Mockito.any(), Mockito.any())).thenReturn(true);
// Bentobox
BentoBox plugin = mock(BentoBox.class);
when(addon.getPlugin()).thenReturn(plugin);
User.setPlugin(plugin);
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(new Answer<String>(){
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgumentAt(1, String.class);
}});
when(plugin.getLocalesManager()).thenReturn(lm);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
pm = mock(PluginManager.class);
when(Bukkit.getPluginManager()).thenReturn(pm);
// Lines
lines = new String[] {"[WELCOME]", "line2", "line3", "line4"};
settings = mock(PluginConfig.class);
when(settings.getWarpLevelRestriction()).thenReturn(10);
when(addon.getSettings()).thenReturn(settings);
level_addon = mock(Level.class);
Optional<Addon> level = Optional.of(level_addon);
when(addon.getLevelAddon()).thenReturn(level);
when(level_addon.getIslandLevel(Mockito.any(), Mockito.any())).thenReturn(11L); // Higher than 10
im = mock(IslandsManager.class);
// On island
when(plugin.getIslands()).thenReturn(im);
when(im.userIsOnIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(true);
}
@Test
public void testWarpSignsListener() {
assertNotNull(new WarpSignsListener(addon));
}
@Test
public void testOnSignBreakNotSign() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(block.getType()).thenReturn(Material.STONE);
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
Mockito.verify(block, Mockito.times(2)).getType();
}
@Test
public void testOnSignBreakWrongWorld() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(addon.inRegisteredWorld(Mockito.any())).thenReturn(false);
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
Mockito.verify(addon).inRegisteredWorld(Mockito.eq(world));
}
@Test
public void testOnSignBreakNullState() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(block.getState()).thenReturn(null);
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
Mockito.verify(block).getState();
}
@Test
public void testOnSignNotWelcomeSign() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(s.getLine(Mockito.anyInt())).thenReturn(ChatColor.RED + "[WELCOME]");
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
Mockito.verify(s).getLine(Mockito.eq(0));
Mockito.verify(addon).getConfig();
}
@Test
public void testOnSignNotRealWelcomeSign() {
// Right text, but not in the right position
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(s.getLocation()).thenReturn(mock(Location.class));
wsl.onSignBreak(e);
assertFalse(e.isCancelled());
Mockito.verify(wsm).getWarpList(Mockito.eq(world));
Mockito.verify(s).getLocation();
}
@Test
public void testOnSignRemovePlayerSignWrongPlayer() {
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
wsl.onSignBreak(e);
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("warps.error.no-remove");
}
@Test
public void testOnSignRemovePlayerSignPlayerIsOp() {
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(player.isOp()).thenReturn(true);
wsl.onSignBreak(e);
// Success!
assertFalse(e.isCancelled());
Mockito.verify(pm).callEvent(Mockito.any());
}
@Test
public void testOnSignRemovePlayerSignPlayerHasPerm() {
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
wsl.onSignBreak(e);
// Success!
assertFalse(e.isCancelled());
Mockito.verify(pm).callEvent(Mockito.any());
}
@Test
public void testOnSignRemoveCorrectPlayer() {
WarpSignsListener wsl = new WarpSignsListener(addon);
BlockBreakEvent e = new BlockBreakEvent(block, player);
wsl.onSignBreak(e);
// Success!
assertFalse(e.isCancelled());
Mockito.verify(pm).callEvent(Mockito.any());
}
/**
* Sign create
*/
@Test
public void testOnCreateWrongWorld() {
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
when(addon.inRegisteredWorld(Mockito.any())).thenReturn(false);
wsl.onSignWarpCreate(e);
Mockito.verify(addon).inRegisteredWorld(Mockito.eq(world));
}
@Test
public void testOnCreateWrongText() {
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
lines = new String[] {"line1", "line2", "line3", "line4"};
when(player.hasPermission(Mockito.anyString())).thenReturn(false);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(config).getString(Mockito.anyString());
Mockito.verify(player, Mockito.never()).sendMessage(Mockito.anyString());
}
@Test
public void testOnCreateNoPerm() {
when(player.hasPermission(Mockito.anyString())).thenReturn(false);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.error.no-permission");
}
@Test
public void testOnLevelPresentNotHighEnough() {
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
when(level_addon.getIslandLevel(Mockito.any(), Mockito.any())).thenReturn(1L);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.error.not-enough-level");
}
@Test
public void testOnNoIsland() {
when(im.userIsOnIsland(Mockito.any(World.class), Mockito.any(User.class))).thenReturn(false);
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.error.not-on-island");
assertEquals(ChatColor.RED + "[WELCOME]", e.getLine(0));
}
@Test
public void testCreateNoSignAlreadyUniqueSpot() {
when(wsm.getWarp(Mockito.any(), Mockito.any())).thenReturn(null);
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.success");
assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0));
}
@Test
public void testCreateNoSignAlreadyDuplicateSpot() {
when(wsm.addWarp(Mockito.any(), Mockito.any())).thenReturn(false);
when(wsm.getWarp(Mockito.any(), Mockito.any())).thenReturn(null);
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.error.duplicate");
assertEquals(ChatColor.RED + "[WELCOME]", e.getLine(0));
}
@Test
public void testCreateNoSignDeactivateOldSign() {
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
SignChangeEvent e = new SignChangeEvent(block, player, lines);
wsl.onSignWarpCreate(e);
Mockito.verify(player).sendMessage("warps.success");
assertEquals(ChatColor.GREEN + "[WELCOME]", e.getLine(0));
Mockito.verify(s).setLine(0, ChatColor.RED + "[WELCOME]");
}
}