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.

NickKeepManagerTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.nickkeep;
  18. import com.dmdirc.Channel;
  19. import com.dmdirc.config.profiles.Profile;
  20. import com.dmdirc.events.ChannelNickChangeEvent;
  21. import com.dmdirc.interfaces.Connection;
  22. import com.dmdirc.events.eventbus.EventBus;
  23. import com.google.common.collect.Lists;
  24. import java.util.Optional;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.mockito.Mock;
  29. import org.mockito.runners.MockitoJUnitRunner;
  30. import static org.mockito.Matchers.anyString;
  31. import static org.mockito.Mockito.never;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.when;
  34. @RunWith(MockitoJUnitRunner.class)
  35. public class NickKeepManagerTest {
  36. @Mock private EventBus eventBus;
  37. @Mock private ChannelNickChangeEvent event;
  38. @Mock private Channel channel;
  39. @Mock private Connection connection;
  40. @Mock private Profile profile;
  41. private NickKeepManager instance;
  42. @Before
  43. public void setUp() throws Exception {
  44. instance = new NickKeepManager(eventBus);
  45. when(event.getChannel()).thenReturn(channel);
  46. when(channel.getConnection()).thenReturn(Optional.of(connection));
  47. when(connection.getProfile()).thenReturn(profile);
  48. when(profile.getNicknames()).thenReturn(Lists.newArrayList("one", "two", "three"));
  49. }
  50. @Test
  51. public void testLoad() throws Exception {
  52. instance.load();
  53. verify(eventBus).subscribe(instance);
  54. }
  55. @Test
  56. public void testUnload() throws Exception {
  57. instance.unload();
  58. verify(eventBus).unsubscribe(instance);
  59. }
  60. @Test
  61. public void testHandleNickChange_NoConnection() throws Exception {
  62. when(channel.getConnection()).thenReturn(Optional.empty());
  63. instance.handleNickChange(event);
  64. verify(connection, never()).setNickname("one");
  65. }
  66. @Test
  67. public void testHandleNickChange_NoCurrentNickname() throws Exception {
  68. when(connection.getNickname()).thenReturn(Optional.empty());
  69. instance.handleNickChange(event);
  70. verify(connection, never()).setNickname("one");
  71. }
  72. @Test
  73. public void testHandleNickChange_NoNicknames() throws Exception {
  74. when(profile.getNicknames()).thenReturn(Lists.newArrayList());
  75. instance.handleNickChange(event);
  76. verify(connection, never()).setNickname(anyString());
  77. }
  78. @Test
  79. public void testHandleNickChange_NotDesired() throws Exception {
  80. when(event.getOldNick()).thenReturn("RAR");
  81. when(connection.getNickname()).thenReturn(Optional.of("one"));
  82. instance.handleNickChange(event);
  83. verify(connection, never()).setNickname("one");
  84. }
  85. @Test
  86. public void testHandleNickChange_Desired() throws Exception {
  87. when(event.getOldNick()).thenReturn("one");
  88. when(connection.getNickname()).thenReturn(Optional.of("RAR"));
  89. instance.handleNickChange(event);
  90. verify(connection).setNickname("one");
  91. }
  92. }