51ff128e5dc97e2ff104a6fb40da84c4eb2a10d9
1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.InputStream
;
6 import be
.nikiroo
.utils
.IOUtils
;
7 import be
.nikiroo
.utils
.NextableInputStream
;
8 import be
.nikiroo
.utils
.NextableInputStreamStep
;
9 import be
.nikiroo
.utils
.test
.TestCase
;
10 import be
.nikiroo
.utils
.test
.TestLauncher
;
12 public class NextableInputStreamTest
extends TestLauncher
{
13 public NextableInputStreamTest(String
[] args
) {
14 super("NextableInputStream test", args
);
16 addTest(new TestCase("Simple byte array reading") {
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
);
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
]);
34 addTest(new TestCase("Stop at 12") {
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
);
44 "The resulting array has not the correct number of items",
46 for (int i
= 0; i
< actual
.length
; i
++) {
47 assertEquals("Item " + i
+ " (0-based) is not the same",
48 expected
[i
], actual
[i
]);
53 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
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));
61 byte[] actual1
= IOUtils
.toByteArray(in
);
62 byte[] expected1
= new byte[] { 42 };
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
]);
71 assertEquals("Cannot get SECOND entry", true, in
.next());
72 byte[] actual2
= IOUtils
.toByteArray(in
);
73 byte[] expected2
= new byte[] { 0, 127 };
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
]);
82 assertEquals("Cannot get next THIRD entry", true, in
.next());
83 byte[] actual3
= IOUtils
.toByteArray(in
);
84 byte[] expected3
= new byte[] { 51, 11 };
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
]);