From fc3f685de0cf75a78f78b48ce8b220f43724b5d8 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 21 Nov 2015 23:09:04 +0100 Subject: [PATCH] Create UtilsTest Had to create a getGameServer() method in AuthMe in order to be able to catch this call in tests and replace it with a mock implementation of the actual server. The supertype method getServer() is declared as final, which makes it impossible to mock with Mockito. While there are frameworks that manipulate the bytecode to mock final calls, it is much easier and less risky to simply delegate the server retrieval through another method. --- src/main/java/fr/xephi/authme/AuthMe.java | 11 ++- src/main/java/fr/xephi/authme/util/Utils.java | 73 ++++--------------- .../java/fr/xephi/authme/util/UtilsTest.java | 73 +++++++++++++++++++ 3 files changed, 98 insertions(+), 59 deletions(-) create mode 100644 src/test/java/fr/xephi/authme/util/UtilsTest.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index ba8342510..fda2bde0d 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -22,7 +22,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.mcstats.Metrics; @@ -933,4 +932,14 @@ public class AuthMe extends JavaPlugin { public Management getManagement() { return management; } + + /** + * Returns the server instance running this plugin. Use this method in favor of + * {@link AuthMe#getServer()} for testability purposes. + * + * @return the server instance + */ + public Server getGameServer() { + return super.getServer(); + } } diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index 957a948f3..aceb6d74c 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -27,7 +27,7 @@ import java.util.zip.GZIPInputStream; /** */ -public class Utils { +public final class Utils { public static AuthMe plugin; @@ -45,11 +45,11 @@ public class Utils { } } - // Check and Download GeoIP data if not exist - /** - * Method checkGeoIP. - - * @return boolean */ + private Utils() { + // Utility class + } + + // Check and Download GeoIP data if it doesn't exist public static boolean checkGeoIP() { if (lookupService != null) { return true; @@ -59,14 +59,15 @@ public class Utils { if (lookupService == null) { try { lookupService = new LookupService(data); - ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com"); + ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, " + + "available at http://www.maxmind.com"); return true; } catch (IOException e) { return false; } } } - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + plugin.getGameServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { try { @@ -96,11 +97,6 @@ public class Utils { return false; } - /** - * Method getCountryCode. - * @param ip String - - * @return String */ public static String getCountryCode(String ip) { if (checkGeoIP()) { return lookupService.getCountry(ip).getCode(); @@ -108,11 +104,6 @@ public class Utils { return "--"; } - /** - * Method getCountryName. - * @param ip String - - * @return String */ public static String getCountryName(String ip) { if (checkGeoIP()) { return lookupService.getCountry(ip).getName(); @@ -121,7 +112,7 @@ public class Utils { } /** - * Set the group of a player, by it's AuthMe group type. + * Set the group of a player, by its AuthMe group type. * * @param player The player. * @param group The group type. @@ -206,11 +197,6 @@ public class Utils { } // TODO: Move to a Manager - /** - * Method checkAuth. - * @param player Player - - * @return boolean */ public static boolean checkAuth(Player player) { if (player == null || Utils.isUnrestricted(player)) { return true; @@ -230,20 +216,11 @@ public class Utils { return false; } - /** - * Method isUnrestricted. - * @param player Player - - * @return boolean */ public static boolean isUnrestricted(Player player) { return Settings.isAllowRestrictedIp && !Settings.getUnrestrictedName.isEmpty() && (Settings.getUnrestrictedName.contains(player.getName())); } - /** - * Method useGroupSystem. - - * @return boolean */ private static boolean useGroupSystem() { return Settings.isPermissionCheckEnabled && !Settings.getUnloggedinGroup.isEmpty(); } @@ -286,12 +263,14 @@ public class Utils { * Used for force player GameMode */ /** - * Method forceGM. - * @param player Player + * Force the game mode of a player. + * + * @param player the player to modify. */ public static void forceGM(Player player) { - if (!plugin.getPermissionsManager().hasPermission(player, "authme.bypassforcesurvival")) + if (!plugin.getPermissionsManager().hasPermission(player, "authme.bypassforcesurvival")) { player.setGameMode(GameMode.SURVIVAL); + } } /** @@ -303,10 +282,6 @@ public class Utils { LOGGEDIN } - /** - * Method purgeDirectory. - * @param file File - */ public static void purgeDirectory(File file) { if (!file.isDirectory()) { return; @@ -325,10 +300,6 @@ public class Utils { } } - /** - * Method getOnlinePlayers. - - * @return Collection */ @SuppressWarnings("unchecked") public static Collection getOnlinePlayers() { if (getOnlinePlayersIsCollection) { @@ -348,22 +319,12 @@ public class Utils { return Collections.emptyList(); } - /** - * Method getPlayer. - * @param name String - - * @return Player */ @SuppressWarnings("deprecation") public static Player getPlayer(String name) { name = name.toLowerCase(); return plugin.getServer().getPlayer(name); } - /** - * Method isNPC. - * @param player Entity - - * @return boolean */ public static boolean isNPC(final Entity player) { try { if (player.hasMetadata("NPC")) { @@ -379,10 +340,6 @@ public class Utils { } } - /** - * Method teleportToSpawn. - * @param player Player - */ public static void teleportToSpawn(Player player) { if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { Location spawn = plugin.getSpawnLocation(player); diff --git a/src/test/java/fr/xephi/authme/util/UtilsTest.java b/src/test/java/fr/xephi/authme/util/UtilsTest.java new file mode 100644 index 000000000..85aaf4e2a --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/UtilsTest.java @@ -0,0 +1,73 @@ +package fr.xephi.authme.util; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.AuthMeMockUtil; +import fr.xephi.authme.permission.PermissionsManager; +import org.bukkit.GameMode; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitTask; +import org.junit.Before; +import org.junit.Test; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.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; + +/** + * Test for the {@link Utils} class. + */ +public class UtilsTest { + + private AuthMe authMeMock; + private PermissionsManager permissionsManagerMock; + + @Before + public void setUpMocks() { + AuthMeMockUtil.mockAuthMeInstance(); + authMeMock = AuthMe.getInstance(); + + permissionsManagerMock = mock(PermissionsManager.class); + when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock); + + Server serverMock = mock(Server.class); + when(authMeMock.getGameServer()).thenReturn(serverMock); + + BukkitScheduler schedulerMock = mock(BukkitScheduler.class); + when(serverMock.getScheduler()).thenReturn(schedulerMock); + when(schedulerMock.runTaskAsynchronously(any(Plugin.class), any(Runnable.class))) + .thenReturn(mock(BukkitTask.class)); + } + + @Test + public void shouldForceSurvivalGameMode() { + // given + Player player = mock(Player.class); + given(permissionsManagerMock.hasPermission(player, "authme.bypassforcesurvival")).willReturn(false); + + // when + Utils.forceGM(player); + + // then + verify(player).setGameMode(GameMode.SURVIVAL); + } + + @Test + public void shouldNotForceGameModeForUserWithBypassPermission() { + // given + Player player = mock(Player.class); + given(permissionsManagerMock.hasPermission(player, "authme.bypassforcesurvival")).willReturn(true); + + // when + Utils.forceGM(player); + + // then + verify(player, never()).setGameMode(GameMode.SURVIVAL); + } + +}