e2fc80bf38275b666fdf8f892eece519e1d80537
[nikiroo-utils.git] / src / be / nikiroo / utils / test_code / BufferedInputStreamTest.java
1 package be.nikiroo.utils.test_code;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5
6 import be.nikiroo.utils.BufferedInputStream;
7 import be.nikiroo.utils.IOUtils;
8 import be.nikiroo.utils.test.TestCase;
9 import be.nikiroo.utils.test.TestLauncher;
10
11 public class BufferedInputStreamTest extends TestLauncher {
12 public BufferedInputStreamTest(String[] args) {
13 super("BufferedInputStream test", args);
14
15 addTest(new TestCase("Simple InputStream reading") {
16 @Override
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);
22 }
23 });
24
25 addTest(new TestCase("Simple byte array reading") {
26 @Override
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);
31 }
32 });
33 }
34
35 static void checkArrays(TestCase test, String prefix, InputStream in,
36 byte[] expected) throws Exception {
37 byte[] actual = IOUtils.toByteArray(in);
38 test.assertEquals("The " + prefix
39 + " resulting array has not the correct number of items",
40 expected.length, actual.length);
41 for (int i = 0; i < actual.length; i++) {
42 test.assertEquals("Item " + i + " (0-based) is not the same",
43 expected[i], actual[i]);
44 }
45 }
46 }