| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import java.io.ByteArrayInputStream; |
| 4 | import java.io.InputStream; |
| 5 | |
| 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; |
| 10 | |
| 11 | 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(prefix + ": item " + i |
| 43 | + " (0-based) is not the same", expected[i], actual[i]); |
| 44 | } |
| 45 | } |
| 46 | } |