Commit | Line | Data |
---|---|---|
2e7584da NR |
1 | package be.nikiroo.utils.test_code; |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
4 | import java.io.InputStream; | |
5 | ||
6 | import be.nikiroo.utils.IOUtils; | |
7 | import be.nikiroo.utils.NextableInputStream; | |
4098af70 | 8 | import be.nikiroo.utils.NextableInputStreamStep; |
2e7584da NR |
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 | InputStream bin = new ByteArrayInputStream(expected); | |
21 | NextableInputStream in = new NextableInputStream(bin); | |
22 | byte[] actual = IOUtils.toByteArray(in); | |
23 | ||
24 | assertEquals( | |
25 | "The resulting array has not the same number of items", | |
26 | expected.length, actual.length); | |
27 | for (int i = 0; i < expected.length; i++) { | |
28 | assertEquals("Item " + i + " (0-based) is not the same", | |
29 | expected[i], actual[i]); | |
30 | } | |
31 | } | |
32 | }); | |
4098af70 N |
33 | |
34 | addTest(new TestCase("Stop at 12") { | |
35 | @Override | |
36 | public void test() throws Exception { | |
37 | byte[] expected = new byte[] { 42, 12, 0, 127 }; | |
38 | InputStream bin = new ByteArrayInputStream(expected); | |
39 | NextableInputStream in = new NextableInputStream(bin); | |
40 | in.addStep(new NextableInputStreamStep(12)); | |
41 | byte[] actual = IOUtils.toByteArray(in); | |
42 | ||
43 | assertEquals( | |
44 | "The resulting array has not the correct number of items", | |
45 | 1, actual.length); | |
46 | for (int i = 0; i < actual.length; i++) { | |
47 | assertEquals("Item " + i + " (0-based) is not the same", | |
48 | expected[i], actual[i]); | |
49 | } | |
50 | } | |
51 | }); | |
52 | ||
53 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { | |
54 | @Override | |
55 | public void test() throws Exception { | |
56 | byte[] expected = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
57 | NextableInputStream in = new NextableInputStream( | |
58 | new ByteArrayInputStream(expected)); | |
59 | in.addStep(new NextableInputStreamStep(12)); | |
60 | ||
61 | byte[] actual1 = IOUtils.toByteArray(in); | |
62 | byte[] expected1 = new byte[] { 42 }; | |
63 | assertEquals( | |
64 | "The FIRST resulting array has not the correct number of items", | |
65 | expected1.length, actual1.length); | |
66 | for (int i = 0; i < actual1.length; i++) { | |
67 | assertEquals("Item " + i + " (0-based) is not the same", | |
68 | expected1[i], actual1[i]); | |
69 | } | |
70 | ||
71 | assertEquals("Cannot get SECOND entry", true, in.next()); | |
72 | byte[] actual2 = IOUtils.toByteArray(in); | |
73 | byte[] expected2 = new byte[] { 0, 127 }; | |
74 | assertEquals( | |
75 | "The SECOND resulting array has not the correct number of items", | |
76 | expected2.length, actual2.length); | |
77 | for (int i = 0; i < actual2.length; i++) { | |
78 | assertEquals("Item " + i + " (0-based) is not the same", | |
79 | expected2[i], actual2[i]); | |
80 | } | |
81 | ||
82 | assertEquals("Cannot get next THIRD entry", true, in.next()); | |
83 | byte[] actual3 = IOUtils.toByteArray(in); | |
84 | byte[] expected3 = new byte[] { 51, 11 }; | |
85 | assertEquals( | |
86 | "The THIRD resulting array has not the correct number of items", | |
87 | expected3.length, actual3.length); | |
88 | for (int i = 0; i < actual3.length; i++) { | |
89 | assertEquals("Item " + i + " (0-based) is not the same", | |
90 | expected3[i], actual3[i]); | |
91 | } | |
92 | } | |
93 | }); | |
2e7584da NR |
94 | } |
95 | } |