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 org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.is;
7 import static org.mockito.ArgumentCaptor.forClass;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.verify;
11 import net.pterodactylus.irc.Connection;
12 import net.pterodactylus.irc.event.ConnectionEstablished;
14 import com.google.common.eventbus.EventBus;
15 import org.junit.Test;
16 import org.mockito.ArgumentCaptor;
19 * Unit test for {@link ConnectionEstablishHandler}.
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
23 public class ConnectionEstablishHandlerTest {
25 private final EventBus eventBus = mock(EventBus.class);
26 private final Connection connection = mock(Connection.class);
27 private final ConnectionEstablishHandler handler =
28 new ConnectionEstablishHandler(eventBus, connection);
31 public void freshHandlerWillHandleTheCorrectCommands() {
32 for (String command : asList("001", "002", "003", "004")) {
33 assertThat(handler.willHandle(createReply(command)), is(true));
38 public void sendingAllNecessaryCommandsResultsInAnEstablishedConnection() {
39 handler.handleReply(createReply("001"));
40 handler.handleReply(createReply("002"));
41 handler.handleReply(createReply("003"));
42 handler.handleReply(createReply("004"));
43 ArgumentCaptor<ConnectionEstablished> eventCaptor = forClass(
44 ConnectionEstablished.class);
45 verify(eventBus).post(eventCaptor.capture());
46 ConnectionEstablished event = eventCaptor.getValue();
47 assertThat(event.connection(), is(connection));
51 public void afterAnEstablishedConnectionTheHandlerWillNotHandleTheCommandsAnymore() {
52 sendingAllNecessaryCommandsResultsInAnEstablishedConnection();
53 for (String command : asList("001", "002", "003", "004")) {
54 assertThat(handler.willHandle(createReply(command)), is(false));