#654 Add test verifying nothing is sent for empty message

- Including various test simplifications
This commit is contained in:
ljacqu 2016-04-15 12:27:34 +02:00
parent b825f52a16
commit 6074ba59d5
2 changed files with 23 additions and 31 deletions

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.output;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.WrapperMock;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.junit.Before; import org.junit.Before;
@ -15,11 +14,15 @@ import java.io.File;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeThat; import static org.junit.Assume.assumeThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -34,7 +37,6 @@ public class MessagesIntegrationTest {
@BeforeClass @BeforeClass
public static void setup() { public static void setup() {
WrapperMock.createInstance();
TestHelper.setupLogger(); TestHelper.setupLogger();
} }
@ -61,7 +63,7 @@ public class MessagesIntegrationTest {
String[] message = messages.retrieve(key); String[] message = messages.retrieve(key);
// then // then
String[] lines = new String[]{"This test message", "includes", "some new lines"}; String[] lines = new String[]{"We've got", "new lines", "and ' apostrophes"};
assertThat(message, equalTo(lines)); assertThat(message, equalTo(lines));
} }
@ -74,7 +76,7 @@ public class MessagesIntegrationTest {
String message = messages.retrieveSingle(key); String message = messages.retrieveSingle(key);
// then // then
assertThat(message, equalTo("This test message\nincludes\nsome new lines")); assertThat(message, equalTo("We've got\nnew lines\nand ' apostrophes"));
} }
@Test @Test
@ -91,16 +93,16 @@ public class MessagesIntegrationTest {
} }
@Test @Test
public void shouldRetainApostrophes() { public void shouldNotSendEmptyMessage() {
// given // given
MessageKey key = MessageKey.NOT_LOGGED_IN; MessageKey key = MessageKey.EMAIL_ALREADY_USED_ERROR;
CommandSender sender = mock(CommandSender.class);
// when // when
String[] message = messages.retrieve(key); messages.send(sender, key);
// then // then
assertThat(message, arrayWithSize(1)); verify(sender, never()).sendMessage(anyString());
assertThat(message[0], equalTo("Apostrophes ' should be loaded correctly, don't you think?"));
} }
@Test @Test
@ -126,10 +128,10 @@ public class MessagesIntegrationTest {
messages.send(player, key); messages.send(player, key);
// then // then
String[] lines = new String[]{"This test message", "includes", "some new lines"}; String[] lines = new String[]{"We've got", "new lines", "and ' apostrophes"};
for (String line : lines) { ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(player).sendMessage(line); verify(player, times(3)).sendMessage(captor.capture());
} assertThat(captor.getAllValues(), contains(lines));
} }
@Test @Test
@ -142,14 +144,11 @@ public class MessagesIntegrationTest {
messages.send(sender, key, "1234"); messages.send(sender, key, "1234");
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender, times(1)).sendMessage(argThat(equalTo("Use /captcha 1234 to solve the captcha")));
verify(sender, times(1)).sendMessage(captor.capture());
String message = captor.getValue();
assertThat(message, equalTo("Use /captcha 1234 to solve the captcha"));
} }
@Test @Test
public void shouldNotThrowForKeyWithNoTagReplacements() { public void shouldNotLogErrorForKeyWithNoTagReplacements() {
// given // given
MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR; MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR;
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
@ -158,10 +157,7 @@ public class MessagesIntegrationTest {
messages.send(sender, key); messages.send(sender, key);
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(sender).sendMessage(argThat(equalTo("Use /captcha THE_CAPTCHA to solve the captcha")));
verify(sender, times(1)).sendMessage(captor.capture());
String message = captor.getValue();
assertThat(message, equalTo("Use /captcha THE_CAPTCHA to solve the captcha"));
} }
@Test @Test
@ -175,13 +171,11 @@ public class MessagesIntegrationTest {
messages.send(mock(CommandSender.class), key, "rep", "rep2"); messages.send(mock(CommandSender.class), key, "rep", "rep2");
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger).warning(argThat(containsString("Invalid number of replacements")));
verify(logger).warning(captor.capture());
assertThat(captor.getValue(), containsString("Invalid number of replacements"));
} }
@Test @Test
public void shouldThrowForReplacementsOnKeyWithNoTags() { public void shouldSendErrorForReplacementsOnKeyWithNoTags() {
// given // given
Logger logger = mock(Logger.class); Logger logger = mock(Logger.class);
ConsoleLogger.setLogger(logger); ConsoleLogger.setLogger(logger);
@ -191,9 +185,7 @@ public class MessagesIntegrationTest {
messages.send(mock(CommandSender.class), key, "Replacement"); messages.send(mock(CommandSender.class), key, "Replacement");
// then // then
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger).warning(argThat(containsString("Invalid number of replacements")));
verify(logger).warning(captor.capture());
assertThat(captor.getValue(), containsString("Invalid number of replacements"));
} }
@Test @Test

View File

@ -1,9 +1,9 @@
# Sample messages file # Sample messages file
unknown_user: 'This test message&nincludes&nsome new lines' unknown_user: 'We''ve got&nnew lines&nand '' apostrophes'
unsafe_spawn: '&cHere we have&bdefined some colors &dand some other &lthings' unsafe_spawn: '&cHere we have&bdefined some colors &dand some other &lthings'
not_logged_in: 'Apostrophes '' should be loaded correctly, don''t you think?'
reg_voluntarily: 'You can register yourself to the server with the command "/register <password> <ConfirmPassword>"' reg_voluntarily: 'You can register yourself to the server with the command "/register <password> <ConfirmPassword>"'
usage_log: '&cUsage: /login <password>' usage_log: '&cUsage: /login <password>'
wrong_pwd: '&cWrong password!' wrong_pwd: '&cWrong password!'
wrong_captcha: 'Use /captcha THE_CAPTCHA to solve the captcha' wrong_captcha: 'Use /captcha THE_CAPTCHA to solve the captcha'
email_already_used: ''