You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerEventHandlerTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc;
  23. import com.dmdirc.events.AppErrorEvent;
  24. import com.dmdirc.events.DMDircEvent;
  25. import com.dmdirc.events.ServerCtcpEvent;
  26. import com.dmdirc.events.UserErrorEvent;
  27. import com.dmdirc.parser.common.CallbackManager;
  28. import com.dmdirc.parser.common.ParserError;
  29. import com.dmdirc.parser.interfaces.ChannelInfo;
  30. import com.dmdirc.parser.interfaces.ClientInfo;
  31. import com.dmdirc.parser.interfaces.Parser;
  32. import com.dmdirc.parser.interfaces.callbacks.CallbackInterface;
  33. import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;
  34. import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
  35. import com.dmdirc.parser.interfaces.callbacks.PrivateActionListener;
  36. import com.dmdirc.parser.interfaces.callbacks.PrivateCtcpListener;
  37. import com.dmdirc.parser.interfaces.callbacks.PrivateMessageListener;
  38. import com.google.common.collect.Lists;
  39. import java.util.Date;
  40. import org.junit.Before;
  41. import org.junit.Test;
  42. import org.junit.runner.RunWith;
  43. import org.mockito.ArgumentCaptor;
  44. import org.mockito.Mock;
  45. import org.mockito.runners.MockitoJUnitRunner;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.mockito.Matchers.eq;
  50. import static org.mockito.Mockito.mock;
  51. import static org.mockito.Mockito.verify;
  52. import static org.mockito.Mockito.when;
  53. @RunWith(MockitoJUnitRunner.class)
  54. public class ServerEventHandlerTest {
  55. @Mock private Server server;
  56. @Mock private Parser parser;
  57. @Mock private CallbackManager callbackManager;
  58. @Mock private DMDircMBassador eventBus;
  59. @Mock private ChannelInfo channelInfo;
  60. @Mock private ClientInfo clientInfo;
  61. @Mock private Query query;
  62. @Mock private Date date;
  63. @Before
  64. public void setUp() {
  65. when(server.getParser()).thenReturn(parser);
  66. when(server.getState()).thenReturn(ServerState.CONNECTED);
  67. when(parser.getCallbackManager()).thenReturn(callbackManager);
  68. final ServerEventHandler handler = new ServerEventHandler(server, eventBus);
  69. handler.registerCallbacks();
  70. }
  71. @Test
  72. public void testSelfJoinAddsChannel() {
  73. final ChannelSelfJoinListener listener = getCallback(ChannelSelfJoinListener.class);
  74. listener.onChannelSelfJoin(parser, date, channelInfo);
  75. verify(server).addChannel(channelInfo);
  76. }
  77. @Test
  78. public void testOnPrivateMessageCreatesQuery() {
  79. when(server.hasQuery("ho!st@name")).thenReturn(false);
  80. when(server.getQuery("ho!st@name")).thenReturn(query);
  81. final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
  82. listener.onPrivateMessage(parser, date, "message", "ho!st@name");
  83. verify(server).getQuery("ho!st@name");
  84. }
  85. @Test
  86. public void testOnPrivateMessageRelaysMessage() {
  87. when(server.hasQuery("ho!st@name")).thenReturn(false);
  88. when(server.getQuery("ho!st@name")).thenReturn(query);
  89. final PrivateMessageListener listener = getCallback(PrivateMessageListener.class);
  90. listener.onPrivateMessage(parser, date, "message", "ho!st@name");
  91. verify(query).onPrivateMessage(parser, date, "message", "ho!st@name");
  92. }
  93. @Test
  94. public void testOnPrivateActionCreatesQuery() {
  95. when(server.hasQuery("ho!st@name")).thenReturn(false);
  96. when(server.getQuery("ho!st@name")).thenReturn(query);
  97. final PrivateActionListener listener = getCallback(PrivateActionListener.class);
  98. listener.onPrivateAction(parser, date, "message", "ho!st@name");
  99. verify(server).getQuery("ho!st@name");
  100. }
  101. @Test
  102. public void testOnPrivateActionRelaysMessage() {
  103. when(server.hasQuery("ho!st@name")).thenReturn(false);
  104. when(server.getQuery("ho!st@name")).thenReturn(query);
  105. final PrivateActionListener listener = getCallback(PrivateActionListener.class);
  106. listener.onPrivateAction(parser, date, "message", "ho!st@name");
  107. verify(query).onPrivateAction(parser, date, "message", "ho!st@name");
  108. }
  109. @Test
  110. public void testOnErrorInfoEmitsUserErrorEvent() {
  111. final ParserError error = mock(ParserError.class);
  112. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  113. when(error.getLastLine()).thenReturn("lastline");
  114. when(server.getAddress()).thenReturn("serveraddress");
  115. when(error.getData()).thenReturn("DATA");
  116. when(error.isUserError()).thenReturn(true);
  117. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  118. listener.onErrorInfo(parser, date, error);
  119. final UserErrorEvent event = getAsyncEvent(UserErrorEvent.class);
  120. assertEquals("DATA", event.getMessage());
  121. }
  122. @Test
  123. public void testOnErrorInfoEmitsAppErrorEvent() {
  124. final ParserError error = mock(ParserError.class);
  125. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  126. when(error.getLastLine()).thenReturn("lastline");
  127. when(server.getAddress()).thenReturn("serveraddress");
  128. when(error.getData()).thenReturn("DATA");
  129. when(error.isUserError()).thenReturn(false);
  130. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  131. listener.onErrorInfo(parser, date, error);
  132. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  133. assertEquals("DATA", event.getMessage());
  134. }
  135. @Test
  136. public void testOnErrorInfoIncludesConnectionDetails() {
  137. final ParserError error = mock(ParserError.class);
  138. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  139. when(error.getLastLine()).thenReturn("lastline");
  140. when(server.getAddress()).thenReturn("serveraddress");
  141. when(error.getData()).thenReturn("DATA");
  142. when(error.isUserError()).thenReturn(false);
  143. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  144. listener.onErrorInfo(parser, date, error);
  145. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  146. assertNotNull(event.getThrowable());
  147. assertTrue(event.getThrowable().getMessage().contains("lastline"));
  148. assertTrue(event.getThrowable().getMessage().contains("iline1"));
  149. assertTrue(event.getThrowable().getMessage().contains("iline2"));
  150. assertTrue(event.getThrowable().getMessage().contains("serveraddress"));
  151. }
  152. @Test
  153. public void testOnErrorInfoIncludesException() {
  154. final ParserError error = mock(ParserError.class);
  155. final Exception exception = mock(Exception.class);
  156. when(parser.getServerInformationLines()).thenReturn(Lists.newArrayList("iline1", "iline2"));
  157. when(error.getLastLine()).thenReturn("lastline");
  158. when(server.getAddress()).thenReturn("serveraddress");
  159. when(error.getData()).thenReturn("DATA");
  160. when(error.isException()).thenReturn(true);
  161. when(error.getException()).thenReturn(exception);
  162. final ErrorInfoListener listener = getCallback(ErrorInfoListener.class);
  163. listener.onErrorInfo(parser, date, error);
  164. final AppErrorEvent event = getAsyncEvent(AppErrorEvent.class);
  165. assertNotNull(event.getThrowable());
  166. assertEquals(exception, event.getThrowable());
  167. }
  168. @Test
  169. public void testOnPrivateCTCPRaisesEvent() {
  170. when(parser.getClient("host!na@me")).thenReturn(clientInfo);
  171. when(server.parseHostmask("host!na@me")).thenReturn(new String[]{"host", "na", "me"});
  172. final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
  173. listener.onPrivateCTCP(parser, date, "type", "message", "host!na@me");
  174. final ServerCtcpEvent event = getEvent(ServerCtcpEvent.class);
  175. assertEquals("type", event.getType());
  176. assertEquals("message", event.getContent());
  177. assertEquals(clientInfo, event.getClient());
  178. }
  179. @Test
  180. public void testOnPrivateCTCPSendsReplyIfEventUnhandled() {
  181. when(parser.getClient("host!na@me")).thenReturn(clientInfo);
  182. when(server.parseHostmask("host!na@me")).thenReturn(new String[]{"host", "na", "me"});
  183. final PrivateCtcpListener listener = getCallback(PrivateCtcpListener.class);
  184. listener.onPrivateCTCP(parser, date, "type", "message", "host!na@me");
  185. verify(server).sendCTCPReply("host", "type", "message");
  186. }
  187. private <T extends CallbackInterface> T getCallback(final Class<T> type) {
  188. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  189. verify(callbackManager).addCallback(eq(type), captor.capture());
  190. assertNotNull(captor.getValue());
  191. return captor.getValue();
  192. }
  193. private <T extends DMDircEvent> T getAsyncEvent(final Class<T> type) {
  194. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  195. verify(eventBus).publishAsync(captor.capture());
  196. assertNotNull(captor.getValue());
  197. return captor.getValue();
  198. }
  199. private <T extends DMDircEvent> T getEvent(final Class<T> type) {
  200. final ArgumentCaptor<T> captor = ArgumentCaptor.forClass(type);
  201. verify(eventBus).publish(captor.capture());
  202. assertNotNull(captor.getValue());
  203. return captor.getValue();
  204. }
  205. }