Migrate buffer tests to JUnit 5 (#11305)

Motivation:

JUnit 5 is more expressive, extensible, and composable in many ways, and it's better able to run tests in parallel.

Modifications:

Use JUnit5 in tests

Result:

Related to https://github.com/netty/netty/issues/10757
This commit is contained in:
Riley Park 2021-05-27 00:22:02 -07:00 committed by Norman Maurer
parent ac3f823cce
commit b89a807d15
48 changed files with 2740 additions and 1207 deletions

View File

@ -16,11 +16,11 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public abstract class AbstractByteBufAllocatorTest<T extends AbstractByteBufAllocator> extends ByteBufAllocatorTest {
@ -108,7 +108,7 @@ public abstract class AbstractByteBufAllocatorTest<T extends AbstractByteBufAllo
// Double the size of the buffer
buffer.capacity(capacity << 1);
capacity = buffer.capacity();
assertEquals(buffer.toString(), expectedUsedMemory(allocator, capacity), metric.usedDirectMemory());
assertEquals(expectedUsedMemory(allocator, capacity), metric.usedDirectMemory(), buffer.toString());
buffer.release();
assertEquals(expectedUsedMemoryAfterRelease(allocator, capacity), metric.usedDirectMemory());

File diff suppressed because it is too large Load Diff

View File

@ -18,8 +18,9 @@ package io.netty.buffer;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -39,14 +40,15 @@ import static io.netty.util.internal.EmptyArrays.EMPTY_BYTES;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* An abstract test class for composite channel buffers
@ -63,7 +65,7 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
@Override
protected ByteBuf newBuffer(int length, int maxCapacity) {
Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE);
Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE);
List<ByteBuf> buffers = new ArrayList<ByteBuf>();
for (int i = 0; i < length + 45; i += 45) {
@ -1253,33 +1255,43 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
cbuf.release();
}
@Test(expected = ConcurrentModificationException.class)
@Test
public void testIteratorConcurrentModificationAdd() {
CompositeByteBuf cbuf = newCompositeBuffer();
cbuf.addComponent(EMPTY_BUFFER);
Iterator<ByteBuf> it = cbuf.iterator();
final Iterator<ByteBuf> it = cbuf.iterator();
cbuf.addComponent(EMPTY_BUFFER);
assertTrue(it.hasNext());
try {
it.next();
assertThrows(ConcurrentModificationException.class, new Executable() {
@Override
public void execute() {
it.next();
}
});
} finally {
cbuf.release();
}
}
@Test(expected = ConcurrentModificationException.class)
@Test
public void testIteratorConcurrentModificationRemove() {
CompositeByteBuf cbuf = newCompositeBuffer();
cbuf.addComponent(EMPTY_BUFFER);
Iterator<ByteBuf> it = cbuf.iterator();
final Iterator<ByteBuf> it = cbuf.iterator();
cbuf.removeComponent(0);
assertTrue(it.hasNext());
try {
it.next();
assertThrows(ConcurrentModificationException.class, new Executable() {
@Override
public void execute() {
it.next();
}
});
} finally {
cbuf.release();
}
@ -1564,52 +1576,67 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
assertTrue(cbuf.release());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testOverflowWhileAddingComponent() {
int capacity = 1024 * 1024; // 1MB
ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
try {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponent(duplicate);
duplicate.retain();
}
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponent(duplicate);
duplicate.retain();
}
}
});
} finally {
compositeByteBuf.release();
}
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testOverflowWhileAddingComponentsViaVarargs() {
int capacity = 1024 * 1024; // 1MB
ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
try {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponents(duplicate);
duplicate.retain();
}
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponents(duplicate);
duplicate.retain();
}
}
});
} finally {
compositeByteBuf.release();
}
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testOverflowWhileAddingComponentsViaIterable() {
int capacity = 1024 * 1024; // 1MB
ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
try {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponents(Collections.singletonList(duplicate));
duplicate.retain();
}
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
for (int i = 0; i >= 0; i += buffer.readableBytes()) {
ByteBuf duplicate = buffer.duplicate();
compositeByteBuf.addComponents(Collections.singletonList(duplicate));
duplicate.retain();
}
}
});
} finally {
compositeByteBuf.release();
}

View File

@ -15,15 +15,16 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest {
@ -51,12 +52,16 @@ public abstract class AbstractPooledByteBufTest extends AbstractByteBufTest {
buf.release();
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void ensureWritableWithNotEnoughSpaceShouldThrow() {
ByteBuf buf = newBuffer(1, 10);
final ByteBuf buf = newBuffer(1, 10);
try {
buf.ensureWritable(11);
fail();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
buf.ensureWritable(11);
}
});
} finally {
buf.release();
}

View File

@ -16,7 +16,8 @@
package io.netty.buffer;
import io.netty.util.IllegalReferenceCountException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.io.IOException;
import java.io.InputStream;
@ -27,33 +28,49 @@ import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class AbstractReferenceCountedByteBufTest {
@Test(expected = IllegalReferenceCountException.class)
@Test
public void testRetainOverflow() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
referenceCounted.setRefCnt(Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, referenceCounted.refCnt());
referenceCounted.retain();
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
referenceCounted.retain();
}
});
}
@Test(expected = IllegalReferenceCountException.class)
@Test
public void testRetainOverflow2() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
assertEquals(1, referenceCounted.refCnt());
referenceCounted.retain(Integer.MAX_VALUE);
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
referenceCounted.retain(Integer.MAX_VALUE);
}
});
}
@Test(expected = IllegalReferenceCountException.class)
@Test
public void testReleaseOverflow() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
referenceCounted.setRefCnt(0);
assertEquals(0, referenceCounted.refCnt());
referenceCounted.release(Integer.MAX_VALUE);
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
referenceCounted.release(Integer.MAX_VALUE);
}
});
}
@Test
@ -68,20 +85,30 @@ public class AbstractReferenceCountedByteBufTest {
}
}
@Test(expected = IllegalReferenceCountException.class)
@Test
public void testRetainResurrect() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
assertTrue(referenceCounted.release());
assertEquals(0, referenceCounted.refCnt());
referenceCounted.retain();
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
referenceCounted.retain();
}
});
}
@Test(expected = IllegalReferenceCountException.class)
@Test
public void testRetainResurrect2() {
AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
final AbstractReferenceCountedByteBuf referenceCounted = newReferenceCounted();
assertTrue(referenceCounted.release());
assertEquals(0, referenceCounted.refCnt());
referenceCounted.retain(2);
assertThrows(IllegalReferenceCountException.class, new Executable() {
@Override
public void execute() {
referenceCounted.retain(2);
}
});
}
private static AbstractReferenceCountedByteBuf newReferenceCounted() {

View File

@ -16,9 +16,9 @@
package io.netty.buffer;
import static io.netty.buffer.Unpooled.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import io.netty.util.CharsetUtil;
import io.netty.util.ResourceLeakTracker;

View File

@ -15,8 +15,7 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Tests big-endian composite channel buffers
@ -27,9 +26,9 @@ public class BigEndianCompositeByteBufTest extends AbstractCompositeByteBufTest
}
@Override
@Test(expected = UnsupportedOperationException.class)
@Test
public void testInternalNioBufferAfterRelease() {
super.testInternalNioBufferAfterRelease();
testInternalNioBufferAfterRelease0(UnsupportedOperationException.class);
}
}

View File

@ -15,11 +15,13 @@
*/
package io.netty.buffer;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.ByteOrder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Tests big-endian direct channel buffers

View File

@ -15,9 +15,11 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests big-endian heap channel buffers
@ -31,13 +33,23 @@ public class BigEndianHeapByteBufTest extends AbstractByteBufTest {
return buffer;
}
@Test(expected = NullPointerException.class)
@Test
public void shouldNotAllowNullInConstructor1() {
new UnpooledHeapByteBuf(null, new byte[1], 0);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new UnpooledHeapByteBuf(null, new byte[1], 0);
}
});
}
@Test(expected = NullPointerException.class)
@Test
public void shouldNotAllowNullInConstructor2() {
new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, null, 0);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new UnpooledHeapByteBuf(UnpooledByteBufAllocator.DEFAULT, null, 0);
}
});
}
}

View File

@ -17,15 +17,15 @@ package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
public class BigEndianUnsafeDirectByteBufTest extends BigEndianDirectByteBufTest {
@Before
@BeforeEach
@Override
public void init() {
Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe());
Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests");
super.init();
}

View File

@ -15,18 +15,17 @@
*/
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
public class BigEndianUnsafeNoCleanerDirectByteBufTest extends BigEndianDirectByteBufTest {
@Before
@BeforeEach
@Override
public void init() {
Assume.assumeTrue("java.nio.DirectByteBuffer.<init>(long, int) not found, skip tests",
PlatformDependent.useDirectBufferNoCleaner());
Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(),
"java.nio.DirectByteBuffer.<init>(long, int) not found, skip tests");
super.init();
}

View File

@ -15,9 +15,9 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public abstract class ByteBufAllocatorTest {

View File

@ -16,7 +16,7 @@
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.nio.ByteOrder;
import java.util.Random;

View File

@ -15,13 +15,22 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.Charset;
import static io.netty.util.internal.EmptyArrays.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Tests channel buffer streams
@ -279,15 +288,20 @@ public class ByteBufStreamTest {
in2.close();
}
@Test(expected = EOFException.class)
@Test
public void testReadByteLengthRespected() throws Exception {
// case1
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(new byte[] { 1, 2, 3, 4, 5, 6 });
ByteBufInputStream in = new ByteBufInputStream(buf, 0);
final ByteBufInputStream in = new ByteBufInputStream(buf, 0);
try {
in.readByte();
assertThrows(EOFException.class, new Executable() {
@Override
public void execute() throws IOException {
in.readBoolean();
}
});
} finally {
buf.release();
in.close();

View File

@ -17,9 +17,10 @@ package io.netty.buffer;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
@ -35,23 +36,22 @@ import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@RunWith(Parameterized.class)
public class ByteBufUtilTest {
private static final String PARAMETERIZED_NAME = "bufferType = {0}";
private enum BufferType {
DIRECT_UNPOOLED, DIRECT_POOLED, HEAP_POOLED, HEAP_UNPOOLED
}
private final BufferType bufferType;
public ByteBufUtilTest(BufferType bufferType) {
this.bufferType = bufferType;
}
private ByteBuf buffer(int capacity) {
private ByteBuf buffer(BufferType bufferType, int capacity) {
switch (bufferType) {
case DIRECT_UNPOOLED:
@ -67,7 +67,6 @@ public class ByteBufUtilTest {
}
}
@Parameterized.Parameters(name = "bufferType = {0}")
public static Collection<Object[]> noUnsafe() {
return Arrays.asList(new Object[][] {
{ BufferType.DIRECT_POOLED },
@ -99,14 +98,24 @@ public class ByteBufUtilTest {
}
}
@Test(expected = IllegalArgumentException.class)
@Test
public void decodeHexDumpWithOddLength() {
ByteBufUtil.decodeHexDump("abc");
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
ByteBufUtil.decodeHexDump("abc");
}
});
}
@Test(expected = IllegalArgumentException.class)
@Test
public void decodeHexDumpWithInvalidChar() {
ByteBufUtil.decodeHexDump("fg");
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
ByteBufUtil.decodeHexDump("fg");
}
});
}
@Test
@ -160,10 +169,10 @@ public class ByteBufUtilTest {
Math.max(b1.length, b2.length) * 2));
}
@Test (expected = IllegalArgumentException.class)
@Test
public void notEqualsBufferUnderflow() {
byte[] b1 = new byte[8];
byte[] b2 = new byte[16];
final byte[] b1 = new byte[8];
final byte[] b2 = new byte[16];
Random rand = new Random();
rand.nextBytes(b1);
rand.nextBytes(b2);
@ -171,23 +180,28 @@ public class ByteBufUtilTest {
final int iB2 = iB1 + b1.length;
final int length = b1.length - iB1;
System.arraycopy(b1, iB1, b2, iB2, length - 1);
assertFalse(ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2,
-1));
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
ByteBufUtil.equals(Unpooled.wrappedBuffer(b1), iB1, Unpooled.wrappedBuffer(b2), iB2, -1);
}
});
}
@SuppressWarnings("deprecation")
@Test
public void writeShortBE() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void writeShortBE(BufferType bufferType) {
int expected = 0x1234;
ByteBuf buf = buffer(2).order(ByteOrder.BIG_ENDIAN);
ByteBuf buf = buffer(bufferType, 2).order(ByteOrder.BIG_ENDIAN);
ByteBufUtil.writeShortBE(buf, expected);
assertEquals(expected, buf.readShort());
buf.resetReaderIndex();
assertEquals(ByteBufUtil.swapShort((short) expected), buf.readShortLE());
buf.release();
buf = buffer(2).order(ByteOrder.LITTLE_ENDIAN);
buf = buffer(bufferType, 2).order(ByteOrder.LITTLE_ENDIAN);
ByteBufUtil.writeShortBE(buf, expected);
assertEquals(ByteBufUtil.swapShort((short) expected), buf.readShortLE());
buf.resetReaderIndex();
@ -216,18 +230,19 @@ public class ByteBufUtilTest {
}
@SuppressWarnings("deprecation")
@Test
public void writeMediumBE() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void writeMediumBE(BufferType bufferType) {
int mediumValue = 0x123456;
ByteBuf buf = buffer(4).order(ByteOrder.BIG_ENDIAN);
ByteBuf buf = buffer(bufferType, 4).order(ByteOrder.BIG_ENDIAN);
ByteBufUtil.writeMediumBE(buf, mediumValue);
assertEquals(mediumValue, buf.readMedium());
buf.resetReaderIndex();
assertEquals(ByteBufUtil.swapMedium(mediumValue), buf.readMediumLE());
buf.release();
buf = buffer(4).order(ByteOrder.LITTLE_ENDIAN);
buf = buffer(bufferType, 4).order(ByteOrder.LITTLE_ENDIAN);
ByteBufUtil.writeMediumBE(buf, mediumValue);
assertEquals(ByteBufUtil.swapMedium(mediumValue), buf.readMediumLE());
buf.resetReaderIndex();
@ -235,12 +250,13 @@ public class ByteBufUtilTest {
buf.release();
}
@Test
public void testWriteUsAscii() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAscii(BufferType bufferType) {
String usAscii = "NettyRocks";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeAscii(buf2, usAscii);
assertEquals(buf, buf2);
@ -249,12 +265,13 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUsAsciiSwapped() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAsciiSwapped(BufferType bufferType) {
String usAscii = "NettyRocks";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
SwappedByteBuf buf2 = new SwappedByteBuf(buffer(16));
SwappedByteBuf buf2 = new SwappedByteBuf(buffer(bufferType, 16));
ByteBufUtil.writeAscii(buf2, usAscii);
assertEquals(buf, buf2);
@ -263,13 +280,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUsAsciiWrapped() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAsciiWrapped(BufferType bufferType) {
String usAscii = "NettyRocks";
ByteBuf buf = unreleasableBuffer(buffer(16));
ByteBuf buf = unreleasableBuffer(buffer(bufferType, 16));
assertWrapped(buf);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = unreleasableBuffer(buffer(16));
ByteBuf buf2 = unreleasableBuffer(buffer(bufferType, 16));
assertWrapped(buf2);
ByteBufUtil.writeAscii(buf2, usAscii);
@ -279,13 +297,14 @@ public class ByteBufUtilTest {
buf2.unwrap().release();
}
@Test
public void testWriteUsAsciiComposite() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAsciiComposite(BufferType bufferType) {
String usAscii = "NettyRocks";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = Unpooled.compositeBuffer().addComponent(
buffer(8)).addComponent(buffer(24));
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24));
// write some byte so we start writing with an offset.
buf2.writeByte(1);
ByteBufUtil.writeAscii(buf2, usAscii);
@ -297,13 +316,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUsAsciiCompositeWrapped() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAsciiCompositeWrapped(BufferType bufferType) {
String usAscii = "NettyRocks";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent(
buffer(8)).addComponent(buffer(24)));
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)));
// write some byte so we start writing with an offset.
buf2.writeByte(1);
ByteBufUtil.writeAscii(buf2, usAscii);
@ -315,12 +335,13 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8(BufferType bufferType) {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, usAscii);
assertEquals(buf, buf2);
@ -329,13 +350,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8Composite() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8Composite(BufferType bufferType) {
String utf8 = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = Unpooled.compositeBuffer().addComponent(
buffer(8)).addComponent(buffer(24));
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24));
// write some byte so we start writing with an offset.
buf2.writeByte(1);
ByteBufUtil.writeUtf8(buf2, utf8);
@ -347,13 +369,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8CompositeWrapped() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8CompositeWrapped(BufferType bufferType) {
String utf8 = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(utf8.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = new WrappedCompositeByteBuf(Unpooled.compositeBuffer().addComponent(
buffer(8)).addComponent(buffer(24)));
buffer(bufferType, 8)).addComponent(buffer(bufferType, 24)));
// write some byte so we start writing with an offset.
buf2.writeByte(1);
ByteBufUtil.writeUtf8(buf2, utf8);
@ -365,8 +388,9 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8Surrogates() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8Surrogates(BufferType bufferType) {
// leading surrogate + trailing surrogate
String surrogateString = new StringBuilder(2)
.append('a')
@ -374,9 +398,9 @@ public class ByteBufUtilTest {
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -386,16 +410,17 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidOnlyTrailingSurrogate() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidOnlyTrailingSurrogate(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('a')
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -405,16 +430,17 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidOnlyLeadingSurrogate() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidOnlyLeadingSurrogate(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('a')
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -424,17 +450,18 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidSurrogatesSwitched() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidSurrogatesSwitched(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('a')
.append('\uDC00')
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -444,17 +471,18 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidTwoLeadingSurrogates() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidTwoLeadingSurrogates(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('a')
.append('\uD800')
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -463,17 +491,18 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidTwoTrailingSurrogates() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidTwoTrailingSurrogates(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('a')
.append('\uDC00')
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -483,14 +512,15 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidEndOnLeadingSurrogate() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidEndOnLeadingSurrogate(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('\uD800')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -500,14 +530,15 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8InvalidEndOnTrailingSurrogate() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidEndOnTrailingSurrogate(BufferType bufferType) {
String surrogateString = new StringBuilder(2)
.append('\uDC00')
.toString();
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, surrogateString);
assertEquals(buf, buf2);
@ -517,13 +548,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUsAsciiString() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUsAsciiString(BufferType bufferType) {
AsciiString usAscii = new AsciiString("NettyRocks");
int expectedCapacity = usAscii.length();
ByteBuf buf = buffer(expectedCapacity);
ByteBuf buf = buffer(bufferType, expectedCapacity);
buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = buffer(expectedCapacity);
ByteBuf buf2 = buffer(bufferType, expectedCapacity);
ByteBufUtil.writeAscii(buf2, usAscii);
assertEquals(buf, buf2);
@ -532,13 +564,14 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8Wrapped() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8Wrapped(BufferType bufferType) {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = unreleasableBuffer(buffer(16));
ByteBuf buf = unreleasableBuffer(buffer(bufferType, 16));
assertWrapped(buf);
buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = unreleasableBuffer(buffer(16));
ByteBuf buf2 = unreleasableBuffer(buffer(bufferType, 16));
assertWrapped(buf2);
ByteBufUtil.writeUtf8(buf2, usAscii);
@ -552,12 +585,13 @@ public class ByteBufUtilTest {
assertTrue(buf instanceof WrappedByteBuf);
}
@Test
public void testWriteUtf8Subsequence() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8Subsequence(BufferType bufferType) {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.substring(5, 18).getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, usAscii, 5, 18);
assertEquals(buf, buf2);
@ -566,12 +600,13 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testWriteUtf8SubsequenceSplitSurrogate() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8SubsequenceSplitSurrogate(BufferType bufferType) {
String usAscii = "\uD800\uDC00"; // surrogate pair: one code point, two chars
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.substring(0, 1).getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
ByteBufUtil.writeUtf8(buf2, usAscii, 0, 1);
assertEquals(buf, buf2);
@ -580,12 +615,13 @@ public class ByteBufUtilTest {
buf2.release();
}
@Test
public void testReserveAndWriteUtf8Subsequence() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testReserveAndWriteUtf8Subsequence(BufferType bufferType) {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
buf.writeBytes(usAscii.substring(5, 18).getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = buffer(16);
ByteBuf buf2 = buffer(bufferType, 16);
int count = ByteBufUtil.reserveAndWriteUtf8(buf2, usAscii, 5, 18, 16);
assertEquals(buf, buf2);
@ -610,9 +646,9 @@ public class ByteBufUtilTest {
int invoke(Object... args);
}
private void testInvalidSubsequences(TestMethod method) {
private void testInvalidSubsequences(BufferType bufferType, TestMethod method) {
for (int [] range : INVALID_RANGES) {
ByteBuf buf = buffer(16);
ByteBuf buf = buffer(bufferType, 16);
try {
method.invoke(buf, "Some UTF-8 like äÄ∏ŒŒ", range[0], range[1]);
fail("Did not throw IndexOutOfBoundsException for range (" + range[0] + ", " + range[1] + ")");
@ -625,9 +661,10 @@ public class ByteBufUtilTest {
}
}
@Test
public void testWriteUtf8InvalidSubsequences() {
testInvalidSubsequences(new TestMethod() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testWriteUtf8InvalidSubsequences(BufferType bufferType) {
testInvalidSubsequences(bufferType, new TestMethod() {
@Override
public int invoke(Object... args) {
return ByteBufUtil.writeUtf8((ByteBuf) args[0], (String) args[1],
@ -636,9 +673,10 @@ public class ByteBufUtilTest {
});
}
@Test
public void testReserveAndWriteUtf8InvalidSubsequences() {
testInvalidSubsequences(new TestMethod() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testReserveAndWriteUtf8InvalidSubsequences(BufferType bufferType) {
testInvalidSubsequences(bufferType, new TestMethod() {
@Override
public int invoke(Object... args) {
return ByteBufUtil.reserveAndWriteUtf8((ByteBuf) args[0], (String) args[1],
@ -647,14 +685,16 @@ public class ByteBufUtilTest {
});
}
@Test
public void testUtf8BytesInvalidSubsequences() {
testInvalidSubsequences(new TestMethod() {
@Override
public int invoke(Object... args) {
return ByteBufUtil.utf8Bytes((String) args[1], (Integer) args[2], (Integer) args[3]);
}
});
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testUtf8BytesInvalidSubsequences(BufferType bufferType) {
testInvalidSubsequences(bufferType,
new TestMethod() {
@Override
public int invoke(Object... args) {
return ByteBufUtil.utf8Bytes((String) args[1], (Integer) args[2], (Integer) args[3]);
}
});
}
@Test
@ -673,21 +713,23 @@ public class ByteBufUtilTest {
buffer.release();
}
@Test
public void testToStringDoesNotThrowIndexOutOfBounds() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testToStringDoesNotThrowIndexOutOfBounds(BufferType bufferType) {
CompositeByteBuf buffer = Unpooled.compositeBuffer();
try {
byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8);
buffer.addComponent(buffer(bytes.length).writeBytes(bytes));
buffer.addComponent(buffer(bytes.length).writeBytes(bytes));
buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes));
buffer.addComponent(buffer(bufferType, bytes.length).writeBytes(bytes));
assertEquals("1234", buffer.toString(bytes.length, bytes.length, CharsetUtil.UTF_8));
} finally {
buffer.release();
}
}
@Test
public void testIsTextWithUtf8() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testIsTextWithUtf8(BufferType bufferType) {
byte[][] validUtf8Bytes = {
"netty".getBytes(CharsetUtil.UTF_8),
{(byte) 0x24},
@ -700,7 +742,7 @@ public class ByteBufUtilTest {
(byte) 0xF0, (byte) 0x90, (byte) 0x8D, (byte) 0x88} // multiple characters
};
for (byte[] bytes : validUtf8Bytes) {
assertIsText(bytes, true, CharsetUtil.UTF_8);
assertIsText(bufferType, bytes, true, CharsetUtil.UTF_8);
}
byte[][] invalidUtf8Bytes = {
{(byte) 0x80},
@ -716,31 +758,34 @@ public class ByteBufUtilTest {
{(byte) 0xED, (byte) 0xAF, (byte) 0x80} // out of upper bound
};
for (byte[] bytes : invalidUtf8Bytes) {
assertIsText(bytes, false, CharsetUtil.UTF_8);
assertIsText(bufferType, bytes, false, CharsetUtil.UTF_8);
}
}
@Test
public void testIsTextWithoutOptimization() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testIsTextWithoutOptimization(BufferType bufferType) {
byte[] validBytes = {(byte) 0x01, (byte) 0xD8, (byte) 0x37, (byte) 0xDC};
byte[] invalidBytes = {(byte) 0x01, (byte) 0xD8};
assertIsText(validBytes, true, CharsetUtil.UTF_16LE);
assertIsText(invalidBytes, false, CharsetUtil.UTF_16LE);
assertIsText(bufferType, validBytes, true, CharsetUtil.UTF_16LE);
assertIsText(bufferType, invalidBytes, false, CharsetUtil.UTF_16LE);
}
@Test
public void testIsTextWithAscii() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testIsTextWithAscii(BufferType bufferType) {
byte[] validBytes = {(byte) 0x00, (byte) 0x01, (byte) 0x37, (byte) 0x7F};
byte[] invalidBytes = {(byte) 0x80, (byte) 0xFF};
assertIsText(validBytes, true, CharsetUtil.US_ASCII);
assertIsText(invalidBytes, false, CharsetUtil.US_ASCII);
assertIsText(bufferType, validBytes, true, CharsetUtil.US_ASCII);
assertIsText(bufferType, invalidBytes, false, CharsetUtil.US_ASCII);
}
@Test
public void testIsTextWithInvalidIndexAndLength() {
ByteBuf buffer = buffer(4);
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testIsTextWithInvalidIndexAndLength(BufferType bufferType) {
ByteBuf buffer = buffer(bufferType, 4);
try {
buffer.writeBytes(new byte[4]);
int[][] validIndexLengthPairs = {
@ -772,33 +817,37 @@ public class ByteBufUtilTest {
}
}
@Test
public void testUtf8Bytes() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testUtf8Bytes(BufferType bufferType) {
final String s = "Some UTF-8 like äÄ∏ŒŒ";
checkUtf8Bytes(s);
checkUtf8Bytes(bufferType, s);
}
@Test
public void testUtf8BytesWithSurrogates() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testUtf8BytesWithSurrogates(BufferType bufferType) {
final String s = "a\uD800\uDC00b";
checkUtf8Bytes(s);
checkUtf8Bytes(bufferType, s);
}
@Test
public void testUtf8BytesWithNonSurrogates3Bytes() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testUtf8BytesWithNonSurrogates3Bytes(BufferType bufferType) {
final String s = "a\uE000b";
checkUtf8Bytes(s);
checkUtf8Bytes(bufferType, s);
}
@Test
public void testUtf8BytesWithNonSurrogatesNonAscii() {
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testUtf8BytesWithNonSurrogatesNonAscii(BufferType bufferType) {
final char nonAscii = (char) 0x81;
final String s = "a" + nonAscii + "b";
checkUtf8Bytes(s);
checkUtf8Bytes(bufferType, s);
}
private void checkUtf8Bytes(final CharSequence charSequence) {
final ByteBuf buf = buffer(ByteBufUtil.utf8MaxBytes(charSequence));
private void checkUtf8Bytes(BufferType bufferType, final CharSequence charSequence) {
final ByteBuf buf = buffer(bufferType, ByteBufUtil.utf8MaxBytes(charSequence));
try {
final int writtenBytes = ByteBufUtil.writeUtf8(buf, charSequence);
final int utf8Bytes = ByteBufUtil.utf8Bytes(charSequence);
@ -808,8 +857,8 @@ public class ByteBufUtilTest {
}
}
private void assertIsText(byte[] bytes, boolean expected, Charset charset) {
ByteBuf buffer = buffer(bytes.length);
private void assertIsText(BufferType bufferType, byte[] bytes, boolean expected, Charset charset) {
ByteBuf buffer = buffer(bufferType, bytes.length);
try {
buffer.writeBytes(bytes);
assertEquals(expected, ByteBufUtil.isText(buffer, charset));
@ -818,9 +867,10 @@ public class ByteBufUtilTest {
}
}
@Test
public void testIsTextMultiThreaded() throws Throwable {
assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED));
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testIsTextMultiThreaded(BufferType bufferType) throws Throwable {
assumeTrue(bufferType == BufferType.HEAP_UNPOOLED);
final ByteBuf buffer = Unpooled.copiedBuffer("Hello, World!", CharsetUtil.ISO_8859_1);
try {
@ -859,9 +909,10 @@ public class ByteBufUtilTest {
}
}
@Test
public void testGetBytes() {
final ByteBuf buf = buffer(4);
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testGetBytes(BufferType bufferType) {
final ByteBuf buf = buffer(bufferType, 4);
try {
checkGetBytes(buf);
} finally {
@ -869,10 +920,11 @@ public class ByteBufUtilTest {
}
}
@Test
public void testGetBytesHeapWithNonZeroArrayOffset() {
assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED));
final ByteBuf buf = buffer(5);
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testGetBytesHeapWithNonZeroArrayOffset(BufferType bufferType) {
assumeTrue(bufferType == BufferType.HEAP_UNPOOLED);
final ByteBuf buf = buffer(bufferType, 5);
try {
buf.setByte(0, 0x05);
@ -889,10 +941,11 @@ public class ByteBufUtilTest {
}
}
@Test
public void testGetBytesHeapWithArrayLengthGreaterThanCapacity() {
assumeThat(bufferType, is(BufferType.HEAP_UNPOOLED));
final ByteBuf buf = buffer(5);
@ParameterizedTest(name = PARAMETERIZED_NAME)
@MethodSource("noUnsafe")
public void testGetBytesHeapWithArrayLengthGreaterThanCapacity(BufferType bufferType) {
assumeTrue(bufferType == BufferType.HEAP_UNPOOLED);
final ByteBuf buf = buffer(bufferType, 5);
try {
buf.setByte(4, 0x05);

View File

@ -16,11 +16,11 @@
package io.netty.buffer;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class ByteProcessorTest {
@Test

View File

@ -16,10 +16,10 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests buffer consolidation

View File

@ -15,9 +15,12 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DefaultByteBufHolderTest {

View File

@ -15,9 +15,11 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Tests duplicated channel buffers
@ -40,9 +42,14 @@ public class DuplicatedByteBufTest extends AbstractByteBufTest {
buf.release();
}
@Test(expected = NullPointerException.class)
@Test
public void shouldNotAllowNullInConstructor() {
new DuplicatedByteBuf(null);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new DuplicatedByteBuf(null);
}
});
}
// See https://github.com/netty/netty/issues/1800

View File

@ -12,14 +12,18 @@
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/package io.netty.buffer;
*/
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class EmptyByteBufTest {

View File

@ -15,9 +15,9 @@
*/
package io.netty.buffer;
import org.junit.Assume;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -28,7 +28,12 @@ import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import static io.netty.buffer.Unpooled.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class FixedCompositeByteBufTest {
@ -36,147 +41,207 @@ public class FixedCompositeByteBufTest {
return new FixedCompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, buffers);
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBoolean() {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setBoolean(0, true);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setBoolean(0, true);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetByte() {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setByte(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setByte(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBytesWithByteBuf() {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
ByteBuf src = wrappedBuffer(new byte[4]);
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf src = wrappedBuffer(new byte[4]);
try {
buf.setBytes(0, src);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setBytes(0, src);
}
});
} finally {
buf.release();
src.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBytesWithByteBuffer() {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setBytes(0, ByteBuffer.wrap(new byte[4]));
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesWithInputStream() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetBytesWithChannel() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setBytes(0, new ScatteringByteChannel() {
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public long read(ByteBuffer[] dsts, int offset, int length) {
return 0;
public void execute() {
buf.setBytes(0, ByteBuffer.wrap(new byte[4]));
}
@Override
public long read(ByteBuffer[] dsts) {
return 0;
}
@Override
public int read(ByteBuffer dst) {
return 0;
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() {
}
}, 4);
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetChar() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
@Test
public void testSetBytesWithInputStream() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setChar(0, 'b');
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() throws IOException {
buf.setBytes(0, new ByteArrayInputStream(new byte[4]), 4);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetDouble() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
@Test
public void testSetBytesWithChannel() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setDouble(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() throws IOException {
buf.setBytes(0, new ScatteringByteChannel() {
@Override
public long read(ByteBuffer[] dsts, int offset, int length) {
return 0;
}
@Override
public long read(ByteBuffer[] dsts) {
return 0;
}
@Override
public int read(ByteBuffer dst) {
return 0;
}
@Override
public boolean isOpen() {
return true;
}
@Override
public void close() {
}
}, 4);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetFloat() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
@Test
public void testSetChar() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setFloat(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setChar(0, 'b');
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetDouble() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setDouble(0, 1);
}
});
} finally {
buf.release();
}
}
@Test
public void testSetFloat() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setFloat(0, 1);
}
});
} finally {
buf.release();
}
}
@Test
public void testSetInt() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setInt(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setInt(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetLong() {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setLong(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setLong(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
public void testSetMedium() throws IOException {
ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
@Test
public void testSetMedium() {
final ByteBuf buf = newBuffer(wrappedBuffer(new byte[8]));
try {
buf.setMedium(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setMedium(0, 1);
}
});
} finally {
buf.release();
}
@ -394,7 +459,7 @@ public class FixedCompositeByteBufTest {
@Test
public void testHasMemoryAddressWhenEmpty() {
Assume.assumeTrue(EMPTY_BUFFER.hasMemoryAddress());
Assumptions.assumeTrue(EMPTY_BUFFER.hasMemoryAddress());
ByteBuf buf = newBuffer(new ByteBuf[0]);
assertTrue(buf.hasMemoryAddress());
assertEquals(EMPTY_BUFFER.memoryAddress(), buf.memoryAddress());
@ -441,15 +506,19 @@ public class FixedCompositeByteBufTest {
buf.release();
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void testHasNoArrayWhenMultipleBuffers() {
ByteBuf buf1 = buffer(10);
ByteBuf buf2 = buffer(10);
ByteBuf buf = newBuffer(buf1, buf2);
final ByteBuf buf = newBuffer(buf1, buf2);
assertFalse(buf.hasArray());
try {
buf.array();
fail();
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
buf.array();
}
});
} finally {
buf.release();
}

View File

@ -15,7 +15,8 @@
*/
package io.netty.buffer;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import java.nio.ByteOrder;

View File

@ -15,7 +15,7 @@
*/
package io.netty.buffer;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteOrder;

View File

@ -16,15 +16,15 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
public class LittleEndianUnsafeDirectByteBufTest extends LittleEndianDirectByteBufTest {
@Before
@BeforeEach
@Override
public void init() {
Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe());
Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests");
super.init();
}

View File

@ -16,16 +16,16 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
public class LittleEndianUnsafeNoCleanerDirectByteBufTest extends LittleEndianDirectByteBufTest {
@Before
@BeforeEach
@Override
public void init() {
Assume.assumeTrue("java.nio.DirectByteBuffer.<init>(long, int) not found, skip tests",
PlatformDependent.useDirectBufferNoCleaner());
Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(),
"java.nio.DirectByteBuffer.<init>(long, int) not found, skip tests");
super.init();
}

View File

@ -32,7 +32,7 @@ class LongPriorityQueueTest {
final LongPriorityQueue pq = new LongPriorityQueue();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
public void execute() {
pq.offer(LongPriorityQueue.NO_VALUE);
}
});

View File

@ -17,13 +17,13 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PoolArenaTest {
@ -38,7 +38,7 @@ public class PoolArenaTest {
int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025};
int[] expectedResult = {16, 16, 512, 1024, 1024, 1280};
for (int i = 0; i < reqCapacities.length; i ++) {
Assert.assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i])));
assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i])));
}
}
@ -48,7 +48,7 @@ public class PoolArenaTest {
int[] reqCapacities = {0, 15, 510, 1024, 1023, 1025};
int[] expectedResult = {16, 64, 512, 1024, 1024, 1280};
for (int i = 0; i < reqCapacities.length; i ++) {
Assert.assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i])));
assertEquals(expectedResult[i], arena.sizeIdx2size(arena.size2SizeIdx(reqCapacities[i])));
}
}
@ -58,9 +58,9 @@ public class PoolArenaTest {
for (int sz = 0; sz <= CHUNK_SIZE; sz++) {
int sizeIdx = arena.size2SizeIdx(sz);
Assert.assertTrue(sz <= arena.sizeIdx2size(sizeIdx));
assertTrue(sz <= arena.sizeIdx2size(sizeIdx));
if (sizeIdx > 0) {
Assert.assertTrue(sz > arena.sizeIdx2size(sizeIdx - 1));
assertTrue(sz > arena.sizeIdx2size(sizeIdx - 1));
}
}
}
@ -74,15 +74,15 @@ public class PoolArenaTest {
int maxPages = CHUNK_SIZE >> pageShifts;
for (int pages = 1; pages <= maxPages; pages++) {
int pageIdxFloor = arena.pages2pageIdxFloor(pages);
Assert.assertTrue(pages << pageShifts >= arena.pageIdx2size(pageIdxFloor));
assertTrue(pages << pageShifts >= arena.pageIdx2size(pageIdxFloor));
if (pageIdxFloor > 0 && pages < maxPages) {
Assert.assertTrue(pages << pageShifts < arena.pageIdx2size(pageIdxFloor + 1));
assertTrue(pages << pageShifts < arena.pageIdx2size(pageIdxFloor + 1));
}
int pageIdxCeiling = arena.pages2pageIdx(pages);
Assert.assertTrue(pages << pageShifts <= arena.pageIdx2size(pageIdxCeiling));
assertTrue(pages << pageShifts <= arena.pageIdx2size(pageIdxCeiling));
if (pageIdxCeiling > 0) {
Assert.assertTrue(pages << pageShifts > arena.pageIdx2size(pageIdxCeiling - 1));
assertTrue(pages << pageShifts > arena.pageIdx2size(pageIdxCeiling - 1));
}
}
}
@ -122,24 +122,24 @@ public class PoolArenaTest {
// create normal buffer
final ByteBuf b2 = allocator.directBuffer(8192 * 5);
Assert.assertNotNull(b1);
Assert.assertNotNull(b2);
assertNotNull(b1);
assertNotNull(b2);
// then release buffer to deallocated memory while threadlocal cache has been disabled
// allocations counter value must equals deallocations counter value
Assert.assertTrue(b1.release());
Assert.assertTrue(b2.release());
assertTrue(b1.release());
assertTrue(b2.release());
Assert.assertTrue(allocator.directArenas().size() >= 1);
assertTrue(allocator.directArenas().size() >= 1);
final PoolArenaMetric metric = allocator.directArenas().get(0);
Assert.assertEquals(2, metric.numDeallocations());
Assert.assertEquals(2, metric.numAllocations());
assertEquals(2, metric.numDeallocations());
assertEquals(2, metric.numAllocations());
Assert.assertEquals(1, metric.numSmallDeallocations());
Assert.assertEquals(1, metric.numSmallAllocations());
Assert.assertEquals(1, metric.numNormalDeallocations());
Assert.assertEquals(1, metric.numNormalAllocations());
assertEquals(1, metric.numSmallDeallocations());
assertEquals(1, metric.numSmallAllocations());
assertEquals(1, metric.numNormalDeallocations());
assertEquals(1, metric.numNormalAllocations());
}
@Test

View File

@ -15,18 +15,18 @@
*/
package io.netty.buffer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import java.nio.ByteOrder;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertSame;
public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDirectByteBufTest {
private static final int directMemoryCacheAlignment = 1;
private static PooledByteBufAllocator allocator;
@BeforeClass
@BeforeAll
public static void setUpAllocator() {
allocator = new PooledByteBufAllocator(
true,
@ -40,7 +40,7 @@ public class PooledAlignedBigEndianDirectByteBufTest extends PooledBigEndianDire
directMemoryCacheAlignment);
}
@AfterClass
@AfterAll
public static void releaseAllocator() {
allocator = null;
}

View File

@ -17,7 +17,7 @@ package io.netty.buffer;
import java.nio.ByteOrder;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertSame;
/**
* Tests big-endian direct channel buffers

View File

@ -20,8 +20,8 @@ import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.SystemPropertyUtil;
import org.junit.Assume;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@ -32,14 +32,15 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import org.junit.jupiter.api.Timeout;
import static io.netty.buffer.PoolChunk.runOffset;
import static io.netty.buffer.PoolChunk.runPages;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest<PooledByteBufAllocator> {
@ -144,13 +145,13 @@ public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest<Poo
@Test
public void testArenaMetricsNoCacheAlign() {
Assume.assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
Assumptions.assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
testArenaMetrics0(new PooledByteBufAllocator(true, 2, 2, 8192, 11, 0, 0, 0, true, 64), 100, 0, 100, 100);
}
@Test
public void testArenaMetricsCacheAlign() {
Assume.assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
Assumptions.assumeTrue(PooledByteBufAllocator.isDirectMemoryCacheAlignmentSupported());
testArenaMetrics0(new PooledByteBufAllocator(true, 2, 2, 8192, 11, 1000, 1000, 1000, true, 64), 100, 1, 1, 0);
}
@ -338,12 +339,14 @@ public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest<Poo
}
}
@Test (timeout = 4000)
@Test
@Timeout(value = 4000, unit = MILLISECONDS)
public void testThreadCacheDestroyedByThreadCleaner() throws InterruptedException {
testThreadCacheDestroyed(false);
}
@Test (timeout = 4000)
@Test
@Timeout(value = 4000, unit = MILLISECONDS)
public void testThreadCacheDestroyedAfterExitRun() throws InterruptedException {
testThreadCacheDestroyed(true);
}
@ -403,7 +406,8 @@ public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest<Poo
assertTrue(threadCachesCreated.get());
}
@Test(timeout = 3000)
@Test
@Timeout(value = 3000, unit = MILLISECONDS)
public void testNumThreadCachesWithNoDirectArenas() throws InterruptedException {
int numHeapArenas = 1;
final PooledByteBufAllocator allocator =
@ -422,7 +426,8 @@ public class PooledByteBufAllocatorTest extends AbstractByteBufAllocatorTest<Poo
assertEquals(0, allocator.metric().numThreadLocalCaches());
}
@Test(timeout = 3000)
@Test
@Timeout(value = 3000, unit = MILLISECONDS)
public void testThreadCacheToArenaMappings() throws InterruptedException {
int numArenas = 2;
final PooledByteBufAllocator allocator =

View File

@ -17,7 +17,7 @@ package io.netty.buffer;
import java.nio.ByteOrder;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertSame;
/**
* Tests little-endian direct channel buffers

View File

@ -17,7 +17,7 @@ package io.netty.buffer;
import java.nio.ByteOrder;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertSame;
/**
* Tests little-endian heap channel buffers

View File

@ -15,7 +15,8 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.io.IOException;
import java.io.InputStream;
@ -31,11 +32,11 @@ import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
import static io.netty.buffer.Unpooled.LITTLE_ENDIAN;
import static io.netty.buffer.Unpooled.buffer;
import static io.netty.buffer.Unpooled.unmodifiableBuffer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -44,9 +45,14 @@ import static org.mockito.Mockito.when;
*/
public class ReadOnlyByteBufTest {
@Test(expected = NullPointerException.class)
@Test
public void shouldNotAllowNullInConstructor() {
new ReadOnlyByteBuf(null);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new ReadOnlyByteBuf(null);
}
});
}
@Test
@ -127,59 +133,114 @@ public class ReadOnlyByteBufTest {
assertEquals(27, roBuf.capacity());
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectDiscardReadBytes() {
unmodifiableBuffer(EMPTY_BUFFER).discardReadBytes();
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).discardReadBytes();
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetByte() {
unmodifiableBuffer(EMPTY_BUFFER).setByte(0, (byte) 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setByte(0, (byte) 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetShort() {
unmodifiableBuffer(EMPTY_BUFFER).setShort(0, (short) 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setShort(0, (short) 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetMedium() {
unmodifiableBuffer(EMPTY_BUFFER).setMedium(0, 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setMedium(0, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetInt() {
unmodifiableBuffer(EMPTY_BUFFER).setInt(0, 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setInt(0, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetLong() {
unmodifiableBuffer(EMPTY_BUFFER).setLong(0, 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setLong(0, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
public void shouldRejectSetBytes1() throws IOException {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (InputStream) null, 0);
@Test
public void shouldRejectSetBytes1() {
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() throws IOException {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (InputStream) null, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
public void shouldRejectSetBytes2() throws IOException {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ScatteringByteChannel) null, 0);
@Test
public void shouldRejectSetBytes2() {
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() throws IOException {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ScatteringByteChannel) null, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetBytes3() {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (byte[]) null, 0, 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() throws IOException {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (byte[]) null, 0, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetBytes4() {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ByteBuf) null, 0, 0);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ByteBuf) null, 0, 0);
}
});
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void shouldRejectSetBytes5() {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ByteBuffer) null);
assertThrows(UnsupportedOperationException.class, new Executable() {
@Override
public void execute() {
unmodifiableBuffer(EMPTY_BUFFER).setBytes(0, (ByteBuffer) null);
}
});
}
@Test
@ -211,13 +272,17 @@ public class ReadOnlyByteBufTest {
readOnly.release();
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void ensureWritableShouldThrow() {
ByteBuf buf = buffer(1);
ByteBuf readOnly = buf.asReadOnly();
final ByteBuf readOnly = buf.asReadOnly();
try {
readOnly.ensureWritable(1);
fail();
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
readOnly.ensureWritable(1);
}
});
} finally {
buf.release();
}

View File

@ -16,11 +16,11 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ReadOnlyByteBufferBufTest extends ReadOnlyDirectByteBufferBufTest {
@Override

View File

@ -16,8 +16,8 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -27,6 +27,12 @@ import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException;
import java.nio.channels.FileChannel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ReadOnlyDirectByteBufferBufTest {
protected ByteBuf buffer(ByteBuffer buffer) {
@ -40,20 +46,25 @@ public class ReadOnlyDirectByteBufferBufTest {
@Test
public void testIsContiguous() {
ByteBuf buf = buffer(allocate(4).asReadOnlyBuffer());
Assert.assertTrue(buf.isContiguous());
assertTrue(buf.isContiguous());
buf.release();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testConstructWithWritable() {
buffer(allocate(1));
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
buffer(ReadOnlyDirectByteBufferBufTest.this.allocate(1));
}
});
}
@Test
public void shouldIndicateNotWritable() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
Assert.assertFalse(buf.isWritable());
assertFalse(buf.isWritable());
} finally {
buf.release();
}
@ -63,7 +74,7 @@ public class ReadOnlyDirectByteBufferBufTest {
public void shouldIndicateNotWritableAnyNumber() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
Assert.assertFalse(buf.isWritable(1));
assertFalse(buf.isWritable(1));
} finally {
buf.release();
}
@ -74,7 +85,7 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
int result = buf.ensureWritable(1, false);
Assert.assertEquals(1, result);
assertEquals(1, result);
} finally {
buf.release();
}
@ -85,99 +96,144 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
int result = buf.ensureWritable(1, true);
Assert.assertEquals(1, result);
assertEquals(1, result);
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void ensureWritableShouldThrow() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()).clear();
try {
buf.ensureWritable(1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.ensureWritable(1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetByte() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setByte(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setByte(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetInt() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setInt(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setInt(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetShort() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setShort(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setShort(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetMedium() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setMedium(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setMedium(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetLong() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setLong(0, 1);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setLong(0, 1);
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBytesViaArray() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
buf.setBytes(0, "test".getBytes());
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setBytes(0, "test".getBytes());
}
});
} finally {
buf.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBytesViaBuffer() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
ByteBuf copy = Unpooled.copyInt(1);
final ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
final ByteBuf copy = Unpooled.copyInt(1);
try {
buf.setBytes(0, copy);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() {
buf.setBytes(0, copy);
}
});
} finally {
buf.release();
copy.release();
}
}
@Test(expected = ReadOnlyBufferException.class)
@Test
public void testSetBytesViaStream() throws IOException {
ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
final ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
try {
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
assertThrows(ReadOnlyBufferException.class, new Executable() {
@Override
public void execute() throws Throwable {
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
}
});
} finally {
buf.release();
}
@ -188,12 +244,12 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(
((ByteBuffer) allocate(2).put(new byte[] { (byte) 1, (byte) 2 }).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getByte(0));
Assert.assertEquals(2, buf.getByte(1));
assertEquals(1, buf.getByte(0));
assertEquals(2, buf.getByte(1));
Assert.assertEquals(1, buf.readByte());
Assert.assertEquals(2, buf.readByte());
Assert.assertFalse(buf.isReadable());
assertEquals(1, buf.readByte());
assertEquals(2, buf.readByte());
assertFalse(buf.isReadable());
buf.release();
}
@ -202,12 +258,12 @@ public class ReadOnlyDirectByteBufferBufTest {
public void testGetReadInt() {
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getInt(0));
Assert.assertEquals(2, buf.getInt(4));
assertEquals(1, buf.getInt(0));
assertEquals(2, buf.getInt(4));
Assert.assertEquals(1, buf.readInt());
Assert.assertEquals(2, buf.readInt());
Assert.assertFalse(buf.isReadable());
assertEquals(1, buf.readInt());
assertEquals(2, buf.readInt());
assertFalse(buf.isReadable());
buf.release();
}
@ -217,12 +273,12 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(((ByteBuffer) allocate(8)
.putShort((short) 1).putShort((short) 2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getShort(0));
Assert.assertEquals(2, buf.getShort(2));
assertEquals(1, buf.getShort(0));
assertEquals(2, buf.getShort(2));
Assert.assertEquals(1, buf.readShort());
Assert.assertEquals(2, buf.readShort());
Assert.assertFalse(buf.isReadable());
assertEquals(1, buf.readShort());
assertEquals(2, buf.readShort());
assertFalse(buf.isReadable());
buf.release();
}
@ -232,25 +288,30 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(((ByteBuffer) allocate(16)
.putLong(1).putLong(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.getLong(0));
Assert.assertEquals(2, buf.getLong(8));
assertEquals(1, buf.getLong(0));
assertEquals(2, buf.getLong(8));
Assert.assertEquals(1, buf.readLong());
Assert.assertEquals(2, buf.readLong());
Assert.assertFalse(buf.isReadable());
assertEquals(1, buf.readLong());
assertEquals(2, buf.readLong());
assertFalse(buf.isReadable());
buf.release();
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testGetBytesByteBuffer() {
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
// Ensure destination buffer is bigger then what is in the ByteBuf.
ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
ByteBuf buffer = buffer(((ByteBuffer) allocate(bytes.length)
final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
final ByteBuf buffer = buffer(((ByteBuffer) allocate(bytes.length)
.put(bytes).flip()).asReadOnlyBuffer());
try {
buffer.getBytes(buffer.readerIndex(), nioBuffer);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
buffer.getBytes(buffer.readerIndex(), nioBuffer);
}
});
} finally {
buffer.release();
}
@ -261,7 +322,7 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
ByteBuf copy = buf.copy();
Assert.assertEquals(buf, copy);
assertEquals(buf, copy);
buf.release();
copy.release();
@ -272,7 +333,7 @@ public class ReadOnlyDirectByteBufferBufTest {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
ByteBuf copy = buf.copy(1, 9);
Assert.assertEquals(buf.slice(1, 9), copy);
assertEquals(buf.slice(1, 9), copy);
buf.release();
copy.release();
@ -285,7 +346,7 @@ public class ReadOnlyDirectByteBufferBufTest {
.putLong(1).flip().position(1)).asReadOnlyBuffer());
ByteBuf slice = buf.slice();
Assert.assertEquals(buf, slice);
assertEquals(buf, slice);
buf.release();
}
@ -294,12 +355,12 @@ public class ReadOnlyDirectByteBufferBufTest {
public void testWrapBufferRoundTrip() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(1, buf.readInt());
assertEquals(1, buf.readInt());
ByteBuffer nioBuffer = buf.nioBuffer();
// Ensure this can be accessed without throwing a BufferUnderflowException
Assert.assertEquals(2, nioBuffer.getInt());
assertEquals(2, nioBuffer.getInt());
buf.release();
}
@ -329,7 +390,7 @@ public class ReadOnlyDirectByteBufferBufTest {
b2 = buffer(dup);
Assert.assertEquals(b2, b1.slice(2, 2));
assertEquals(b2, b1.slice(2, 2));
} finally {
if (b1 != null) {
b1.release();
@ -351,10 +412,10 @@ public class ReadOnlyDirectByteBufferBufTest {
public void testMemoryAddress() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
Assert.assertFalse(buf.hasMemoryAddress());
assertFalse(buf.hasMemoryAddress());
try {
buf.memoryAddress();
Assert.fail();
fail();
} catch (UnsupportedOperationException expected) {
// expected
}

View File

@ -16,22 +16,22 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import static org.junit.Assume.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
public class ReadOnlyUnsafeDirectByteBufferBufTest extends ReadOnlyDirectByteBufferBufTest {
/**
* Needs unsafe to run
*/
@BeforeClass
@BeforeAll
public static void assumeConditions() {
assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe());
assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests");
}
@Override
@ -44,7 +44,7 @@ public class ReadOnlyUnsafeDirectByteBufferBufTest extends ReadOnlyDirectByteBuf
public void testMemoryAddress() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
try {
Assert.assertTrue(buf.hasMemoryAddress());
assertTrue(buf.hasMemoryAddress());
buf.memoryAddress();
} finally {
buf.release();

View File

@ -16,7 +16,7 @@
package io.netty.buffer;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RetainedDuplicatedByteBufTest extends DuplicatedByteBufTest {
@Override

View File

@ -16,8 +16,7 @@
package io.netty.buffer;
import org.junit.Assert;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class RetainedSlicedByteBufTest extends SlicedByteBufTest {
@ -25,7 +24,7 @@ public class RetainedSlicedByteBufTest extends SlicedByteBufTest {
protected ByteBuf newSlice(ByteBuf buffer, int offset, int length) {
ByteBuf slice = buffer.retainedSlice(offset, length);
buffer.release();
Assert.assertEquals(buffer.refCnt(), slice.refCnt());
assertEquals(buffer.refCnt(), slice.refCnt());
return slice;
}
}

View File

@ -16,15 +16,15 @@
package io.netty.buffer;
import io.netty.util.ResourceLeakTracker;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayDeque;
import java.util.Queue;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SimpleLeakAwareByteBufTest extends BigEndianHeapByteBufTest {
private final Class<? extends ByteBuf> clazz = leakClass();
@ -46,14 +46,14 @@ public class SimpleLeakAwareByteBufTest extends BigEndianHeapByteBufTest {
return new SimpleLeakAwareByteBuf(buffer, tracker);
}
@Before
@BeforeEach
@Override
public void init() {
super.init();
trackers.clear();
}
@After
@AfterEach
@Override
public void dispose() {
super.dispose();

View File

@ -18,17 +18,17 @@ package io.netty.buffer;
import io.netty.util.ByteProcessor;
import io.netty.util.ResourceLeakTracker;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayDeque;
import java.util.Queue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBufTest {
@ -47,14 +47,14 @@ public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBuf
return new SimpleLeakAwareCompositeByteBuf(buffer, tracker);
}
@Before
@BeforeEach
@Override
public void init() {
super.init();
trackers.clear();
}
@After
@AfterEach
@Override
public void dispose() {
super.dispose();

View File

@ -16,16 +16,18 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Tests sliced channel buffers
@ -34,7 +36,7 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
@Override
protected final ByteBuf newBuffer(int length, int maxCapacity) {
Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE);
Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE);
int offset = length == 0 ? 0 : PlatformDependent.threadLocalRandom().nextInt(length);
ByteBuf buffer = Unpooled.buffer(length * 2);
ByteBuf slice = newSlice(buffer, offset, length);
@ -54,69 +56,124 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
buf.release();
}
@Test(expected = NullPointerException.class)
@Test
public void shouldNotAllowNullInConstructor() {
new SlicedByteBuf(null, 0, 0);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
new SlicedByteBuf(null, 0, 0);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testInternalNioBuffer() {
super.testInternalNioBuffer();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testInternalNioBuffer();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception {
super.testDuplicateReadGatheringByteChannelMultipleThreads();
public void testDuplicateReadGatheringByteChannelMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testDuplicateReadGatheringByteChannelMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception {
super.testSliceReadGatheringByteChannelMultipleThreads();
public void testSliceReadGatheringByteChannelMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testSliceReadGatheringByteChannelMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateReadOutputStreamMultipleThreads() throws Exception {
super.testDuplicateReadOutputStreamMultipleThreads();
public void testDuplicateReadOutputStreamMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testDuplicateReadOutputStreamMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceReadOutputStreamMultipleThreads() throws Exception {
super.testSliceReadOutputStreamMultipleThreads();
public void testSliceReadOutputStreamMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testSliceReadOutputStreamMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateBytesInArrayMultipleThreads() throws Exception {
super.testDuplicateBytesInArrayMultipleThreads();
public void testDuplicateBytesInArrayMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testDuplicateBytesInArrayMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceBytesInArrayMultipleThreads() throws Exception {
super.testSliceBytesInArrayMultipleThreads();
public void testSliceBytesInArrayMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
SlicedByteBufTest.super.testSliceBytesInArrayMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testNioBufferExposeOnlyRegion() {
super.testNioBufferExposeOnlyRegion();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testNioBufferExposeOnlyRegion();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testGetReadOnlyDirectDst() {
super.testGetReadOnlyDirectDst();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testGetReadOnlyDirectDst();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testGetReadOnlyHeapDst() {
super.testGetReadOnlyHeapDst();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testGetReadOnlyHeapDst();
}
});
}
@Test
@ -143,12 +200,12 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
// Ignore for SlicedByteBuf
}
@Ignore("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run")
@Disabled("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run")
@Override
public void testDuplicateCapacityChange() {
}
@Ignore("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run")
@Disabled("Sliced ByteBuf objects don't allow the capacity to change. So this test would fail and shouldn't be run")
@Override
public void testRetainedDuplicateCapacityChange() {
}
@ -201,41 +258,66 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
}
@Override
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testGetBytesByteBuffer() {
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
// Ensure destination buffer is bigger then what is wrapped in the ByteBuf.
ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes).slice(0, bytes.length - 1);
final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
final ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes).slice(0, bytes.length - 1);
try {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
}
});
} finally {
wrappedBuffer.release();
}
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUsAsciiCharSequenceExpand() {
super.testWriteUsAsciiCharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testWriteUsAsciiCharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUtf8CharSequenceExpand() {
super.testWriteUtf8CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testWriteUtf8CharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteIso88591CharSequenceExpand() {
super.testWriteIso88591CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testWriteIso88591CharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUtf16CharSequenceExpand() {
super.testWriteUtf16CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
SlicedByteBufTest.super.testWriteUtf16CharSequenceExpand();
}
});
}
@Test
@ -254,14 +336,18 @@ public class SlicedByteBufTest extends AbstractByteBufTest {
slice.release();
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void ensureWritableWithNotEnoughSpaceShouldThrow() {
ByteBuf slice = newBuffer(10);
final ByteBuf slice = newBuffer(10);
ByteBuf unwrapped = slice.unwrap();
unwrapped.writerIndex(unwrapped.writerIndex() + 5);
try {
slice.ensureWritable(1);
fail();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
slice.ensureWritable(1);
}
});
} finally {
slice.release();
}

View File

@ -16,7 +16,8 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito;
import java.io.InputStream;
@ -31,7 +32,12 @@ import java.util.Map.Entry;
import static io.netty.buffer.Unpooled.*;
import static io.netty.util.internal.EmptyArrays.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Tests channel buffers
@ -694,11 +700,16 @@ public class UnpooledTest {
wrapped.release();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void skipBytesNegativeLength() {
ByteBuf buf = buffer(8);
final ByteBuf buf = buffer(8);
try {
buf.skipBytes(-1);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() throws Throwable {
buf.skipBytes(-1);
}
});
} finally {
buf.release();
}
@ -722,27 +733,37 @@ public class UnpooledTest {
assertEquals(0, wrapped.refCnt());
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testGetBytesByteBuffer() {
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
// Ensure destination buffer is bigger then what is wrapped in the ByteBuf.
ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
ByteBuf wrappedBuffer = wrappedBuffer(bytes);
final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
final ByteBuf wrappedBuffer = wrappedBuffer(bytes);
try {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Throwable {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
}
});
} finally {
wrappedBuffer.release();
}
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testGetBytesByteBuffer2() {
byte[] bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
// Ensure destination buffer is bigger then what is wrapped in the ByteBuf.
ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
ByteBuf wrappedBuffer = wrappedBuffer(bytes, 0, bytes.length);
final ByteBuffer nioBuffer = ByteBuffer.allocate(bytes.length + 1);
final ByteBuf wrappedBuffer = wrappedBuffer(bytes, 0, bytes.length);
try {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Throwable {
wrappedBuffer.getBytes(wrappedBuffer.readerIndex(), nioBuffer);
}
});
} finally {
wrappedBuffer.release();
}

View File

@ -15,13 +15,13 @@
*/
package io.netty.buffer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static io.netty.buffer.Unpooled.buffer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UnreleaseableByteBufTest {

View File

@ -16,21 +16,23 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.nio.ByteBuffer;
import static io.netty.util.internal.PlatformDependent.directBufferAddress;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class UnsafeByteBufUtilTest {
@Before
@BeforeEach
public void checkHasUnsafe() {
Assume.assumeTrue("sun.misc.Unsafe not found, skip tests", PlatformDependent.hasUnsafe());
Assumptions.assumeTrue(PlatformDependent.hasUnsafe(), "sun.misc.Unsafe not found, skip tests");
}
@Test
@ -49,7 +51,7 @@ public class UnsafeByteBufUtilTest {
byte[] check = new byte[length];
targetBuffer.getBytes(0, check, 0, length);
assertArrayEquals("The byte array's copy does not equal the original", testData, check);
assertArrayEquals(testData, check, "The byte array's copy does not equal the original");
} finally {
targetBuffer.release();
}
@ -82,7 +84,7 @@ public class UnsafeByteBufUtilTest {
byte[] check = new byte[length];
targetBuffer.getBytes(0, check, 0, length);
assertArrayEquals("The byte array's copy does not equal the original", testData, check);
assertArrayEquals(testData, check, "The byte array's copy does not equal the original");
} finally {
targetBuffer.release();
b1.release();
@ -105,7 +107,7 @@ public class UnsafeByteBufUtilTest {
final byte[] check = new byte[length];
targetBuffer.getBytes(0, check, 0, length);
assertArrayEquals("The byte array's copy does not equal the original", testData, check);
assertArrayEquals(testData, check, "The byte array's copy does not equal the original");
} finally {
targetBuffer.release();
}
@ -135,60 +137,100 @@ public class UnsafeByteBufUtilTest {
}
}
@Test(expected = NullPointerException.class)
@Test
public void testSetBytesWithNullByteArray() {
final UnpooledByteBufAllocator alloc = new UnpooledByteBufAllocator(true);
final UnpooledDirectByteBuf targetBuffer = new UnpooledDirectByteBuf(alloc, 8, 8);
try {
UnsafeByteBufUtil.setBytes(targetBuffer,
directBufferAddress(targetBuffer.nioBuffer()), 0, (byte[]) null, 0, 8);
assertThrows(NullPointerException.class, new Executable() {
@Override
public void execute() {
UnsafeByteBufUtil.setBytes(targetBuffer,
directBufferAddress(targetBuffer.nioBuffer()), 0, (byte[]) null, 0, 8);
}
});
} finally {
targetBuffer.release();
}
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds() {
// negative index
testSetBytesOutOfBounds0(4, 4, -1, 0, 4);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// negative index
testSetBytesOutOfBounds0(4, 4, -1, 0, 4);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds2() {
// negative length
testSetBytesOutOfBounds0(4, 4, 0, 0, -1);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// negative length
testSetBytesOutOfBounds0(4, 4, 0, 0, -1);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds3() {
// buffer length oversize
testSetBytesOutOfBounds0(4, 8, 0, 0, 5);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// buffer length oversize
testSetBytesOutOfBounds0(4, 8, 0, 0, 5);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds4() {
// buffer length oversize
testSetBytesOutOfBounds0(4, 4, 3, 0, 3);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// buffer length oversize
testSetBytesOutOfBounds0(4, 4, 3, 0, 3);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds5() {
// negative srcIndex
testSetBytesOutOfBounds0(4, 4, 0, -1, 4);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// negative srcIndex
testSetBytesOutOfBounds0(4, 4, 0, -1, 4);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds6() {
// src length oversize
testSetBytesOutOfBounds0(8, 4, 0, 0, 5);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// src length oversize
testSetBytesOutOfBounds0(8, 4, 0, 0, 5);
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
public void testSetBytesOutOfBounds7() {
// src length oversize
testSetBytesOutOfBounds0(4, 4, 0, 1, 4);
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
// src length oversize
testSetBytesOutOfBounds0(4, 4, 0, 1, 4);
}
});
}
private static void testSetBytesOutOfBounds0(int lengthOfBuffer,

View File

@ -16,134 +16,227 @@
package io.netty.buffer;
import io.netty.util.internal.PlatformDependent;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class WrappedUnpooledUnsafeByteBufTest extends BigEndianUnsafeDirectByteBufTest {
@Before
@BeforeEach
@Override
public void init() {
Assume.assumeTrue("PlatformDependent.useDirectBufferNoCleaner() returned false, skip tests",
PlatformDependent.useDirectBufferNoCleaner());
Assumptions.assumeTrue(PlatformDependent.useDirectBufferNoCleaner(),
"PlatformDependent.useDirectBufferNoCleaner() returned false, skip tests");
super.init();
}
@Override
protected ByteBuf newBuffer(int length, int maxCapacity) {
Assume.assumeTrue(maxCapacity == Integer.MAX_VALUE);
Assumptions.assumeTrue(maxCapacity == Integer.MAX_VALUE);
return new WrappedUnpooledUnsafeDirectByteBuf(UnpooledByteBufAllocator.DEFAULT,
PlatformDependent.allocateMemory(length), length, true);
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testInternalNioBuffer() {
super.testInternalNioBuffer();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testInternalNioBuffer();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateReadGatheringByteChannelMultipleThreads() throws Exception {
super.testDuplicateReadGatheringByteChannelMultipleThreads();
public void testDuplicateReadGatheringByteChannelMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testDuplicateReadGatheringByteChannelMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceReadGatheringByteChannelMultipleThreads() throws Exception {
super.testSliceReadGatheringByteChannelMultipleThreads();
public void testSliceReadGatheringByteChannelMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testSliceReadGatheringByteChannelMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateReadOutputStreamMultipleThreads() throws Exception {
super.testDuplicateReadOutputStreamMultipleThreads();
public void testDuplicateReadOutputStreamMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testDuplicateReadOutputStreamMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceReadOutputStreamMultipleThreads() throws Exception {
super.testSliceReadOutputStreamMultipleThreads();
public void testSliceReadOutputStreamMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testSliceReadOutputStreamMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testDuplicateBytesInArrayMultipleThreads() throws Exception {
super.testDuplicateBytesInArrayMultipleThreads();
public void testDuplicateBytesInArrayMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testDuplicateBytesInArrayMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testSliceBytesInArrayMultipleThreads() throws Exception {
super.testSliceBytesInArrayMultipleThreads();
public void testSliceBytesInArrayMultipleThreads() {
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() throws Exception {
WrappedUnpooledUnsafeByteBufTest.super.testSliceBytesInArrayMultipleThreads();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testNioBufferExposeOnlyRegion() {
super.testNioBufferExposeOnlyRegion();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testNioBufferExposeOnlyRegion();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testGetReadOnlyDirectDst() {
super.testGetReadOnlyDirectDst();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testGetReadOnlyDirectDst();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testGetReadOnlyHeapDst() {
super.testGetReadOnlyHeapDst();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testGetReadOnlyHeapDst();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testReadBytes() {
super.testReadBytes();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testReadBytes();
}
});
}
@Test(expected = IllegalArgumentException.class)
@Test
@Override
public void testDuplicateCapacityChange() {
super.testDuplicateCapacityChange();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testDuplicateCapacityChange();
}
});
}
@Test(expected = IllegalArgumentException.class)
@Test
@Override
public void testRetainedDuplicateCapacityChange() {
super.testRetainedDuplicateCapacityChange();
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testRetainedDuplicateCapacityChange();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testLittleEndianWithExpand() {
super.testLittleEndianWithExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testLittleEndianWithExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUsAsciiCharSequenceExpand() {
super.testWriteUsAsciiCharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testWriteUsAsciiCharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUtf8CharSequenceExpand() {
super.testWriteUtf8CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testWriteUtf8CharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteIso88591CharSequenceExpand() {
super.testWriteIso88591CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testWriteIso88591CharSequenceExpand();
}
});
}
@Test(expected = IndexOutOfBoundsException.class)
@Test
@Override
public void testWriteUtf16CharSequenceExpand() {
super.testWriteUtf16CharSequenceExpand();
assertThrows(IndexOutOfBoundsException.class, new Executable() {
@Override
public void execute() {
WrappedUnpooledUnsafeByteBufTest.super.testWriteUtf16CharSequenceExpand();
}
});
}
@Test

View File

@ -15,7 +15,10 @@
*/
package io.netty.buffer.search;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class BitapSearchProcessorFactoryTest {
@ -24,9 +27,14 @@ public class BitapSearchProcessorFactoryTest {
new BitapSearchProcessorFactory(new byte[64]);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testRejectTooLongNeedle() {
new BitapSearchProcessorFactory(new byte[65]);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new BitapSearchProcessorFactory(new byte[65]);
}
});
}
}

View File

@ -18,9 +18,9 @@ package io.netty.buffer.search;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MultiSearchProcessorTest {

View File

@ -18,17 +18,13 @@ package io.netty.buffer.search;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.Arrays;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(Parameterized.class)
public class SearchProcessorTest {
private enum Algorithm {
@ -53,54 +49,48 @@ public class SearchProcessorTest {
abstract SearchProcessorFactory newFactory(byte[] needle);
}
@Parameters(name = "{0} algorithm")
public static Object[] algorithms() {
return Algorithm.values();
}
@Parameter
public Algorithm algorithm;
@Test
public void testSearch() {
@ParameterizedTest
@EnumSource(Algorithm.class)
public void testSearch(Algorithm algorithm) {
final ByteBuf haystack = Unpooled.copiedBuffer("abc☺", CharsetUtil.UTF_8);
assertEquals(0, haystack.forEachByte(factory("a").newSearchProcessor()));
assertEquals(1, haystack.forEachByte(factory("ab").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory("abc").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory("abc☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("abc☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("abc☺x").newSearchProcessor()));
assertEquals(0, haystack.forEachByte(factory(algorithm, "a").newSearchProcessor()));
assertEquals(1, haystack.forEachByte(factory(algorithm, "ab").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory(algorithm, "abc").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory(algorithm, "abc☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "abc☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "abc☺x").newSearchProcessor()));
assertEquals(1, haystack.forEachByte(factory("b").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory("bc").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory("bc☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("bc☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("bc☺x").newSearchProcessor()));
assertEquals(1, haystack.forEachByte(factory(algorithm, "b").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory(algorithm, "bc").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory(algorithm, "bc☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "bc☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "bc☺x").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory("c").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory("c☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("c☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("c☺x").newSearchProcessor()));
assertEquals(2, haystack.forEachByte(factory(algorithm, "c").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory(algorithm, "c☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "c☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "c☺x").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory("").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("☺x").newSearchProcessor()));
assertEquals(5, haystack.forEachByte(factory(algorithm, "").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "☺☺").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "☺x").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("z").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("aa").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("ba").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("abcd").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory("abcde").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "z").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "aa").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "ba").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "abcd").newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, "abcde").newSearchProcessor()));
haystack.release();
}
@Test
public void testRepeating() {
@ParameterizedTest
@EnumSource(Algorithm.class)
public void testRepeating(Algorithm algorithm) {
final ByteBuf haystack = Unpooled.copiedBuffer("abcababc", CharsetUtil.UTF_8);
final int length = haystack.readableBytes();
SearchProcessor processor = factory("ab").newSearchProcessor();
SearchProcessor processor = factory(algorithm, "ab").newSearchProcessor();
assertEquals(1, haystack.forEachByte(processor));
assertEquals(4, haystack.forEachByte(2, length - 2, processor));
@ -110,11 +100,12 @@ public class SearchProcessorTest {
haystack.release();
}
@Test
public void testOverlapping() {
@ParameterizedTest
@EnumSource(Algorithm.class)
public void testOverlapping(Algorithm algorithm) {
final ByteBuf haystack = Unpooled.copiedBuffer("ababab", CharsetUtil.UTF_8);
final int length = haystack.readableBytes();
SearchProcessor processor = factory("bab").newSearchProcessor();
SearchProcessor processor = factory(algorithm, "bab").newSearchProcessor();
assertEquals(3, haystack.forEachByte(processor));
assertEquals(5, haystack.forEachByte(4, length - 4, processor));
@ -123,8 +114,9 @@ public class SearchProcessorTest {
haystack.release();
}
@Test
public void testLongInputs() {
@ParameterizedTest
@EnumSource(Algorithm.class)
public void testLongInputs(Algorithm algorithm) {
final int haystackLen = 1024;
final int needleLen = 64;
@ -133,21 +125,22 @@ public class SearchProcessorTest {
final ByteBuf haystack = Unpooled.copiedBuffer(haystackBytes); // 00000...00001
final byte[] needleBytes = new byte[needleLen]; // 000...000
assertEquals(needleLen - 1, haystack.forEachByte(factory(needleBytes).newSearchProcessor()));
assertEquals(needleLen - 1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor()));
needleBytes[needleLen - 1] = 1; // 000...001
assertEquals(haystackLen - 1, haystack.forEachByte(factory(needleBytes).newSearchProcessor()));
assertEquals(haystackLen - 1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor()));
needleBytes[needleLen - 1] = 2; // 000...002
assertEquals(-1, haystack.forEachByte(factory(needleBytes).newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor()));
needleBytes[needleLen - 1] = 0;
needleBytes[0] = 1; // 100...000
assertEquals(-1, haystack.forEachByte(factory(needleBytes).newSearchProcessor()));
assertEquals(-1, haystack.forEachByte(factory(algorithm, needleBytes).newSearchProcessor()));
}
@Test
public void testUniqueLen64Substrings() {
@ParameterizedTest
@EnumSource(Algorithm.class)
public void testUniqueLen64Substrings(Algorithm algorithm) {
final byte[] haystackBytes = new byte[32 * 65]; // 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, ...
int pos = 0;
for (int i = 1; i <= 64; i++) {
@ -159,16 +152,16 @@ public class SearchProcessorTest {
for (int start = 0; start < haystackBytes.length - 64; start++) {
final byte[] needle = Arrays.copyOfRange(haystackBytes, start, start + 64);
assertEquals(start + 63, haystack.forEachByte(factory(needle).newSearchProcessor()));
assertEquals(start + 63, haystack.forEachByte(factory(algorithm, needle).newSearchProcessor()));
}
}
private SearchProcessorFactory factory(byte[] needle) {
private SearchProcessorFactory factory(Algorithm algorithm, byte[] needle) {
return algorithm.newFactory(needle);
}
private SearchProcessorFactory factory(String needle) {
return factory(needle.getBytes(CharsetUtil.UTF_8));
private SearchProcessorFactory factory(Algorithm algorithm, String needle) {
return factory(algorithm, needle.getBytes(CharsetUtil.UTF_8));
}
}