Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NickKeepManagerTest.java 4.0KB

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