NextableInputStream: startsWith()
[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);
d6f9bd9f 21 in.next();
2e7584da
NR
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 };
63b46ca9
NR
38 NextableInputStream in = new NextableInputStream(
39 new ByteArrayInputStream(expected),
40 new NextableInputStreamStep(12));
4098af70 41
d6f9bd9f 42 checkNext(this, "FIRST", in, new byte[] { 42 });
4098af70
N
43 }
44 });
45
46 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
47 @Override
48 public void test() throws Exception {
63b46ca9 49 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
4098af70 50 NextableInputStream in = new NextableInputStream(
63b46ca9
NR
51 new ByteArrayInputStream(data),
52 new NextableInputStreamStep(12));
4098af70 53
d6f9bd9f
NR
54 checkNext(this, "FIRST", in, new byte[] { 42 });
55 checkNext(this, "SECOND", in, new byte[] { 0, 127 });
56 checkNext(this, "THIRD", in, new byte[] { 51, 11 });
63b46ca9
NR
57 }
58 });
4098af70 59
63b46ca9
NR
60 addTest(new TestCase("Encapsulation") {
61 @Override
62 public void test() throws Exception {
63 byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 };
64 NextableInputStream in4 = new NextableInputStream(
65 new ByteArrayInputStream(data),
66 new NextableInputStreamStep(4));
67 NextableInputStream subIn12 = new NextableInputStream(in4,
68 new NextableInputStreamStep(12));
4098af70 69
d6f9bd9f
NR
70 in4.next();
71 checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 });
72 checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 });
63b46ca9
NR
73
74 assertEquals("The subIn still has some data", false,
75 subIn12.next());
dd3034f7 76
d6f9bd9f 77 checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 });
dd3034f7
NR
78 }
79 });
80
81 addTest(new TestCase("UTF-8 text lines test") {
82 @Override
83 public void test() throws Exception {
84 String ln1 = "Ligne première";
85 String ln2 = "Ligne la deuxième du nom";
86 byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8");
87 NextableInputStream in = new NextableInputStream(
88 new ByteArrayInputStream(data),
89 new NextableInputStreamStep('\n'));
90
d6f9bd9f
NR
91 checkNext(this, "FIRST", in, ln1.getBytes("UTF-8"));
92 checkNext(this, "SECOND", in, ln2.getBytes("UTF-8"));
93 }
94 });
95
96 addTest(new TestCase("nextAll()") {
97 @Override
98 public void test() throws Exception {
99 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
100 NextableInputStream in = new NextableInputStream(
101 new ByteArrayInputStream(data),
102 new NextableInputStreamStep(12));
103
104 checkNext(this, "FIRST", in, new byte[] { 42 });
105 checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11,
106 12 });
107 assertEquals("The stream still has some data", false, in.next());
108 }
109 });
110
111 addTest(new TestCase("getBytesRead()") {
112 @Override
113 public void test() throws Exception {
114 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
115 NextableInputStream in = new NextableInputStream(
116 new ByteArrayInputStream(data),
117 new NextableInputStreamStep(12));
118
119 in.nextAll();
120 IOUtils.toByteArray(in);
121
122 assertEquals("The number of bytes read is not correct",
123 data.length, in.getBytesRead());
124 }
125 });
126
127 addTest(new TestCase("bytes array input") {
128 @Override
129 public void test() throws Exception {
130 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
131 NextableInputStream in = new NextableInputStream(data,
132 new NextableInputStreamStep(12));
133
134 checkNext(this, "FIRST", in, new byte[] { 42 });
135 checkNext(this, "SECOND", in, new byte[] { 0, 127 });
136 checkNext(this, "THIRD", in, new byte[] { 51, 11 });
4098af70
N
137 }
138 });
2e7584da 139 }
63b46ca9 140
d6f9bd9f
NR
141 static void checkNext(TestCase test, String prefix, NextableInputStream in,
142 byte[] expected) throws Exception {
143 test.assertEquals("Cannot get " + prefix + " entry", true, in.next());
144 byte[] actual = IOUtils.toByteArray(in);
145 test.assertEquals("The " + prefix
146 + " resulting array has not the correct number of items",
147 expected.length, actual.length);
148 for (int i = 0; i < actual.length; i++) {
149 test.assertEquals("Item " + i + " (0-based) is not the same",
150 expected[i], actual[i]);
63b46ca9 151 }
d6f9bd9f
NR
152 }
153
154 static void checkNextAll(TestCase test, String prefix,
155 NextableInputStream in, byte[] expected) throws Exception {
156 test.assertEquals("Cannot get " + prefix + " entries", true,
157 in.nextAll());
63b46ca9
NR
158 byte[] actual = IOUtils.toByteArray(in);
159 test.assertEquals("The " + prefix
160 + " resulting array has not the correct number of items",
161 expected.length, actual.length);
162 for (int i = 0; i < actual.length; i++) {
163 test.assertEquals("Item " + i + " (0-based) is not the same",
164 expected[i], actual[i]);
165 }
166 }
2e7584da 167}