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);
22 checkNext(this, "READ", in
, expected
);
26 addTest(new TestCase("Stop at 12") {
28 public void test() throws Exception
{
29 byte[] expected
= new byte[] { 42, 12, 0, 127 };
30 NextableInputStream in
= new NextableInputStream(
31 new ByteArrayInputStream(expected
),
32 new NextableInputStreamStep(12));
34 checkNext(this, "FIRST", in
, new byte[] { 42 });
38 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
40 public void test() throws Exception
{
41 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
42 NextableInputStream in
= new NextableInputStream(
43 new ByteArrayInputStream(data
),
44 new NextableInputStreamStep(12));
46 checkNext(this, "FIRST", in
, new byte[] { 42 });
47 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
48 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
52 addTest(new TestCase("Encapsulation") {
54 public void test() throws Exception
{
55 byte[] data
= new byte[] { 42, 12, 0, 4, 127, 12, 5 };
56 NextableInputStream in4
= new NextableInputStream(
57 new ByteArrayInputStream(data
),
58 new NextableInputStreamStep(4));
59 NextableInputStream subIn12
= new NextableInputStream(in4
,
60 new NextableInputStreamStep(12));
63 checkNext(this, "SUB FIRST", subIn12
, new byte[] { 42 });
64 checkNext(this, "SUB SECOND", subIn12
, new byte[] { 0 });
66 assertEquals("The subIn still has some data", false,
69 checkNext(this, "MAIN LAST", in4
, new byte[] { 127, 12, 5 });
73 addTest(new TestCase("UTF-8 text lines test") {
75 public void test() throws Exception
{
76 String ln1
= "Ligne première";
77 String ln2
= "Ligne la deuxième du nom";
78 byte[] data
= (ln1
+ "\n" + ln2
).getBytes("UTF-8");
79 NextableInputStream in
= new NextableInputStream(
80 new ByteArrayInputStream(data
),
81 new NextableInputStreamStep('\n'));
83 checkNext(this, "FIRST", in
, ln1
.getBytes("UTF-8"));
84 checkNext(this, "SECOND", in
, ln2
.getBytes("UTF-8"));
88 addTest(new TestCase("nextAll()") {
90 public void test() throws Exception
{
91 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
92 NextableInputStream in
= new NextableInputStream(
93 new ByteArrayInputStream(data
),
94 new NextableInputStreamStep(12));
96 checkNext(this, "FIRST", in
, new byte[] { 42 });
97 checkNextAll(this, "REST", in
, new byte[] { 0, 127, 12, 51, 11,
99 assertEquals("The stream still has some data", false, in
.next());
103 addTest(new TestCase("getBytesRead()") {
105 public void test() throws Exception
{
106 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
107 NextableInputStream in
= new NextableInputStream(
108 new ByteArrayInputStream(data
),
109 new NextableInputStreamStep(12));
112 IOUtils
.toByteArray(in
);
114 assertEquals("The number of bytes read is not correct",
115 data
.length
, in
.getBytesRead());
119 addTest(new TestCase("bytes array input") {
121 public void test() throws Exception
{
122 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
123 NextableInputStream in
= new NextableInputStream(data
,
124 new NextableInputStreamStep(12));
126 checkNext(this, "FIRST", in
, new byte[] { 42 });
127 checkNext(this, "SECOND", in
, new byte[] { 0, 127 });
128 checkNext(this, "THIRD", in
, new byte[] { 51, 11 });
132 addTest(new TestCase("Skip data") {
134 public void test() throws Exception
{
135 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
136 NextableInputStream in
= new NextableInputStream(data
, null);
139 byte[] rest
= new byte[] { 12, 51, 11, 12 };
142 assertEquals("STARTS_WITH OK_1", true, in
.startsWith(rest
));
143 assertEquals("STARTS_WITH KO_1", false,
144 in
.startsWith(new byte[] { 0 }));
145 assertEquals("STARTS_WITH KO_2", false, in
.startsWith(data
));
146 assertEquals("STARTS_WITH KO_3", false,
147 in
.startsWith(new byte[] { 1, 2, 3 }));
148 assertEquals("STARTS_WITH OK_2", true, in
.startsWith(rest
));
149 assertEquals("READ REST", IOUtils
.readSmallStream(in
),
155 addTest(new TestCase("Starts with") {
157 public void test() throws Exception
{
158 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
159 NextableInputStream in
= new NextableInputStream(data
, null);
163 assertEquals("It actually starts with that", true,
164 in
.startsWith(new byte[] { 42 }));
165 assertEquals("It actually starts with that", true,
166 in
.startsWith(new byte[] { 42, 12 }));
167 assertEquals("It actually is the same array", true,
168 in
.startsWith(data
));
171 assertEquals("It actually does not start with that", false,
172 in
.startsWith(new byte[] { 12 }));
174 "It actually does not start with that",
176 in
.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11,
181 in
.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, 12,
183 fail("Searching a prefix bigger than the array should throw an IOException");
184 } catch (IOException e
) {
191 addTest(new TestCase("Starts with strings") {
193 public void test() throws Exception
{
194 String text
= "Fanfan et Toto vont à la mer";
195 byte[] data
= text
.getBytes("UTF-8");
196 NextableInputStream in
= new NextableInputStream(data
, null);
200 assertEquals("It actually starts with that", true,
202 assertEquals("It actually starts with that", true,
203 in
.startsWith("Fanfan et"));
204 assertEquals("It actually is the same text", true,
205 in
.startsWith(text
));
208 assertEquals("It actually does not start with that", false,
209 in
.startsWith("Toto"));
210 assertEquals("It actually does not start with that", false,
211 in
.startsWith("Fanfan et Toto vont à la mee"));
215 in
.startsWith("Fanfan et Toto vont à la mer.");
216 fail("Searching a prefix bigger than the array should throw an IOException");
217 } catch (IOException e
) {
224 addTest(new TestCase("Starts With strings + steps") {
226 public void test() throws Exception
{
227 String data
= "{\nREF: fanfan\n}";
228 NextableInputStream in
= new NextableInputStream(
229 data
.getBytes("UTF-8"), new NextableInputStreamStep(
233 assertEquals("STARTS_WITH OK", true, in
.startsWith("{"));
235 assertEquals("STARTS_WITH WHEN SPENT", false,
238 checkNext(this, "PARTIAL CONTENT", in
,
239 "REF: fanfan".getBytes("UTF-8"));
243 addTest(new TestCase("InputStream is(String)") {
245 public void test() throws Exception
{
246 String data
= "{\nREF: fanfan\n}";
247 NextableInputStream in
= new NextableInputStream(
248 new ByteArrayInputStream(data
.getBytes("UTF-8")),
249 new NextableInputStreamStep('\n'));
252 assertEquals("Item 1 OK", true, in
.is("{"));
253 assertEquals("Item 1 KO_1", false, in
.is("|"));
254 assertEquals("Item 1 KO_2", false, in
.is("{}"));
257 assertEquals("Item 2 OK", true, in
.is("REF: fanfan"));
258 assertEquals("Item 2 KO", false, in
.is("REF: fanfan."));
259 IOUtils
.readSmallStream(in
);
261 assertEquals("Item 3 OK", true, in
.is("}"));
267 addTest(new TestCase("Bytes NextAll test") {
269 public void test() throws Exception
{
270 byte[] data
= new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
271 NextableInputStream in
= new NextableInputStream(
272 new ByteArrayInputStream(data
),
273 new NextableInputStreamStep(12));
275 checkNext(this, "FIRST", in
, new byte[] { 42 });
276 checkNextAll(this, "SECOND", in
, new byte[] { 0, 127, 12, 51,
281 addTest(new TestCase("String NextAll test") {
283 public void test() throws Exception
{
284 String d1
= "^java.lang.String";
285 String d2
= "\"http://example.com/query.html\"";
286 String data
= d1
+ ":" + d2
;
287 NextableInputStream in
= new NextableInputStream(
288 new ByteArrayInputStream(data
.getBytes("UTF-8")),
289 new NextableInputStreamStep(':'));
291 checkNext(this, "FIRST", in
, d1
.getBytes("UTF-8"));
292 checkNextAll(this, "SECOND", in
, d2
.getBytes("UTF-8"));
296 addTest(new TestCase("NextAll in Next test") {
298 public void test() throws Exception
{
299 String line1
= "première ligne";
300 String d1
= "^java.lang.String";
301 String d2
= "\"http://example.com/query.html\"";
302 String line3
= "end of lines";
303 String data
= line1
+ "\n" + d1
+ ":" + d2
+ "\n" + line3
;
305 NextableInputStream inL
= new NextableInputStream(
306 new ByteArrayInputStream(data
.getBytes("UTF-8")),
307 new NextableInputStreamStep('\n'));
309 checkNext(this, "Line 1", inL
, line1
.getBytes("UTF-8"));
312 NextableInputStream in
= new NextableInputStream(inL
,
313 new NextableInputStreamStep(':'));
315 checkNext(this, "Line 2 FIRST", in
, d1
.getBytes("UTF-8"));
316 checkNextAll(this, "Line 2 SECOND", in
, d2
.getBytes("UTF-8"));
321 static void checkNext(TestCase test
, String prefix
, NextableInputStream in
,
322 byte[] expected
) throws Exception
{
323 test
.assertEquals("Cannot get " + prefix
+ " entry", true, in
.next());
324 checkArrays(test
, prefix
, in
, expected
);
327 static void checkNextAll(TestCase test
, String prefix
,
328 NextableInputStream in
, byte[] expected
) throws Exception
{
329 test
.assertEquals("Cannot get " + prefix
+ " entries", true,
331 checkArrays(test
, prefix
, in
, expected
);
334 static void checkArrays(TestCase test
, String prefix
,
335 NextableInputStream in
, byte[] expected
) throws Exception
{
336 byte[] actual
= IOUtils
.toByteArray(in
);
337 test
.assertEquals("The " + prefix
338 + " resulting array has not the correct number of items",
339 expected
.length
, actual
.length
);
340 for (int i
= 0; i
< actual
.length
; i
++) {
341 test
.assertEquals("Item " + i
+ " (0-based) is not the same",
342 expected
[i
], actual
[i
]);