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);
22 byte[] actual
= IOUtils
.toByteArray(in
);
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
]);
34 addTest(new TestCase("Stop at 12") {
36 public void test() throws Exception
{
37 byte[] expected
= new byte[] { 42, 12, 0, 127 };
38 NextableInputStream in
= new NextableInputStream(
39 new ByteArrayInputStream(expected
),
40 new NextableInputStreamStep(12));
42 checkNext(this, "FIRST", in
, new byte[] { 42 });
46 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
48 public void test() throws Exception
{
49 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
50 NextableInputStream in
= new NextableInputStream(
51 new ByteArrayInputStream(data
),
52 new NextableInputStreamStep(12));
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 });
60 addTest(new TestCase("Encapsulation") {
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));
71 checkNext(this, "SUB FIRST", subIn12
, new byte[] { 42 });
72 checkNext(this, "SUB SECOND", subIn12
, new byte[] { 0 });
74 assertEquals("The subIn still has some data", false,
77 checkNext(this, "MAIN LAST", in4
, new byte[] { 127, 12, 5 });
81 addTest(new TestCase("UTF-8 text lines test") {
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'));
91 checkNext(this, "FIRST", in
, ln1
.getBytes("UTF-8"));
92 checkNext(this, "SECOND", in
, ln2
.getBytes("UTF-8"));
96 addTest(new TestCase("nextAll()") {
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));
104 checkNext(this, "FIRST", in
, new byte[] { 42 });
105 checkNextAll(this, "REST", in
, new byte[] { 0, 127, 12, 51, 11,
107 assertEquals("The stream still has some data", false, in
.next());
111 addTest(new TestCase("getBytesRead()") {
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));
120 IOUtils
.toByteArray(in
);
122 assertEquals("The number of bytes read is not correct",
123 data
.length
, in
.getBytesRead());
127 addTest(new TestCase("bytes array input") {
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));
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 });
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
]);
154 static void checkNextAll(TestCase test
, String prefix
,
155 NextableInputStream in
, byte[] expected
) throws Exception
{
156 test
.assertEquals("Cannot get " + prefix
+ " entries", true,
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
]);