1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
5 import be
.nikiroo
.utils
.IOUtils
;
6 import be
.nikiroo
.utils
.streams
.NextableInputStream
;
7 import be
.nikiroo
.utils
.streams
.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 checkNext(this, "READ", in
, expected
);
25 addTest(new TestCase("Stop at 12") {
27 public void test() throws Exception
{
28 byte[] expected
= new byte[] { 42, 12, 0, 127 };
29 NextableInputStream in
= new NextableInputStream(
30 new ByteArrayInputStream(expected
),
31 new NextableInputStreamStep(12));
33 checkNext(this, "FIRST", in
, new byte[] { 42 });
37 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
39 public void test() throws Exception
{
40 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
41 NextableInputStream in
= new NextableInputStream(
42 new ByteArrayInputStream(data
),
43 new NextableInputStreamStep(12));
45 checkNext(this, "FIRST", in
, new byte[] { 42 });
46 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
47 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
51 addTest(new TestCase("Encapsulation") {
53 public void test() throws Exception
{
54 byte[] data
= new byte[] { 42, 12, 0, 4, 127, 12, 5 };
55 NextableInputStream in4
= new NextableInputStream(
56 new ByteArrayInputStream(data
),
57 new NextableInputStreamStep(4));
58 NextableInputStream subIn12
= new NextableInputStream(in4
,
59 new NextableInputStreamStep(12));
62 checkNext(this, "SUB FIRST", subIn12
, new byte[] { 42 });
63 checkNext(this, "SUB SECOND", subIn12
, new byte[] { 0 });
65 assertEquals("The subIn still has some data", false,
68 checkNext(this, "MAIN LAST", in4
, new byte[] { 127, 12, 5 });
72 addTest(new TestCase("UTF-8 text lines test") {
74 public void test() throws Exception
{
75 String ln1
= "Ligne première";
76 String ln2
= "Ligne la deuxième du nom";
77 byte[] data
= (ln1
+ "\n" + ln2
).getBytes("UTF-8");
78 NextableInputStream in
= new NextableInputStream(
79 new ByteArrayInputStream(data
),
80 new NextableInputStreamStep('\n'));
82 checkNext(this, "FIRST", in
, ln1
.getBytes("UTF-8"));
83 checkNext(this, "SECOND", in
, ln2
.getBytes("UTF-8"));
87 addTest(new TestCase("nextAll()") {
89 public void test() throws Exception
{
90 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
91 NextableInputStream in
= new NextableInputStream(
92 new ByteArrayInputStream(data
),
93 new NextableInputStreamStep(12));
95 checkNext(this, "FIRST", in
, new byte[] { 42 });
96 checkNextAll(this, "REST", in
, new byte[] { 0, 127, 12, 51, 11,
98 assertEquals("The stream still has some data", false, in
.next());
102 addTest(new TestCase("getBytesRead()") {
104 public void test() throws Exception
{
105 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
106 NextableInputStream in
= new NextableInputStream(
107 new ByteArrayInputStream(data
),
108 new NextableInputStreamStep(12));
111 IOUtils
.toByteArray(in
);
113 assertEquals("The number of bytes read is not correct",
114 data
.length
, in
.getBytesRead());
118 addTest(new TestCase("bytes array input") {
120 public void test() throws Exception
{
121 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
122 NextableInputStream in
= new NextableInputStream(data
,
123 new NextableInputStreamStep(12));
125 checkNext(this, "FIRST", in
, new byte[] { 42 });
126 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
127 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
131 addTest(new TestCase("Skip data") {
133 public void test() throws Exception
{
134 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
135 NextableInputStream in
= new NextableInputStream(data
, null);
138 byte[] rest
= new byte[] { 12, 51, 11, 12 };
141 assertEquals("STARTS_WITH OK_1", true, in
.startsWith(rest
));
142 assertEquals("STARTS_WITH KO_1", false,
143 in
.startsWith(new byte[] { 0 }));
144 assertEquals("STARTS_WITH KO_2", false, in
.startsWith(data
));
145 assertEquals("STARTS_WITH KO_3", false,
146 in
.startsWith(new byte[] { 1, 2, 3 }));
147 assertEquals("STARTS_WITH OK_2", true, in
.startsWith(rest
));
148 assertEquals("READ REST", IOUtils
.readSmallStream(in
),
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 }));
173 "It actually does not start with that",
175 in
.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11,
180 "A search term bigger than the whole data cannot be found in the data",
181 false, in
.startsWith(new byte[] { 42, 12, 0, 127, 12,
188 addTest(new TestCase("Starts with strings") {
190 public void test() throws Exception
{
191 String text
= "Fanfan et Toto vont à la mer";
192 byte[] data
= text
.getBytes("UTF-8");
193 NextableInputStream in
= new NextableInputStream(data
, null);
197 assertEquals("It actually starts with that", true,
199 assertEquals("It actually starts with that", true,
200 in
.startsWith("Fanfan et"));
201 assertEquals("It actually is the same text", true,
202 in
.startsWith(text
));
205 assertEquals("It actually does not start with that", false,
206 in
.startsWith("Toto"));
207 assertEquals("It actually does not start with that", false,
208 in
.startsWith("Fanfan et Toto vont à la mee"));
212 "A search term bigger than the whole data cannot be found in the data",
213 false, in
.startsWith("Fanfan et Toto vont à la mer."));
219 addTest(new TestCase("Starts With strings + steps") {
221 public void test() throws Exception
{
222 String data
= "{\nREF: fanfan\n}";
223 NextableInputStream in
= new NextableInputStream(
224 data
.getBytes("UTF-8"), new NextableInputStreamStep(
228 assertEquals("STARTS_WITH OK", true, in
.startsWith("{"));
230 assertEquals("STARTS_WITH WHEN SPENT", false,
233 checkNext(this, "PARTIAL CONTENT", in
,
234 "REF: fanfan".getBytes("UTF-8"));
238 addTest(new TestCase("InputStream is(String)") {
240 public void test() throws Exception
{
241 String data
= "{\nREF: fanfan\n}";
242 NextableInputStream in
= new NextableInputStream(
243 new ByteArrayInputStream(data
.getBytes("UTF-8")),
244 new NextableInputStreamStep('\n'));
247 assertEquals("Item 1 OK", true, in
.is("{"));
248 assertEquals("Item 1 KO_1", false, in
.is("|"));
249 assertEquals("Item 1 KO_2", false, in
.is("{}"));
252 assertEquals("Item 2 OK", true, in
.is("REF: fanfan"));
253 assertEquals("Item 2 KO", false, in
.is("REF: fanfan."));
254 IOUtils
.readSmallStream(in
);
256 assertEquals("Item 3 OK", true, in
.is("}"));
262 addTest(new TestCase("Bytes NextAll test") {
264 public void test() throws Exception
{
265 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
266 NextableInputStream in
= new NextableInputStream(
267 new ByteArrayInputStream(data
),
268 new NextableInputStreamStep(12));
270 checkNext(this, "FIRST", in
, new byte[] { 42 });
271 checkNextAll(this, "SECOND", in
, new byte[] { 0, 127, 12, 51,
276 addTest(new TestCase("String NextAll test") {
278 public void test() throws Exception
{
279 String d1
= "^java.lang.String";
280 String d2
= "\"http://example.com/query.html\"";
281 String data
= d1
+ ":" + d2
;
282 NextableInputStream in
= new NextableInputStream(
283 new ByteArrayInputStream(data
.getBytes("UTF-8")),
284 new NextableInputStreamStep(':'));
286 checkNext(this, "FIRST", in
, d1
.getBytes("UTF-8"));
287 checkNextAll(this, "SECOND", in
, d2
.getBytes("UTF-8"));
291 addTest(new TestCase("NextAll in Next test") {
293 public void test() throws Exception
{
294 String line1
= "première ligne";
295 String d1
= "^java.lang.String";
296 String d2
= "\"http://example.com/query.html\"";
297 String line3
= "end of lines";
298 String data
= line1
+ "\n" + d1
+ ":" + d2
+ "\n" + line3
;
300 NextableInputStream inL
= new NextableInputStream(
301 new ByteArrayInputStream(data
.getBytes("UTF-8")),
302 new NextableInputStreamStep('\n'));
304 checkNext(this, "Line 1", inL
, line1
.getBytes("UTF-8"));
307 NextableInputStream in
= new NextableInputStream(inL
,
308 new NextableInputStreamStep(':'));
310 checkNext(this, "Line 2 FIRST", in
, d1
.getBytes("UTF-8"));
311 checkNextAll(this, "Line 2 SECOND", in
, d2
.getBytes("UTF-8"));
316 static void checkNext(TestCase test
, String prefix
, NextableInputStream in
,
317 byte[] expected
) throws Exception
{
318 test
.assertEquals("Cannot get " + prefix
+ " entry", true, in
.next());
319 checkArrays(test
, prefix
, in
, expected
);
322 static void checkNextAll(TestCase test
, String prefix
,
323 NextableInputStream in
, byte[] expected
) throws Exception
{
324 test
.assertEquals("Cannot get " + prefix
+ " entries", true,
326 checkArrays(test
, prefix
, in
, expected
);
329 static void checkArrays(TestCase test
, String prefix
,
330 NextableInputStream in
, byte[] expected
) throws Exception
{
331 byte[] actual
= IOUtils
.toByteArray(in
);
332 test
.assertEquals("The " + prefix
333 + " resulting array has not the correct number of items",
334 expected
.length
, actual
.length
);
335 for (int i
= 0; i
< actual
.length
; i
++) {
336 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
337 expected
[i
], actual
[i
]);