Add unit test for connection-established handler.
[xudocci.git] / src / test / java / net / pterodactylus / irc / connection / ConnectionEstablishHandlerTest.java
1 package net.pterodactylus.irc.connection;
2
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;
10
11 import net.pterodactylus.irc.Connection;
12 import net.pterodactylus.irc.event.ConnectionEstablished;
13
14 import com.google.common.eventbus.EventBus;
15 import org.junit.Test;
16 import org.mockito.ArgumentCaptor;
17
18 /**
19  * Unit test for {@link ConnectionEstablishHandler}.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22  */
23 public class ConnectionEstablishHandlerTest {
24
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);
29
30         @Test
31         public void freshHandlerWillHandleTheCorrectCommands() {
32                 for (String command : asList("001", "002", "003", "004")) {
33                         assertThat(handler.willHandle(createReply(command)), is(true));
34                 }
35         }
36
37         @Test
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));
48         }
49
50         @Test
51         public void afterAnEstablishedConnectionTheHandlerWillNotHandleTheCommandsAnymore() {
52                 sendingAllNecessaryCommandsResultsInAnEstablishedConnection();
53                 for (String command : asList("001", "002", "003", "004")) {
54                         assertThat(handler.willHandle(createReply(command)), is(false));
55                 }
56         }
57
58 }