#306 Create tests for CommandService and PlayerCommand

This commit is contained in:
ljacqu 2015-12-26 20:58:05 +01:00
parent 27fee3ca64
commit 47e97244a7
5 changed files with 277 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.RandomString;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Wrapper;
import org.bukkit.entity.Player;
import java.util.List;
@ -17,10 +18,12 @@ public class CaptchaCommand extends PlayerCommand {
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
final String playerNameLowerCase = player.getName().toLowerCase();
final String captcha = arguments.get(0);
final AuthMe plugin = AuthMe.getInstance();
Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
PlayerCache playerCache = wrapper.getPlayerCache();
// Command logic
if (PlayerCache.getInstance().isAuthenticated(playerNameLowerCase)) {
if (playerCache.isAuthenticated(playerNameLowerCase)) {
commandService.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
return;
}

View File

@ -0,0 +1,178 @@
package fr.xephi.authme.command;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.Management;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link CommandService}.
*/
public class CommandServiceTest {
private AuthMe authMe;
private CommandMapper commandMapper;
private HelpProvider helpProvider;
private Messages messages;
private CommandService commandService;
@Before
public void setUpService() {
authMe = mock(AuthMe.class);
commandMapper = mock(CommandMapper.class);
helpProvider = mock(HelpProvider.class);
messages = mock(Messages.class);
commandService = new CommandService(authMe, commandMapper, helpProvider, messages);
}
@Test
public void shouldSendMessage() {
// given
CommandSender sender = mock(CommandSender.class);
// when
commandService.send(sender, MessageKey.INVALID_EMAIL);
// then
verify(messages).send(sender, MessageKey.INVALID_EMAIL);
}
@Test
public void shouldSendMessageWithReplacements() {
// given
CommandSender sender = mock(Player.class);
// when
commandService.send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
// then
verify(messages).send(sender, MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "10");
}
@Test
public void shouldMapPartsToCommand() {
// given
CommandSender sender = mock(Player.class);
List<String> commandParts = Arrays.asList("authme", "test", "test2");
FoundCommandResult givenResult = mock(FoundCommandResult.class);
given(commandMapper.mapPartsToCommand(sender, commandParts)).willReturn(givenResult);
// when
FoundCommandResult result = commandService.mapPartsToCommand(sender, commandParts);
// then
assertThat(result, equalTo(givenResult));
verify(commandMapper).mapPartsToCommand(sender, commandParts);
}
@Test
public void shouldOutputMappingError() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult result = mock(FoundCommandResult.class);
// when
commandService.outputMappingError(sender, result);
// then
verify(commandMapper).outputStandardError(sender, result);
}
@Test
@Ignore
public void shouldRunTaskInAsync() {
// given
Runnable runnable = mock(Runnable.class);
// when
commandService.runTaskAsynchronously(runnable);
// then
// TODO ljacqu 20151226: AuthMe#getServer() is final, i.e. not mockable
}
@Test
@Ignore
public void shouldGetDataSource() {
// TODO ljacqu 20151226: Cannot mock calls to fields
}
@Test
public void shouldOutputHelp() {
// given
CommandSender sender = mock(CommandSender.class);
FoundCommandResult result = mock(FoundCommandResult.class);
int options = HelpProvider.SHOW_LONG_DESCRIPTION;
List<String> messages = Arrays.asList("Test message 1", "Other test message", "Third message for test");
given(helpProvider.printHelp(sender, result, options)).willReturn(messages);
// when
commandService.outputHelp(sender, result, options);
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(3)).sendMessage(captor.capture());
assertThat(captor.getAllValues(), equalTo(messages));
}
@Test
public void shouldReturnManagementObject() {
// given
Management management = mock(Management.class);
given(authMe.getManagement()).willReturn(management);
// when
Management result = commandService.getManagement();
// then
assertThat(result, equalTo(management));
verify(authMe).getManagement();
}
@Test
public void shouldReturnPermissionsManager() {
// given
PermissionsManager manager = mock(PermissionsManager.class);
given(authMe.getPermissionsManager()).willReturn(manager);
// when
PermissionsManager result = commandService.getPermissionsManager();
// then
assertThat(result, equalTo(manager));
verify(authMe).getPermissionsManager();
}
@Test
public void shouldRetrieveMessage() {
// given
MessageKey key = MessageKey.USAGE_CAPTCHA;
String[] givenMessages = new String[]{"Lorem ipsum...", "Test line test"};
given(messages.retrieve(key)).willReturn(givenMessages);
// when
String[] result = commandService.retrieveMessage(key);
// then
assertThat(result, equalTo(givenMessages));
verify(messages).retrieve(key);
}
}

View File

@ -0,0 +1,87 @@
package fr.xephi.authme.command;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* Test for {@link PlayerCommand}.
*/
public class PlayerCommandTest {
@Test
public void shouldRejectNonPlayerSender() {
// given
CommandSender sender = mock(BlockCommandSender.class);
PlayerCommandImpl command = new PlayerCommandImpl();
// when
command.executeCommand(sender, Collections.EMPTY_LIST, mock(CommandService.class));
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(1)).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("only for players"));
}
@Test
public void shouldCallRunCommandForPlayer() {
// given
Player player = mock(Player.class);
List<String> arguments = Arrays.asList("arg1", "testarg2");
CommandService service = mock(CommandService.class);
PlayerCommandImpl command = new PlayerCommandImpl();
// when
command.executeCommand(player, arguments, service);
// then
verify(player, times(1)).sendMessage("testarg2");
}
@Test
public void shouldRejectNonPlayerAndSendAlternative() {
// given
CommandSender sender = mock(CommandSender.class);
PlayerCommandWithAlt command = new PlayerCommandWithAlt();
// when
command.executeCommand(sender, Collections.EMPTY_LIST, mock(CommandService.class));
// then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(sender, times(1)).sendMessage(captor.capture());
assertThat(captor.getValue(), containsString("use /authme test <command> instead"));
}
private static class PlayerCommandImpl extends PlayerCommand {
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
player.sendMessage(arguments.get(1));
}
}
private static class PlayerCommandWithAlt extends PlayerCommand {
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
throw new IllegalStateException("Should not be called");
}
@Override
public String getAlternativeCommand() {
return "/authme test <command>";
}
}
}

View File

@ -16,6 +16,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@ -29,11 +30,13 @@ import static org.mockito.Mockito.when;
public class CaptchaCommandTest {
private WrapperMock wrapperMock;
private CommandService commandService;
@Before
public void setUpWrapperMock() {
wrapperMock = WrapperMock.createInstance();
Settings.useCaptcha = true;
commandService = mock(CommandService.class);
}
@Test
@ -58,10 +61,10 @@ public class CaptchaCommandTest {
ExecutableCommand command = new CaptchaCommand();
// when
command.executeCommand(player, new ArrayList<String>(), mock(CommandService.class));
command.executeCommand(player, Collections.singletonList("1234"), mock(CommandService.class));
// then
verify(wrapperMock.getMessages()).send(player, MessageKey.USAGE_LOGIN);
verify(commandService).send(player, MessageKey.USAGE_LOGIN);
}
private static Player mockPlayerWithName(String name) {

View File

@ -83,14 +83,13 @@ public class WrapperMock extends Wrapper {
return mocks.get(mockClass) != null;
}
@SuppressWarnings("unchecked")
private <T> T getMock(Class<?> clazz) {
private <T> T getMock(Class<T> clazz) {
Object o = mocks.get(clazz);
if (o == null) {
o = Mockito.mock(clazz);
mocks.put(clazz, o);
}
return (T) o;
return clazz.cast(o);
}