Add unit test for connection-established handler.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Oct 2014 14:13:08 +0000 (16:13 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Oct 2014 14:13:21 +0000 (16:13 +0200)
src/test/java/net/pterodactylus/irc/connection/ConnectionEstablishHandlerTest.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/irc/connection/ConnectionEstablishHandlerTest.java b/src/test/java/net/pterodactylus/irc/connection/ConnectionEstablishHandlerTest.java
new file mode 100644 (file)
index 0000000..dc9ad5a
--- /dev/null
@@ -0,0 +1,58 @@
+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));
+               }
+       }
+
+}