1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
5 import be
.nikiroo
.utils
.IOUtils
;
6 import be
.nikiroo
.utils
.NextableInputStream
;
7 import be
.nikiroo
.utils
.NextableInputStreamStep
;
8 import be
.nikiroo
.utils
.test
.TestCase
;
9 import be
.nikiroo
.utils
.test
.TestLauncher
;
11 public class NextableInputStreamTest
extends TestLauncher
{
12 public NextableInputStreamTest(String
[] args
) {
13 super("NextableInputStream test", args
);
15 addTest(new TestCase("Simple byte array reading") {
17 public void test() throws Exception
{
18 byte[] expected
= new byte[] { 42, 12, 0, 127 };
19 NextableInputStream in
= new NextableInputStream(
20 new ByteArrayInputStream(expected
), null);
21 byte[] actual
= IOUtils
.toByteArray(in
);
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
]);
33 addTest(new TestCase("Stop at 12") {
35 public void test() throws Exception
{
36 byte[] expected
= new byte[] { 42, 12, 0, 127 };
37 NextableInputStream in
= new NextableInputStream(
38 new ByteArrayInputStream(expected
),
39 new NextableInputStreamStep(12));
41 checkNext(this, false, "FIRST", in
, new byte[] { 42 });
45 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
47 public void test() throws Exception
{
48 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
49 NextableInputStream in
= new NextableInputStream(
50 new ByteArrayInputStream(data
),
51 new NextableInputStreamStep(12));
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 });
59 addTest(new TestCase("Encapsulation") {
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));
69 checkNext(this, false, "SUB FIRST", subIn12
, new byte[] { 42 });
70 checkNext(this, true, "SUB SECOND", subIn12
, new byte[] { 0 });
72 assertEquals("The subIn still has some data", false,
75 checkNext(this, true, "MAIN LAST", in4
,
76 new byte[] { 127, 12, 5 });
80 addTest(new TestCase("UTF-8 text lines test") {
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'));
90 checkNext(this, false, "FIRST", in
, ln1
.getBytes("UTF-8"));
91 checkNext(this, true, "SECOND", in
, ln2
.getBytes("UTF-8"));
96 static void checkNext(TestCase test
, boolean callNext
, String prefix
,
97 NextableInputStream in
, byte[] expected
) throws Exception
{
99 test
.assertEquals("Cannot get " + prefix
+ " entry", true,
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
]);