--- /dev/null
+package net.pterodactylus.irc.connection;
+
+import static java.util.Arrays.asList;
+import static net.pterodactylus.irc.connection.Replies.createReply;
+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 net.pterodactylus.irc.Connection;
+import net.pterodactylus.irc.event.ConnectionEstablished;
+
+import com.google.common.eventbus.EventBus;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * Unit test for {@link ConnectionEstablishHandler}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ConnectionEstablishHandlerTest {
+
+ private final EventBus eventBus = mock(EventBus.class);
+ private final Connection connection = mock(Connection.class);
+ private final ConnectionEstablishHandler handler =
+ new ConnectionEstablishHandler(eventBus, connection);
+
+ @Test
+ public void freshHandlerWillHandleTheCorrectCommands() {
+ for (String command : asList("001", "002", "003", "004")) {
+ assertThat(handler.willHandle(createReply(command)), is(true));
+ }
+ }
+
+ @Test
+ public void sendingAllNecessaryCommandsResultsInAnEstablishedConnection() {
+ handler.handleReply(createReply("001"));
+ handler.handleReply(createReply("002"));
+ handler.handleReply(createReply("003"));
+ handler.handleReply(createReply("004"));
+ ArgumentCaptor<ConnectionEstablished> eventCaptor = forClass(
+ ConnectionEstablished.class);
+ verify(eventBus).post(eventCaptor.capture());
+ ConnectionEstablished event = eventCaptor.getValue();
+ assertThat(event.connection(), is(connection));
+ }
+
+ @Test
+ public void afterAnEstablishedConnectionTheHandlerWillNotHandleTheCommandsAnymore() {
+ sendingAllNecessaryCommandsResultsInAnEstablishedConnection();
+ for (String command : asList("001", "002", "003", "004")) {
+ assertThat(handler.willHandle(createReply(command)), is(false));
+ }
+ }
+
+}