1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
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 NextableInputStream in
= new NextableInputStream(
21 new ByteArrayInputStream(expected
), null);
23 byte[] actual
= IOUtils
.toByteArray(in
);
26 "The resulting array has not the same number of items",
27 expected
.length
, actual
.length
);
28 for (int i
= 0; i
< expected
.length
; i
++) {
29 assertEquals("Item " + i
+ " (0-based) is not the same",
30 expected
[i
], actual
[i
]);
35 addTest(new TestCase("Stop at 12") {
37 public void test() throws Exception
{
38 byte[] expected
= new byte[] { 42, 12, 0, 127 };
39 NextableInputStream in
= new NextableInputStream(
40 new ByteArrayInputStream(expected
),
41 new NextableInputStreamStep(12));
43 checkNext(this, "FIRST", in
, new byte[] { 42 });
47 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
49 public void test() throws Exception
{
50 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
51 NextableInputStream in
= new NextableInputStream(
52 new ByteArrayInputStream(data
),
53 new NextableInputStreamStep(12));
55 checkNext(this, "FIRST", in
, new byte[] { 42 });
56 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
57 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
61 addTest(new TestCase("Encapsulation") {
63 public void test() throws Exception
{
64 byte[] data
= new byte[] { 42, 12, 0, 4, 127, 12, 5 };
65 NextableInputStream in4
= new NextableInputStream(
66 new ByteArrayInputStream(data
),
67 new NextableInputStreamStep(4));
68 NextableInputStream subIn12
= new NextableInputStream(in4
,
69 new NextableInputStreamStep(12));
72 checkNext(this, "SUB FIRST", subIn12
, new byte[] { 42 });
73 checkNext(this, "SUB SECOND", subIn12
, new byte[] { 0 });
75 assertEquals("The subIn still has some data", false,
78 checkNext(this, "MAIN LAST", in4
, new byte[] { 127, 12, 5 });
82 addTest(new TestCase("UTF-8 text lines test") {
84 public void test() throws Exception
{
85 String ln1
= "Ligne première";
86 String ln2
= "Ligne la deuxième du nom";
87 byte[] data
= (ln1
+ "\n" + ln2
).getBytes("UTF-8");
88 NextableInputStream in
= new NextableInputStream(
89 new ByteArrayInputStream(data
),
90 new NextableInputStreamStep('\n'));
92 checkNext(this, "FIRST", in
, ln1
.getBytes("UTF-8"));
93 checkNext(this, "SECOND", in
, ln2
.getBytes("UTF-8"));
97 addTest(new TestCase("nextAll()") {
99 public void test() throws Exception
{
100 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
101 NextableInputStream in
= new NextableInputStream(
102 new ByteArrayInputStream(data
),
103 new NextableInputStreamStep(12));
105 checkNext(this, "FIRST", in
, new byte[] { 42 });
106 checkNextAll(this, "REST", in
,
107 new byte[] { 0, 127, 12, 51, 11, 12 });
108 assertEquals("The stream still has some data", false,
113 addTest(new TestCase("getBytesRead()") {
115 public void test() throws Exception
{
116 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
117 NextableInputStream in
= new NextableInputStream(
118 new ByteArrayInputStream(data
),
119 new NextableInputStreamStep(12));
122 IOUtils
.toByteArray(in
);
124 assertEquals("The number of bytes read is not correct",
125 data
.length
, in
.getBytesRead());
129 addTest(new TestCase("bytes array input") {
131 public void test() throws Exception
{
132 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
133 NextableInputStream in
= new NextableInputStream(data
,
134 new NextableInputStreamStep(12));
136 checkNext(this, "FIRST", in
, new byte[] { 42 });
137 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
138 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
142 addTest(new TestCase("Skip data") {
144 public void test() throws Exception
{
145 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
146 NextableInputStream in
= new NextableInputStream(data
, null);
150 checkArrays(this, "ONLY", in
, new byte[] { 12, 51, 11, 12 });
154 addTest(new TestCase("Starts with") {
156 public void test() throws Exception
{
157 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
158 NextableInputStream in
= new NextableInputStream(data
, null);
162 assertEquals("It actually starts with that", true,
163 in
.startsWith(new byte[] { 42 }));
164 assertEquals("It actually starts with that", true,
165 in
.startsWith(new byte[] { 42, 12 }));
166 assertEquals("It actually is the same array", true,
167 in
.startsWith(data
));
170 assertEquals("It actually does not start with that", false,
171 in
.startsWith(new byte[] { 12 }));
172 assertEquals("It actually does not start with that", false,
174 new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 }));
179 new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 });
180 fail("Searching a prefix bigger than the array should throw an IOException");
181 } catch (IOException e
) {
186 addTest(new TestCase("Starts with strings") {
188 public void test() throws Exception
{
189 String text
= "Fanfan et Toto vont à la mer";
190 byte[] data
= text
.getBytes("UTF-8");
191 NextableInputStream in
= new NextableInputStream(data
, null);
195 assertEquals("It actually starts with that", true,
196 in
.startsWiths("F"));
197 assertEquals("It actually starts with that", true,
198 in
.startsWiths("Fanfan et"));
199 assertEquals("It actually is the same text", true,
200 in
.startsWiths(text
));
203 assertEquals("It actually does not start with that", false,
204 in
.startsWiths("Toto"));
205 assertEquals("It actually does not start with that", false,
206 in
.startsWiths("Fanfan et Toto vont à la mee"));
210 in
.startsWiths("Fanfan et Toto vont à la mer.");
211 fail("Searching a prefix bigger than the array should throw an IOException");
212 } catch (IOException e
) {
218 static void checkNext(TestCase test
, String prefix
, NextableInputStream in
,
219 byte[] expected
) throws Exception
{
220 test
.assertEquals("Cannot get " + prefix
+ " entry", true, in
.next());
221 checkArrays(test
, prefix
, in
, expected
);
224 static void checkNextAll(TestCase test
, String prefix
,
225 NextableInputStream in
, byte[] expected
) throws Exception
{
226 test
.assertEquals("Cannot get " + prefix
+ " entries", true,
228 checkArrays(test
, prefix
, in
, expected
);
231 static void checkArrays(TestCase test
, String prefix
,
232 NextableInputStream in
, byte[] expected
) throws Exception
{
233 byte[] actual
= IOUtils
.toByteArray(in
);
234 test
.assertEquals("The " + prefix
235 + " resulting array has not the correct number of items",
236 expected
.length
, actual
.length
);
237 for (int i
= 0; i
< actual
.length
; i
++) {
238 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
239 expected
[i
], actual
[i
]);