From 47e97244a7d2cd05f830459adb9345ea9fbffccb Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 26 Dec 2015 20:58:05 +0100 Subject: [PATCH] #306 Create tests for CommandService and PlayerCommand --- .../executable/captcha/CaptchaCommand.java | 7 +- .../authme/command/CommandServiceTest.java | 178 ++++++++++++++++++ .../authme/command/PlayerCommandTest.java | 87 +++++++++ .../captcha/CaptchaCommandTest.java | 7 +- .../fr/xephi/authme/util/WrapperMock.java | 5 +- 5 files changed, 277 insertions(+), 7 deletions(-) create mode 100644 src/test/java/fr/xephi/authme/command/CommandServiceTest.java create mode 100644 src/test/java/fr/xephi/authme/command/PlayerCommandTest.java diff --git a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java index a68399356..b9e236817 100644 --- a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java @@ -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 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; } diff --git a/src/test/java/fr/xephi/authme/command/CommandServiceTest.java b/src/test/java/fr/xephi/authme/command/CommandServiceTest.java new file mode 100644 index 000000000..7780db7a7 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/CommandServiceTest.java @@ -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 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 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 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); + } +} diff --git a/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java b/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java new file mode 100644 index 000000000..3fcc05ca6 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/PlayerCommandTest.java @@ -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 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 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 captor = ArgumentCaptor.forClass(String.class); + verify(sender, times(1)).sendMessage(captor.capture()); + assertThat(captor.getValue(), containsString("use /authme test instead")); + } + + + private static class PlayerCommandImpl extends PlayerCommand { + @Override + public void runCommand(Player player, List arguments, CommandService commandService) { + player.sendMessage(arguments.get(1)); + } + } + + private static class PlayerCommandWithAlt extends PlayerCommand { + @Override + public void runCommand(Player player, List arguments, CommandService commandService) { + throw new IllegalStateException("Should not be called"); + } + @Override + public String getAlternativeCommand() { + return "/authme test "; + } + } +} diff --git a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java index f10aba6ba..60c72bd31 100644 --- a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java @@ -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(), 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) { diff --git a/src/test/java/fr/xephi/authme/util/WrapperMock.java b/src/test/java/fr/xephi/authme/util/WrapperMock.java index 83ed23cd2..057f38bdb 100644 --- a/src/test/java/fr/xephi/authme/util/WrapperMock.java +++ b/src/test/java/fr/xephi/authme/util/WrapperMock.java @@ -83,14 +83,13 @@ public class WrapperMock extends Wrapper { return mocks.get(mockClass) != null; } - @SuppressWarnings("unchecked") - private T getMock(Class clazz) { + private T getMock(Class clazz) { Object o = mocks.get(clazz); if (o == null) { o = Mockito.mock(clazz); mocks.put(clazz, o); } - return (T) o; + return clazz.cast(o); }