#411 Forced commands: initial implementation

This commit is contained in:
ljacqu
2016-11-17 21:02:01 +01:00
parent 40cf79d2c1
commit 4214c6dc80
11 changed files with 351 additions and 30 deletions
@@ -4,7 +4,12 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -17,6 +22,7 @@ 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.verify;
/**
* Test for {@link BukkitService}.
@@ -24,10 +30,21 @@ import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class BukkitServiceTest {
private BukkitService bukkitService;
@Mock
private AuthMe authMe;
@Mock
private Settings settings;
@Mock
private Server server;
@Before
public void constructBukkitService() {
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
bukkitService = new BukkitService(authMe, settings);
}
/**
* Checks that {@link BukkitService#getOnlinePlayersIsCollection} is initialized to {@code true} on startup;
@@ -35,11 +52,7 @@ public class BukkitServiceTest {
*/
@Test
public void shouldHavePlayerListAsCollectionMethod() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
// when
// given / when
boolean doesMethodReturnCollection = ReflectionTestUtils
.getFieldValue(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection");
@@ -50,8 +63,6 @@ public class BukkitServiceTest {
@Test
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection", false);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayers",
ReflectionTestUtils.getMethod(BukkitServiceTest.class, "onlinePlayersImpl"));
@@ -63,6 +74,33 @@ public class BukkitServiceTest {
assertThat(players, hasSize(2));
}
@Test
public void shouldDispatchCommand() {
// given
CommandSender sender = mock(CommandSender.class);
String command = "help test abc";
// when
bukkitService.dispatchCommand(sender, command);
// then
verify(server).dispatchCommand(sender, command);
}
@Test
public void shouldDispatchConsoleCommand() {
// given
ConsoleCommandSender consoleSender = mock(ConsoleCommandSender.class);
given(server.getConsoleSender()).willReturn(consoleSender);
String command = "my command";
// when
bukkitService.dispatchConsoleCommand(command);
// then
verify(server).dispatchCommand(consoleSender, command);
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{