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.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

View File

@ -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:

View File

@ -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);

View File

@ -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)