1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.InputStream
;
6 import be
.nikiroo
.utils
.IOUtils
;
7 import be
.nikiroo
.utils
.streams
.BufferedInputStream
;
8 import be
.nikiroo
.utils
.test
.TestCase
;
9 import be
.nikiroo
.utils
.test
.TestLauncher
;
11 class BufferedInputStreamTest
extends TestLauncher
{
12 public BufferedInputStreamTest(String
[] args
) {
13 super("BufferedInputStream test", args
);
15 addTest(new TestCase("Simple InputStream reading") {
17 public void test() throws Exception
{
18 byte[] expected
= new byte[] { 42, 12, 0, 127 };
19 BufferedInputStream in
= new BufferedInputStream(
20 new ByteArrayInputStream(expected
));
21 checkArrays(this, "FIRST", in
, expected
);
25 addTest(new TestCase("Simple byte array reading") {
27 public void test() throws Exception
{
28 byte[] expected
= new byte[] { 42, 12, 0, 127 };
29 BufferedInputStream in
= new BufferedInputStream(expected
);
30 checkArrays(this, "FIRST", in
, expected
);
34 addTest(new TestCase("Byte array is(byte[])") {
36 public void test() throws Exception
{
37 byte[] expected
= new byte[] { 42, 12, 0, 127 };
38 BufferedInputStream in
= new BufferedInputStream(expected
);
40 "The array should be considered identical to its source",
41 true, in
.is(expected
));
43 "The array should be considered different to that one",
44 false, in
.is(new byte[] { 42, 12, 0, 121 }));
49 addTest(new TestCase("InputStream is(byte[])") {
51 public void test() throws Exception
{
52 byte[] expected
= new byte[] { 42, 12, 0, 127 };
53 BufferedInputStream in
= new BufferedInputStream(
54 new ByteArrayInputStream(expected
));
56 "The array should be considered identical to its source",
57 true, in
.is(expected
));
59 "The array should be considered different to that one",
60 false, in
.is(new byte[] { 42, 12, 0, 121 }));
65 addTest(new TestCase("Byte array is(String)") {
67 public void test() throws Exception
{
68 String expected
= "Testy";
69 BufferedInputStream in
= new BufferedInputStream(
70 expected
.getBytes("UTF-8"));
72 "The array should be considered identical to its source",
73 true, in
.is(expected
));
75 "The array should be considered different to that one",
76 false, in
.is("Autre"));
78 "The array should be considered different to that one",
79 false, in
.is("Test"));
84 addTest(new TestCase("InputStream is(String)") {
86 public void test() throws Exception
{
87 String expected
= "Testy";
88 BufferedInputStream in
= new BufferedInputStream(
89 new ByteArrayInputStream(expected
.getBytes("UTF-8")));
91 "The array should be considered identical to its source",
92 true, in
.is(expected
));
94 "The array should be considered different to that one",
95 false, in
.is("Autre"));
97 "The array should be considered different to that one",
98 false, in
.is("Testy."));
104 static void checkArrays(TestCase test
, String prefix
, InputStream in
,
105 byte[] expected
) throws Exception
{
106 byte[] actual
= IOUtils
.toByteArray(in
);
107 test
.assertEquals("The " + prefix
108 + " resulting array has not the correct number of items",
109 expected
.length
, actual
.length
);
110 for (int i
= 0; i
< actual
.length
; i
++) {
111 test
.assertEquals(prefix
+ ": item " + i
112 + " (0-based) is not the same", expected
[i
], actual
[i
]);