|
@@ -25,9 +25,13 @@ package com.dmdirc.parser.irc;
|
25
|
25
|
import com.dmdirc.parser.interfaces.Encoder;
|
26
|
26
|
import com.dmdirc.parser.irc.IRCReader.ReadLine;
|
27
|
27
|
|
|
28
|
+import java.io.ByteArrayInputStream;
|
28
|
29
|
import java.io.IOException;
|
29
|
30
|
import java.io.InputStream;
|
30
|
31
|
import java.nio.charset.Charset;
|
|
32
|
+import java.text.SimpleDateFormat;
|
|
33
|
+import java.util.Arrays;
|
|
34
|
+import java.util.Date;
|
31
|
35
|
|
32
|
36
|
import org.junit.Assert;
|
33
|
37
|
import org.junit.Test;
|
|
@@ -40,12 +44,9 @@ public class IRCReaderTest {
|
40
|
44
|
/** Reads and verifies a single line. */
|
41
|
45
|
@Test
|
42
|
46
|
public void testReadLine() throws IOException {
|
43
|
|
- final InputStream stream = mock(InputStream.class);
|
|
47
|
+ final InputStream stream = new ByteArrayInputStream("test\r\n".getBytes());
|
44
|
48
|
final Encoder encoder = mock(Encoder.class);
|
45
|
49
|
|
46
|
|
- when(stream.read()).thenReturn((int) 't', (int) 'e', (int) 's',
|
47
|
|
- (int) 't', (int) '\r', (int) '\n');
|
48
|
|
-
|
49
|
50
|
final IRCReader reader = new IRCReader(stream, encoder);
|
50
|
51
|
final ReadLine line = reader.readLine();
|
51
|
52
|
|
|
@@ -55,12 +56,9 @@ public class IRCReaderTest {
|
55
|
56
|
/** Reads and verifies a single line with only a trailing LF. */
|
56
|
57
|
@Test
|
57
|
58
|
public void testReadLineWithOnlyLF() throws IOException {
|
58
|
|
- final InputStream stream = mock(InputStream.class);
|
|
59
|
+ final InputStream stream = new ByteArrayInputStream("test\n".getBytes());
|
59
|
60
|
final Encoder encoder = mock(Encoder.class);
|
60
|
61
|
|
61
|
|
- when(stream.read()).thenReturn((int) 't', (int) 'e', (int) 's',
|
62
|
|
- (int) 't', (int) '\n');
|
63
|
|
-
|
64
|
62
|
final IRCReader reader = new IRCReader(stream, encoder);
|
65
|
63
|
final ReadLine line = reader.readLine();
|
66
|
64
|
|
|
@@ -70,12 +68,9 @@ public class IRCReaderTest {
|
70
|
68
|
/** Reads and verifies a single line with a trailing parameter. */
|
71
|
69
|
@Test
|
72
|
70
|
public void testReadLineWithParameter() throws IOException {
|
73
|
|
- final InputStream stream = mock(InputStream.class);
|
|
71
|
+ final InputStream stream = new ByteArrayInputStream("te t :foo\r\n".getBytes());
|
74
|
72
|
final Encoder encoder = mock(Encoder.class);
|
75
|
73
|
|
76
|
|
- when(stream.read()).thenReturn((int) 't', (int) 'e', (int) ' ',
|
77
|
|
- (int) 't', (int) ' ', (int) ':', (int) 'f', (int) 'o',
|
78
|
|
- (int) 'o', (int) '\r', (int) '\n');
|
79
|
74
|
when(encoder.encode((String) isNull(), (String) isNull(),
|
80
|
75
|
(byte[]) anyObject(), anyInt(), eq(3)))
|
81
|
76
|
.thenReturn("encoded");
|
|
@@ -90,15 +85,9 @@ public class IRCReaderTest {
|
90
|
85
|
/** Reads and verifies a single line with a trailing parameter. */
|
91
|
86
|
@Test
|
92
|
87
|
public void testReadLineWithMultipleSpaces() throws IOException {
|
93
|
|
- final InputStream stream = mock(InputStream.class);
|
|
88
|
+ final InputStream stream = new ByteArrayInputStream("foo bar baz :qux baz\r\n".getBytes());
|
94
|
89
|
final Encoder encoder = mock(Encoder.class);
|
95
|
90
|
|
96
|
|
- when(stream.read()).thenReturn((int) 'f', (int) 'o', (int) 'o',
|
97
|
|
- (int) ' ', (int) 'b', (int) 'a', (int) 'r',
|
98
|
|
- (int) ' ', (int) ' ', (int) 'b', (int) 'a', (int) 'z',
|
99
|
|
- (int) ' ', (int) ' ', (int) ':', (int) 'q', (int) 'u',
|
100
|
|
- (int) 'x', (int) ' ', (int) ' ', (int) 'b', (int) 'a', (int) 'z',
|
101
|
|
- (int) '\r', (int) '\n');
|
102
|
91
|
when(encoder.encode((String) isNull(), (String) isNull(),
|
103
|
92
|
(byte[]) anyObject(), eq(15), eq(8)))
|
104
|
93
|
.thenReturn("qux baz");
|
|
@@ -106,20 +95,15 @@ public class IRCReaderTest {
|
106
|
95
|
final IRCReader reader = new IRCReader(stream, encoder);
|
107
|
96
|
final ReadLine line = reader.readLine();
|
108
|
97
|
|
109
|
|
- assertArrayEquals(new String[]{"foo", "bar", "baz", "qux baz",},
|
110
|
|
- line.getTokens());
|
|
98
|
+ assertArrayEquals(new String[]{"foo", "bar", "baz", "qux baz",}, line.getTokens());
|
111
|
99
|
}
|
112
|
100
|
|
113
|
101
|
/** Verifies that a source is extracted properly. */
|
114
|
102
|
@Test
|
115
|
103
|
public void testGetSource() throws IOException {
|
116
|
|
- final InputStream stream = mock(InputStream.class);
|
|
104
|
+ final InputStream stream = new ByteArrayInputStream(":src x :o\r\n".getBytes());
|
117
|
105
|
final Encoder encoder = mock(Encoder.class);
|
118
|
106
|
|
119
|
|
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
|
120
|
|
- (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
|
121
|
|
- (int) 'o', (int) '\r', (int) '\n');
|
122
|
|
-
|
123
|
107
|
new IRCReader(stream, encoder).readLine();
|
124
|
108
|
|
125
|
109
|
verify(encoder).encode(eq("src"), anyString(),
|
|
@@ -129,13 +113,9 @@ public class IRCReaderTest {
|
129
|
113
|
/** Verifies that no source is passed if the source is empty. */
|
130
|
114
|
@Test
|
131
|
115
|
public void testGetEmptySource() throws IOException {
|
132
|
|
- final InputStream stream = mock(InputStream.class);
|
|
116
|
+ final InputStream stream = new ByteArrayInputStream(": rc x :o\r\n".getBytes());
|
133
|
117
|
final Encoder encoder = mock(Encoder.class);
|
134
|
118
|
|
135
|
|
- when(stream.read()).thenReturn((int) ':', (int) ' ', (int) 'r',
|
136
|
|
- (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
|
137
|
|
- (int) 'o', (int) '\r', (int) '\n');
|
138
|
|
-
|
139
|
119
|
new IRCReader(stream, encoder).readLine();
|
140
|
120
|
|
141
|
121
|
verify(encoder).encode((String) isNull(), anyString(),
|
|
@@ -145,13 +125,9 @@ public class IRCReaderTest {
|
145
|
125
|
/** Verifies that a destination is extracted properly. */
|
146
|
126
|
@Test
|
147
|
127
|
public void testGetDestination() throws IOException {
|
148
|
|
- final InputStream stream = mock(InputStream.class);
|
|
128
|
+ final InputStream stream = new ByteArrayInputStream(":src x y :z\r\n".getBytes());
|
149
|
129
|
final Encoder encoder = mock(Encoder.class);
|
150
|
130
|
|
151
|
|
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
|
152
|
|
- (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) 'y',
|
153
|
|
- (int) ' ', (int) ':', (int) 'z', (int) '\r', (int) '\n');
|
154
|
|
-
|
155
|
131
|
new IRCReader(stream, encoder).readLine();
|
156
|
132
|
|
157
|
133
|
verify(encoder).encode(anyString(), eq("y"), (byte[]) anyObject(),
|
|
@@ -161,13 +137,9 @@ public class IRCReaderTest {
|
161
|
137
|
/** Verifies that no destination is extracted if there's no source. */
|
162
|
138
|
@Test
|
163
|
139
|
public void testGetDestinationNoSource() throws IOException {
|
164
|
|
- final InputStream stream = mock(InputStream.class);
|
|
140
|
+ final InputStream stream = new ByteArrayInputStream("_src x y :z\r\n".getBytes());
|
165
|
141
|
final Encoder encoder = mock(Encoder.class);
|
166
|
142
|
|
167
|
|
- when(stream.read()).thenReturn((int) '_', (int) 's', (int) 'r',
|
168
|
|
- (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) 'y',
|
169
|
|
- (int) ' ', (int) ':', (int) 'z', (int) '\r', (int) '\n');
|
170
|
|
-
|
171
|
143
|
new IRCReader(stream, encoder).readLine();
|
172
|
144
|
|
173
|
145
|
verify(encoder).encode(anyString(), (String) isNull(),
|
|
@@ -177,13 +149,9 @@ public class IRCReaderTest {
|
177
|
149
|
/** Verifies that no destination is extracted if there are too few args. */
|
178
|
150
|
@Test
|
179
|
151
|
public void testGetDestinationTooShort() throws IOException {
|
180
|
|
- final InputStream stream = mock(InputStream.class);
|
|
152
|
+ final InputStream stream = new ByteArrayInputStream(":src x :abz\r\n".getBytes());
|
181
|
153
|
final Encoder encoder = mock(Encoder.class);
|
182
|
154
|
|
183
|
|
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
|
184
|
|
- (int) 'c', (int) ' ', (int) 'x', (int) ' ', (int) ':',
|
185
|
|
- (int) 'a', (int) 'b', (int) 'z', (int) '\r', (int) '\n');
|
186
|
|
-
|
187
|
155
|
new IRCReader(stream, encoder).readLine();
|
188
|
156
|
|
189
|
157
|
verify(encoder).encode(anyString(), (String) isNull(),
|
|
@@ -193,18 +161,12 @@ public class IRCReaderTest {
|
193
|
161
|
/** Verifies that a numeric's destination is extracted properly. */
|
194
|
162
|
@Test
|
195
|
163
|
public void testGetDestinationWithNumeric() throws IOException {
|
196
|
|
- final InputStream stream = mock(InputStream.class);
|
|
164
|
+ final InputStream stream = new ByteArrayInputStream(":src 1 x y z :x\r\n".getBytes());
|
197
|
165
|
final Encoder encoder = mock(Encoder.class);
|
198
|
166
|
|
199
|
|
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
|
200
|
|
- (int) 'c', (int) ' ', (int) '1', (int) ' ', (int) 'x',
|
201
|
|
- (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
|
202
|
|
- (int) ':', (int) 'x', (int) '\r', (int) '\n');
|
203
|
|
-
|
204
|
167
|
new IRCReader(stream, encoder).readLine();
|
205
|
168
|
|
206
|
|
- verify(encoder).encode(anyString(), eq("y"),
|
207
|
|
- (byte[]) anyObject(), anyInt(), anyInt());
|
|
169
|
+ verify(encoder).encode(anyString(), eq("y"), (byte[]) anyObject(), anyInt(), anyInt());
|
208
|
170
|
}
|
209
|
171
|
|
210
|
172
|
/** Verifies that the close call is proxied to the stream. */
|
|
@@ -224,20 +186,95 @@ public class IRCReaderTest {
|
224
|
186
|
final InputStream stream = mock(InputStream.class);
|
225
|
187
|
final Encoder encoder = mock(Encoder.class);
|
226
|
188
|
|
227
|
|
- when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r',
|
228
|
|
- (int) 'c', (int) ' ', (int) '1', (int) ' ', 0xF6,
|
229
|
|
- (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
|
|
189
|
+ when(stream.read()).thenReturn((int) ':', (int) 's', (int) 'r', (int) 'c', (int) ' ',
|
|
190
|
+ (int) '1', (int) ' ', 0xF6, (int) ' ', (int) 'y', (int) ' ', (int) 'z', (int) ' ',
|
230
|
191
|
(int) ':', (int) 'x', (int) '\r', (int) '\n');
|
231
|
192
|
|
232
|
193
|
when(encoder.encode(anyString(), anyString(),
|
233
|
194
|
(byte[]) any(), anyInt(), anyInt())).thenReturn("x");
|
234
|
195
|
|
235
|
|
- final ReadLine line = new IRCReader(stream, encoder,
|
236
|
|
- Charset.forName("UTF-8")).readLine();
|
|
196
|
+ final ReadLine line = new IRCReader(stream, encoder, Charset.forName("UTF-8")).readLine();
|
|
197
|
+
|
|
198
|
+ Assert.assertArrayEquals(new String[]{":src", "1", "\uFFFD", "y", "z", "x"}, line.getTokens());
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ /** Verify tokeniser with TSIRC Time Stamp */
|
|
202
|
+ @Test
|
|
203
|
+ public void testReadLineTSIRC() throws IOException {
|
|
204
|
+ final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123@:test ing"));
|
237
|
205
|
|
238
|
|
- Assert.assertArrayEquals(new String[] {
|
239
|
|
- ":src", "1", "\uFFFD", "y", "z", "x"
|
240
|
|
- }, line.getTokens());
|
|
206
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
207
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
208
|
+ assertEquals("123", line.getTags().get("tsirc date"));
|
|
209
|
+ assertNull(line.getTags().get("time"));
|
241
|
210
|
}
|
242
|
211
|
|
|
212
|
+ /** Verify tokeniser with IRCv3 tags */
|
|
213
|
+ @Test
|
|
214
|
+ public void testReadLineIRCv3TS() throws IOException {
|
|
215
|
+ final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
216
|
+ final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing"));
|
|
217
|
+
|
|
218
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
219
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
220
|
+ assertNull(line.getTags().get("tsirc date"));
|
|
221
|
+ assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+ /** Verify tokeniser with a numeric tag timestamp */
|
|
226
|
+ @Test
|
|
227
|
+ public void testReadLineNumericTag() throws IOException {
|
|
228
|
+ final ReadLine line = new ReadLine("", IRCParser.tokeniseLine("@123 :test ing"));
|
|
229
|
+
|
|
230
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
231
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
232
|
+ assertTrue(line.getTags().containsKey("123"));
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+ /** Verify line with TSIRC Time Stamp */
|
|
237
|
+ @Test
|
|
238
|
+ public void testReaderTSIRC() throws IOException {
|
|
239
|
+ final InputStream stream = new ByteArrayInputStream("@123@:test ing\r\n".getBytes());
|
|
240
|
+ final Encoder encoder = mock(Encoder.class);
|
|
241
|
+
|
|
242
|
+ final IRCReader reader = new IRCReader(stream, encoder);
|
|
243
|
+ final ReadLine line = reader.readLine();
|
|
244
|
+
|
|
245
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
246
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
247
|
+ assertEquals("123", line.getTags().get("tsirc date"));
|
|
248
|
+ assertNull(line.getTags().get("time"));
|
|
249
|
+ }
|
|
250
|
+
|
|
251
|
+ /** Verify line with IRCv3 timestamp */
|
|
252
|
+ @Test
|
|
253
|
+ public void testReaderIRCv3TS() throws IOException {
|
|
254
|
+ final SimpleDateFormat servertime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
255
|
+ final InputStream stream = new ByteArrayInputStream(("@time="+servertime.format(new Date(123))+";something=else;foobar; :test ing\r\n").getBytes());
|
|
256
|
+ final Encoder encoder = mock(Encoder.class);
|
|
257
|
+
|
|
258
|
+ final IRCReader reader = new IRCReader(stream, encoder);
|
|
259
|
+ final ReadLine line = reader.readLine();
|
|
260
|
+
|
|
261
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
262
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
263
|
+ assertNull(line.getTags().get("tsirc date"));
|
|
264
|
+ assertEquals(servertime.format(new Date(123)), line.getTags().get("time"));
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+ /** Verify line with a numeric tag timestamp */
|
|
268
|
+ @Test
|
|
269
|
+ public void testReaderNumericTag() throws IOException {
|
|
270
|
+ final InputStream stream = new ByteArrayInputStream("@123 :test ing\r\n".getBytes());
|
|
271
|
+ final Encoder encoder = mock(Encoder.class);
|
|
272
|
+
|
|
273
|
+ final IRCReader reader = new IRCReader(stream, encoder);
|
|
274
|
+ final ReadLine line = reader.readLine();
|
|
275
|
+
|
|
276
|
+ assertEquals(":test", line.getTokens()[0]);
|
|
277
|
+ assertEquals("ing", line.getTokens()[1]);
|
|
278
|
+ assertTrue(line.getTags().containsKey("123"));
|
|
279
|
+ }
|
243
|
280
|
}
|