diff --git a/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java index d6ba119412..fb63c01869 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractByteBufAllocatorTest.java @@ -15,6 +15,7 @@ */ package io.netty.buffer; +import io.netty.util.internal.PlatformDependent; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -25,6 +26,11 @@ public abstract class AbstractByteBufAllocatorTest extends ByteBufAllocatorTest @Override protected abstract AbstractByteBufAllocator newAllocator(boolean preferDirect); + @Override + protected boolean isDirectExpected(boolean preferDirect) { + return preferDirect && PlatformDependent.hasUnsafe(); + } + @Override protected final int defaultMaxCapacity() { return AbstractByteBufAllocator.DEFAULT_MAX_CAPACITY; diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java index 76121c3df1..71ddd4a774 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufAllocatorTest.java @@ -37,7 +37,7 @@ public abstract class ByteBufAllocatorTest { ByteBufAllocator allocator = newAllocator(preferDirect); ByteBuf buffer = allocator.buffer(1); try { - assertBuffer(buffer, preferDirect, 1, defaultMaxCapacity()); + assertBuffer(buffer, isDirectExpected(preferDirect), 1, defaultMaxCapacity()); } finally { buffer.release(); } @@ -53,12 +53,14 @@ public abstract class ByteBufAllocatorTest { ByteBufAllocator allocator = newAllocator(preferDirect); ByteBuf buffer = allocator.buffer(1, maxCapacity); try { - assertBuffer(buffer, preferDirect, 1, maxCapacity); + assertBuffer(buffer, isDirectExpected(preferDirect), 1, maxCapacity); } finally { buffer.release(); } } + protected abstract boolean isDirectExpected(boolean preferDirect); + @Test public void testHeapBuffer() { testHeapBuffer(true); diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java index 2040fdaafc..8a7241f617 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Lz4FrameEncoderTest.java @@ -18,6 +18,7 @@ package io.netty.handler.codec.compression; import java.io.InputStream; import java.util.zip.Checksum; +import io.netty.util.internal.PlatformDependent; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -37,6 +38,7 @@ import org.mockito.MockitoAnnotations; import static io.netty.handler.codec.compression.Lz4Constants.DEFAULT_SEED; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.when; public class Lz4FrameEncoderTest extends AbstractEncoderTest { @@ -111,7 +113,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { testAllocateBuffer(blockSize, NONALLOCATABLE_SIZE, false); } - private void testAllocateBuffer(int blockSize, int bufSize, boolean isDirect) { + private void testAllocateBuffer(int blockSize, int bufSize, boolean preferDirect) { // allocate the input buffer to an arbitrary size less than the blockSize ByteBuf in = ByteBufAllocator.DEFAULT.buffer(bufSize, bufSize); in.writerIndex(in.capacity()); @@ -119,13 +121,17 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest { ByteBuf out = null; try { Lz4FrameEncoder encoder = newEncoder(blockSize, Lz4FrameEncoder.DEFAULT_MAX_ENCODE_SIZE); - out = encoder.allocateBuffer(ctx, in, isDirect); + out = encoder.allocateBuffer(ctx, in, preferDirect); Assert.assertNotNull(out); if (NONALLOCATABLE_SIZE == bufSize) { Assert.assertFalse(out.isWritable()); } else { Assert.assertTrue(out.writableBytes() > 0); - Assert.assertEquals(isDirect, out.isDirect()); + if (!preferDirect) { + // Only check if preferDirect is not true as if a direct buffer is returned or not depends on + // if sun.misc.Unsafe is present. + assertFalse(out.isDirect()); + } } } finally { in.release();