From: David ‘Bombe’ Roden Date: Sat, 18 Oct 2014 14:13:08 +0000 (+0200) Subject: Add unit test for connection-established handler. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=767b7be6c7e6f5a0d96895ab5019b7eca0982a59;p=xudocci.git Add unit test for connection-established handler. --- 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 index 0000000..dc9ad5a --- /dev/null +++ b/src/test/java/net/pterodactylus/irc/connection/ConnectionEstablishHandlerTest.java @@ -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 David ‘Bombe’ Roden + */ +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 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)); + } + } + +}