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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.channelwho;
  18. import com.dmdirc.config.ConfigBinder;
  19. import com.dmdirc.events.ChannelUserAwayEvent;
  20. import com.dmdirc.events.DisplayProperty;
  21. import com.dmdirc.events.ServerNumericEvent;
  22. import com.dmdirc.interfaces.Connection;
  23. import com.dmdirc.interfaces.ConnectionManager;
  24. import com.dmdirc.interfaces.EventBus;
  25. import com.dmdirc.interfaces.GroupChat;
  26. import com.dmdirc.interfaces.GroupChatManager;
  27. import com.dmdirc.interfaces.GroupChatUser;
  28. import com.dmdirc.interfaces.User;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.google.common.collect.Lists;
  32. import java.util.Optional;
  33. import java.util.concurrent.ScheduledExecutorService;
  34. import java.util.concurrent.ScheduledFuture;
  35. import java.util.concurrent.TimeUnit;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.junit.runner.RunWith;
  39. import org.mockito.ArgumentCaptor;
  40. import org.mockito.Captor;
  41. import org.mockito.Mock;
  42. import org.mockito.runners.MockitoJUnitRunner;
  43. import static org.junit.Assert.assertEquals;
  44. import static org.mockito.Matchers.any;
  45. import static org.mockito.Matchers.anyLong;
  46. import static org.mockito.Matchers.eq;
  47. import static org.mockito.Mockito.doReturn;
  48. import static org.mockito.Mockito.never;
  49. import static org.mockito.Mockito.verify;
  50. import static org.mockito.Mockito.when;
  51. @RunWith(MockitoJUnitRunner.class)
  52. public class ConnectionHandlerTest {
  53. @Mock private AggregateConfigProvider config;
  54. @Mock private ConfigBinder configBinder;
  55. @Mock private WindowModel windowModel;
  56. @Mock private EventBus eventBus;
  57. @Mock private ScheduledExecutorService scheduledExecutorService;
  58. @Mock private ScheduledFuture<?> scheduledFuture;
  59. @Mock private ConnectionManager connectionManager;
  60. @Mock private Connection connection;
  61. @Mock private GroupChat groupChat;
  62. @Mock private GroupChatUser groupChatUser;
  63. @Mock private User user;
  64. @Mock private GroupChatManager groupChatManager;
  65. @Mock private ServerNumericEvent serverNumericEvent;
  66. @Mock private ChannelUserAwayEvent channelUserAwayEvent;
  67. @Captor private ArgumentCaptor<ChannelUserAwayEvent> eventArgumentCaptor;
  68. private ConnectionHandler instance;
  69. @Before
  70. public void setUp() throws Exception {
  71. doReturn(scheduledFuture).when(scheduledExecutorService)
  72. .scheduleAtFixedRate(any(Runnable.class), anyLong(), anyLong(), any());
  73. when(windowModel.getEventBus()).thenReturn(eventBus);
  74. when(connection.getWindowModel()).thenReturn(windowModel);
  75. when(config.getBinder()).thenReturn(configBinder);
  76. when(connectionManager.getConnections()).thenReturn(Lists.newArrayList(connection));
  77. when(connection.getGroupChatManager()).thenReturn(groupChatManager);
  78. when(groupChatManager.getChannels()).thenReturn(Lists.newArrayList(groupChat));
  79. when(groupChat.getWindowModel()).thenReturn(windowModel);
  80. when(groupChat.getConnection()).thenReturn(Optional.of(connection));
  81. when(configBinder.withDefaultDomain("domain")).thenReturn(configBinder);
  82. when(windowModel.getConfigManager()).thenReturn(config);
  83. when(groupChatUser.getNickname()).thenReturn("nickname");
  84. when(channelUserAwayEvent.getUser()).thenReturn(groupChatUser);
  85. when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
  86. when(groupChatUser.getUser()).thenReturn(user);
  87. instance = new ConnectionHandler(config, scheduledExecutorService, eventBus,
  88. "domain", connection);
  89. instance.handleWhoInterval(5);
  90. when(serverNumericEvent.getConnection()).thenReturn(connection);
  91. when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
  92. }
  93. @Test
  94. public void testLoad() throws Exception {
  95. instance.load();
  96. verify(configBinder).bind(instance, ConnectionHandler.class);
  97. verify(eventBus).subscribe(instance);
  98. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  99. eq(TimeUnit.MILLISECONDS));
  100. }
  101. @Test
  102. public void testUnload() throws Exception {
  103. instance.unload();
  104. verify(configBinder).unbind(instance);
  105. verify(scheduledExecutorService).shutdown();
  106. verify(eventBus).unsubscribe(instance);
  107. }
  108. @Test
  109. public void testHandleWhoInterval() throws Exception {
  110. instance.handleWhoInterval(10);
  111. verify(scheduledFuture).cancel(false);
  112. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  113. eq(TimeUnit.MILLISECONDS));
  114. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(10L), eq(10L),
  115. eq(TimeUnit.MILLISECONDS));
  116. }
  117. @Test
  118. public void testCheckWho_True() throws Exception {
  119. when(config.getOptionBool("domain", "sendwho")).thenReturn(true);
  120. instance.checkWho();
  121. verify(config).getOptionBool("domain", "sendwho");
  122. verify(groupChat).requestUsersInfo();
  123. }
  124. @Test
  125. public void testCheckWho_False() throws Exception {
  126. when(config.getOptionBool("domain", "sendwho")).thenReturn(false);
  127. instance.checkWho();
  128. verify(config).getOptionBool("domain", "sendwho");
  129. verify(groupChat, never()).requestUsersInfo();
  130. }
  131. @Test
  132. public void testHandleAwayEvent_WithReason() throws Exception {
  133. when(channelUserAwayEvent.getReason()).thenReturn(Optional.of("reason"));
  134. instance.load();
  135. instance.handleAwayEvent(channelUserAwayEvent);
  136. verify(channelUserAwayEvent, never())
  137. .setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  138. verify(connection, never()).requestUserInfo(any());
  139. }
  140. @Test
  141. public void testHandleAwayEvent_WithoutReason() throws Exception {
  142. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  143. instance.load();
  144. instance.handleAwayEvent(channelUserAwayEvent);
  145. verify(channelUserAwayEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  146. verify(connection).requestUserInfo(any());
  147. }
  148. @Test
  149. public void testHandleServerNumericEvent_301() throws Exception {
  150. when(serverNumericEvent.getNumeric()).thenReturn(301);
  151. when(serverNumericEvent.getArgs()).thenReturn(
  152. new String[]{"", "", "", "nickname", "reason"});
  153. instance.load();
  154. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  155. when(groupChatUser.getGroupChat()).thenReturn(groupChat);
  156. instance.handleAwayEvent(channelUserAwayEvent);
  157. instance.handleServerNumericEvent(serverNumericEvent);
  158. verify(eventBus).publishAsync(eventArgumentCaptor.capture());
  159. assertEquals("nickname", eventArgumentCaptor.getValue().getUser().getNickname());
  160. assertEquals("reason", eventArgumentCaptor.getValue().getReason().get());
  161. }
  162. @Test
  163. public void testHandleServerNumericEvent_101() throws Exception {
  164. when(serverNumericEvent.getNumeric()).thenReturn(101);
  165. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  166. instance.load();
  167. instance.handleAwayEvent(channelUserAwayEvent);
  168. instance.handleServerNumericEvent(serverNumericEvent);
  169. verify(serverNumericEvent, never()).getArgs();
  170. verify(eventBus, never()).publishAsync(any());
  171. }
  172. }