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