Browse Source

Unit test for IRC Reader

Change-Id: Ib2b5179775c4d6580382691d77fbd3318da28d16
Reviewed-on: http://gerrit.dmdirc.com/1776
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.6.5
Chris Smith 13 years ago
parent
commit
92943485f7
1 changed files with 195 additions and 0 deletions
  1. 195
    0
      test/com/dmdirc/parser/irc/IRCReaderTest.java

+ 195
- 0
test/com/dmdirc/parser/irc/IRCReaderTest.java View File

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

Loading…
Cancel
Save