| 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 | addTest(new TestCase("Byte array is(byte[])") { |
| 35 | @Override |
| 36 | public void test() throws Exception { |
| 37 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 38 | BufferedInputStream in = new BufferedInputStream(expected); |
| 39 | assertEquals( |
| 40 | "The array should be considered identical to its source", |
| 41 | true, in.is(expected)); |
| 42 | assertEquals( |
| 43 | "The array should be considered different to that one", |
| 44 | false, in.is(new byte[] { 42, 12, 0, 121 })); |
| 45 | in.close(); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | addTest(new TestCase("InputStream is(byte[])") { |
| 50 | @Override |
| 51 | public void test() throws Exception { |
| 52 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 53 | BufferedInputStream in = new BufferedInputStream( |
| 54 | new ByteArrayInputStream(expected)); |
| 55 | assertEquals( |
| 56 | "The array should be considered identical to its source", |
| 57 | true, in.is(expected)); |
| 58 | assertEquals( |
| 59 | "The array should be considered different to that one", |
| 60 | false, in.is(new byte[] { 42, 12, 0, 121 })); |
| 61 | in.close(); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | addTest(new TestCase("Byte array is(String)") { |
| 66 | @Override |
| 67 | public void test() throws Exception { |
| 68 | String expected = "Testy"; |
| 69 | BufferedInputStream in = new BufferedInputStream( |
| 70 | expected.getBytes("UTF-8")); |
| 71 | assertEquals( |
| 72 | "The array should be considered identical to its source", |
| 73 | true, in.is(expected)); |
| 74 | assertEquals( |
| 75 | "The array should be considered different to that one", |
| 76 | false, in.is("Autre")); |
| 77 | assertEquals( |
| 78 | "The array should be considered different to that one", |
| 79 | false, in.is("Test")); |
| 80 | in.close(); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | addTest(new TestCase("InputStream is(String)") { |
| 85 | @Override |
| 86 | public void test() throws Exception { |
| 87 | String expected = "Testy"; |
| 88 | BufferedInputStream in = new BufferedInputStream( |
| 89 | new ByteArrayInputStream(expected.getBytes("UTF-8"))); |
| 90 | assertEquals( |
| 91 | "The array should be considered identical to its source", |
| 92 | true, in.is(expected)); |
| 93 | assertEquals( |
| 94 | "The array should be considered different to that one", |
| 95 | false, in.is("Autre")); |
| 96 | assertEquals( |
| 97 | "The array should be considered different to that one", |
| 98 | false, in.is("Testy.")); |
| 99 | in.close(); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | |
| 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]); |
| 113 | } |
| 114 | } |
| 115 | } |