1 package net.pterodactylus.irc.connection;
3 import static java.util.Arrays.asList;
4 import static net.pterodactylus.irc.connection.Replies.createReply;
5 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey;
6 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
7 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
8 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
9 import static org.hamcrest.MatcherAssert.assertThat;
10 import static org.hamcrest.Matchers.is;
11 import static org.mockito.ArgumentCaptor.forClass;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.event.ChannelNotJoined;
17 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
19 import com.google.common.eventbus.EventBus;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
24 * Unit test for {@link ChannelNotJoinedHandler}.
26 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28 public class ChannelNotJoinedHandlerTest {
30 private final EventBus eventBus = mock(EventBus.class);
31 private final Connection connection = mock(Connection.class);
32 private final ChannelNotJoinedHandler handler =
33 new ChannelNotJoinedHandler(eventBus, connection);
36 public void handlerWillOnlyReactToSpecificCommands() {
37 for (String command : asList("473", "474", "475", "477")) {
38 assertThat(handler.willHandle(createReply(command)), is(true));
43 public void handlerWillNotWantToHandleOtherCommands() {
44 for (String command : asList("PRIVMSG", "NOTICE", "JOIN", "KICK",
45 "PART", "QUIT", "331", "433")) {
46 assertThat(handler.willHandle(createReply(command)), is(false));
51 public void bannedChannelNotJoinedIsRecognizedCorrectly() {
52 handler.handleReply(createReply("474", "#test"));
53 verifyReasonIs(banned);
56 private void verifyReasonIs(Reason reason) {
57 ArgumentCaptor<ChannelNotJoined> channelNotJoined = forClass(
58 ChannelNotJoined.class);
59 verify(eventBus).post(channelNotJoined.capture());
60 assertThat(channelNotJoined.getValue().channel(), is("#test"));
61 assertThat(channelNotJoined.getValue().reason(), is(reason));
62 assertThat(channelNotJoined.getValue().connection(), is(connection));
66 public void inviteOnlyChannelNotJoinedIsRecognizedCorrectly() {
67 handler.handleReply(createReply("473", "#test"));
68 verifyReasonIs(inviteOnly);
72 public void channelWithBadChannelKeyNotJoinedIsRecognizedCorrectly() {
73 handler.handleReply(createReply("475", "#test"));
74 verifyReasonIs(badChannelKey);
78 public void channelRequiringRegisteredNicknamesNotJoinedIsRecognizedCorrectly() {
79 handler.handleReply(createReply("477", "#test"));
80 verifyReasonIs(registeredNicknamesOnly);