From e24cf5ee1b6f06840e4307abb17549856ee09297 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 17 Oct 2014 19:32:06 +0200 Subject: [PATCH] Add unit test for the channel-not-joined handler. --- .../connection/ChannelNotJoinedHandlerTest.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/test/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandlerTest.java diff --git a/src/test/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandlerTest.java b/src/test/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandlerTest.java new file mode 100644 index 0000000..3c27ebc --- /dev/null +++ b/src/test/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandlerTest.java @@ -0,0 +1,96 @@ +package net.pterodactylus.irc.connection; + +import static java.util.Arrays.asList; +import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey; +import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned; +import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly; +import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentCaptor.forClass; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import net.pterodactylus.irc.Connection; +import net.pterodactylus.irc.Reply; +import net.pterodactylus.irc.event.ChannelNotJoined; +import net.pterodactylus.irc.event.ChannelNotJoined.Reason; + +import com.google.common.eventbus.EventBus; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +/** + * Unit test for {@link ChannelNotJoinedHandler}. + * + * @author David ‘Bombe’ Roden + */ +public class ChannelNotJoinedHandlerTest { + + private final EventBus eventBus = mock(EventBus.class); + private final Connection connection = mock(Connection.class); + private final ChannelNotJoinedHandler handler = + new ChannelNotJoinedHandler(eventBus, connection); + + @Test + public void handlerWillOnlyReactToSpecificCommands() { + for (String command : asList("473", "474", "475", "477")) { + assertThat(handler.willHandle(createReply(command)), is(true)); + } + } + + @Test + public void handlerWillNotWantToHandleOtherCommands() { + for (String command : asList("PRIVMSG", "NOTICE", "JOIN", "KICK", + "PART", "QUIT", "331", "433")) { + assertThat(handler.willHandle(createReply(command)), is(false)); + } + } + + private Reply createReply(String command) { + Reply reply = mock(Reply.class); + return when(reply.command()).thenReturn(command).getMock(); + } + + @Test + public void bannedChannelNotJoinedIsRecognizedCorrectly() { + handler.handleReply(createReply("474", "#test")); + verifyReasonIs(banned); + } + + private void verifyReasonIs(Reason reason) { + ArgumentCaptor channelNotJoined = forClass( + ChannelNotJoined.class); + verify(eventBus).post(channelNotJoined.capture()); + assertThat(channelNotJoined.getValue().channel(), is("#test")); + assertThat(channelNotJoined.getValue().reason(), is(reason)); + assertThat(channelNotJoined.getValue().connection(), is(connection)); + } + + @Test + public void inviteOnlyChannelNotJoinedIsRecognizedCorrectly() { + handler.handleReply(createReply("473", "#test")); + verifyReasonIs(inviteOnly); + } + + @Test + public void channelWithBadChannelKeyNotJoinedIsRecognizedCorrectly() { + handler.handleReply(createReply("475", "#test")); + verifyReasonIs(badChannelKey); + } + + @Test + public void channelRequiringRegisteredNicknamesNotJoinedIsRecognizedCorrectly() { + handler.handleReply(createReply("477", "#test")); + verifyReasonIs(registeredNicknamesOnly); + } + + private Reply createReply(String command, String channel) { + final Reply reply = mock(Reply.class); + when(reply.command()).thenReturn(command); + when(reply.parameters()).thenReturn(asList(":some.server", channel)); + return reply; + } + +} -- 2.7.4