From b916a38d80adf9e6d68bcb1eaa3c4690c607960a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 28 Nov 2015 17:13:33 +0100 Subject: [PATCH] Fix UtilsTest and replace the last test setups not to use reflections --- .../executable/logout/LogoutCommand.java | 3 +- src/main/java/fr/xephi/authme/util/Utils.java | 12 ++++--- .../executable/logout/LogoutCommandTest.java | 9 +++-- .../java/fr/xephi/authme/util/UtilsTest.java | 33 ++++++++++++++----- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java b/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java index 4d4af6039..e1918d071 100644 --- a/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/logout/LogoutCommand.java @@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.logout; import fr.xephi.authme.AuthMe; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.util.Wrapper; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -18,7 +19,7 @@ public class LogoutCommand extends ExecutableCommand { } // Get the player instance - final AuthMe plugin = AuthMe.getInstance(); + final AuthMe plugin = Wrapper.getInstance().getAuthMe(); final Player player = (Player) sender; // Logout the player diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index 90e556a45..2d19ded45 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -53,18 +53,22 @@ public final class Utils { */ public static boolean setGroup(Player player, GroupType group) { // Check whether the permissions check is enabled - if (!Settings.isPermissionCheckEnabled) + if (!Settings.isPermissionCheckEnabled) { return false; + } // Get the permissions manager, and make sure it's valid PermissionsManager permsMan = plugin.getPermissionsManager(); - if (permsMan == null) + if (permsMan == null) { ConsoleLogger.showError("Failed to access permissions manager instance, shutting down."); - assert permsMan != null; + return false; + } // Make sure group support is available - if (!permsMan.hasGroupSupport()) + if (!permsMan.hasGroupSupport()) { ConsoleLogger.showError("The current permissions system doesn't have group support, unable to set group!"); + return false; + } switch (group) { case UNREGISTERED: diff --git a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java index 8416b5fdc..a08784814 100644 --- a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java @@ -1,10 +1,10 @@ package fr.xephi.authme.command.executable.logout; import fr.xephi.authme.AuthMe; -import fr.xephi.authme.AuthMeMockUtil; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.process.Management; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.WrapperMock; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -12,6 +12,8 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.io.File; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -25,8 +27,9 @@ public class LogoutCommandTest { @Before public void initializeAuthMeMock() { - AuthMeMockUtil.mockAuthMeInstance(); - AuthMe pluginMock = AuthMe.getInstance(); + WrapperMock wrapper = WrapperMock.createInstance(); + wrapper.setDataFolder(new File("/")); + AuthMe pluginMock = wrapper.getAuthMe(); Settings.captchaLength = 10; managementMock = mock(Management.class); diff --git a/src/test/java/fr/xephi/authme/util/UtilsTest.java b/src/test/java/fr/xephi/authme/util/UtilsTest.java index 1b1ab4a58..57b3fc75a 100644 --- a/src/test/java/fr/xephi/authme/util/UtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/UtilsTest.java @@ -8,10 +8,12 @@ import fr.xephi.authme.settings.Settings; import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +import java.io.File; import java.lang.reflect.Field; import java.util.Collection; import java.util.logging.Logger; @@ -25,18 +27,33 @@ import static org.mockito.Mockito.*; /** * Test for the {@link Utils} class. */ -@Ignore -// TODO ljacqu 20151126: Fix tests public class UtilsTest { - private AuthMe authMeMock; + private static WrapperMock wrapperMock; + private static AuthMe authMeMock; private PermissionsManager permissionsManagerMock; + /** + * The Utils class initializes its fields in a {@code static} block which is only executed once during the JUnit + * tests, too. It is therefore important to initialize the mocks once with {@code @BeforeClass}. Initializing with + * {@code @Before} as we usually do will create mocks that won't have any use in the Utils class. + */ + @BeforeClass + public static void setUpMocks() { + wrapperMock = WrapperMock.createInstance(); + wrapperMock.setDataFolder(new File("/")); + authMeMock = wrapperMock.getAuthMe(); + } + @Before - public void setUpMocks() { - WrapperMock w = WrapperMock.getInstance(); - authMeMock = w.getAuthMe(); - permissionsManagerMock = Mockito.mock(PermissionsManager.class); + public void setIndirectMocks() { + // Since the mocks aren't set up for each test case it is important to reset them when verifying whether or not + // they have been called. We want to return null for permissions manager once so we initialize a mock for it + // before every test -- this is OK because it is retrieved via authMeMock. It is just crucial that authMeMock + // remain the same object. + reset(authMeMock); + + permissionsManagerMock = mock(PermissionsManager.class); when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock); } @@ -89,7 +106,6 @@ public class UtilsTest { Settings.isPermissionCheckEnabled = true; given(authMeMock.getPermissionsManager()).willReturn(null); Player player = mock(Player.class); - AuthMeMockUtil.mockSingletonForClass(ConsoleLogger.class, "wrapper", Wrapper.getInstance()); // when boolean result = Utils.addNormal(player, "test_group"); @@ -128,6 +144,7 @@ public class UtilsTest { } // Note: This method is used through reflections + @SuppressWarnings("unused") public static Player[] onlinePlayersImpl() { return new Player[]{ mock(Player.class), mock(Player.class)