diff --git a/src/main/java/fr/xephi/authme/task/MessageTask.java b/src/main/java/fr/xephi/authme/task/MessageTask.java index 86f380fc8..74ef45f1a 100644 --- a/src/main/java/fr/xephi/authme/task/MessageTask.java +++ b/src/main/java/fr/xephi/authme/task/MessageTask.java @@ -1,13 +1,9 @@ package fr.xephi.authme.task; import fr.xephi.authme.data.auth.PlayerCache; -import fr.xephi.authme.data.limbo.LimboCache; import fr.xephi.authme.service.BukkitService; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.scheduler.BukkitTask; - -import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND; /** * Message shown to a player in a regular interval as long as he is not logged in. @@ -41,7 +37,7 @@ public class MessageTask extends BukkitRunnable { cancel(); } - if(isMuted) { + if (isMuted) { return; } 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 cfdc51dc1..f2ba19c54 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 @@ -3,7 +3,10 @@ package fr.xephi.authme.command.executable.captcha; import fr.xephi.authme.data.CaptchaManager; import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.command.CommandService; +import fr.xephi.authme.data.limbo.LimboCache; +import fr.xephi.authme.data.limbo.LimboPlayer; import fr.xephi.authme.message.MessageKey; +import fr.xephi.authme.task.MessageTask; import org.bukkit.entity.Player; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,6 +39,9 @@ public class CaptchaCommandTest { @Mock private CommandService commandService; + @Mock + private LimboCache limboCache; + @Test public void shouldDetectIfPlayerIsLoggedIn() { // given @@ -76,6 +82,10 @@ public class CaptchaCommandTest { given(captchaManager.isCaptchaRequired(name)).willReturn(true); String captchaCode = "3991"; given(captchaManager.checkCode(name, captchaCode)).willReturn(true); + MessageTask messageTask = mock(MessageTask.class); + LimboPlayer limboPlayer = mock(LimboPlayer.class); + given(limboPlayer.getMessageTask()).willReturn(messageTask); + given(limboCache.getPlayerData(name)).willReturn(limboPlayer); // when command.executeCommand(player, Collections.singletonList(captchaCode)); @@ -86,6 +96,7 @@ public class CaptchaCommandTest { verifyNoMoreInteractions(captchaManager); verify(commandService).send(player, MessageKey.CAPTCHA_SUCCESS); verify(commandService).send(player, MessageKey.LOGIN_MESSAGE); + verify(messageTask).setMuted(false); verifyNoMoreInteractions(commandService); } diff --git a/src/test/java/fr/xephi/authme/task/LimboPlayerTaskManagerTest.java b/src/test/java/fr/xephi/authme/task/LimboPlayerTaskManagerTest.java index 681df5fa1..8f0949f15 100644 --- a/src/test/java/fr/xephi/authme/task/LimboPlayerTaskManagerTest.java +++ b/src/test/java/fr/xephi/authme/task/LimboPlayerTaskManagerTest.java @@ -19,6 +19,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyLong; @@ -64,16 +65,18 @@ public class LimboPlayerTaskManagerTest { given(limboCache.getPlayerData(name)).willReturn(limboPlayer); MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE; given(messages.retrieve(key)).willReturn(new String[]{"Please register!"}); - given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(12); + int interval = 12; + given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(interval); given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); // when limboPlayerTaskManager.registerMessageTask(name, false); // then - MessageTask bukkitTask = mock(MessageTask.class); - verify(limboPlayer).setMessageTask(bukkitTask); + verify(limboPlayer).setMessageTask(any(MessageTask.class)); verify(messages).retrieve(key); + verify(bukkitService).runTaskTimer( + any(MessageTask.class), eq(2L * TICKS_PER_SECOND), eq((long) interval * TICKS_PER_SECOND)); } @Test @@ -98,8 +101,6 @@ public class LimboPlayerTaskManagerTest { String name = "Tester1"; LimboPlayer limboPlayer = mock(LimboPlayer.class); given(limboCache.getPlayerData(name)).willReturn(limboPlayer); - BukkitTask bukkiTask = mock(BukkitTask.class); - given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(0); // when @@ -121,7 +122,6 @@ public class LimboPlayerTaskManagerTest { given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE)) .willReturn(new String[]{"Please register", "Use /register"}); - MessageTask bukkiTask = mock(MessageTask.class); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8); given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); @@ -129,7 +129,7 @@ public class LimboPlayerTaskManagerTest { limboPlayerTaskManager.registerMessageTask(name, false); // then - verify(limboPlayer).setMessageTask(bukkiTask); + verify(limboPlayer).setMessageTask(any(MessageTask.class)); verify(messages).retrieve(MessageKey.REGISTER_EMAIL_MESSAGE); verify(existingMessageTask).cancel(); }