|
| 1 | +/* |
| 2 | + * Copyright 2015 Stanley Shyiko |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.shyiko.mysql.binlog.io; |
| 17 | + |
| 18 | +import org.testng.annotations.Test; |
| 19 | + |
| 20 | +import java.io.ByteArrayInputStream; |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +import static org.testng.Assert.assertEquals; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author <a href="mailto:stanley.shyiko@gmail.com">Stanley Shyiko</a> |
| 27 | + */ |
| 28 | +public class BufferedSocketInputStreamTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testCorrectness() throws Exception { |
| 32 | + BufferedSocketInputStream in = new BufferedSocketInputStream(new ByteArrayInputStream(new byte[]{ |
| 33 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}), 5); |
| 34 | + assertEquals(in.read(), 0); |
| 35 | + assertEquals(in.read(), 1); |
| 36 | + byte[] buf = new byte[6]; |
| 37 | + assertEquals(in.read(buf, 0, buf.length), 3); // data remaining in BSIS buffer |
| 38 | + assertEquals(Arrays.copyOf(buf, 3), new byte[] {2, 3, 4}); |
| 39 | + assertEquals(in.read(buf, 0, buf.length), 6); |
| 40 | + assertEquals(buf, new byte[] {5, 6, 7, 8, 9, 10}); |
| 41 | + assertEquals(in.read(buf, 0, 3), 3); |
| 42 | + assertEquals(Arrays.copyOf(buf, 3), new byte[] {11, 12, 13}); |
| 43 | + assertEquals(in.read(buf, 0, 3), 2); // data remaining in BSIS buffer |
| 44 | + assertEquals(Arrays.copyOf(buf, 2), new byte[] {14, 15}); |
| 45 | + assertEquals(in.read(), 16); |
| 46 | + assertEquals(in.read(), (byte) -1); |
| 47 | + } |
| 48 | +} |
0 commit comments