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
.streams
.NextableInputStream
;
8 import be
.nikiroo
.utils
.streams
.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
, new byte[] { 0, 127, 12, 51, 11,
108 assertEquals("The stream still has some data", false, in
.next());
112 addTest(new TestCase("getBytesRead()") {
114 public void test() throws Exception
{
115 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
116 NextableInputStream in
= new NextableInputStream(
117 new ByteArrayInputStream(data
),
118 new NextableInputStreamStep(12));
121 IOUtils
.toByteArray(in
);
123 assertEquals("The number of bytes read is not correct",
124 data
.length
, in
.getBytesRead());
128 addTest(new TestCase("bytes array input") {
130 public void test() throws Exception
{
131 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
132 NextableInputStream in
= new NextableInputStream(data
,
133 new NextableInputStreamStep(12));
135 checkNext(this, "FIRST", in
, new byte[] { 42 });
136 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
137 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
141 addTest(new TestCase("Skip data") {
143 public void test() throws Exception
{
144 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
145 NextableInputStream in
= new NextableInputStream(data
, null);
148 byte[] rest
= new byte[] { 12, 51, 11, 12 };
151 assertEquals("STARTS_WITH OK_1", true, in
.startsWith(rest
));
152 assertEquals("STARTS_WITH KO_1", false,
153 in
.startsWith(new byte[] { 0 }));
154 assertEquals("STARTS_WITH KO_2", false, in
.startsWith(data
));
155 assertEquals("STARTS_WITH KO_3", false,
156 in
.startsWith(new byte[] { 1, 2, 3 }));
157 assertEquals("STARTS_WITH OK_2", true, in
.startsWith(rest
));
158 assertEquals("READ REST", IOUtils
.readSmallStream(in
),
164 addTest(new TestCase("Starts with") {
166 public void test() throws Exception
{
167 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
168 NextableInputStream in
= new NextableInputStream(data
, null);
172 assertEquals("It actually starts with that", true,
173 in
.startsWith(new byte[] { 42 }));
174 assertEquals("It actually starts with that", true,
175 in
.startsWith(new byte[] { 42, 12 }));
176 assertEquals("It actually is the same array", true,
177 in
.startsWith(data
));
180 assertEquals("It actually does not start with that", false,
181 in
.startsWith(new byte[] { 12 }));
183 "It actually does not start with that",
185 in
.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11,
190 in
.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, 12,
192 fail("Searching a prefix bigger than the array should throw an IOException");
193 } catch (IOException e
) {
200 addTest(new TestCase("Starts with strings") {
202 public void test() throws Exception
{
203 String text
= "Fanfan et Toto vont à la mer";
204 byte[] data
= text
.getBytes("UTF-8");
205 NextableInputStream in
= new NextableInputStream(data
, null);
209 assertEquals("It actually starts with that", true,
211 assertEquals("It actually starts with that", true,
212 in
.startsWith("Fanfan et"));
213 assertEquals("It actually is the same text", true,
214 in
.startsWith(text
));
217 assertEquals("It actually does not start with that", false,
218 in
.startsWith("Toto"));
219 assertEquals("It actually does not start with that", false,
220 in
.startsWith("Fanfan et Toto vont à la mee"));
224 in
.startsWith("Fanfan et Toto vont à la mer.");
225 fail("Searching a prefix bigger than the array should throw an IOException");
226 } catch (IOException e
) {
233 addTest(new TestCase("Starts With strings + steps") {
235 public void test() throws Exception
{
236 String data
= "{\nREF: fanfan\n}";
237 NextableInputStream in
= new NextableInputStream(
238 data
.getBytes("UTF-8"), new NextableInputStreamStep(
242 assertEquals("STARTS_WITH OK", true, in
.startsWith("{"));
244 assertEquals("STARTS_WITH WHEN SPENT", false,
247 checkNext(this, "PARTIAL CONTENT", in
,
248 "REF: fanfan".getBytes("UTF-8"));
252 addTest(new TestCase("InputStream is(String)") {
254 public void test() throws Exception
{
255 String data
= "{\nREF: fanfan\n}";
256 NextableInputStream in
= new NextableInputStream(
257 new ByteArrayInputStream(data
.getBytes("UTF-8")),
258 new NextableInputStreamStep('\n'));
261 assertEquals("Item 1 OK", true, in
.is("{"));
262 assertEquals("Item 1 KO_1", false, in
.is("|"));
263 assertEquals("Item 1 KO_2", false, in
.is("{}"));
266 assertEquals("Item 2 OK", true, in
.is("REF: fanfan"));
267 assertEquals("Item 2 KO", false, in
.is("REF: fanfan."));
268 IOUtils
.readSmallStream(in
);
270 assertEquals("Item 3 OK", true, in
.is("}"));
277 static void checkNext(TestCase test
, String prefix
, NextableInputStream in
,
278 byte[] expected
) throws Exception
{
279 test
.assertEquals("Cannot get " + prefix
+ " entry", true, in
.next());
280 checkArrays(test
, prefix
, in
, expected
);
283 static void checkNextAll(TestCase test
, String prefix
,
284 NextableInputStream in
, byte[] expected
) throws Exception
{
285 test
.assertEquals("Cannot get " + prefix
+ " entries", true,
287 checkArrays(test
, prefix
, in
, expected
);
290 static void checkArrays(TestCase test
, String prefix
,
291 NextableInputStream in
, byte[] expected
) throws Exception
{
292 byte[] actual
= IOUtils
.toByteArray(in
);
293 test
.assertEquals("The " + prefix
294 + " resulting array has not the correct number of items",
295 expected
.length
, actual
.length
);
296 for (int i
= 0; i
< actual
.length
; i
++) {
297 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
298 expected
[i
], actual
[i
]);