Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ConnectionHandlerTest.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.config.ConfigBinder;
  24. import com.dmdirc.events.ChannelUserAwayEvent;
  25. import com.dmdirc.events.DisplayProperty;
  26. import com.dmdirc.events.ServerNumericEvent;
  27. import com.dmdirc.interfaces.Connection;
  28. import com.dmdirc.interfaces.ConnectionManager;
  29. import com.dmdirc.interfaces.EventBus;
  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 EventBus 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(groupChatUser.getUser()).thenReturn(user);
  92. instance = new ConnectionHandler(config, scheduledExecutorService, eventBus,
  93. "domain", connection);
  94. instance.handleWhoInterval(5);
  95. when(serverNumericEvent.getConnection()).thenReturn(connection);
  96. when(channelUserAwayEvent.getChannel()).thenReturn(groupChat);
  97. }
  98. @Test
  99. public void testLoad() throws Exception {
  100. instance.load();
  101. verify(configBinder).bind(instance, ConnectionHandler.class);
  102. verify(eventBus).subscribe(instance);
  103. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  104. eq(TimeUnit.MILLISECONDS));
  105. }
  106. @Test
  107. public void testUnload() throws Exception {
  108. instance.unload();
  109. verify(configBinder).unbind(instance);
  110. verify(scheduledExecutorService).shutdown();
  111. verify(eventBus).unsubscribe(instance);
  112. }
  113. @Test
  114. public void testHandleWhoInterval() throws Exception {
  115. instance.handleWhoInterval(10);
  116. verify(scheduledFuture).cancel(false);
  117. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L),
  118. eq(TimeUnit.MILLISECONDS));
  119. verify(scheduledExecutorService).scheduleAtFixedRate(any(Runnable.class), eq(10L), eq(10L),
  120. eq(TimeUnit.MILLISECONDS));
  121. }
  122. @Test
  123. public void testCheckWho_True() throws Exception {
  124. when(config.getOptionBool("domain", "sendwho")).thenReturn(true);
  125. instance.checkWho();
  126. verify(config).getOptionBool("domain", "sendwho");
  127. verify(groupChat).requestUsersInfo();
  128. }
  129. @Test
  130. public void testCheckWho_False() throws Exception {
  131. when(config.getOptionBool("domain", "sendwho")).thenReturn(false);
  132. instance.checkWho();
  133. verify(config).getOptionBool("domain", "sendwho");
  134. verify(groupChat, never()).requestUsersInfo();
  135. }
  136. @Test
  137. public void testHandleAwayEvent_WithReason() throws Exception {
  138. when(channelUserAwayEvent.getReason()).thenReturn(Optional.of("reason"));
  139. instance.load();
  140. instance.handleAwayEvent(channelUserAwayEvent);
  141. verify(channelUserAwayEvent, never())
  142. .setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  143. verify(connection, never()).requestUserInfo(any());
  144. }
  145. @Test
  146. public void testHandleAwayEvent_WithoutReason() throws Exception {
  147. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  148. instance.load();
  149. instance.handleAwayEvent(channelUserAwayEvent);
  150. verify(channelUserAwayEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
  151. verify(connection).requestUserInfo(any());
  152. }
  153. @Test
  154. public void testHandleServerNumericEvent_301() throws Exception {
  155. when(serverNumericEvent.getNumeric()).thenReturn(301);
  156. when(serverNumericEvent.getArgs()).thenReturn(
  157. new String[]{"", "", "", "nickname", "reason"});
  158. instance.load();
  159. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  160. when(groupChatUser.getGroupChat()).thenReturn(groupChat);
  161. instance.handleAwayEvent(channelUserAwayEvent);
  162. instance.handleServerNumericEvent(serverNumericEvent);
  163. verify(eventBus).publishAsync(eventArgumentCaptor.capture());
  164. assertEquals("nickname", eventArgumentCaptor.getValue().getUser().getNickname());
  165. assertEquals("reason", eventArgumentCaptor.getValue().getReason().get());
  166. }
  167. @Test
  168. public void testHandleServerNumericEvent_101() throws Exception {
  169. when(serverNumericEvent.getNumeric()).thenReturn(101);
  170. when(channelUserAwayEvent.getReason()).thenReturn(Optional.empty());
  171. instance.load();
  172. instance.handleAwayEvent(channelUserAwayEvent);
  173. instance.handleServerNumericEvent(serverNumericEvent);
  174. verify(serverNumericEvent, never()).getArgs();
  175. verify(eventBus, never()).publishAsync(any());
  176. }
  177. }