| 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, new byte[] { 0, 127, 12, 51, 11, |
| 107 | 12 }); |
| 108 | assertEquals("The stream still has some data", false, in.next()); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | addTest(new TestCase("getBytesRead()") { |
| 113 | @Override |
| 114 | public void test() throws Exception { |
| 115 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 116 | NextableInputStream in = new NextableInputStream( |
| 117 | new ByteArrayInputStream(data), |
| 118 | new NextableInputStreamStep(12)); |
| 119 | |
| 120 | in.nextAll(); |
| 121 | IOUtils.toByteArray(in); |
| 122 | |
| 123 | assertEquals("The number of bytes read is not correct", |
| 124 | data.length, in.getBytesRead()); |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | addTest(new TestCase("bytes array input") { |
| 129 | @Override |
| 130 | public void test() throws Exception { |
| 131 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 132 | NextableInputStream in = new NextableInputStream(data, |
| 133 | new NextableInputStreamStep(12)); |
| 134 | |
| 135 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
| 136 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); |
| 137 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | addTest(new TestCase("Skip data") { |
| 142 | @Override |
| 143 | public void test() throws Exception { |
| 144 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 145 | NextableInputStream in = new NextableInputStream(data, null); |
| 146 | in.next(); |
| 147 | |
| 148 | byte[] rest = new byte[] { 12, 51, 11, 12 }; |
| 149 | |
| 150 | in.skip(4); |
| 151 | assertEquals("STARTS_WITH OK_1", true, in.startsWith(rest)); |
| 152 | assertEquals("STARTS_WITH KO_1", false, |
| 153 | in.startsWith(new byte[] { 0 })); |
| 154 | assertEquals("STARTS_WITH KO_2", false, in.startsWith(data)); |
| 155 | assertEquals("STARTS_WITH KO_3", false, |
| 156 | in.startsWith(new byte[] { 1, 2, 3 })); |
| 157 | assertEquals("STARTS_WITH OK_2", true, in.startsWith(rest)); |
| 158 | assertEquals("READ REST", IOUtils.readSmallStream(in), |
| 159 | new String(rest)); |
| 160 | in.close(); |
| 161 | } |
| 162 | }); |
| 163 | |
| 164 | addTest(new TestCase("Starts with") { |
| 165 | @Override |
| 166 | public void test() throws Exception { |
| 167 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
| 168 | NextableInputStream in = new NextableInputStream(data, null); |
| 169 | in.next(); |
| 170 | |
| 171 | // yes |
| 172 | assertEquals("It actually starts with that", true, |
| 173 | in.startsWith(new byte[] { 42 })); |
| 174 | assertEquals("It actually starts with that", true, |
| 175 | in.startsWith(new byte[] { 42, 12 })); |
| 176 | assertEquals("It actually is the same array", true, |
| 177 | in.startsWith(data)); |
| 178 | |
| 179 | // no |
| 180 | assertEquals("It actually does not start with that", false, |
| 181 | in.startsWith(new byte[] { 12 })); |
| 182 | assertEquals( |
| 183 | "It actually does not start with that", |
| 184 | false, |
| 185 | in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, |
| 186 | 11 })); |
| 187 | |
| 188 | // too big |
| 189 | try { |
| 190 | in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, |
| 191 | 0 }); |
| 192 | fail("Searching a prefix bigger than the array should throw an IOException"); |
| 193 | } catch (IOException e) { |
| 194 | } |
| 195 | |
| 196 | in.close(); |
| 197 | } |
| 198 | }); |
| 199 | |
| 200 | addTest(new TestCase("Starts with strings") { |
| 201 | @Override |
| 202 | public void test() throws Exception { |
| 203 | String text = "Fanfan et Toto vont à la mer"; |
| 204 | byte[] data = text.getBytes("UTF-8"); |
| 205 | NextableInputStream in = new NextableInputStream(data, null); |
| 206 | in.next(); |
| 207 | |
| 208 | // yes |
| 209 | assertEquals("It actually starts with that", true, |
| 210 | in.startsWith("F")); |
| 211 | assertEquals("It actually starts with that", true, |
| 212 | in.startsWith("Fanfan et")); |
| 213 | assertEquals("It actually is the same text", true, |
| 214 | in.startsWith(text)); |
| 215 | |
| 216 | // no |
| 217 | assertEquals("It actually does not start with that", false, |
| 218 | in.startsWith("Toto")); |
| 219 | assertEquals("It actually does not start with that", false, |
| 220 | in.startsWith("Fanfan et Toto vont à la mee")); |
| 221 | |
| 222 | // too big |
| 223 | try { |
| 224 | in.startsWith("Fanfan et Toto vont à la mer."); |
| 225 | fail("Searching a prefix bigger than the array should throw an IOException"); |
| 226 | } catch (IOException e) { |
| 227 | } |
| 228 | |
| 229 | in.close(); |
| 230 | } |
| 231 | }); |
| 232 | |
| 233 | addTest(new TestCase("Starts With strings + steps") { |
| 234 | @Override |
| 235 | public void test() throws Exception { |
| 236 | String data = "{\nREF: fanfan\n}"; |
| 237 | NextableInputStream in = new NextableInputStream( |
| 238 | data.getBytes("UTF-8"), new NextableInputStreamStep( |
| 239 | '\n')); |
| 240 | in.next(); |
| 241 | |
| 242 | assertEquals("STARTS_WITH OK", true, in.startsWith("{")); |
| 243 | in.skip(1); |
| 244 | assertEquals("STARTS_WITH WHEN SPENT", false, |
| 245 | in.startsWith("{")); |
| 246 | |
| 247 | checkNext(this, "PARTIAL CONTENT", in, |
| 248 | "REF: fanfan".getBytes("UTF-8")); |
| 249 | } |
| 250 | }); |
| 251 | |
| 252 | addTest(new TestCase("InputStream is(String)") { |
| 253 | @Override |
| 254 | public void test() throws Exception { |
| 255 | String data = "{\nREF: fanfan\n}"; |
| 256 | NextableInputStream in = new NextableInputStream( |
| 257 | new ByteArrayInputStream(data.getBytes("UTF-8")), |
| 258 | new NextableInputStreamStep('\n')); |
| 259 | |
| 260 | in.next(); |
| 261 | assertEquals("Item 1 OK", true, in.is("{")); |
| 262 | assertEquals("Item 1 KO_1", false, in.is("|")); |
| 263 | assertEquals("Item 1 KO_2", false, in.is("{}")); |
| 264 | in.skip(1); |
| 265 | in.next(); |
| 266 | assertEquals("Item 2 OK", true, in.is("REF: fanfan")); |
| 267 | assertEquals("Item 2 KO", false, in.is("REF: fanfan.")); |
| 268 | IOUtils.readSmallStream(in); |
| 269 | in.next(); |
| 270 | assertEquals("Item 3 OK", true, in.is("}")); |
| 271 | |
| 272 | in.close(); |
| 273 | } |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | static void checkNext(TestCase test, String prefix, NextableInputStream in, |
| 278 | byte[] expected) throws Exception { |
| 279 | test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); |
| 280 | checkArrays(test, prefix, in, expected); |
| 281 | } |
| 282 | |
| 283 | static void checkNextAll(TestCase test, String prefix, |
| 284 | NextableInputStream in, byte[] expected) throws Exception { |
| 285 | test.assertEquals("Cannot get " + prefix + " entries", true, |
| 286 | in.nextAll()); |
| 287 | checkArrays(test, prefix, in, expected); |
| 288 | } |
| 289 | |
| 290 | static void checkArrays(TestCase test, String prefix, |
| 291 | NextableInputStream in, byte[] expected) throws Exception { |
| 292 | byte[] actual = IOUtils.toByteArray(in); |
| 293 | test.assertEquals("The " + prefix |
| 294 | + " resulting array has not the correct number of items", |
| 295 | expected.length, actual.length); |
| 296 | for (int i = 0; i < actual.length; i++) { |
| 297 | test.assertEquals("Item " + i + " (0-based) is not the same", |
| 298 | expected[i], actual[i]); |
| 299 | } |
| 300 | } |
| 301 | } |