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.

IRCReaderTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2006-2013 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;
  23. import com.dmdirc.parser.interfaces.Encoder;
  24. import com.dmdirc.parser.irc.IRCReader.ReadLine;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.nio.charset.Charset;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import static org.junit.Assert.*;
  31. import static org.mockito.Mockito.*;
  32. public class IRCReaderTest {
  33. /** Reads and verifies a single line. */
  34. @Test
  35. public void testReadLine() throws IOException {
  36. final InputStream stream = mock(InputStream.class);
  37. final Encoder encoder = mock(Encoder.class);
  38. when(stream.read()).thenReturn((int) 't', (int) 'e', (int) 's',
  39. (int) 't', (int) '\r', (int) '\n');
  40. final IRCReader reader = new IRCReader(stream, encoder);
  41. final ReadLine line = reader.readLine();
  42. assertEquals("test", line.getLine());
  43. }
  44. /** Reads and verifies a single line with only a trailing LF. */
  45. @Test
  46. public void testReadLineWithOnlyLF() throws IOException {
  47. final InputStream stream = mock(InputStream.class);
  48. final Encoder encoder = mock(Encoder.class);
  49. when(stream.read()).thenReturn((int) 't', (int) 'e', (int) 's',
  50. (int) 't', (int) '\n');
  51. final IRCReader reader = new IRCReader(stream, encoder);
  52. final ReadLine line = reader.readLine();
  53. assertEquals("test", line.getLine());
  54. }
  55. /** Reads and verifies a single line with a trailing parameter. */
  56. @Test
  57. public void testReadLineWithParameter() throws IOException {
  58. final InputStream stream = mock(InputStream.class);
  59. final Encoder encoder = mock(Encoder.class);
  60. when(stream.read()).thenReturn((int) 't', (int) 'e', (int) ' ',
  61. (int) 't', (int) ' ', (int) ':', (int) 'f', (int) 'o',
  62. (int) 'o', (int) '\r', (int) '\n');
  63. when(encoder.encode((String) isNull(), (String) isNull(),
  64. (byte[]) anyObject(), anyInt(), eq(3)))
  65. .thenReturn("encoded");
  66. final IRCReader reader = new IRCReader(stream, encoder);
  67. final ReadLine line = reader.readLine();
  68. assertArrayEquals(new String[]{"te", "t", "encoded",},
  69. line.getTokens());
  70. }
  71. /** Reads and verifies a single line with a trailing parameter. */
  72. @Test
  73. public void testReadLineWithMultipleSpaces() throws IOException {
  74. final InputStream stream = mock(InputStream.class);
  75. final Encoder encoder = mock(Encoder.class);
  76. when(stream.read()).thenReturn((int) 'f', (int) 'o', (int) 'o',
  77. (int) ' ', (int) 'b', (int) 'a', (int) 'r',
  78. (int) ' ', (int) ' ', (int) 'b', (int) 'a', (int) 'z',
  79. (int) ' ', (int) ' ', (int) ':', (int) 'q', (int) 'u',
  80. (int) 'x', (int) ' ', (int) ' ', (int) 'b', (int) 'a', (int) 'z',
  81. (int) '\r', (int) '\n');
  82. when(encoder.encode((String) isNull(), (String) isNull(),
  83. (byte[]) anyObject(), eq(15), eq(8)))
  84. .thenReturn("qux baz");
  85. final IRCReader reader = new IRCReader(stream, encoder);
  86. final ReadLine line = reader.readLine();
  87. assertArrayEquals(new String[]{"foo", "bar", "baz", "qux baz",},
  88. line.getTokens());
  89. }
  90. /** Verifies that a source is extracted properly. */
  91. @Test
  92. public void testGetSource() throws IOException {
  93. final InputStream stream = mock(InputStream.class);
  94. final Encoder encoder = mock(Encoder.class);
  95. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
  96. (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
  97. (int) 'o', (int) '\r', (int) '\n');
  98. new IRCReader(stream, encoder).readLine();
  99. verify(encoder).encode(eq("src"), anyString(),
  100. (byte[]) anyObject(), anyInt(), anyInt());
  101. }
  102. /** Verifies that no source is passed if the source is empty. */
  103. @Test
  104. public void testGetEmptySource() throws IOException {
  105. final InputStream stream = mock(InputStream.class);
  106. final Encoder encoder = mock(Encoder.class);
  107. when(stream.read()).thenReturn((int) ':', (int) ' ', (int) 'r',
  108. (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
  109. (int) 'o', (int) '\r', (int) '\n');
  110. new IRCReader(stream, encoder).readLine();
  111. verify(encoder).encode((String) isNull(), anyString(),
  112. (byte[]) anyObject(), anyInt(), anyInt());
  113. }
  114. /** Verifies that a destination is extracted properly. */
  115. @Test
  116. public void testGetDestination() throws IOException {
  117. final InputStream stream = mock(InputStream.class);
  118. final Encoder encoder = mock(Encoder.class);
  119. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
  120. (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) 'y',
  121. (int) ' ', (int) ':', (int) 'z', (int) '\r', (int) '\n');
  122. new IRCReader(stream, encoder).readLine();
  123. verify(encoder).encode(anyString(), eq("y"), (byte[]) anyObject(),
  124. anyInt(), anyInt());
  125. }
  126. /** Verifies that no destination is extracted if there's no source. */
  127. @Test
  128. public void testGetDestinationNoSource() throws IOException {
  129. final InputStream stream = mock(InputStream.class);
  130. final Encoder encoder = mock(Encoder.class);
  131. when(stream.read()).thenReturn((int) '_', (int) 's', (int) 'r',
  132. (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) 'y',
  133. (int) ' ', (int) ':', (int) 'z', (int) '\r', (int) '\n');
  134. new IRCReader(stream, encoder).readLine();
  135. verify(encoder).encode(anyString(), (String) isNull(),
  136. (byte[]) anyObject(), anyInt(), anyInt());
  137. }
  138. /** Verifies that no destination is extracted if there are too few args. */
  139. @Test
  140. public void testGetDestinationTooShort() throws IOException {
  141. final InputStream stream = mock(InputStream.class);
  142. final Encoder encoder = mock(Encoder.class);
  143. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
  144. (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
  145. (int) 'a', (int) 'b', (int) 'z', (int) '\r', (int) '\n');
  146. new IRCReader(stream, encoder).readLine();
  147. verify(encoder).encode(anyString(), (String) isNull(),
  148. (byte[]) anyObject(), anyInt(), anyInt());
  149. }
  150. /** Verifies that a numeric's destination is extracted properly. */
  151. @Test
  152. public void testGetDestinationWithNumeric() throws IOException {
  153. final InputStream stream = mock(InputStream.class);
  154. final Encoder encoder = mock(Encoder.class);
  155. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
  156. (int) 'c', (int) ' ', (int) '1', (int) ' ', (int) 'x',
  157. (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
  158. (int) ':', (int) 'x', (int) '\r', (int) '\n');
  159. new IRCReader(stream, encoder).readLine();
  160. verify(encoder).encode(anyString(), eq("y"),
  161. (byte[]) anyObject(), anyInt(), anyInt());
  162. }
  163. /** Verifies that the close call is proxied to the stream. */
  164. @Test
  165. public void testClose() throws IOException {
  166. final InputStream stream = mock(InputStream.class);
  167. final Encoder encoder = mock(Encoder.class);
  168. new IRCReader(stream, encoder).close();
  169. verify(stream).close();
  170. }
  171. /** Verifies that the reader works with improperly coded unicode. */
  172. @Test
  173. public void testHandlesBadCoding() throws IOException {
  174. final InputStream stream = mock(InputStream.class);
  175. final Encoder encoder = mock(Encoder.class);
  176. when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
  177. (int) 'c', (int) ' ', (int) '1', (int) ' ', 0xF6,
  178. (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
  179. (int) ':', (int) 'x', (int) '\r', (int) '\n');
  180. when(encoder.encode(anyString(), anyString(),
  181. (byte[]) any(), anyInt(), anyInt())).thenReturn("x");
  182. final ReadLine line = new IRCReader(stream, encoder,
  183. Charset.forName("UTF-8")).readLine();
  184. Assert.assertArrayEquals(new String[] {
  185. ":src", "1", "\uFFFD", "y", "z", "x"
  186. }, line.getTokens());
  187. }
  188. }