From 371657510261d8c3483e7208c4aa8eac446396ac Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 30 Jun 2021 14:12:50 +0200 Subject: [PATCH] Migrate codec to junit5 (#11426) (#11430) Motivation: We should update to use junit5 in all modules. Modifications: Adjust codec tests to use junit5 Result: Part of https://github.com/netty/netty/issues/10757 --- .../codec/compression/JdkZlibTest.java | 17 +++++++--- .../codec/compression/ZlibCrossTest2.java | 14 ++++++-- .../handler/codec/compression/ZlibTest.java | 32 +++++++++++-------- 3 files changed, 43 insertions(+), 20 deletions(-) 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 fcd9dbca6c..32336a84fc 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 @@ -21,13 +21,17 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; import org.apache.commons.compress.utils.IOUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.IOException; import java.util.Arrays; import java.util.Queue; -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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public class JdkZlibTest extends ZlibTest { @@ -42,10 +46,15 @@ public class JdkZlibTest extends ZlibTest { return new JdkZlibDecoder(wrapper, maxAllocation); } - @Test(expected = DecompressionException.class) + @Test @Override public void testZLIB_OR_NONE3() throws Exception { - super.testZLIB_OR_NONE3(); + assertThrows(DecompressionException.class, new Executable() { + @Override + public void execute() throws Throwable { + JdkZlibTest.super.testZLIB_OR_NONE3(); + } + }); } @Test diff --git a/codec/src/test/java/io/netty/handler/codec/compression/ZlibCrossTest2.java b/codec/src/test/java/io/netty/handler/codec/compression/ZlibCrossTest2.java index 64e9db812e..306b652e43 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/ZlibCrossTest2.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/ZlibCrossTest2.java @@ -15,7 +15,10 @@ */ package io.netty.handler.codec.compression; -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 ZlibCrossTest2 extends ZlibTest { @@ -29,9 +32,14 @@ public class ZlibCrossTest2 extends ZlibTest { return new JdkZlibDecoder(wrapper, maxAllocation); } - @Test(expected = DecompressionException.class) + @Test @Override public void testZLIB_OR_NONE3() throws Exception { - super.testZLIB_OR_NONE3(); + assertThrows(DecompressionException.class, new Executable() { + @Override + public void execute() throws Throwable { + ZlibCrossTest2.super.testZLIB_OR_NONE3(); + } + }); } } 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 8769438cc7..3a9b44de8e 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 @@ -25,7 +25,8 @@ import io.netty.util.CharsetUtil; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.EmptyArrays; import io.netty.util.internal.PlatformDependent; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -35,7 +36,11 @@ import java.util.zip.DeflaterOutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -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.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class ZlibTest { @@ -209,7 +214,7 @@ public abstract class ZlibTest { buf.release(); decoded = true; } - assertFalse("should decode nothing", decoded); + assertFalse(decoded, "should decode nothing"); assertFalse(chDecoderZlib.finish()); } finally { @@ -366,19 +371,20 @@ public abstract class ZlibTest { public void testMaxAllocation() throws Exception { int maxAllocation = 1024; ZlibDecoder decoder = createDecoder(ZlibWrapper.ZLIB, maxAllocation); - EmbeddedChannel chDecoder = new EmbeddedChannel(decoder); + final EmbeddedChannel chDecoder = new EmbeddedChannel(decoder); TestByteBufAllocator alloc = new TestByteBufAllocator(chDecoder.alloc()); chDecoder.config().setAllocator(alloc); - try { - chDecoder.writeInbound(Unpooled.wrappedBuffer(deflate(BYTES_LARGE))); - fail("decompressed size > maxAllocation, so should have thrown exception"); - } catch (DecompressionException e) { - assertTrue(e.getMessage().startsWith("Decompression buffer has reached maximum size")); - assertEquals(maxAllocation, alloc.getMaxAllocation()); - assertTrue(decoder.isClosed()); - assertFalse(chDecoder.finish()); - } + DecompressionException e = assertThrows(DecompressionException.class, new Executable() { + @Override + public void execute() throws Throwable { + chDecoder.writeInbound(Unpooled.wrappedBuffer(deflate(BYTES_LARGE))); + } + }); + assertTrue(e.getMessage().startsWith("Decompression buffer has reached maximum size")); + assertEquals(maxAllocation, alloc.getMaxAllocation()); + assertTrue(decoder.isClosed()); + assertFalse(chDecoder.finish()); } private static byte[] gzip(byte[] bytes) throws IOException {