Move getOnlinePlayers() from Utils to BukkitService; delete Wrapper

This commit is contained in:
ljacqu
2016-04-23 15:24:41 +02:00
parent a78e0408c6
commit 59d3bc95c0
26 changed files with 262 additions and 428 deletions
@@ -0,0 +1,66 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Collection;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test for {@link BukkitService}.
*/
@RunWith(MockitoJUnitRunner.class)
public class BukkitServiceTest {
@Mock
private AuthMe authMe;
/**
* Checks that {@link BukkitService#getOnlinePlayersIsCollection} is initialized to {@code true} on startup;
* the test scope is configured with a Bukkit implementation that returns a Collection and not an array.
*/
@Test
public void shouldHavePlayerListAsCollectionMethod() {
// given
BukkitService bukkitService = new BukkitService(authMe);
// when
boolean doesMethodReturnCollection = (Boolean) ReflectionTestUtils
.getFieldValue(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection");
// then
assertThat(doesMethodReturnCollection, equalTo(true));
}
@Test
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
BukkitService bukkitService = new BukkitService(authMe);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection", false);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayers",
ReflectionTestUtils.getMethod(BukkitServiceTest.class, "onlinePlayersImpl"));
// when
Collection<? extends Player> players = bukkitService.getOnlinePlayers();
// then
assertThat(players, hasSize(2));
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{
mock(Player.class), mock(Player.class)
};
}
}
@@ -1,106 +0,0 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.Settings;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Collection;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
/**
* Test for the {@link Utils} class.
*/
public class UtilsTest {
private static AuthMe authMeMock;
/**
* 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 = WrapperMock.createInstance();
authMeMock = wrapperMock.getAuthMe();
TestHelper.setupLogger();
}
@Before
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);
}
@Test
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
@Ignore
// TODO ljacqu 20160206: Running this test with all others results in an error
// because Utils is used elsewhere. The AuthMe field is set in a static block
// so creating the WrapperMock here will have no effect
public void shouldNotAddToNormalGroupIfPermManagerIsNull() {
// given
Settings.isPermissionCheckEnabled = true;
given(authMeMock.getPermissionsManager()).willReturn(null);
Player player = mock(Player.class);
// when
boolean result = Utils.addNormal(player, "test_group");
// then
assertThat(result, equalTo(false));
verify(authMeMock).getPermissionsManager();
}
@Test
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayersIsCollection", false);
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayers",
ReflectionTestUtils.getMethod(UtilsTest.class, "onlinePlayersImpl"));
// when
Collection<? extends Player> players = Utils.getOnlinePlayers();
// then
assertThat(players, hasSize(2));
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{
mock(Player.class), mock(Player.class)
};
}
}
@@ -1,90 +0,0 @@
package fr.xephi.authme.util;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.output.Messages;
import org.bukkit.Server;
import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Mockito;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Class returning mocks for all calls in {@link Wrapper}.
* This class keeps track of its mocks and will always return
* the same one for each type.
*/
public class WrapperMock extends Wrapper {
private Map<Class<?>, Object> mocks = new HashMap<>();
private WrapperMock() {
super();
}
/**
* Create a new instance of the WrapperMock and inject it as singleton into the Wrapper class.
*
* @return The created singleton
*/
public static WrapperMock createInstance() {
WrapperMock instance = new WrapperMock();
Wrapper.setSingleton(instance);
return instance;
}
@Override
public Server getServer() {
return getMock(Server.class);
}
@Override
public AuthMe getAuthMe() {
return getMock(AuthMe.class);
}
@Override
public BukkitScheduler getScheduler() {
return getMock(BukkitScheduler.class);
}
@Override
public Messages getMessages() {
return getMock(Messages.class);
}
@Override
public PlayerCache getPlayerCache() {
return getMock(PlayerCache.class);
}
@Override
public File getDataFolder() {
return new File("/");
}
/**
* Return whether a mock of the given class type was created, i.e. verify whether a certain method was executed on
* the Wrapper to retrieve an entity.
*
* @param mockClass The class of the mock to verify
*
* @return True if the mock has been created, false otherwise
*/
public boolean wasMockCalled(Class<?> mockClass) {
return mocks.get(mockClass) != null;
}
private <T> T getMock(Class<T> clazz) {
Object o = mocks.get(clazz);
if (o == null) {
o = Mockito.mock(clazz);
mocks.put(clazz, o);
}
return clazz.cast(o);
}
}