From 51a536fd3068ecff95dd27adf047299122ade8ea Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 28 Aug 2013 11:00:08 +0200 Subject: [PATCH] Free up all buffers after the tests --- .../handler/codec/compression/JZlibTest.java | 66 -------------- .../codec/compression/JdkZlibTest.java | 13 +++ .../handler/codec/compression/ZlibTest.java | 91 ++++++++++--------- 3 files changed, 61 insertions(+), 109 deletions(-) diff --git a/codec/src/test/java/io/netty/handler/codec/compression/JZlibTest.java b/codec/src/test/java/io/netty/handler/codec/compression/JZlibTest.java index 462862ea19..28f3919c60 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/JZlibTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/JZlibTest.java @@ -15,15 +15,6 @@ */ package io.netty.handler.codec.compression; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.util.CharsetUtil; -import org.junit.Test; - -import static org.junit.Assert.*; -import static org.junit.Assert.assertEquals; - public class JZlibTest extends ZlibTest { @Override @@ -35,61 +26,4 @@ public class JZlibTest extends ZlibTest { protected ZlibDecoder createDecoder(ZlibWrapper wrapper) { return new JZlibDecoder(wrapper); } - - @Test - public void testZLIB_OR_NONE() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); - - EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.NONE)); - - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); - - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE)); - - chDecoderZlibOrNone.writeInbound(deflatedData); - assertTrue(chDecoderZlibOrNone.finish()); - - assertEquals(data, chDecoderZlibOrNone.readInbound()); - } - - @Test - public void testZLIB_OR_NONE2() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); - - EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.ZLIB)); - - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); - - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE)); - - chDecoderZlibOrNone.writeInbound(deflatedData); - assertTrue(chDecoderZlibOrNone.finish()); - - assertEquals(data, chDecoderZlibOrNone.readInbound()); - } - - @Test - public void testZLIB_OR_NONE3() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); - - EmbeddedChannel chEncoder = new EmbeddedChannel(new JZlibEncoder(ZlibWrapper.GZIP)); - - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); - - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderZlibOrNone = new EmbeddedChannel(new JZlibDecoder(ZlibWrapper.ZLIB_OR_NONE)); - - chDecoderZlibOrNone.writeInbound(deflatedData); - assertTrue(chDecoderZlibOrNone.finish()); - - assertEquals(data, chDecoderZlibOrNone.readInbound()); - } } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java b/codec/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java index c8e494abe3..efc010a1af 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java @@ -30,8 +30,21 @@ public class JdkZlibTest extends ZlibTest { return new JdkZlibDecoder(wrapper); } + @Override @Test(expected = IllegalArgumentException.class) public void testZLIB_OR_NONE() throws Exception { new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE); } + + @Override + @Test(expected = IllegalArgumentException.class) + public void testZLIB_OR_NONE2() throws Exception { + new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE); + } + + @Override + @Test(expected = IllegalArgumentException.class) + public void testZLIB_OR_NONE3() throws Exception { + new JdkZlibDecoder(ZlibWrapper.ZLIB_OR_NONE); + } } diff --git a/codec/src/test/java/io/netty/handler/codec/compression/ZlibTest.java b/codec/src/test/java/io/netty/handler/codec/compression/ZlibTest.java index 898711f8e9..6835d38fe9 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/ZlibTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/ZlibTest.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.zip.GZIPOutputStream; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public abstract class ZlibTest { @@ -41,64 +42,68 @@ public abstract class ZlibTest { EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP)); chDecoderGZip.writeInbound(deflatedData.copy()); assertTrue(chDecoderGZip.finish()); - assertEquals(chDecoderGZip.readInbound(), data); + ByteBuf buf = (ByteBuf) chDecoderGZip.readInbound(); + assertEquals(buf, data); + assertNull(chDecoderGZip.readInbound()); + data.release(); + deflatedData.release(); + buf.release(); + } + + protected void testCompress(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception { + ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); + EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper)); + + chEncoder.writeOutbound(data.copy()); + assertTrue(chEncoder.finish()); + + EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper)); + for (;;) { + ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); + if (deflatedData == null) { + break; + } + chDecoderZlib.writeInbound(deflatedData); + } + + assertTrue(chDecoderZlib.finish()); + + ByteBuf buf = (ByteBuf) chDecoderZlib.readInbound(); + assertEquals(data, buf); + assertNull(chDecoderZlib.readInbound()); + + data.release(); + buf.release(); } @Test public void testZLIB() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); - - EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.ZLIB)); - - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); - - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(ZlibWrapper.ZLIB)); - - chDecoderZlib.writeInbound(deflatedData.copy()); - assertTrue(chDecoderZlib.finish()); - - assertEquals(data, chDecoderZlib.readInbound()); + testCompress(ZlibWrapper.ZLIB, ZlibWrapper.ZLIB); } @Test public void testNONE() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); - - EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.NONE)); - - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); - - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderZlibNone = new EmbeddedChannel(createDecoder(ZlibWrapper.NONE)); - - chDecoderZlibNone.writeInbound(deflatedData.copy()); - assertTrue(chDecoderZlibNone.finish()); - - assertEquals(data, chDecoderZlibNone.readInbound()); + testCompress(ZlibWrapper.NONE, ZlibWrapper.NONE); } @Test public void testGZIP() throws Exception { - ByteBuf data = Unpooled.copiedBuffer("test", CharsetUtil.UTF_8); + testCompress(ZlibWrapper.GZIP, ZlibWrapper.GZIP); + } - EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(ZlibWrapper.GZIP)); + @Test + public void testZLIB_OR_NONE() throws Exception { + testCompress(ZlibWrapper.NONE, ZlibWrapper.ZLIB_OR_NONE); + } - chEncoder.writeOutbound(data.copy()); - assertTrue(chEncoder.finish()); + @Test + public void testZLIB_OR_NONE2() throws Exception { + testCompress(ZlibWrapper.ZLIB, ZlibWrapper.ZLIB_OR_NONE); + } - ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); - - EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP)); - - chDecoderGZip.writeInbound(deflatedData.copy()); - assertTrue(chDecoderGZip.finish()); - - assertEquals(data, chDecoderGZip.readInbound()); + @Test + public void testZLIB_OR_NONE3() throws Exception { + testCompress(ZlibWrapper.GZIP, ZlibWrapper.ZLIB_OR_NONE); } private static byte[] gzip(String message) throws IOException {