| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import java.io.ByteArrayInputStream; |
| 4 | import java.io.IOException; |
| 5 | |
| 6 | import be.nikiroo.utils.IOUtils; |
| 7 | import be.nikiroo.utils.streams.NextableInputStream; |
| 8 | import be.nikiroo.utils.streams.NextableInputStreamStep; |
| 9 | import be.nikiroo.utils.test.TestCase; |
| 10 | import be.nikiroo.utils.test.TestLauncher; |
| 11 | |
| 12 | public class NextableInputStreamTest extends TestLauncher { |
| 13 | public NextableInputStreamTest(String[] args) { |
| 14 | super("NextableInputStream test", args); |
| 15 | |
| 16 | addTest(new TestCase("Simple byte array reading") { |
| 17 | @Override |
| 18 | public void test() throws Exception { |
| 19 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 20 | NextableInputStream in = new NextableInputStream( |
| 21 | new ByteArrayInputStream(expected), null); |
| 22 | in.next(); |
| 23 | byte[] actual = IOUtils.toByteArray(in); |
| 24 | |
| 25 | assertEquals( |
| 26 | "The resulting array has not the same number of items", |
| 27 | expected.length, actual.length); |
| 28 | for (int i = 0; i < expected.length; i++) { |
| 29 | assertEquals("Item " + i + " (0-based) is not the same", |
| 30 | expected[i], actual[i]); |
| 31 | } |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | addTest(new TestCase("Stop at 12") { |
| 36 | @Override |
| 37 | public void test() throws Exception { |
| 38 | byte[] expected = new byte[] { 42, 12, 0, 127 }; |
| 39 | NextableInputStream in = new NextableInputStream( |
| 40 | new ByteArrayInputStream(expected), |
| 41 | new NextableInputStreamStep(12)); |
| 42 | |
| 43 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { |
| 48 | @Override |
| 49 | public void test() throws Exception { |
| 50 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 51 | NextableInputStream in = new NextableInputStream( |
| 52 | new ByteArrayInputStream(data), |
| 53 | new NextableInputStreamStep(12)); |
| 54 | |
| 55 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 56 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); |
| 57 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | addTest(new TestCase("Encapsulation") { |
| 62 | @Override |
| 63 | public void test() throws Exception { |
| 64 | byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; |
| 65 | NextableInputStream in4 = new NextableInputStream( |
| 66 | new ByteArrayInputStream(data), |
| 67 | new NextableInputStreamStep(4)); |
| 68 | NextableInputStream subIn12 = new NextableInputStream(in4, |
| 69 | new NextableInputStreamStep(12)); |
| 70 | |
| 71 | in4.next(); |
| 72 | checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); |
| 73 | checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); |
| 74 | |
| 75 | assertEquals("The subIn still has some data", false, |
| 76 | subIn12.next()); |
| 77 | |
| 78 | checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); |
| 79 | } |
| 80 | }); |
| 81 | |
| 82 | addTest(new TestCase("UTF-8 text lines test") { |
| 83 | @Override |
| 84 | public void test() throws Exception { |
| 85 | String ln1 = "Ligne première"; |
| 86 | String ln2 = "Ligne la deuxième du nom"; |
| 87 | byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); |
| 88 | NextableInputStream in = new NextableInputStream( |
| 89 | new ByteArrayInputStream(data), |
| 90 | new NextableInputStreamStep('\n')); |
| 91 | |
| 92 | checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); |
| 93 | checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | addTest(new TestCase("nextAll()") { |
| 98 | @Override |
| 99 | public void test() throws Exception { |
| 100 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 101 | NextableInputStream in = new NextableInputStream( |
| 102 | new ByteArrayInputStream(data), |
| 103 | new NextableInputStreamStep(12)); |
| 104 | |
| 105 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 106 | checkNextAll(this, "REST", in, |
| 107 | new byte[] { 0, 127, 12, 51, 11, 12 }); |
| 108 | assertEquals("The stream still has some data", false, |
| 109 | in.next()); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | addTest(new TestCase("getBytesRead()") { |
| 114 | @Override |
| 115 | public void test() throws Exception { |
| 116 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 117 | NextableInputStream in = new NextableInputStream( |
| 118 | new ByteArrayInputStream(data), |
| 119 | new NextableInputStreamStep(12)); |
| 120 | |
| 121 | in.nextAll(); |
| 122 | IOUtils.toByteArray(in); |
| 123 | |
| 124 | assertEquals("The number of bytes read is not correct", |
| 125 | data.length, in.getBytesRead()); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | addTest(new TestCase("bytes array input") { |
| 130 | @Override |
| 131 | public void test() throws Exception { |
| 132 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 133 | NextableInputStream in = new NextableInputStream(data, |
| 134 | new NextableInputStreamStep(12)); |
| 135 | |
| 136 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 137 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); |
| 138 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | addTest(new TestCase("Skip data") { |
| 143 | @Override |
| 144 | public void test() throws Exception { |
| 145 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 146 | NextableInputStream in = new NextableInputStream(data, null); |
| 147 | in.next(); |
| 148 | |
| 149 | in.skip(4); |
| 150 | checkArrays(this, "ONLY", in, new byte[] { 12, 51, 11, 12 }); |
| 151 | } |
| 152 | }); |
| 153 | |
| 154 | addTest(new TestCase("Starts with") { |
| 155 | @Override |
| 156 | public void test() throws Exception { |
| 157 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 158 | NextableInputStream in = new NextableInputStream(data, null); |
| 159 | in.next(); |
| 160 | |
| 161 | // yes |
| 162 | assertEquals("It actually starts with that", true, |
| 163 | in.startsWith(new byte[] { 42 })); |
| 164 | assertEquals("It actually starts with that", true, |
| 165 | in.startsWith(new byte[] { 42, 12 })); |
| 166 | assertEquals("It actually is the same array", true, |
| 167 | in.startsWith(data)); |
| 168 | |
| 169 | // no |
| 170 | assertEquals("It actually does not start with that", false, |
| 171 | in.startsWith(new byte[] { 12 })); |
| 172 | assertEquals("It actually does not start with that", false, |
| 173 | in.startsWith( |
| 174 | new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 })); |
| 175 | |
| 176 | // too big |
| 177 | try { |
| 178 | in.startsWith( |
| 179 | new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 }); |
| 180 | fail("Searching a prefix bigger than the array should throw an IOException"); |
| 181 | } catch (IOException e) { |
| 182 | } |
| 183 | } |
| 184 | }); |
| 185 | |
| 186 | addTest(new TestCase("Starts with strings") { |
| 187 | @Override |
| 188 | public void test() throws Exception { |
| 189 | String text = "Fanfan et Toto vont à la mer"; |
| 190 | byte[] data = text.getBytes("UTF-8"); |
| 191 | NextableInputStream in = new NextableInputStream(data, null); |
| 192 | in.next(); |
| 193 | |
| 194 | // yes |
| 195 | assertEquals("It actually starts with that", true, |
| 196 | in.startsWiths("F")); |
| 197 | assertEquals("It actually starts with that", true, |
| 198 | in.startsWiths("Fanfan et")); |
| 199 | assertEquals("It actually is the same text", true, |
| 200 | in.startsWiths(text)); |
| 201 | |
| 202 | // no |
| 203 | assertEquals("It actually does not start with that", false, |
| 204 | in.startsWiths("Toto")); |
| 205 | assertEquals("It actually does not start with that", false, |
| 206 | in.startsWiths("Fanfan et Toto vont à la mee")); |
| 207 | |
| 208 | // too big |
| 209 | try { |
| 210 | in.startsWiths("Fanfan et Toto vont à la mer."); |
| 211 | fail("Searching a prefix bigger than the array should throw an IOException"); |
| 212 | } catch (IOException e) { |
| 213 | } |
| 214 | } |
| 215 | }); |
| 216 | } |
| 217 | |
| 218 | static void checkNext(TestCase test, String prefix, NextableInputStream in, |
| 219 | byte[] expected) throws Exception { |
| 220 | test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); |
| 221 | checkArrays(test, prefix, in, expected); |
| 222 | } |
| 223 | |
| 224 | static void checkNextAll(TestCase test, String prefix, |
| 225 | NextableInputStream in, byte[] expected) throws Exception { |
| 226 | test.assertEquals("Cannot get " + prefix + " entries", true, |
| 227 | in.nextAll()); |
| 228 | checkArrays(test, prefix, in, expected); |
| 229 | } |
| 230 | |
| 231 | static void checkArrays(TestCase test, String prefix, |
| 232 | NextableInputStream in, byte[] expected) throws Exception { |
| 233 | byte[] actual = IOUtils.toByteArray(in); |
| 234 | test.assertEquals("The " + prefix |
| 235 | + " resulting array has not the correct number of items", |
| 236 | expected.length, actual.length); |
| 237 | for (int i = 0; i < actual.length; i++) { |
| 238 | test.assertEquals("Item " + i + " (0-based) is not the same", |
| 239 | expected[i], actual[i]); |
| 240 | } |
| 241 | } |
| 242 | } |