Add more tests to EmptyByteBufTest

- Ensure an EmptyByteBuf has an array, an NIO buffer, and a memory
  address at the same time
- Add an assertion that checks if EMPTY_BUFFER is an EmptyByteBuf,
  just in case we make a mistake in the future
This commit is contained in:
Trustin Lee 2014-12-30 15:47:33 +09:00
parent f26be2e973
commit a666acce6d
2 changed files with 40 additions and 0 deletions

View File

@ -93,6 +93,10 @@ public final class Unpooled {
*/
public static final ByteBuf EMPTY_BUFFER = ALLOC.buffer(0, 0);
static {
assert EMPTY_BUFFER instanceof EmptyByteBuf: "EMPTY_BUFFER must be an EmptyByteBuf.";
}
/**
* Creates a new big-endian Java heap buffer with reasonably small initial capacity, which
* expands its capacity boundlessly on demand.

View File

@ -17,6 +17,9 @@
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class EmptyByteBufTest {
@Test
@ -32,4 +35,37 @@ public class EmptyByteBufTest {
Assert.assertFalse(empty.isReadable());
Assert.assertFalse(empty.isReadable(1));
}
@Test
public void testArray() {
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
assertThat(empty.hasArray(), is(true));
assertThat(empty.array().length, is(0));
assertThat(empty.arrayOffset(), is(0));
}
@Test
public void testNioBuffer() {
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
assertThat(empty.nioBufferCount(), is(1));
assertThat(empty.nioBuffer().position(), is(0));
assertThat(empty.nioBuffer().limit(), is(0));
assertThat(empty.nioBuffer(), is(sameInstance(empty.nioBuffer())));
assertThat(empty.nioBuffer(), is(sameInstance(empty.internalNioBuffer(0, 0))));
}
@Test
public void testMemoryAddress() {
EmptyByteBuf empty = new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
if (empty.hasMemoryAddress()) {
assertThat(empty.memoryAddress(), is(not(0L)));
} else {
try {
empty.memoryAddress();
fail();
} catch (UnsupportedOperationException ignored) {
// Ignore.
}
}
}
}