Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Process001Test.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2017 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.parser.irc.processors;
  23. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.irc.IRCClientInfo;
  25. import com.dmdirc.parser.irc.IRCParser;
  26. import com.dmdirc.parser.irc.ProcessingManager;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.ArgumentCaptor;
  31. import org.mockito.Mock;
  32. import org.mockito.runners.MockitoJUnitRunner;
  33. import java.time.LocalDateTime;
  34. import static org.junit.Assert.assertEquals;
  35. import static org.junit.Assert.assertTrue;
  36. import static org.junit.Assume.assumeFalse;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.ArgumentMatchers.anyBoolean;
  39. import static org.mockito.ArgumentMatchers.anyString;
  40. import static org.mockito.ArgumentMatchers.eq;
  41. import static org.mockito.Mockito.doAnswer;
  42. import static org.mockito.Mockito.mock;
  43. import static org.mockito.Mockito.never;
  44. import static org.mockito.Mockito.verify;
  45. import static org.mockito.Mockito.when;
  46. @RunWith(MockitoJUnitRunner.class)
  47. public class Process001Test {
  48. @Mock private IRCParser parser;
  49. @Mock private ProcessingManager processingManager;
  50. @Mock private IRCClientInfo localClient;
  51. private Process001 processor;
  52. @Before
  53. public void setup() {
  54. when(parser.getLocalClient()).thenReturn(localClient);
  55. processor = new Process001(parser);
  56. }
  57. @Test
  58. public void testSets001Received() {
  59. when(localClient.isFake()).thenReturn(true);
  60. assumeFalse(parser.got001);
  61. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "userName", "Hello!");
  62. assertTrue(parser.got001);
  63. }
  64. @Test
  65. public void testUpdatesServerName() {
  66. when(localClient.isFake()).thenReturn(true);
  67. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "userName", "Hello!");
  68. verify(parser).updateServerName("test.server.com");
  69. }
  70. @Test
  71. public void testAddsLocalClientIfFake() {
  72. when(localClient.isFake()).thenReturn(true);
  73. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "userName", "Hello!");
  74. verify(parser).addClient(localClient);
  75. }
  76. @Test
  77. public void testUpdatesLocalClientIfFake() {
  78. when(localClient.isFake()).thenReturn(true);
  79. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "userName", "Hello!");
  80. verify(localClient).setUserBits(eq("userName"), eq(true), anyBoolean());
  81. verify(localClient).setFake(false);
  82. }
  83. @Test
  84. public void testIgnoresDuplicate001WithSameNick() {
  85. when(localClient.getNickname()).thenReturn("UsernaME");
  86. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "userName", "Hello!");
  87. verify(parser, never()).addClient(any());
  88. verify(parser, never()).forceRemoveClient(any());
  89. }
  90. @Test
  91. public void testReplacesLocalUserOnDuplicate001() {
  92. when(localClient.getNickname()).thenReturn("UsernaME");
  93. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "newName", "Hello!");
  94. verify(parser).forceRemoveClient(localClient);
  95. verify(localClient).setUserBits(eq("newName"), eq(true), anyBoolean());
  96. verify(parser).addClient(localClient);
  97. }
  98. @Test
  99. public void testRaisesFatalErrorIfDuplicate001CausesNicknameCollision() {
  100. setupLocalClientToTrackNicknameChanges();
  101. when(parser.isKnownClient("newName")).thenReturn(true);
  102. when(parser.getClient("newName")).thenReturn(mock(IRCClientInfo.class));
  103. processor.process(LocalDateTime.now(), "001", ":test.server.com", "001", "newName", "Hello!");
  104. final ArgumentCaptor<ParserError> errorCaptor = ArgumentCaptor.forClass(ParserError.class);
  105. verify(parser).callErrorInfo(errorCaptor.capture());
  106. assertEquals(ParserError.ERROR_FATAL, errorCaptor.getValue().getLevel());
  107. }
  108. /**
  109. * Ensures that the local client mock returns new nicknames after setUserBits is called.
  110. */
  111. private void setupLocalClientToTrackNicknameChanges() {
  112. final StringBuilder nickname = new StringBuilder("UserName");
  113. when(localClient.getNickname()).thenAnswer(invocation -> nickname.toString());
  114. doAnswer(invocation -> {
  115. nickname.setLength(0);
  116. nickname.append(invocation.<String>getArgument(0));
  117. return null;
  118. }).when(localClient).setUserBits(anyString(), eq(true), anyBoolean());
  119. }
  120. }