Fix UtilsTest and replace the last test setups not to use reflections

This commit is contained in:
ljacqu 2015-11-28 17:13:33 +01:00
parent 67244d5e7b
commit b916a38d80
4 changed files with 41 additions and 16 deletions

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -18,7 +19,7 @@ public class LogoutCommand extends ExecutableCommand {
} }
// Get the player instance // Get the player instance
final AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = Wrapper.getInstance().getAuthMe();
final Player player = (Player) sender; final Player player = (Player) sender;
// Logout the player // Logout the player

View File

@ -53,18 +53,22 @@ public final class Utils {
*/ */
public static boolean setGroup(Player player, GroupType group) { public static boolean setGroup(Player player, GroupType group) {
// Check whether the permissions check is enabled // Check whether the permissions check is enabled
if (!Settings.isPermissionCheckEnabled) if (!Settings.isPermissionCheckEnabled) {
return false; return false;
}
// Get the permissions manager, and make sure it's valid // Get the permissions manager, and make sure it's valid
PermissionsManager permsMan = plugin.getPermissionsManager(); PermissionsManager permsMan = plugin.getPermissionsManager();
if (permsMan == null) if (permsMan == null) {
ConsoleLogger.showError("Failed to access permissions manager instance, shutting down."); ConsoleLogger.showError("Failed to access permissions manager instance, shutting down.");
assert permsMan != null; return false;
}
// Make sure group support is available // 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!"); ConsoleLogger.showError("The current permissions system doesn't have group support, unable to set group!");
return false;
}
switch (group) { switch (group) {
case UNREGISTERED: case UNREGISTERED:

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.logout; package fr.xephi.authme.command.executable.logout;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.process.Management; import fr.xephi.authme.process.Management;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -12,6 +12,8 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.io.File;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
@ -25,8 +27,9 @@ public class LogoutCommandTest {
@Before @Before
public void initializeAuthMeMock() { public void initializeAuthMeMock() {
AuthMeMockUtil.mockAuthMeInstance(); WrapperMock wrapper = WrapperMock.createInstance();
AuthMe pluginMock = AuthMe.getInstance(); wrapper.setDataFolder(new File("/"));
AuthMe pluginMock = wrapper.getAuthMe();
Settings.captchaLength = 10; Settings.captchaLength = 10;
managementMock = mock(Management.class); managementMock = mock(Management.class);

View File

@ -8,10 +8,12 @@ import fr.xephi.authme.settings.Settings;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.io.File;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Collection; import java.util.Collection;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -25,18 +27,33 @@ import static org.mockito.Mockito.*;
/** /**
* Test for the {@link Utils} class. * Test for the {@link Utils} class.
*/ */
@Ignore
// TODO ljacqu 20151126: Fix tests
public class UtilsTest { public class UtilsTest {
private AuthMe authMeMock; private static WrapperMock wrapperMock;
private static AuthMe authMeMock;
private PermissionsManager permissionsManagerMock; 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 @Before
public void setUpMocks() { public void setIndirectMocks() {
WrapperMock w = WrapperMock.getInstance(); // Since the mocks aren't set up for each test case it is important to reset them when verifying whether or not
authMeMock = w.getAuthMe(); // they have been called. We want to return null for permissions manager once so we initialize a mock for it
permissionsManagerMock = Mockito.mock(PermissionsManager.class); // 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); when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock);
} }
@ -89,7 +106,6 @@ public class UtilsTest {
Settings.isPermissionCheckEnabled = true; Settings.isPermissionCheckEnabled = true;
given(authMeMock.getPermissionsManager()).willReturn(null); given(authMeMock.getPermissionsManager()).willReturn(null);
Player player = mock(Player.class); Player player = mock(Player.class);
AuthMeMockUtil.mockSingletonForClass(ConsoleLogger.class, "wrapper", Wrapper.getInstance());
// when // when
boolean result = Utils.addNormal(player, "test_group"); boolean result = Utils.addNormal(player, "test_group");
@ -128,6 +144,7 @@ public class UtilsTest {
} }
// Note: This method is used through reflections // Note: This method is used through reflections
@SuppressWarnings("unused")
public static Player[] onlinePlayersImpl() { public static Player[] onlinePlayersImpl() {
return new Player[]{ return new Player[]{
mock(Player.class), mock(Player.class) mock(Player.class), mock(Player.class)