| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import java.io.ByteArrayInputStream; |
| 4 | |
| 5 | import be.nikiroo.utils.IOUtils; |
| 6 | import be.nikiroo.utils.NextableInputStream; |
| 7 | import be.nikiroo.utils.NextableInputStreamStep; |
| 8 | import be.nikiroo.utils.test.TestCase; |
| 9 | import be.nikiroo.utils.test.TestLauncher; |
| 10 | |
| 11 | public class NextableInputStreamTest extends TestLauncher { |
| 12 | public NextableInputStreamTest(String[] args) { |
| 13 | super("NextableInputStream test", args); |
| 14 | |
| 15 | addTest(new TestCase("Simple byte array reading") { |
| 16 | @Override |
| 17 | public void test() throws Exception { |
| 18 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 19 | NextableInputStream in = new NextableInputStream( |
| 20 | new ByteArrayInputStream(expected), null); |
| 21 | byte[] actual = IOUtils.toByteArray(in); |
| 22 | |
| 23 | assertEquals( |
| 24 | "The resulting array has not the same number of items", |
| 25 | expected.length, actual.length); |
| 26 | for (int i = 0; i < expected.length; i++) { |
| 27 | assertEquals("Item " + i + " (0-based) is not the same", |
| 28 | expected[i], actual[i]); |
| 29 | } |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | addTest(new TestCase("Stop at 12") { |
| 34 | @Override |
| 35 | public void test() throws Exception { |
| 36 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 37 | NextableInputStream in = new NextableInputStream( |
| 38 | new ByteArrayInputStream(expected), |
| 39 | new NextableInputStreamStep(12)); |
| 40 | |
| 41 | checkNext(this, false, "FIRST", in, new byte[] { 42 }); |
| 42 | } |
| 43 | }); |
| 44 | |
| 45 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { |
| 46 | @Override |
| 47 | public void test() throws Exception { |
| 48 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 49 | NextableInputStream in = new NextableInputStream( |
| 50 | new ByteArrayInputStream(data), |
| 51 | new NextableInputStreamStep(12)); |
| 52 | |
| 53 | checkNext(this, false, "FIRST", in, new byte[] { 42 }); |
| 54 | checkNext(this, true, "SECOND", in, new byte[] { 0, 127 }); |
| 55 | checkNext(this, true, "THIRD", in, new byte[] { 51, 11 }); |
| 56 | } |
| 57 | }); |
| 58 | |
| 59 | addTest(new TestCase("Encapsulation") { |
| 60 | @Override |
| 61 | public void test() throws Exception { |
| 62 | byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; |
| 63 | NextableInputStream in4 = new NextableInputStream( |
| 64 | new ByteArrayInputStream(data), |
| 65 | new NextableInputStreamStep(4)); |
| 66 | NextableInputStream subIn12 = new NextableInputStream(in4, |
| 67 | new NextableInputStreamStep(12)); |
| 68 | |
| 69 | checkNext(this, false, "SUB FIRST", subIn12, new byte[] { 42 }); |
| 70 | checkNext(this, true, "SUB SECOND", subIn12, new byte[] { 0 }); |
| 71 | |
| 72 | assertEquals("The subIn still has some data", false, |
| 73 | subIn12.next()); |
| 74 | |
| 75 | checkNext(this, true, "MAIN LAST", in4, |
| 76 | new byte[] { 127, 12, 5 }); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | addTest(new TestCase("UTF-8 text lines test") { |
| 81 | @Override |
| 82 | public void test() throws Exception { |
| 83 | String ln1 = "Ligne première"; |
| 84 | String ln2 = "Ligne la deuxième du nom"; |
| 85 | byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); |
| 86 | NextableInputStream in = new NextableInputStream( |
| 87 | new ByteArrayInputStream(data), |
| 88 | new NextableInputStreamStep('\n')); |
| 89 | |
| 90 | checkNext(this, false, "FIRST", in, ln1.getBytes("UTF-8")); |
| 91 | checkNext(this, true, "SECOND", in, ln2.getBytes("UTF-8")); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | static void checkNext(TestCase test, boolean callNext, String prefix, |
| 97 | NextableInputStream in, byte[] expected) throws Exception { |
| 98 | if (callNext) { |
| 99 | test.assertEquals("Cannot get " + prefix + " entry", true, |
| 100 | in.next()); |
| 101 | } |
| 102 | byte[] actual = IOUtils.toByteArray(in); |
| 103 | test.assertEquals("The " + prefix |
| 104 | + " resulting array has not the correct number of items", |
| 105 | expected.length, actual.length); |
| 106 | for (int i = 0; i < actual.length; i++) { |
| 107 | test.assertEquals("Item " + i + " (0-based) is not the same", |
| 108 | expected[i], actual[i]); |
| 109 | } |
| 110 | } |
| 111 | } |