[#1273] Fix resource leaks in test

This commit is contained in:
Norman Maurer 2013-04-22 09:47:44 +02:00
parent c01d37ad05
commit ab685de7a3

View File

@ -15,6 +15,7 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -22,9 +23,12 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ReadOnlyBufferException; import java.nio.ReadOnlyBufferException;
import java.util.ArrayDeque;
import java.util.Queue;
public class ReadOnlyDirectByteBufferBufTest { public class ReadOnlyDirectByteBufferBufTest {
private final Queue<ByteBuf> buffers = new ArrayDeque<ByteBuf>();
protected ByteBuf buffer(ByteBuffer buffer) { protected ByteBuf buffer(ByteBuffer buffer) {
return new ReadOnlyByteBufferBuf(UnpooledByteBufAllocator.DEFAULT, buffer); return new ReadOnlyByteBufferBuf(UnpooledByteBufAllocator.DEFAULT, buffer);
} }
@ -33,62 +37,85 @@ public class ReadOnlyDirectByteBufferBufTest {
return ByteBuffer.allocateDirect(size); return ByteBuffer.allocateDirect(size);
} }
@After
public void dispose() {
for (;;) {
ByteBuf buf = buffers.poll();
if (buf == null) {
break;
}
buf.release();
}
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testConstructWithWritable() { public void testConstructWithWritable() {
buffer(allocate(1)); buffers.add(buffer(allocate(1)));
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetByte() { public void testSetByte() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setByte(0, 1); buf.setByte(0, 1);
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetInt() { public void testSetInt() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setInt(0, 1); buf.setInt(0, 1);
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetShort() { public void testSetShort() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setShort(0, 1); buf.setShort(0, 1);
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetMedium() { public void testSetMedium() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setMedium(0, 1); buf.setMedium(0, 1);
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetLong() { public void testSetLong() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setLong(0, 1); buf.setLong(0, 1);
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaArray() { public void testSetBytesViaArray() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setBytes(0, "test".getBytes()); buf.setBytes(0, "test".getBytes());
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaBuffer() { public void testSetBytesViaBuffer() {
ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer()); ByteBuf buf = buffer(allocate(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setBytes(0, Unpooled.copyInt(1)); buf.setBytes(0, Unpooled.copyInt(1));
} }
@Test(expected = ReadOnlyBufferException.class) @Test(expected = ReadOnlyBufferException.class)
public void testSetBytesViaStream() throws IOException { public void testSetBytesViaStream() throws IOException {
ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer()); ByteBuf buf = buffer(ByteBuffer.allocateDirect(8).asReadOnlyBuffer());
buffers.add(buf);
buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2); buf.setBytes(0, new ByteArrayInputStream("test".getBytes()), 2);
buf.release();
} }
@Test @Test
public void testGetReadByte() { public void testGetReadByte() {
ByteBuf buf = buffer(((ByteBuffer) allocate(2).put(new byte[]{(byte) 1, (byte) 2}).flip()).asReadOnlyBuffer()); ByteBuf buf = buffer(((ByteBuffer) allocate(2).put(new byte[]{(byte) 1, (byte) 2}).flip()).asReadOnlyBuffer());
buffers.add(buf);
Assert.assertEquals(1, buf.getByte(0)); Assert.assertEquals(1, buf.getByte(0));
Assert.assertEquals(2, buf.getByte(1)); Assert.assertEquals(2, buf.getByte(1));
@ -100,6 +127,8 @@ public class ReadOnlyDirectByteBufferBufTest {
@Test @Test
public void testGetReadInt() { public void testGetReadInt() {
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer()); ByteBuf buf = buffer(((ByteBuffer) allocate(8).putInt(1).putInt(2).flip()).asReadOnlyBuffer());
buffers.add(buf);
Assert.assertEquals(1, buf.getInt(0)); Assert.assertEquals(1, buf.getInt(0));
Assert.assertEquals(2, buf.getInt(4)); Assert.assertEquals(2, buf.getInt(4));
@ -112,6 +141,8 @@ public class ReadOnlyDirectByteBufferBufTest {
public void testGetReadShort() { public void testGetReadShort() {
ByteBuf buf = buffer(((ByteBuffer) allocate(8).putShort((short) 1) ByteBuf buf = buffer(((ByteBuffer) allocate(8).putShort((short) 1)
.putShort((short) 2).flip()).asReadOnlyBuffer()); .putShort((short) 2).flip()).asReadOnlyBuffer());
buffers.add(buf);
Assert.assertEquals(1, buf.getShort(0)); Assert.assertEquals(1, buf.getShort(0));
Assert.assertEquals(2, buf.getShort(2)); Assert.assertEquals(2, buf.getShort(2));
@ -123,6 +154,8 @@ public class ReadOnlyDirectByteBufferBufTest {
@Test @Test
public void testGetReadLong() { public void testGetReadLong() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer()); ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
buffers.add(buf);
Assert.assertEquals(1, buf.getLong(0)); Assert.assertEquals(1, buf.getLong(0));
Assert.assertEquals(2, buf.getLong(8)); Assert.assertEquals(2, buf.getLong(8));
@ -134,12 +167,21 @@ public class ReadOnlyDirectByteBufferBufTest {
@Test @Test
public void testCopy() { public void testCopy() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer()); ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(buf, buf.copy()); buffers.add(buf);
ByteBuf copy = buf.copy();
buffers.add(copy);
Assert.assertEquals(buf, copy);
} }
@Test @Test
public void testCopyWithOffset() { public void testCopyWithOffset() {
ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer()); ByteBuf buf = buffer(((ByteBuffer) allocate(16).putLong(1).putLong(2).flip()).asReadOnlyBuffer());
Assert.assertEquals(buf.slice(1, 9), buf.copy(1, 9)); buffers.add(buf);
ByteBuf copy = buf.copy(1, 9);
buffers.add(copy);
Assert.assertEquals(buf.slice(1, 9), copy);
} }
} }