more tests
[fanfix.git] / src / be / nikiroo / utils / test_code / NextableInputStreamTest.java
CommitLineData
2e7584da
NR
1package be.nikiroo.utils.test_code;
2
3import java.io.ByteArrayInputStream;
2e7584da
NR
4
5import be.nikiroo.utils.IOUtils;
6import be.nikiroo.utils.NextableInputStream;
4098af70 7import be.nikiroo.utils.NextableInputStreamStep;
2e7584da
NR
8import be.nikiroo.utils.test.TestCase;
9import be.nikiroo.utils.test.TestLauncher;
10
11public 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 };
63b46ca9
NR
19 NextableInputStream in = new NextableInputStream(
20 new ByteArrayInputStream(expected), null);
2e7584da
NR
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 });
4098af70
N
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 };
63b46ca9
NR
37 NextableInputStream in = new NextableInputStream(
38 new ByteArrayInputStream(expected),
39 new NextableInputStreamStep(12));
4098af70 40
63b46ca9 41 checkNext(this, false, "FIRST", in, new byte[] { 42 });
4098af70
N
42 }
43 });
44
45 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
46 @Override
47 public void test() throws Exception {
63b46ca9 48 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
4098af70 49 NextableInputStream in = new NextableInputStream(
63b46ca9
NR
50 new ByteArrayInputStream(data),
51 new NextableInputStreamStep(12));
4098af70 52
63b46ca9
NR
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 });
4098af70 58
63b46ca9
NR
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));
4098af70 68
63b46ca9
NR
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());
dd3034f7
NR
74
75 checkNext(this, true, "MAIN LAST", in4,
76 new byte[] { 127, 12, 5 });
77 }
78 });
79
80 addTest(new TestCase("UTF-8 text lines test") {
81 @Override
82 public void test() throws Exception {
83 String ln1 = "Ligne première";
84 String ln2 = "Ligne la deuxième du nom";
85 byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8");
86 NextableInputStream in = new NextableInputStream(
87 new ByteArrayInputStream(data),
88 new NextableInputStreamStep('\n'));
89
90 checkNext(this, false, "FIRST", in, ln1.getBytes("UTF-8"));
91 checkNext(this, true, "SECOND", in, ln2.getBytes("UTF-8"));
4098af70
N
92 }
93 });
2e7584da 94 }
63b46ca9
NR
95
96 static void checkNext(TestCase test, boolean callNext, String prefix,
97 NextableInputStream in, byte[] expected) throws Exception {
98 if (callNext) {
99 test.assertEquals("Cannot get " + prefix + " entry", true,
100 in.next());
101 }
102 byte[] actual = IOUtils.toByteArray(in);
103 test.assertEquals("The " + prefix
104 + " resulting array has not the correct number of items",
105 expected.length, actual.length);
106 for (int i = 0; i < actual.length; i++) {
107 test.assertEquals("Item " + i + " (0-based) is not the same",
108 expected[i], actual[i]);
109 }
110 }
2e7584da 111}