1 package net.pterodactylus.irc.connection;
3 import static java.util.Arrays.asList;
4 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey;
5 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
6 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
7 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
8 import static org.hamcrest.MatcherAssert.assertThat;
9 import static org.hamcrest.Matchers.is;
10 import static org.mockito.ArgumentCaptor.forClass;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
15 import net.pterodactylus.irc.Connection;
16 import net.pterodactylus.irc.Reply;
17 import net.pterodactylus.irc.event.ChannelNotJoined;
18 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
20 import com.google.common.eventbus.EventBus;
21 import org.junit.Test;
22 import org.mockito.ArgumentCaptor;
25 * Unit test for {@link ChannelNotJoinedHandler}.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class ChannelNotJoinedHandlerTest {
31 private final EventBus eventBus = mock(EventBus.class);
32 private final Connection connection = mock(Connection.class);
33 private final ChannelNotJoinedHandler handler =
34 new ChannelNotJoinedHandler(eventBus, connection);
37 public void handlerWillOnlyReactToSpecificCommands() {
38 for (String command : asList("473", "474", "475", "477")) {
39 assertThat(handler.willHandle(createReply(command)), is(true));
44 public void handlerWillNotWantToHandleOtherCommands() {
45 for (String command : asList("PRIVMSG", "NOTICE", "JOIN", "KICK",
46 "PART", "QUIT", "331", "433")) {
47 assertThat(handler.willHandle(createReply(command)), is(false));
51 private Reply createReply(String command) {
52 Reply reply = mock(Reply.class);
53 return when(reply.command()).thenReturn(command).getMock();
57 public void bannedChannelNotJoinedIsRecognizedCorrectly() {
58 handler.handleReply(createReply("474", "#test"));
59 verifyReasonIs(banned);
62 private void verifyReasonIs(Reason reason) {
63 ArgumentCaptor<ChannelNotJoined> channelNotJoined = forClass(
64 ChannelNotJoined.class);
65 verify(eventBus).post(channelNotJoined.capture());
66 assertThat(channelNotJoined.getValue().channel(), is("#test"));
67 assertThat(channelNotJoined.getValue().reason(), is(reason));
68 assertThat(channelNotJoined.getValue().connection(), is(connection));
72 public void inviteOnlyChannelNotJoinedIsRecognizedCorrectly() {
73 handler.handleReply(createReply("473", "#test"));
74 verifyReasonIs(inviteOnly);
78 public void channelWithBadChannelKeyNotJoinedIsRecognizedCorrectly() {
79 handler.handleReply(createReply("475", "#test"));
80 verifyReasonIs(badChannelKey);
84 public void channelRequiringRegisteredNicknamesNotJoinedIsRecognizedCorrectly() {
85 handler.handleReply(createReply("477", "#test"));
86 verifyReasonIs(registeredNicknamesOnly);
89 private Reply createReply(String command, String channel) {
90 final Reply reply = mock(Reply.class);
91 when(reply.command()).thenReturn(command);
92 when(reply.parameters()).thenReturn(asList(":some.server", channel));