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