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.4KB

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