Simple NextableInputStream
[nikiroo-utils.git] / src / be / nikiroo / utils / test_code / NextableInputStreamTest.java
1 package be.nikiroo.utils.test_code;
2
3 import java.io.ByteArrayInputStream;
4
5 import be.nikiroo.utils.IOUtils;
6 import be.nikiroo.utils.NextableInputStream;
7 import be.nikiroo.utils.NextableInputStreamStep;
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 };
19 NextableInputStream in = new NextableInputStream(
20 new ByteArrayInputStream(expected), null);
21 byte[] actual = IOUtils.toByteArray(in);
22
23 assertEquals(
24 "The resulting array has not the same number of items",
25 expected.length, actual.length);
26 for (int i = 0; i < expected.length; i++) {
27 assertEquals("Item " + i + " (0-based) is not the same",
28 expected[i], actual[i]);
29 }
30 }
31 });
32
33 addTest(new TestCase("Stop at 12") {
34 @Override
35 public void test() throws Exception {
36 byte[] expected = new byte[] { 42, 12, 0, 127 };
37 NextableInputStream in = new NextableInputStream(
38 new ByteArrayInputStream(expected),
39 new NextableInputStreamStep(12));
40
41 checkNext(this, false, "FIRST", in, new byte[] { 42 });
42 }
43 });
44
45 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
46 @Override
47 public void test() throws Exception {
48 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
49 NextableInputStream in = new NextableInputStream(
50 new ByteArrayInputStream(data),
51 new NextableInputStreamStep(12));
52
53 checkNext(this, false, "FIRST", in, new byte[] { 42 });
54 checkNext(this, true, "SECOND", in, new byte[] { 0, 127 });
55 checkNext(this, true, "THIRD", in, new byte[] { 51, 11 });
56 }
57 });
58
59 addTest(new TestCase("Encapsulation") {
60 @Override
61 public void test() throws Exception {
62 byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 };
63 NextableInputStream in4 = new NextableInputStream(
64 new ByteArrayInputStream(data),
65 new NextableInputStreamStep(4));
66 NextableInputStream subIn12 = new NextableInputStream(in4,
67 new NextableInputStreamStep(12));
68
69 checkNext(this, false, "SUB FIRST", subIn12, new byte[] { 42 });
70 checkNext(this, true, "SUB SECOND", subIn12, new byte[] { 0 });
71
72 assertEquals("The subIn still has some data", false,
73 subIn12.next());
74
75 checkNext(this, true, "MAIN LAST", in4, new byte[] { 127, 12, 5 });
76 }
77 });
78 }
79
80 static void checkNext(TestCase test, boolean callNext, String prefix,
81 NextableInputStream in, byte[] expected) throws Exception {
82 if (callNext) {
83 test.assertEquals("Cannot get " + prefix + " entry", true,
84 in.next());
85 }
86 byte[] actual = IOUtils.toByteArray(in);
87 test.assertEquals("The " + prefix
88 + " resulting array has not the correct number of items",
89 expected.length, actual.length);
90 for (int i = 0; i < actual.length; i++) {
91 test.assertEquals("Item " + i + " (0-based) is not the same",
92 expected[i], actual[i]);
93 }
94 }
95 }