Create Wrapper as singleton; fix UtilsTest

This commit is contained in:
ljacqu 2015-11-25 22:34:27 +01:00
parent 43a60dc091
commit 498c3342f2
8 changed files with 81 additions and 57 deletions

View File

@ -17,7 +17,7 @@ public class AntiBot {
private static final AuthMe plugin = AuthMe.getInstance(); private static final AuthMe plugin = AuthMe.getInstance();
private static final Messages messages = plugin.getMessages(); private static final Messages messages = plugin.getMessages();
private static Wrapper wrapper = new Wrapper(plugin); private static Wrapper wrapper = Wrapper.getInstance();
private static final List<String> antibotPlayers = new ArrayList<>(); private static final List<String> antibotPlayers = new ArrayList<>();
private static AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED; private static AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED;

View File

@ -17,7 +17,7 @@ import java.util.Date;
*/ */
public final class ConsoleLogger { public final class ConsoleLogger {
private static Wrapper wrapper = new Wrapper(AuthMe.getInstance()); private static Wrapper wrapper = Wrapper.getInstance();
private static final DateFormat df = new SimpleDateFormat("[MM-dd HH:mm:ss]"); private static final DateFormat df = new SimpleDateFormat("[MM-dd HH:mm:ss]");
private ConsoleLogger() { private ConsoleLogger() {

View File

@ -17,7 +17,6 @@ public class GeoLiteAPI {
"available at http://www.maxmind.com"; "available at http://www.maxmind.com";
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry" + private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry" +
"/GeoIP.dat.gz"; "/GeoIP.dat.gz";
private static final Wrapper wrapper = new Wrapper(AuthMe.getInstance());
private static final AuthMe plugin = AuthMe.getInstance(); private static final AuthMe plugin = AuthMe.getInstance();
private static LookupService lookupService; private static LookupService lookupService;

View File

@ -26,7 +26,7 @@ import java.util.Collections;
*/ */
public final class Utils { public final class Utils {
private static final AuthMe plugin; private static AuthMe plugin;
private static Wrapper wrapper; private static Wrapper wrapper;
private static boolean getOnlinePlayersIsCollection = false; private static boolean getOnlinePlayersIsCollection = false;
@ -34,7 +34,7 @@ public final class Utils {
static { static {
plugin = AuthMe.getInstance(); plugin = AuthMe.getInstance();
wrapper = new Wrapper(plugin); wrapper = Wrapper.getInstance();
initializeOnlinePlayersIsCollectionField(); initializeOnlinePlayersIsCollectionField();
} }
@ -116,9 +116,10 @@ public final class Utils {
// 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, aborting.");
assert permsMan != null; return false;
}
// Remove old groups // Remove old groups
permsMan.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup, permsMan.removeGroups(player, Arrays.asList(Settings.unRegisteredGroup,

View File

@ -13,22 +13,31 @@ import java.util.logging.Logger;
*/ */
public class Wrapper { public class Wrapper {
private AuthMe authMe; private static Wrapper singleton;
public Wrapper(AuthMe authMe) { /**
this.authMe = authMe; * Package-private constructor for testing purposes to inject a mock instance.
*/
Wrapper() {
}
public static Wrapper getInstance() {
if (singleton == null) {
singleton = new Wrapper();
}
return singleton;
} }
public AuthMe getAuthMe() { public AuthMe getAuthMe() {
return authMe; return AuthMe.getInstance();
} }
public Server getServer() { public Server getServer() {
return authMe.getServer(); return getAuthMe().getServer();
} }
public Logger getLogger() { public Logger getLogger() {
return authMe.getLogger(); return getAuthMe().getLogger();
} }
public BukkitScheduler getScheduler() { public BukkitScheduler getScheduler() {

View File

@ -3,6 +3,7 @@ package fr.xephi.authme;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Messages;
import fr.xephi.authme.util.Wrapper; import fr.xephi.authme.util.Wrapper;
import fr.xephi.authme.util.WrapperMock;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -43,6 +44,11 @@ public final class AuthMeMockUtil {
mockSingletonForClass(PlayerCache.class, "singleton", mock); mockSingletonForClass(PlayerCache.class, "singleton", mock);
} }
public static void initializeWrapperMock() {
WrapperMock wrapper = new WrapperMock();
mockSingletonForClass(Wrapper.class, "singleton", wrapper);
}
/** /**
* Set the given class' {@link Wrapper} field to a mock implementation. * Set the given class' {@link Wrapper} field to a mock implementation.
* *
@ -59,7 +65,7 @@ public final class AuthMeMockUtil {
} }
public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName, AuthMe authMe) { public static Wrapper insertMockWrapperInstance(Class<?> clazz, String fieldName, AuthMe authMe) {
Wrapper wrapperMock = new WrapperMock(authMe); Wrapper wrapperMock = new WrapperMock();
mockSingletonForClass(clazz, fieldName, wrapperMock); mockSingletonForClass(clazz, fieldName, wrapperMock);
return wrapperMock; return wrapperMock;
} }
@ -78,7 +84,7 @@ public final class AuthMeMockUtil {
* @param fieldName The field name * @param fieldName The field name
* @param mock The mock to set for the given field * @param mock The mock to set for the given field
*/ */
private static void mockSingletonForClass(Class<?> clazz, String fieldName, Object mock) { public static void mockSingletonForClass(Class<?> clazz, String fieldName, Object mock) {
try { try {
Field instance = clazz.getDeclaredField(fieldName); Field instance = clazz.getDeclaredField(fieldName);
instance.setAccessible(true); instance.setAccessible(true);

View File

@ -2,63 +2,44 @@ package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil; import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.WrapperMock; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.Server; import fr.xephi.authme.settings.Settings;
import org.bukkit.GameMode;
import org.bukkit.entity.Player; 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.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Collection; import java.util.Collection;
import java.util.logging.Logger;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;
/** /**
* Test for the {@link Utils} class. * Test for the {@link Utils} class.
*/ */
@Ignore
// TODO ljacqu 20151123: Fix test setup
public class UtilsTest { public class UtilsTest {
private AuthMe authMeMock; private AuthMe authMeMock;
private PermissionsManager permissionsManagerMock; private PermissionsManager permissionsManagerMock;
private Wrapper wrapperMock;
@Before @Before
public void setUpMocks() { public void setUpMocks() {
authMeMock = AuthMeMockUtil.mockAuthMeInstance(); authMeMock = AuthMeMockUtil.mockAuthMeInstance();
AuthMeMockUtil.mockSingletonForClass(Utils.class, "plugin", authMeMock);
// We need to create the Wrapper mock before injecting it into Utils because it runs a lot of code in permissionsManagerMock = Mockito.mock(PermissionsManager.class);
// a static block which needs the proper mocks to be set up.
wrapperMock = new WrapperMock(authMeMock);
Server serverMock = wrapperMock.getServer();
BukkitScheduler schedulerMock = mock(BukkitScheduler.class);
when(serverMock.getScheduler()).thenReturn(schedulerMock);
when(schedulerMock.runTaskAsynchronously(any(Plugin.class), any(Runnable.class)))
.thenReturn(mock(BukkitTask.class));
System.out.println("Initialized scheduler mock for server mock");
AuthMeMockUtil.insertMockWrapperInstance(Utils.class, "wrapper", (WrapperMock) wrapperMock);
System.out.println("Iniadfk");
permissionsManagerMock = mock(PermissionsManager.class);
when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock); when(authMeMock.getPermissionsManager()).thenReturn(permissionsManagerMock);
AuthMeMockUtil.initializeWrapperMock();
} }
// TODO ljacques 20151122: The tests for Utils.forceGM somehow can't be set up with the mocks correctly @Test
/*@Test
public void shouldForceSurvivalGameMode() { public void shouldForceSurvivalGameMode() {
// given // given
Player player = mock(Player.class); Player player = mock(Player.class);
@ -68,6 +49,7 @@ public class UtilsTest {
Utils.forceGM(player); Utils.forceGM(player);
// then // then
verify(authMeMock).getPermissionsManager();
verify(player).setGameMode(GameMode.SURVIVAL); verify(player).setGameMode(GameMode.SURVIVAL);
} }
@ -84,10 +66,40 @@ public class UtilsTest {
verify(authMeMock).getPermissionsManager(); verify(authMeMock).getPermissionsManager();
verify(permissionsManagerMock).hasPermission(player, "authme.bypassforcesurvival"); verify(permissionsManagerMock).hasPermission(player, "authme.bypassforcesurvival");
verify(player, never()).setGameMode(any(GameMode.class)); verify(player, never()).setGameMode(any(GameMode.class));
}*/ }
@Test @Test
// Note ljacqu 20151122: This is a heavy test setup with Reflections... If it causes trouble, skip it with @Ignore public void shouldNotAddToNormalGroupIfPermissionsAreDisabled() {
// given
Settings.isPermissionCheckEnabled = false;
Player player = mock(Player.class);
// when
boolean result = Utils.addNormal(player, "test_group");
// then
assertThat(result, equalTo(false));
verify(authMeMock, never()).getPermissionsManager();
}
@Test
public void shouldNotAddToNormalGroupIfPermManagerIsNull() {
// given
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");
// then
assertThat(result, equalTo(false));
verify(authMeMock).getPermissionsManager();
}
@Test
// Note ljacqu 20151122: This is a heavy test setup with reflections... If it causes trouble, skip it with @Ignore
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() { public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given // given
setField("getOnlinePlayersIsCollection", false); setField("getOnlinePlayersIsCollection", false);
@ -114,6 +126,7 @@ public class UtilsTest {
} }
} }
// Note: This method is used through reflections
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)

View File

@ -1,6 +1,6 @@
package fr.xephi.authme; package fr.xephi.authme.util;
import fr.xephi.authme.util.Wrapper; import fr.xephi.authme.AuthMe;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -19,11 +19,7 @@ public class WrapperMock extends Wrapper {
private static Map<Class<?>, Object> mocks = new HashMap<>(); private static Map<Class<?>, Object> mocks = new HashMap<>();
public WrapperMock() { public WrapperMock() {
this((AuthMe) getMock(AuthMe.class)); super();
}
public WrapperMock(AuthMe authMe) {
super(authMe);
} }
@Override @Override