### Question:
LimboPlayerTaskManager { void registerMessageTask(Player player, LimboPlayer limbo, boolean isRegistered) { int interval = settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL); MessageResult result = getMessageKey(player.getName(), isRegistered); if (interval > 0) { String[] joinMessage = messages.retrieveSingle(player, result.messageKey, result.args).split("\n"); MessageTask messageTask = new MessageTask(player, joinMessage); bukkitService.runTaskTimer(messageTask, 2 * TICKS_PER_SECOND, interval * TICKS_PER_SECOND); limbo.setMessageTask(messageTask); } } LimboPlayerTaskManager();   }### Answer:
@Test public void shouldRegisterMessageTask() { Player player = mock(Player.class); LimboPlayer limboPlayer = mock(LimboPlayer.class); MessageKey key = MessageKey.REGISTER_MESSAGE; given(messages.retrieveSingle(player, key)).willReturn("Please register!"); int interval = 12; given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(interval); limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false); verify(limboPlayer).setMessageTask(any(MessageTask.class)); verify(messages).retrieveSingle(player, key); verify(bukkitService).runTaskTimer( any(MessageTask.class), eq(2L * TICKS_PER_SECOND), eq((long) interval * TICKS_PER_SECOND)); }
@Test public void shouldNotScheduleTaskForZeroAsInterval() { Player player = mock(Player.class); LimboPlayer limboPlayer = mock(LimboPlayer.class); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(0); limboPlayerTaskManager.registerMessageTask(player, limboPlayer, true); verifyZeroInteractions(limboPlayer, bukkitService); }
@Test public void shouldCancelExistingMessageTask() { String name = "rats"; Player player = mock(Player.class); given(player.getName()).willReturn(name); LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList("grp"), false, 0.1f, 0.0f); MessageTask existingMessageTask = mock(MessageTask.class); limboPlayer.setMessageTask(existingMessageTask); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8); given(messages.retrieveSingle(player, MessageKey.REGISTER_MESSAGE)).willReturn("Please register!"); limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false); assertThat(limboPlayer.getMessageTask(), not(nullValue())); assertThat(limboPlayer.getMessageTask(), not(sameInstance(existingMessageTask))); verify(registrationCaptchaManager).isCaptchaRequired(name); verify(messages).retrieveSingle(player, MessageKey.REGISTER_MESSAGE); verify(existingMessageTask).cancel(); }
@Test public void shouldInitializeMessageTaskWithCaptchaMessage() { String name = "race"; Player player = mock(Player.class); given(player.getName()).willReturn(name); LimboPlayer limboPlayer = new LimboPlayer(null, true, Collections.singletonList("grp"), false, 0.1f, 0.0f); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(12); given(registrationCaptchaManager.isCaptchaRequired(name)).willReturn(true); String captcha = "M032"; given(registrationCaptchaManager.getCaptchaCodeOrGenerateNew(name)).willReturn(captcha); given(messages.retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha)).willReturn("Need to use captcha"); limboPlayerTaskManager.registerMessageTask(player, limboPlayer, false); assertThat(limboPlayer.getMessageTask(), not(nullValue())); verify(messages).retrieveSingle(player, MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, captcha); }