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.

Process001Test.java 5.3KB

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