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.

ConnectionHandlerTest.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.channelwho;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.config.ConfigBinder;
  25. import com.dmdirc.events.ChannelUserAwayEvent;
  26. import com.dmdirc.events.DisplayProperty;
  27. import com.dmdirc.events.ServerNumericEvent;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.interfaces.ConnectionManager;
  30. import com.dmdirc.interfaces.GroupChat;
  31. import com.dmdirc.interfaces.GroupChatManager;
  32. import com.dmdirc.interfaces.GroupChatUser;
  33. import com.dmdirc.interfaces.User;
  34. import com.dmdirc.interfaces.WindowModel;
  35. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  36. import com.google.common.collect.Lists;
  37. import java.util.Optional;
  38. import java.util.concurrent.ScheduledExecutorService;
  39. import java.util.concurrent.ScheduledFuture;
  40. import java.util.concurrent.TimeUnit;
  41. import org.junit.Before;
  42. import org.junit.Test;
  43. import org.junit.runner.RunWith;
  44. import org.mockito.ArgumentCaptor;
  45. import org.mockito.Captor;
  46. import org.mockito.Mock;
  47. import org.mockito.runners.MockitoJUnitRunner;
  48. import static org.junit.Assert.assertEquals;
  49. import static org.mockito.Matchers.any;
  50. import static org.mockito.Matchers.anyLong;
  51. import static org.mockito.Matchers.eq;
  52. import static org.mockito.Mockito.doReturn;
  53. import static org.mockito.Mockito.never;
  54. import static org.mockito.Mockito.verify;
  55. import static org.mockito.Mockito.when;
  56. @RunWith(MockitoJUnitRunner.class)
  57. public class ConnectionHandlerTest {
  58. @Mock private AggregateConfigProvider config;
  59. @Mock private ConfigBinder configBinder;
  60. @Mock private WindowModel windowModel;
  61. @Mock private DMDircMBassador eventBus;
  62. @Mock private ScheduledExecutorService scheduledExecutorService;
  63. @Mock private ScheduledFuture<?> scheduledFuture;
  64. @Mock private ConnectionManager connectionManager;
  65. @Mock private Connection connection;
  66. @Mock private GroupChat groupChat;
  67. @Mock private GroupChatUser groupChatUser;
  68. @Mock private User user;
  69. @Mock private GroupChatManager groupChatManager;
  70. @Mock private ServerNumericEvent serverNumericEvent;
  71. @Mock private ChannelUserAwayEvent channelUserAwayEvent;
  72. @Captor private ArgumentCaptor<ChannelUserAwayEvent> eventArgumentCaptor;
  73. private ConnectionHandler instance;
  74. @Before
  75. public void setUp() throws Exception {
  76. doReturn(scheduledFuture).when(scheduledExecutorService)
  77. .scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any());
  78. when(windowModel.getEventBus()).thenReturn(eventBus);
  79. when(connection.getWindowModel()).thenReturn(windowModel);
  80. when(config.getBinder()).thenReturn(configBinder);
  81. when(connectionManager.getConnections()).thenReturn(Lists.newArrayList(connection));
  82. when(connection.getGroupChatManager()).thenReturn(groupChatManager);
  83. when(groupChatManager.getChannels()).thenReturn(Lists.newArrayList(groupChat));
  84. when(groupChat.getWindowModel()).thenReturn(windowModel);
  85. when(groupChat.getConnection()).thenReturn(Optional.of(connection));
  86. when(configBinder.withDefaultDomain("domain")).thenReturn(configBinder);
  87. when(windowModel.getConfigManager()).thenReturn(config);
  88. when(groupChatUser.getNickname()).thenReturn("nickname");
  89. when(channelUserAwayEvent.getUser()).thenReturn(groupChatUser);
  90. when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
  91. when(groupChat.getEventBus()).thenReturn(eventBus);
  92. when(groupChatUser.getUser()).thenReturn(user);
  93. instance = new ConnectionHandler(config, scheduledExecutorService, connectionManager,
  94. "domain", connection);
  95. instance.handleWhoInterval(5);
  96. when(serverNumericEvent.getConnection()).thenReturn(connection);
  97. when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
  98. }
  99. @Test
  100. public void testLoad() throws Exception {
  101. instance.load();
  102. verify(configBinder).bind(instance, ConnectionHandler.class);
  103. verify(eventBus).subscribe(instance);
  104. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  105. eq(TimeUnit.MILLISECONDS));
  106. }
  107. @Test
  108. public void testUnload() throws Exception {
  109. instance.unload();
  110. verify(configBinder).unbind(instance);
  111. verify(scheduledExecutorService).shutdown();
  112. verify(eventBus).unsubscribe(instance);
  113. }
  114. @Test
  115. public void testHandleWhoInterval() throws Exception {
  116. instance.handleWhoInterval(10);
  117. verify(scheduledFuture).cancel(false);
  118. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  119. eq(TimeUnit.MILLISECONDS));
  120. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(10L), eq(10L),
  121. eq(TimeUnit.MILLISECONDS));
  122. }
  123. @Test
  124. public void testCheckWho_True() throws Exception {
  125. when(config.getOptionBool("domain", "sendwho")).thenReturn(true);
  126. instance.checkWho();
  127. verify(config).getOptionBool("domain", "sendwho");
  128. verify(groupChat).requestUsersInfo();
  129. }
  130. @Test
  131. public void testCheckWho_False() throws Exception {
  132. when(config.getOptionBool("domain", "sendwho")).thenReturn(false);
  133. instance.checkWho();
  134. verify(config).getOptionBool("domain", "sendwho");
  135. verify(groupChat, never()).requestUsersInfo();
  136. }
  137. @Test
  138. public void testHandleAwayEvent_WithReason() throws Exception {
  139. when(channelUserAwayEvent.getReason()).thenReturn(Optional.of("reason"));
  140. instance.load();
  141. instance.handleAwayEvent(channelUserAwayEvent);
  142. verify(channelUserAwayEvent, never())
  143. .setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  144. verify(connection, never()).requestUserInfo(any());
  145. }
  146. @Test
  147. public void testHandleAwayEvent_WithoutReason() throws Exception {
  148. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  149. instance.load();
  150. instance.handleAwayEvent(channelUserAwayEvent);
  151. verify(channelUserAwayEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  152. verify(connection).requestUserInfo(any());
  153. }
  154. @Test
  155. public void testHandleServerNumericEvent_301() throws Exception {
  156. when(serverNumericEvent.getNumeric()).thenReturn(301);
  157. when(serverNumericEvent.getArgs()).thenReturn(
  158. new String[]{"", "", "", "nickname", "reason"});
  159. instance.load();
  160. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  161. when(groupChatUser.getGroupChat()).thenReturn(groupChat);
  162. instance.handleAwayEvent(channelUserAwayEvent);
  163. instance.handleServerNumericEvent(serverNumericEvent);
  164. verify(eventBus).publishAsync(eventArgumentCaptor.capture());
  165. assertEquals("nickname", eventArgumentCaptor.getValue().getUser().getNickname());
  166. assertEquals("reason", eventArgumentCaptor.getValue().getReason().get());
  167. }
  168. @Test
  169. public void testHandleServerNumericEvent_101() throws Exception {
  170. when(serverNumericEvent.getNumeric()).thenReturn(101);
  171. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  172. instance.load();
  173. instance.handleAwayEvent(channelUserAwayEvent);
  174. instance.handleServerNumericEvent(serverNumericEvent);
  175. verify(serverNumericEvent, never()).getArgs();
  176. verify(eventBus, never()).publishAsync(any());
  177. }
  178. }