Cleanup buffer tests.
Motivation: Some of the tests in the buffer module contained unused code. Some of the tests also used unnecessary inheritance which could be avoided to simplify code. Modifications: Cleanup the test cases. Result: Cleaner code, less cruft.
This commit is contained in:
parent
31ef237085
commit
6faef55aef
@ -60,7 +60,6 @@ public abstract class AbstractByteBufTest {
|
|||||||
private ByteBuf buffer;
|
private ByteBuf buffer;
|
||||||
|
|
||||||
protected abstract ByteBuf newBuffer(int capacity);
|
protected abstract ByteBuf newBuffer(int capacity);
|
||||||
protected abstract ByteBuf[] components();
|
|
||||||
|
|
||||||
protected boolean discardReadBytesDoesNotMoveWritableBytes() {
|
protected boolean discardReadBytesDoesNotMoveWritableBytes() {
|
||||||
return true;
|
return true;
|
||||||
|
@ -46,12 +46,9 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
|
|||||||
this.order = order;
|
this.order = order;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ByteBuf> buffers;
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffers = new ArrayList<ByteBuf>();
|
List<ByteBuf> buffers = new ArrayList<ByteBuf>();
|
||||||
for (int i = 0; i < length + 45; i += 45) {
|
for (int i = 0; i < length + 45; i += 45) {
|
||||||
buffers.add(EMPTY_BUFFER);
|
buffers.add(EMPTY_BUFFER);
|
||||||
buffers.add(wrappedBuffer(new byte[1]));
|
buffers.add(wrappedBuffer(new byte[1]));
|
||||||
@ -74,7 +71,7 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
|
|||||||
buffers.add(EMPTY_BUFFER);
|
buffers.add(EMPTY_BUFFER);
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[buffers.size()])).order(order);
|
ByteBuf buffer = wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[buffers.size()])).order(order);
|
||||||
|
|
||||||
// Truncate to the requested capacity.
|
// Truncate to the requested capacity.
|
||||||
buffer.capacity(length);
|
buffer.capacity(length);
|
||||||
@ -86,11 +83,6 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return buffers.toArray(new ByteBuf[buffers.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Composite buffer does not waste bandwidth on discardReadBytes, but
|
// Composite buffer does not waste bandwidth on discardReadBytes, but
|
||||||
// the test will fail in strict mode.
|
// the test will fail in strict mode.
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,23 +21,16 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest {
|
public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
protected abstract ByteBuf alloc(int length);
|
protected abstract ByteBuf alloc(int length);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = alloc(length);
|
ByteBuf buffer = alloc(length);
|
||||||
assertEquals(0, buffer.writerIndex());
|
assertEquals(0, buffer.writerIndex());
|
||||||
assertEquals(0, buffer.readerIndex());
|
assertEquals(0, buffer.readerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDiscardMarks() {
|
public void testDiscardMarks() {
|
||||||
testDiscardMarks(4);
|
testDiscardMarks(4);
|
||||||
|
@ -24,21 +24,14 @@ import java.nio.ByteOrder;
|
|||||||
*/
|
*/
|
||||||
public class BigEndianDirectByteBufTest extends AbstractByteBufTest {
|
public class BigEndianDirectByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = newDirectBuffer(length);
|
ByteBuf buffer = newDirectBuffer(length);
|
||||||
assertSame(ByteOrder.BIG_ENDIAN, buffer.order());
|
assertSame(ByteOrder.BIG_ENDIAN, buffer.order());
|
||||||
assertEquals(0, buffer.writerIndex());
|
assertEquals(0, buffer.writerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ByteBuf newDirectBuffer(int length) {
|
protected ByteBuf newDirectBuffer(int length) {
|
||||||
return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE);
|
return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
@ -24,20 +24,13 @@ import static org.junit.Assert.*;
|
|||||||
*/
|
*/
|
||||||
public class BigEndianHeapByteBufTest extends AbstractByteBufTest {
|
public class BigEndianHeapByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = Unpooled.buffer(length);
|
ByteBuf buffer = Unpooled.buffer(length);
|
||||||
assertEquals(0, buffer.writerIndex());
|
assertEquals(0, buffer.writerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void shouldNotAllowNullInConstructor1() {
|
public void shouldNotAllowNullInConstructor1() {
|
||||||
new UnpooledHeapByteBuf(null, new byte[1], 0);
|
new UnpooledHeapByteBuf(null, new byte[1], 0);
|
||||||
|
@ -24,22 +24,15 @@ import static org.junit.Assert.assertEquals;
|
|||||||
*/
|
*/
|
||||||
public class DuplicateByteBufTest extends AbstractByteBufTest {
|
public class DuplicateByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
ByteBuf wrapped = Unpooled.buffer(length);
|
ByteBuf wrapped = Unpooled.buffer(length);
|
||||||
buffer = new DuplicatedByteBuf(wrapped);
|
ByteBuf buffer = new DuplicatedByteBuf(wrapped);
|
||||||
assertEquals(wrapped.writerIndex(), buffer.writerIndex());
|
assertEquals(wrapped.writerIndex(), buffer.writerIndex());
|
||||||
assertEquals(wrapped.readerIndex(), buffer.readerIndex());
|
assertEquals(wrapped.readerIndex(), buffer.readerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void shouldNotAllowNullInConstructor() {
|
public void shouldNotAllowNullInConstructor() {
|
||||||
new DuplicatedByteBuf(null);
|
new DuplicatedByteBuf(null);
|
||||||
@ -48,6 +41,7 @@ public class DuplicateByteBufTest extends AbstractByteBufTest {
|
|||||||
// See https://github.com/netty/netty/issues/1800
|
// See https://github.com/netty/netty/issues/1800
|
||||||
@Test
|
@Test
|
||||||
public void testIncreaseCapacityWrapped() {
|
public void testIncreaseCapacityWrapped() {
|
||||||
|
ByteBuf buffer = newBuffer(8);
|
||||||
ByteBuf wrapped = buffer.unwrap();
|
ByteBuf wrapped = buffer.unwrap();
|
||||||
wrapped.writeByte(0);
|
wrapped.writeByte(0);
|
||||||
wrapped.readerIndex(wrapped.readerIndex() + 1);
|
wrapped.readerIndex(wrapped.readerIndex() + 1);
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
* under the License.
|
* under the License.
|
||||||
*/package io.netty.buffer;
|
*/package io.netty.buffer;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
@ -25,15 +24,15 @@ public class EmptyByteBufTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testIsWritable() {
|
public void testIsWritable() {
|
||||||
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
|
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
|
||||||
Assert.assertFalse(empty.isWritable());
|
assertFalse(empty.isWritable());
|
||||||
Assert.assertFalse(empty.isWritable(1));
|
assertFalse(empty.isWritable(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsReadable() {
|
public void testIsReadable() {
|
||||||
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
|
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
|
||||||
Assert.assertFalse(empty.isReadable());
|
assertFalse(empty.isReadable());
|
||||||
Assert.assertFalse(empty.isReadable(1));
|
assertFalse(empty.isReadable(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -58,7 +58,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ByteBuf newBuffer(ByteBuf... buffers) {
|
private static ByteBuf newBuffer(ByteBuf... buffers) {
|
||||||
return new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers);
|
return new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,17 +97,17 @@ public class FixedCompositeByteBufTest {
|
|||||||
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
ByteBuf buf = newBuffer(Unpooled.wrappedBuffer(new byte[8]));
|
||||||
buf.setBytes(0, new ScatteringByteChannel() {
|
buf.setBytes(0, new ScatteringByteChannel() {
|
||||||
@Override
|
@Override
|
||||||
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
|
public long read(ByteBuffer[] dsts, int offset, int length) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long read(ByteBuffer[] dsts) throws IOException {
|
public long read(ByteBuffer[] dsts) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(ByteBuffer dst) throws IOException {
|
public int read(ByteBuffer dst) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() {
|
||||||
}
|
}
|
||||||
}, 4);
|
}, 4);
|
||||||
}
|
}
|
||||||
@ -240,7 +240,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
PooledByteBufAllocator.DEFAULT.directBuffer());
|
PooledByteBufAllocator.DEFAULT.directBuffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGatheringWritesPartial(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
private static void testGatheringWritesPartial(ByteBuf buf1, ByteBuf buf2) throws Exception {
|
||||||
buf1.writeBytes(new byte[]{1, 2, 3, 4});
|
buf1.writeBytes(new byte[]{1, 2, 3, 4});
|
||||||
buf2.writeBytes(new byte[]{1, 2, 3, 4});
|
buf2.writeBytes(new byte[]{1, 2, 3, 4});
|
||||||
ByteBuf buf = newBuffer(buf1, buf2);
|
ByteBuf buf = newBuffer(buf1, buf2);
|
||||||
@ -266,7 +266,7 @@ public class FixedCompositeByteBufTest {
|
|||||||
testGatheringWritesSingleBuf(directBuffer());
|
testGatheringWritesSingleBuf(directBuffer());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGatheringWritesSingleBuf(ByteBuf buf1) throws Exception {
|
private static void testGatheringWritesSingleBuf(ByteBuf buf1) throws Exception {
|
||||||
ByteBuf buf = newBuffer(buf1.writeBytes(new byte[]{1, 2, 3, 4}));
|
ByteBuf buf = newBuffer(buf1.writeBytes(new byte[]{1, 2, 3, 4}));
|
||||||
buf.readerIndex(1);
|
buf.readerIndex(1);
|
||||||
|
|
||||||
|
@ -24,21 +24,14 @@ import java.nio.ByteOrder;
|
|||||||
*/
|
*/
|
||||||
public class LittleEndianDirectByteBufTest extends AbstractByteBufTest {
|
public class LittleEndianDirectByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = newDirectBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
|
ByteBuf buffer = newDirectBuffer(length).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
|
assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order());
|
||||||
assertEquals(0, buffer.writerIndex());
|
assertEquals(0, buffer.writerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ByteBuf newDirectBuffer(int length) {
|
protected ByteBuf newDirectBuffer(int length) {
|
||||||
return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE);
|
return new UnpooledDirectByteBuf(UnpooledByteBufAllocator.DEFAULT, length, Integer.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
@ -24,17 +24,10 @@ import java.nio.ByteOrder;
|
|||||||
*/
|
*/
|
||||||
public class LittleEndianHeapByteBufTest extends AbstractByteBufTest {
|
public class LittleEndianHeapByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = Unpooled.buffer(length).order(ByteOrder.LITTLE_ENDIAN);
|
ByteBuf buffer = Unpooled.buffer(length).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
assertEquals(0, buffer.writerIndex());
|
assertEquals(0, buffer.writerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package io.netty.buffer;
|
package io.netty.buffer;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -23,12 +22,11 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ReadOnlyBufferException;
|
import java.nio.ReadOnlyBufferException;
|
||||||
import java.util.ArrayDeque;
|
|
||||||
import java.util.Queue;
|
import static io.netty.util.ReferenceCountUtil.releaseLater;
|
||||||
|
|
||||||
public class ReadOnlyDirectByteBufferBufTest {
|
public class ReadOnlyDirectByteBufferBufTest {
|
||||||
|
|
||||||
private final Queue<ByteBuf> buffers = new ArrayDeque<ByteBuf>();
|
|
||||||
protected ByteBuf buffer(ByteBuffer buffer) {
|
protected ByteBuf buffer(ByteBuffer buffer) {
|
||||||
return new ReadOnlyByteBufferBuf(UnpooledByteBufAllocator.DEFAULT, buffer);
|
return new ReadOnlyByteBufferBuf(UnpooledByteBufAllocator.DEFAULT, buffer);
|
||||||
}
|
}
|
||||||
@ -37,84 +35,64 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
return ByteBuffer.allocateDirect(size);
|
return ByteBuffer.allocateDirect(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
|
||||||
public void dispose() {
|
|
||||||
for (;;) {
|
|
||||||
ByteBuf buf = buffers.poll();
|
|
||||||
if (buf == null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
buf.release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testConstructWithWritable() {
|
public void testConstructWithWritable() {
|
||||||
buffers.add(buffer(allocate(1)));
|
releaseLater(buffer(allocate(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetByte() {
|
public void testSetByte() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setByte(0, 1);
|
buf.setByte(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetInt() {
|
public void testSetInt() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
|
|
||||||
buf.setInt(0, 1);
|
buf.setInt(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetShort() {
|
public void testSetShort() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setShort(0, 1);
|
buf.setShort(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetMedium() {
|
public void testSetMedium() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setMedium(0, 1);
|
buf.setMedium(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetLong() {
|
public void testSetLong() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setLong(0, 1);
|
buf.setLong(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesViaArray() {
|
public void testSetBytesViaArray() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setBytes(0, "test".getBytes());
|
buf.setBytes(0, "test".getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesViaBuffer() {
|
public void testSetBytesViaBuffer() {
|
||||||
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(allocate(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setBytes(0, Unpooled.copyInt(1));
|
buf.setBytes(0, Unpooled.copyInt(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ReadOnlyBufferException.class)
|
@Test(expected = ReadOnlyBufferException.class)
|
||||||
public void testSetBytesViaStream() throws IOException {
|
public void testSetBytesViaStream() throws IOException {
|
||||||
ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
|
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
|
||||||
buf.release();
|
buf.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetReadByte() {
|
public void testGetReadByte() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(2).put(new byte[]{(byte) 1, (byte) 2}).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(
|
||||||
buffers.add(buf);
|
((ByteBuffer) allocate(2).put(new byte[] { (byte) 1, (byte) 2 }).flip()).asReadOnlyBuffer()));
|
||||||
|
|
||||||
Assert.assertEquals(1, buf.getByte(0));
|
Assert.assertEquals(1, buf.getByte(0));
|
||||||
Assert.assertEquals(2, buf.getByte(1));
|
Assert.assertEquals(2, buf.getByte(1));
|
||||||
@ -126,8 +104,7 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetReadInt() {
|
public void testGetReadInt() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
|
|
||||||
Assert.assertEquals(1, buf.getInt(0));
|
Assert.assertEquals(1, buf.getInt(0));
|
||||||
Assert.assertEquals(2, buf.getInt(4));
|
Assert.assertEquals(2, buf.getInt(4));
|
||||||
@ -139,9 +116,8 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetReadShort() {
|
public void testGetReadShort() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putShort((short) 1)
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(8)
|
||||||
.putShort((short) 2).flip()).asReadOnlyBuffer());
|
.putShort((short) 1).putShort((short) 2).flip()).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
|
|
||||||
Assert.assertEquals(1, buf.getShort(0));
|
Assert.assertEquals(1, buf.getShort(0));
|
||||||
Assert.assertEquals(2, buf.getShort(2));
|
Assert.assertEquals(2, buf.getShort(2));
|
||||||
@ -153,8 +129,8 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetReadLong() {
|
public void testGetReadLong() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(16)
|
||||||
buffers.add(buf);
|
.putLong(1).putLong(2).flip()).asReadOnlyBuffer()));
|
||||||
|
|
||||||
Assert.assertEquals(1, buf.getLong(0));
|
Assert.assertEquals(1, buf.getLong(0));
|
||||||
Assert.assertEquals(2, buf.getLong(8));
|
Assert.assertEquals(2, buf.getLong(8));
|
||||||
@ -166,21 +142,16 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCopy() {
|
public void testCopy() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
ByteBuf copy = releaseLater(buf.copy());
|
||||||
ByteBuf copy = buf.copy();
|
|
||||||
buffers.add(copy);
|
|
||||||
|
|
||||||
Assert.assertEquals(buf, copy);
|
Assert.assertEquals(buf, copy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCopyWithOffset() {
|
public void testCopyWithOffset() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
ByteBuf copy = releaseLater(buf.copy(1, 9));
|
||||||
|
|
||||||
ByteBuf copy = buf.copy(1, 9);
|
|
||||||
buffers.add(copy);
|
|
||||||
|
|
||||||
Assert.assertEquals(buf.slice(1, 9), copy);
|
Assert.assertEquals(buf.slice(1, 9), copy);
|
||||||
}
|
}
|
||||||
@ -188,8 +159,8 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
// Test for https://github.com/netty/netty/issues/1708
|
// Test for https://github.com/netty/netty/issues/1708
|
||||||
@Test
|
@Test
|
||||||
public void testWrapBufferWithNonZeroPosition() {
|
public void testWrapBufferWithNonZeroPosition() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).flip().position(1)).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(16)
|
||||||
buffers.add(buf);
|
.putLong(1).flip().position(1)).asReadOnlyBuffer()));
|
||||||
|
|
||||||
ByteBuf slice = buf.slice();
|
ByteBuf slice = buf.slice();
|
||||||
Assert.assertEquals(buf, slice);
|
Assert.assertEquals(buf, slice);
|
||||||
@ -197,8 +168,7 @@ public class ReadOnlyDirectByteBufferBufTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapBufferRoundTrip() {
|
public void testWrapBufferRoundTrip() {
|
||||||
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
|
ByteBuf buf = releaseLater(buffer(((ByteBuffer) allocate(16).putInt(1).putInt(2).flip()).asReadOnlyBuffer()));
|
||||||
buffers.add(buf);
|
|
||||||
|
|
||||||
Assert.assertEquals(1, buf.readInt());
|
Assert.assertEquals(1, buf.readInt());
|
||||||
|
|
||||||
|
@ -28,21 +28,15 @@ import static org.junit.Assert.*;
|
|||||||
public class SlicedByteBufTest extends AbstractByteBufTest {
|
public class SlicedByteBufTest extends AbstractByteBufTest {
|
||||||
|
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
private ByteBuf buffer;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ByteBuf newBuffer(int length) {
|
protected ByteBuf newBuffer(int length) {
|
||||||
buffer = Unpooled.wrappedBuffer(
|
ByteBuf buffer = Unpooled.wrappedBuffer(
|
||||||
new byte[length * 2], random.nextInt(length - 1) + 1, length);
|
new byte[length * 2], random.nextInt(length - 1) + 1, length);
|
||||||
assertEquals(length, buffer.writerIndex());
|
assertEquals(length, buffer.writerIndex());
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ByteBuf[] components() {
|
|
||||||
return new ByteBuf[] { buffer };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = NullPointerException.class)
|
@Test(expected = NullPointerException.class)
|
||||||
public void shouldNotAllowNullInConstructor() {
|
public void shouldNotAllowNullInConstructor() {
|
||||||
new SlicedByteBuf(null, 0, 0);
|
new SlicedByteBuf(null, 0, 0);
|
||||||
|
Loading…
Reference in New Issue
Block a user