| 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 | in.next(); |
| 22 | byte[] actual = IOUtils.toByteArray(in); |
| 23 | |
| 24 | assertEquals( |
| 25 | "The resulting array has not the same number of items", |
| 26 | expected.length, actual.length); |
| 27 | for (int i = 0; i < expected.length; i++) { |
| 28 | assertEquals("Item " + i + " (0-based) is not the same", |
| 29 | expected[i], actual[i]); |
| 30 | } |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | addTest(new TestCase("Stop at 12") { |
| 35 | @Override |
| 36 | public void test() throws Exception { |
| 37 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 38 | NextableInputStream in = new NextableInputStream( |
| 39 | new ByteArrayInputStream(expected), |
| 40 | new NextableInputStreamStep(12)); |
| 41 | |
| 42 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 43 | } |
| 44 | }); |
| 45 | |
| 46 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { |
| 47 | @Override |
| 48 | public void test() throws Exception { |
| 49 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 50 | NextableInputStream in = new NextableInputStream( |
| 51 | new ByteArrayInputStream(data), |
| 52 | new NextableInputStreamStep(12)); |
| 53 | |
| 54 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 55 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); |
| 56 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | addTest(new TestCase("Encapsulation") { |
| 61 | @Override |
| 62 | public void test() throws Exception { |
| 63 | byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; |
| 64 | NextableInputStream in4 = new NextableInputStream( |
| 65 | new ByteArrayInputStream(data), |
| 66 | new NextableInputStreamStep(4)); |
| 67 | NextableInputStream subIn12 = new NextableInputStream(in4, |
| 68 | new NextableInputStreamStep(12)); |
| 69 | |
| 70 | in4.next(); |
| 71 | checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); |
| 72 | checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); |
| 73 | |
| 74 | assertEquals("The subIn still has some data", false, |
| 75 | subIn12.next()); |
| 76 | |
| 77 | checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | addTest(new TestCase("UTF-8 text lines test") { |
| 82 | @Override |
| 83 | public void test() throws Exception { |
| 84 | String ln1 = "Ligne première"; |
| 85 | String ln2 = "Ligne la deuxième du nom"; |
| 86 | byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); |
| 87 | NextableInputStream in = new NextableInputStream( |
| 88 | new ByteArrayInputStream(data), |
| 89 | new NextableInputStreamStep('\n')); |
| 90 | |
| 91 | checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); |
| 92 | checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | addTest(new TestCase("nextAll()") { |
| 97 | @Override |
| 98 | public void test() throws Exception { |
| 99 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 100 | NextableInputStream in = new NextableInputStream( |
| 101 | new ByteArrayInputStream(data), |
| 102 | new NextableInputStreamStep(12)); |
| 103 | |
| 104 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 105 | checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11, |
| 106 | 12 }); |
| 107 | assertEquals("The stream still has some data", false, in.next()); |
| 108 | } |
| 109 | }); |
| 110 | |
| 111 | addTest(new TestCase("getBytesRead()") { |
| 112 | @Override |
| 113 | public void test() throws Exception { |
| 114 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 115 | NextableInputStream in = new NextableInputStream( |
| 116 | new ByteArrayInputStream(data), |
| 117 | new NextableInputStreamStep(12)); |
| 118 | |
| 119 | in.nextAll(); |
| 120 | IOUtils.toByteArray(in); |
| 121 | |
| 122 | assertEquals("The number of bytes read is not correct", |
| 123 | data.length, in.getBytesRead()); |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | addTest(new TestCase("bytes array input") { |
| 128 | @Override |
| 129 | public void test() throws Exception { |
| 130 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 131 | NextableInputStream in = new NextableInputStream(data, |
| 132 | new NextableInputStreamStep(12)); |
| 133 | |
| 134 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 135 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); |
| 136 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); |
| 137 | } |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | static void checkNext(TestCase test, String prefix, NextableInputStream in, |
| 142 | byte[] expected) throws Exception { |
| 143 | test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); |
| 144 | byte[] actual = IOUtils.toByteArray(in); |
| 145 | test.assertEquals("The " + prefix |
| 146 | + " resulting array has not the correct number of items", |
| 147 | expected.length, actual.length); |
| 148 | for (int i = 0; i < actual.length; i++) { |
| 149 | test.assertEquals("Item " + i + " (0-based) is not the same", |
| 150 | expected[i], actual[i]); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void checkNextAll(TestCase test, String prefix, |
| 155 | NextableInputStream in, byte[] expected) throws Exception { |
| 156 | test.assertEquals("Cannot get " + prefix + " entries", true, |
| 157 | in.nextAll()); |
| 158 | byte[] actual = IOUtils.toByteArray(in); |
| 159 | test.assertEquals("The " + prefix |
| 160 | + " resulting array has not the correct number of items", |
| 161 | expected.length, actual.length); |
| 162 | for (int i = 0; i < actual.length; i++) { |
| 163 | test.assertEquals("Item " + i + " (0-based) is not the same", |
| 164 | expected[i], actual[i]); |
| 165 | } |
| 166 | } |
| 167 | } |