Commit | Line | Data |
---|---|---|
2e7584da NR |
1 | package be.nikiroo.utils.test_code; |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
2e7584da NR |
4 | |
5 | import be.nikiroo.utils.IOUtils; | |
8e76f6ab NR |
6 | import be.nikiroo.utils.streams.NextableInputStream; |
7 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
2e7584da NR |
8 | import be.nikiroo.utils.test.TestCase; |
9 | import be.nikiroo.utils.test.TestLauncher; | |
10 | ||
11 | public class NextableInputStreamTest extends TestLauncher { | |
12 | public NextableInputStreamTest(String[] args) { | |
13 | super("NextableInputStream test", args); | |
14 | ||
15 | addTest(new TestCase("Simple byte array reading") { | |
16 | @Override | |
17 | public void test() throws Exception { | |
18 | byte[] expected = new byte[] { 42, 12, 0, 127 }; | |
63b46ca9 NR |
19 | NextableInputStream in = new NextableInputStream( |
20 | new ByteArrayInputStream(expected), null); | |
d2219aa0 | 21 | checkNext(this, "READ", in, expected); |
2e7584da NR |
22 | } |
23 | }); | |
4098af70 N |
24 | |
25 | addTest(new TestCase("Stop at 12") { | |
26 | @Override | |
27 | public void test() throws Exception { | |
28 | byte[] expected = new byte[] { 42, 12, 0, 127 }; | |
63b46ca9 NR |
29 | NextableInputStream in = new NextableInputStream( |
30 | new ByteArrayInputStream(expected), | |
31 | new NextableInputStreamStep(12)); | |
4098af70 | 32 | |
d6f9bd9f | 33 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
4098af70 N |
34 | } |
35 | }); | |
36 | ||
37 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { | |
38 | @Override | |
39 | public void test() throws Exception { | |
63b46ca9 | 40 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
4098af70 | 41 | NextableInputStream in = new NextableInputStream( |
63b46ca9 NR |
42 | new ByteArrayInputStream(data), |
43 | new NextableInputStreamStep(12)); | |
4098af70 | 44 | |
d6f9bd9f NR |
45 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
46 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); | |
47 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); | |
63b46ca9 NR |
48 | } |
49 | }); | |
4098af70 | 50 | |
63b46ca9 NR |
51 | addTest(new TestCase("Encapsulation") { |
52 | @Override | |
53 | public void test() throws Exception { | |
54 | byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; | |
55 | NextableInputStream in4 = new NextableInputStream( | |
56 | new ByteArrayInputStream(data), | |
57 | new NextableInputStreamStep(4)); | |
58 | NextableInputStream subIn12 = new NextableInputStream(in4, | |
59 | new NextableInputStreamStep(12)); | |
4098af70 | 60 | |
d6f9bd9f NR |
61 | in4.next(); |
62 | checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); | |
63 | checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); | |
63b46ca9 NR |
64 | |
65 | assertEquals("The subIn still has some data", false, | |
66 | subIn12.next()); | |
dd3034f7 | 67 | |
d6f9bd9f | 68 | checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); |
dd3034f7 NR |
69 | } |
70 | }); | |
71 | ||
72 | addTest(new TestCase("UTF-8 text lines test") { | |
73 | @Override | |
74 | public void test() throws Exception { | |
75 | String ln1 = "Ligne première"; | |
76 | String ln2 = "Ligne la deuxième du nom"; | |
77 | byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); | |
78 | NextableInputStream in = new NextableInputStream( | |
79 | new ByteArrayInputStream(data), | |
80 | new NextableInputStreamStep('\n')); | |
81 | ||
d6f9bd9f NR |
82 | checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); |
83 | checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); | |
84 | } | |
85 | }); | |
86 | ||
87 | addTest(new TestCase("nextAll()") { | |
88 | @Override | |
89 | public void test() throws Exception { | |
90 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
91 | NextableInputStream in = new NextableInputStream( | |
92 | new ByteArrayInputStream(data), | |
93 | new NextableInputStreamStep(12)); | |
94 | ||
95 | checkNext(this, "FIRST", in, new byte[] { 42 }); | |
d251f3dd NR |
96 | checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11, |
97 | 12 }); | |
98 | assertEquals("The stream still has some data", false, in.next()); | |
d6f9bd9f NR |
99 | } |
100 | }); | |
101 | ||
102 | addTest(new TestCase("getBytesRead()") { | |
103 | @Override | |
104 | public void test() throws Exception { | |
105 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
106 | NextableInputStream in = new NextableInputStream( | |
107 | new ByteArrayInputStream(data), | |
108 | new NextableInputStreamStep(12)); | |
109 | ||
110 | in.nextAll(); | |
111 | IOUtils.toByteArray(in); | |
112 | ||
113 | assertEquals("The number of bytes read is not correct", | |
114 | data.length, in.getBytesRead()); | |
115 | } | |
116 | }); | |
117 | ||
118 | addTest(new TestCase("bytes array input") { | |
119 | @Override | |
120 | public void test() throws Exception { | |
121 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
122 | NextableInputStream in = new NextableInputStream(data, | |
123 | new NextableInputStreamStep(12)); | |
124 | ||
125 | checkNext(this, "FIRST", in, new byte[] { 42 }); | |
126 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); | |
127 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); | |
4098af70 N |
128 | } |
129 | }); | |
473e5f31 N |
130 | |
131 | addTest(new TestCase("Skip data") { | |
132 | @Override | |
133 | public void test() throws Exception { | |
134 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
135 | NextableInputStream in = new NextableInputStream(data, null); | |
136 | in.next(); | |
137 | ||
d251f3dd NR |
138 | byte[] rest = new byte[] { 12, 51, 11, 12 }; |
139 | ||
473e5f31 | 140 | in.skip(4); |
d251f3dd NR |
141 | assertEquals("STARTS_WITH OK_1", true, in.startsWith(rest)); |
142 | assertEquals("STARTS_WITH KO_1", false, | |
143 | in.startsWith(new byte[] { 0 })); | |
144 | assertEquals("STARTS_WITH KO_2", false, in.startsWith(data)); | |
145 | assertEquals("STARTS_WITH KO_3", false, | |
146 | in.startsWith(new byte[] { 1, 2, 3 })); | |
147 | assertEquals("STARTS_WITH OK_2", true, in.startsWith(rest)); | |
148 | assertEquals("READ REST", IOUtils.readSmallStream(in), | |
149 | new String(rest)); | |
150 | in.close(); | |
473e5f31 N |
151 | } |
152 | }); | |
153 | ||
154 | addTest(new TestCase("Starts with") { | |
155 | @Override | |
156 | public void test() throws Exception { | |
157 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
158 | NextableInputStream in = new NextableInputStream(data, null); | |
159 | in.next(); | |
160 | ||
161 | // yes | |
162 | assertEquals("It actually starts with that", true, | |
163 | in.startsWith(new byte[] { 42 })); | |
164 | assertEquals("It actually starts with that", true, | |
165 | in.startsWith(new byte[] { 42, 12 })); | |
166 | assertEquals("It actually is the same array", true, | |
167 | in.startsWith(data)); | |
168 | ||
169 | // no | |
170 | assertEquals("It actually does not start with that", false, | |
171 | in.startsWith(new byte[] { 12 })); | |
d251f3dd NR |
172 | assertEquals( |
173 | "It actually does not start with that", | |
174 | false, | |
175 | in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, | |
176 | 11 })); | |
473e5f31 N |
177 | |
178 | // too big | |
2e6426c0 NR |
179 | assertEquals( |
180 | "A search term bigger than the whole data cannot be found in the data", | |
181 | false, in.startsWith(new byte[] { 42, 12, 0, 127, 12, | |
182 | 51, 11, 12, 0 })); | |
d251f3dd NR |
183 | |
184 | in.close(); | |
473e5f31 N |
185 | } |
186 | }); | |
187 | ||
188 | addTest(new TestCase("Starts with strings") { | |
189 | @Override | |
190 | public void test() throws Exception { | |
191 | String text = "Fanfan et Toto vont à la mer"; | |
192 | byte[] data = text.getBytes("UTF-8"); | |
193 | NextableInputStream in = new NextableInputStream(data, null); | |
194 | in.next(); | |
195 | ||
196 | // yes | |
197 | assertEquals("It actually starts with that", true, | |
d251f3dd | 198 | in.startsWith("F")); |
473e5f31 | 199 | assertEquals("It actually starts with that", true, |
d251f3dd | 200 | in.startsWith("Fanfan et")); |
473e5f31 | 201 | assertEquals("It actually is the same text", true, |
d251f3dd | 202 | in.startsWith(text)); |
473e5f31 N |
203 | |
204 | // no | |
205 | assertEquals("It actually does not start with that", false, | |
d251f3dd | 206 | in.startsWith("Toto")); |
473e5f31 | 207 | assertEquals("It actually does not start with that", false, |
d251f3dd | 208 | in.startsWith("Fanfan et Toto vont à la mee")); |
2e6426c0 | 209 | |
473e5f31 | 210 | // too big |
2e6426c0 NR |
211 | assertEquals( |
212 | "A search term bigger than the whole data cannot be found in the data", | |
213 | false, in.startsWith("Fanfan et Toto vont à la mer.")); | |
d251f3dd NR |
214 | |
215 | in.close(); | |
216 | } | |
217 | }); | |
218 | ||
219 | addTest(new TestCase("Starts With strings + steps") { | |
220 | @Override | |
221 | public void test() throws Exception { | |
222 | String data = "{\nREF: fanfan\n}"; | |
223 | NextableInputStream in = new NextableInputStream( | |
224 | data.getBytes("UTF-8"), new NextableInputStreamStep( | |
225 | '\n')); | |
226 | in.next(); | |
227 | ||
228 | assertEquals("STARTS_WITH OK", true, in.startsWith("{")); | |
229 | in.skip(1); | |
230 | assertEquals("STARTS_WITH WHEN SPENT", false, | |
231 | in.startsWith("{")); | |
232 | ||
233 | checkNext(this, "PARTIAL CONTENT", in, | |
234 | "REF: fanfan".getBytes("UTF-8")); | |
235 | } | |
236 | }); | |
237 | ||
238 | addTest(new TestCase("InputStream is(String)") { | |
239 | @Override | |
240 | public void test() throws Exception { | |
241 | String data = "{\nREF: fanfan\n}"; | |
242 | NextableInputStream in = new NextableInputStream( | |
243 | new ByteArrayInputStream(data.getBytes("UTF-8")), | |
244 | new NextableInputStreamStep('\n')); | |
245 | ||
246 | in.next(); | |
247 | assertEquals("Item 1 OK", true, in.is("{")); | |
248 | assertEquals("Item 1 KO_1", false, in.is("|")); | |
249 | assertEquals("Item 1 KO_2", false, in.is("{}")); | |
250 | in.skip(1); | |
251 | in.next(); | |
252 | assertEquals("Item 2 OK", true, in.is("REF: fanfan")); | |
253 | assertEquals("Item 2 KO", false, in.is("REF: fanfan.")); | |
254 | IOUtils.readSmallStream(in); | |
255 | in.next(); | |
256 | assertEquals("Item 3 OK", true, in.is("}")); | |
257 | ||
258 | in.close(); | |
473e5f31 N |
259 | } |
260 | }); | |
9e075bff NR |
261 | |
262 | addTest(new TestCase("Bytes NextAll test") { | |
263 | @Override | |
264 | public void test() throws Exception { | |
265 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
266 | NextableInputStream in = new NextableInputStream( | |
267 | new ByteArrayInputStream(data), | |
268 | new NextableInputStreamStep(12)); | |
269 | ||
270 | checkNext(this, "FIRST", in, new byte[] { 42 }); | |
271 | checkNextAll(this, "SECOND", in, new byte[] { 0, 127, 12, 51, | |
272 | 11, 12 }); | |
273 | } | |
274 | }); | |
275 | ||
276 | addTest(new TestCase("String NextAll test") { | |
277 | @Override | |
278 | public void test() throws Exception { | |
279 | String d1 = "^java.lang.String"; | |
280 | String d2 = "\"http://example.com/query.html\""; | |
281 | String data = d1 + ":" + d2; | |
282 | NextableInputStream in = new NextableInputStream( | |
283 | new ByteArrayInputStream(data.getBytes("UTF-8")), | |
284 | new NextableInputStreamStep(':')); | |
285 | ||
286 | checkNext(this, "FIRST", in, d1.getBytes("UTF-8")); | |
287 | checkNextAll(this, "SECOND", in, d2.getBytes("UTF-8")); | |
288 | } | |
289 | }); | |
290 | ||
291 | addTest(new TestCase("NextAll in Next test") { | |
292 | @Override | |
293 | public void test() throws Exception { | |
294 | String line1 = "première ligne"; | |
295 | String d1 = "^java.lang.String"; | |
296 | String d2 = "\"http://example.com/query.html\""; | |
297 | String line3 = "end of lines"; | |
298 | String data = line1 + "\n" + d1 + ":" + d2 + "\n" + line3; | |
299 | ||
300 | NextableInputStream inL = new NextableInputStream( | |
301 | new ByteArrayInputStream(data.getBytes("UTF-8")), | |
302 | new NextableInputStreamStep('\n')); | |
303 | ||
304 | checkNext(this, "Line 1", inL, line1.getBytes("UTF-8")); | |
305 | inL.next(); | |
306 | ||
307 | NextableInputStream in = new NextableInputStream(inL, | |
308 | new NextableInputStreamStep(':')); | |
309 | ||
310 | checkNext(this, "Line 2 FIRST", in, d1.getBytes("UTF-8")); | |
311 | checkNextAll(this, "Line 2 SECOND", in, d2.getBytes("UTF-8")); | |
312 | } | |
313 | }); | |
2e7584da | 314 | } |
63b46ca9 | 315 | |
d6f9bd9f NR |
316 | static void checkNext(TestCase test, String prefix, NextableInputStream in, |
317 | byte[] expected) throws Exception { | |
318 | test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); | |
473e5f31 | 319 | checkArrays(test, prefix, in, expected); |
d6f9bd9f NR |
320 | } |
321 | ||
322 | static void checkNextAll(TestCase test, String prefix, | |
323 | NextableInputStream in, byte[] expected) throws Exception { | |
324 | test.assertEquals("Cannot get " + prefix + " entries", true, | |
325 | in.nextAll()); | |
473e5f31 N |
326 | checkArrays(test, prefix, in, expected); |
327 | } | |
328 | ||
329 | static void checkArrays(TestCase test, String prefix, | |
330 | NextableInputStream in, byte[] expected) throws Exception { | |
63b46ca9 NR |
331 | byte[] actual = IOUtils.toByteArray(in); |
332 | test.assertEquals("The " + prefix | |
333 | + " resulting array has not the correct number of items", | |
334 | expected.length, actual.length); | |
335 | for (int i = 0; i < actual.length; i++) { | |
336 | test.assertEquals("Item " + i + " (0-based) is not the same", | |
337 | expected[i], actual[i]); | |
338 | } | |
339 | } | |
2e7584da | 340 | } |