|
| 1 | +package com.github.shyiko.mysql.binlog.io; |
| 2 | + |
| 3 | +import org.testng.annotations.Test; |
| 4 | + |
| 5 | +import static org.testng.Assert.assertEquals; |
| 6 | + |
| 7 | +public class ByteArrayInputStreamTest { |
| 8 | + @Test |
| 9 | + public void testReadToArray() throws Exception { |
| 10 | + byte[] buff = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
| 11 | + ByteArrayInputStream in = new ByteArrayInputStream(buff); |
| 12 | + assertEquals(in.getPosition(), 0); |
| 13 | + |
| 14 | + byte[] b = new byte[20]; |
| 15 | + |
| 16 | + int read = in.read(b, 0, 0); |
| 17 | + assertEquals(read, 0); |
| 18 | + assertEquals(in.getPosition(), 0); |
| 19 | + |
| 20 | + read = in.read(b, 0, 4); |
| 21 | + assertEquals(read, 4); |
| 22 | + assertEquals(b[3], 3); |
| 23 | + assertEquals(in.getPosition(), 4); |
| 24 | + |
| 25 | + read = in.read(b, 4, 4); |
| 26 | + assertEquals(read, 4); |
| 27 | + assertEquals(b[7], 7); |
| 28 | + assertEquals(in.getPosition(), 8); |
| 29 | + |
| 30 | + read = in.read(b, 8, 4); |
| 31 | + assertEquals(read, 4); |
| 32 | + assertEquals(b[11], 11); |
| 33 | + assertEquals(in.getPosition(), 12); |
| 34 | + |
| 35 | + read = in.read(b, 12, 4); |
| 36 | + assertEquals(read, 4); |
| 37 | + assertEquals(b[15], 15); |
| 38 | + assertEquals(in.getPosition(), 16); |
| 39 | + |
| 40 | + read = in.read(b, 16, 4); |
| 41 | + assertEquals(read, -1); |
| 42 | + assertEquals(in.getPosition(), 16); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testReadToArrayWithinBlockBoundaries() throws Exception { |
| 47 | + byte[] buff = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8}; |
| 48 | + ByteArrayInputStream in = new ByteArrayInputStream(buff); |
| 49 | + byte[] b = new byte[8]; |
| 50 | + |
| 51 | + in.enterBlock(4); |
| 52 | + |
| 53 | + int read = in.read(b, 0, 3); |
| 54 | + assertEquals(read, 3); |
| 55 | + assertEquals(b[2], 2); |
| 56 | + |
| 57 | + read = in.read(b, 3, 3); |
| 58 | + assertEquals(read, 1); |
| 59 | + assertEquals(b[3], 3); |
| 60 | + |
| 61 | + read = in.read(b, 4, 3); |
| 62 | + assertEquals(read, -1); |
| 63 | + } |
| 64 | + |
| 65 | + @Test(expectedExceptions = NullPointerException.class) |
| 66 | + public void testReadToArrayWithNullBuff() throws Exception { |
| 67 | + ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{}); |
| 68 | + in.read(null, 0, 4); |
| 69 | + } |
| 70 | + |
| 71 | + @Test(expectedExceptions = IndexOutOfBoundsException.class) |
| 72 | + public void testReadToArrayWhenLenExceedsBuffSize() throws Exception { |
| 73 | + ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); |
| 74 | + byte[] b = new byte[1]; |
| 75 | + in.read(b, 0, 4); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expectedExceptions = IndexOutOfBoundsException.class) |
| 79 | + public void testReadToArrayWhenOffsetNegative() throws Exception { |
| 80 | + ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); |
| 81 | + byte[] b = new byte[1]; |
| 82 | + in.read(b, -1, 1); |
| 83 | + } |
| 84 | + |
| 85 | + @Test(expectedExceptions = IndexOutOfBoundsException.class) |
| 86 | + public void testReadToArrayWhenLengthNegative() throws Exception { |
| 87 | + ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0, 1, 2}); |
| 88 | + byte[] b = new byte[1]; |
| 89 | + in.read(b, 0, -1); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testPeekAndReadToArray() throws Exception { |
| 94 | + ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{5, 6, 7}); |
| 95 | + byte[] b = new byte[3]; |
| 96 | + assertEquals(in.peek(), 5); |
| 97 | + int read = in.read(b, 0, 3); |
| 98 | + assertEquals(read, 3); |
| 99 | + assertEquals(b[0], 5); |
| 100 | + assertEquals(b[2], 7); |
| 101 | + } |
| 102 | +} |
0 commit comments