From 917952d0a99c121db3a56cc3a1c25eb00f358dec Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 9 Jan 2017 22:04:18 +0100 Subject: [PATCH] Only use Mockito for mocking. Motivation: We used various mocking frameworks. We should only use one... Modifications: Make usage of mocking framework consistent by only using Mockito. Result: Less dependencies and more consistent mocking usage. --- buffer/pom.xml | 4 + .../io/netty/buffer/ReadOnlyByteBufTest.java | 44 +- .../java/io/netty/buffer/UnpooledTest.java | 10 +- codec-http/pom.xml | 4 + .../codec/http/HttpObjectAggregatorTest.java | 6 +- .../codec/spdy/SpdyFrameDecoderTest.java | 480 +++++++----------- codec/pom.xml | 7 +- common/pom.xml | 4 + .../concurrent/PromiseAggregatorTest.java | 93 ++-- .../util/concurrent/PromiseCombinerTest.java | 8 +- .../util/concurrent/PromiseNotifierTest.java | 42 +- .../internal/logging/CommonsLoggerTest.java | 167 +++--- .../logging/InternalLoggerFactoryTest.java | 113 ++--- .../internal/logging/Log4J2LoggerTest.java | 161 +++--- .../internal/logging/Slf4JLoggerTest.java | 202 ++++---- handler/pom.xml | 4 + pom.xml | 46 +- testsuite/pom.xml | 15 - transport/pom.xml | 4 + .../io/netty/channel/AbstractChannelTest.java | 72 +-- .../channel/CompleteChannelFutureTest.java | 24 +- .../channel/FailedChannelFutureTest.java | 6 +- .../channel/SucceededChannelFutureTest.java | 4 +- 23 files changed, 644 insertions(+), 876 deletions(-) diff --git a/buffer/pom.xml b/buffer/pom.xml index 8055b579c7..e4b8641606 100644 --- a/buffer/pom.xml +++ b/buffer/pom.xml @@ -34,5 +34,9 @@ netty-common ${project.version} + + org.mockito + mockito-core + diff --git a/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java b/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java index 5023821a54..5a50f1a597 100644 --- a/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/ReadOnlyByteBufTest.java @@ -16,6 +16,8 @@ package io.netty.buffer; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; import java.io.IOException; import java.io.InputStream; @@ -25,8 +27,8 @@ import java.nio.channels.GatheringByteChannel; import java.nio.channels.ScatteringByteChannel; import static io.netty.buffer.Unpooled.*; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; /** * Tests read-only channel buffers @@ -74,30 +76,28 @@ public class ReadOnlyByteBufTest { @Test public void shouldForwardReadCallsBlindly() throws Exception { - ByteBuf buf = createStrictMock(ByteBuf.class); - expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes(); - expect(buf.maxCapacity()).andReturn(65536).anyTimes(); - expect(buf.readerIndex()).andReturn(0).anyTimes(); - expect(buf.writerIndex()).andReturn(0).anyTimes(); - expect(buf.capacity()).andReturn(0).anyTimes(); + ByteBuf buf = mock(ByteBuf.class); + when(buf.order()).thenReturn(BIG_ENDIAN); + when(buf.maxCapacity()).thenReturn(65536); + when(buf.readerIndex()).thenReturn(0); + when(buf.writerIndex()).thenReturn(0); + when(buf.capacity()).thenReturn(0); - expect(buf.getBytes(1, (GatheringByteChannel) null, 2)).andReturn(3); - expect(buf.getBytes(4, (OutputStream) null, 5)).andReturn(buf); - expect(buf.getBytes(6, (byte[]) null, 7, 8)).andReturn(buf); - expect(buf.getBytes(9, (ByteBuf) null, 10, 11)).andReturn(buf); - expect(buf.getBytes(12, (ByteBuffer) null)).andReturn(buf); - expect(buf.getByte(13)).andReturn(Byte.valueOf((byte) 14)); - expect(buf.getShort(15)).andReturn(Short.valueOf((short) 16)); - expect(buf.getUnsignedMedium(17)).andReturn(18); - expect(buf.getInt(19)).andReturn(20); - expect(buf.getLong(21)).andReturn(22L); + when(buf.getBytes(1, (GatheringByteChannel) null, 2)).thenReturn(3); + when(buf.getBytes(4, (OutputStream) null, 5)).thenReturn(buf); + when(buf.getBytes(6, (byte[]) null, 7, 8)).thenReturn(buf); + when(buf.getBytes(9, (ByteBuf) null, 10, 11)).thenReturn(buf); + when(buf.getBytes(12, (ByteBuffer) null)).thenReturn(buf); + when(buf.getByte(13)).thenReturn(Byte.valueOf((byte) 14)); + when(buf.getShort(15)).thenReturn(Short.valueOf((short) 16)); + when(buf.getUnsignedMedium(17)).thenReturn(18); + when(buf.getInt(19)).thenReturn(20); + when(buf.getLong(21)).thenReturn(22L); ByteBuffer bb = ByteBuffer.allocate(100); - expect(buf.nioBuffer(23, 24)).andReturn(bb); - expect(buf.capacity()).andReturn(27); - - replay(buf); + when(buf.nioBuffer(23, 24)).thenReturn(bb); + when(buf.capacity()).thenReturn(27); ByteBuf roBuf = unmodifiableBuffer(buf); assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2)); @@ -116,8 +116,6 @@ public class ReadOnlyByteBufTest { assertTrue(roBB.isReadOnly()); assertEquals(27, roBuf.capacity()); - - verify(buf); } @Test(expected = UnsupportedOperationException.class) diff --git a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java index d48b818284..5a3c933128 100644 --- a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java +++ b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java @@ -15,9 +15,9 @@ */ package io.netty.buffer; -import org.easymock.EasyMock; import org.junit.After; import org.junit.Test; +import org.mockito.Mockito; import java.io.InputStream; import java.nio.ByteBuffer; @@ -450,19 +450,23 @@ public class UnpooledTest { // Expected } + InputStream inputStream = Mockito.mock(InputStream.class); try { - buf.setBytes(0, EasyMock.createMock(InputStream.class), 0); + buf.setBytes(0, inputStream, 0); fail(); } catch (UnsupportedOperationException e) { // Expected } + Mockito.verifyZeroInteractions(inputStream); + ScatteringByteChannel scatteringByteChannel = Mockito.mock(ScatteringByteChannel.class); try { - buf.setBytes(0, EasyMock.createMock(ScatteringByteChannel.class), 0); + buf.setBytes(0, scatteringByteChannel, 0); fail(); } catch (UnsupportedOperationException e) { // Expected } + Mockito.verifyZeroInteractions(scatteringByteChannel); } @Test diff --git a/codec-http/pom.xml b/codec-http/pom.xml index 48861e5bad..0ed7580cde 100644 --- a/codec-http/pom.xml +++ b/codec-http/pom.xml @@ -44,6 +44,10 @@ jzlib true + + org.mockito + mockito-core + diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java index 1f27da16de..f025a56b5a 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpObjectAggregatorTest.java @@ -22,8 +22,8 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.codec.TooLongFrameException; import io.netty.util.CharsetUtil; -import org.easymock.EasyMock; import org.junit.Test; +import org.mockito.Mockito; import java.util.List; @@ -159,9 +159,9 @@ public class HttpObjectAggregatorTest { @Test(expected = IllegalStateException.class) public void testSetMaxCumulationBufferComponentsAfterInit() throws Exception { HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE); - ChannelHandlerContext ctx = EasyMock.createMock(ChannelHandlerContext.class); - EasyMock.replay(ctx); + ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); aggr.handlerAdded(ctx); + Mockito.verifyNoMoreInteractions(ctx); aggr.setMaxCumulationBufferComponents(10); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyFrameDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyFrameDecoderTest.java index 370aa0e80c..575458154f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyFrameDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyFrameDecoderTest.java @@ -17,110 +17,123 @@ package io.netty.handler.codec.spdy; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.util.ArrayDeque; +import java.util.Queue; import java.util.Random; import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_HEADER_SIZE; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.createStrictMock; -import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; public class SpdyFrameDecoderTest { private static final Random RANDOM = new Random(); - private final SpdyFrameDecoderDelegate delegate = createStrictMock(SpdyFrameDecoderDelegate.class); + private final SpdyFrameDecoderDelegate delegate = mock(SpdyFrameDecoderDelegate.class); + private final TestSpdyFrameDecoderDelegate testDelegate = new TestSpdyFrameDecoderDelegate(); private SpdyFrameDecoder decoder; @Before public void createDecoder() { - decoder = new SpdyFrameDecoder(SpdyVersion.SPDY_3_1, new SpdyFrameDecoderDelegate() { - @Override - public void readDataFrame(int streamId, boolean last, ByteBuf data) { - try { - delegate.readDataFrame(streamId, last, data); - } finally { - // release the data after we delegate it and so checked it. - data.release(); + decoder = new SpdyFrameDecoder(SpdyVersion.SPDY_3_1, testDelegate); + } + + @After + public void releaseBuffers() { + testDelegate.releaseAll(); + } + + private final class TestSpdyFrameDecoderDelegate implements SpdyFrameDecoderDelegate { + private final Queue buffers = new ArrayDeque(); + + @Override + public void readDataFrame(int streamId, boolean last, ByteBuf data) { + delegate.readDataFrame(streamId, last, data); + buffers.add(data); + } + + @Override + public void readSynStreamFrame(int streamId, int associatedToStreamId, + byte priority, boolean last, boolean unidirectional) { + delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, last, unidirectional); + } + + @Override + public void readSynReplyFrame(int streamId, boolean last) { + delegate.readSynReplyFrame(streamId, last); + } + + @Override + public void readRstStreamFrame(int streamId, int statusCode) { + delegate.readRstStreamFrame(streamId, statusCode); + } + + @Override + public void readSettingsFrame(boolean clearPersisted) { + delegate.readSettingsFrame(clearPersisted); + } + + @Override + public void readSetting(int id, int value, boolean persistValue, boolean persisted) { + delegate.readSetting(id, value, persistValue, persisted); + } + + @Override + public void readSettingsEnd() { + delegate.readSettingsEnd(); + } + + @Override + public void readPingFrame(int id) { + delegate.readPingFrame(id); + } + + @Override + public void readGoAwayFrame(int lastGoodStreamId, int statusCode) { + delegate.readGoAwayFrame(lastGoodStreamId, statusCode); + } + + @Override + public void readHeadersFrame(int streamId, boolean last) { + delegate.readHeadersFrame(streamId, last); + } + + @Override + public void readWindowUpdateFrame(int streamId, int deltaWindowSize) { + delegate.readWindowUpdateFrame(streamId, deltaWindowSize); + } + + @Override + public void readHeaderBlock(ByteBuf headerBlock) { + delegate.readHeaderBlock(headerBlock); + buffers.add(headerBlock); + } + + @Override + public void readHeaderBlockEnd() { + delegate.readHeaderBlockEnd(); + } + + @Override + public void readFrameError(String message) { + delegate.readFrameError(message); + } + + void releaseAll() { + for (;;) { + ByteBuf buf = buffers.poll(); + if (buf == null) { + return; } + buf.release(); } - - @Override - public void readSynStreamFrame(int streamId, int associatedToStreamId, - byte priority, boolean last, boolean unidirectional) { - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, last, unidirectional); - } - - @Override - public void readSynReplyFrame(int streamId, boolean last) { - delegate.readSynReplyFrame(streamId, last); - } - - @Override - public void readRstStreamFrame(int streamId, int statusCode) { - delegate.readRstStreamFrame(streamId, statusCode); - } - - @Override - public void readSettingsFrame(boolean clearPersisted) { - delegate.readSettingsFrame(clearPersisted); - } - - @Override - public void readSetting(int id, int value, boolean persistValue, boolean persisted) { - delegate.readSetting(id, value, persistValue, persisted); - } - - @Override - public void readSettingsEnd() { - delegate.readSettingsEnd(); - } - - @Override - public void readPingFrame(int id) { - delegate.readPingFrame(id); - } - - @Override - public void readGoAwayFrame(int lastGoodStreamId, int statusCode) { - delegate.readGoAwayFrame(lastGoodStreamId, statusCode); - } - - @Override - public void readHeadersFrame(int streamId, boolean last) { - delegate.readHeadersFrame(streamId, last); - } - - @Override - public void readWindowUpdateFrame(int streamId, int deltaWindowSize) { - delegate.readWindowUpdateFrame(streamId, deltaWindowSize); - } - - @Override - public void readHeaderBlock(ByteBuf headerBlock) { - try { - delegate.readHeaderBlock(headerBlock); - } finally { - // release the data after we delegate it and so checked it. - headerBlock.release(); - } - } - - @Override - public void readHeaderBlockEnd() { - delegate.readHeaderBlockEnd(); - } - - @Override - public void readFrameError(String message) { - delegate.readFrameError(message); - } - }); + } } private static void encodeDataFrameHeader(ByteBuf buffer, int streamId, byte flags, int length) { @@ -147,10 +160,8 @@ public class SpdyFrameDecoderTest { for (int i = 0; i < 256; i ++) { buf.writeInt(RANDOM.nextInt()); } - delegate.readDataFrame(streamId, false, buf.slice(SPDY_HEADER_SIZE, length)); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readDataFrame(streamId, false, buf.slice(SPDY_HEADER_SIZE, length)); assertFalse(buf.isReadable()); buf.release(); } @@ -164,10 +175,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); - delegate.readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); } @@ -181,10 +190,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); - delegate.readDataFrame(streamId, true, Unpooled.EMPTY_BUFFER); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readDataFrame(streamId, true, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); } @@ -198,10 +205,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); - delegate.readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); } @@ -215,10 +220,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError((String) any()); assertFalse(buf.isReadable()); buf.release(); } @@ -234,11 +237,9 @@ public class SpdyFrameDecoderTest { encodeDataFrameHeader(buf, streamId1, flags, length); encodeDataFrameHeader(buf, streamId2, flags, length); - delegate.readDataFrame(streamId1, false, Unpooled.EMPTY_BUFFER); - delegate.readDataFrame(streamId2, false, Unpooled.EMPTY_BUFFER); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readDataFrame(streamId1, false, Unpooled.EMPTY_BUFFER); + verify(delegate).readDataFrame(streamId2, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); } @@ -259,11 +260,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -284,11 +283,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, true, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, true, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -309,11 +306,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, true); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, true); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -334,11 +329,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -359,11 +352,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -384,11 +375,9 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5 | 0x1F); // should ignore reserved bits buf.writeByte(0xFF); // should ignore reserved bits - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -406,10 +395,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(associatedToStreamId); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -430,10 +417,8 @@ public class SpdyFrameDecoderTest { buf.writeByte(priority << 5); buf.writeByte(0); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -460,13 +445,11 @@ public class SpdyFrameDecoderTest { headerBlock.writeInt(RANDOM.nextInt()); } - delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); - delegate.readHeaderBlock(headerBlock.duplicate()); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); decoder.decode(headerBlock); - verify(delegate); + verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); + verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); @@ -484,11 +467,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readSynReplyFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynReplyFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -504,11 +485,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readSynReplyFrame(streamId, true); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynReplyFrame(streamId, true); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -524,11 +503,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readSynReplyFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynReplyFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -544,11 +521,10 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); // should ignore reserved bit - delegate.readSynReplyFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSynReplyFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); + assertFalse(buf.isReadable()); buf.release(); } @@ -562,10 +538,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -581,10 +555,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -606,13 +578,11 @@ public class SpdyFrameDecoderTest { headerBlock.writeInt(RANDOM.nextInt()); } - delegate.readSynReplyFrame(streamId, false); - delegate.readHeaderBlock(headerBlock.duplicate()); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); decoder.decode(headerBlock); - verify(delegate); + verify(delegate).readSynReplyFrame(streamId, false); + verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); @@ -632,10 +602,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(statusCode); - delegate.readRstStreamFrame(streamId, statusCode); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readRstStreamFrame(streamId, statusCode); assertFalse(buf.isReadable()); buf.release(); } @@ -653,10 +621,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId | 0x80000000); // should ignore reserved bit buf.writeInt(statusCode); - delegate.readRstStreamFrame(streamId, statusCode); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readRstStreamFrame(streamId, statusCode); assertFalse(buf.isReadable()); buf.release(); } @@ -674,10 +640,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(statusCode); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -695,10 +659,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(statusCode); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -716,10 +678,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(statusCode); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -737,10 +697,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(statusCode); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -764,13 +722,10 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readSettingsFrame(false); - delegate.readSetting(id, value, false, false); - expectLastCall().times(numSettings); delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate, times(numSettings)).readSetting(id, value, false, false); assertFalse(buf.isReadable()); buf.release(); } @@ -786,11 +741,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); - delegate.readSettingsFrame(false); - delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -806,11 +759,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); - delegate.readSettingsFrame(true); - delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(true); + verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -834,13 +785,10 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readSettingsFrame(false); - delegate.readSetting(id, value, true, false); - expectLastCall().times(numSettings); delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate, times(numSettings)).readSetting(id, value, true, false); assertFalse(buf.isReadable()); buf.release(); } @@ -864,13 +812,10 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readSettingsFrame(false); - delegate.readSetting(id, value, false, true); - expectLastCall().times(numSettings); delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate, times(numSettings)).readSetting(id, value, false, true); assertFalse(buf.isReadable()); buf.release(); } @@ -886,11 +831,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); - delegate.readSettingsFrame(false); - delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -914,13 +857,10 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readSettingsFrame(false); - delegate.readSetting(id, value, false, false); - expectLastCall().times(numSettings); delegate.readSettingsEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readSettingsFrame(false); + verify(delegate, times(numSettings)).readSetting(id, value, false, false); assertFalse(buf.isReadable()); buf.release(); } @@ -944,10 +884,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -971,10 +909,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(value); } - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -989,9 +925,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeLong(RANDOM.nextLong()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verifyZeroInteractions(delegate); assertFalse(buf.isReadable()); buf.release(); } @@ -1005,9 +940,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); - replay(delegate); decoder.decode(buf); - verify(delegate); + verifyZeroInteractions(delegate); assertFalse(buf.isReadable()); buf.release(); } @@ -1026,11 +960,10 @@ public class SpdyFrameDecoderTest { segment1.writeInt(RANDOM.nextInt()); segment2.writeInt(RANDOM.nextInt()); - replay(delegate); decoder.decode(header); decoder.decode(segment1); decoder.decode(segment2); - verify(delegate); + verifyZeroInteractions(delegate); assertFalse(header.isReadable()); assertFalse(segment1.isReadable()); assertFalse(segment2.isReadable()); @@ -1050,10 +983,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); - delegate.readPingFrame(id); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readPingFrame(id); assertFalse(buf.isReadable()); buf.release(); } @@ -1069,10 +1000,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); - delegate.readPingFrame(id); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readPingFrame(id); assertFalse(buf.isReadable()); buf.release(); } @@ -1088,10 +1017,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -1109,10 +1036,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); - delegate.readGoAwayFrame(lastGoodStreamId, statusCode); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); } @@ -1130,10 +1055,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); - delegate.readGoAwayFrame(lastGoodStreamId, statusCode); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); } @@ -1151,10 +1074,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(lastGoodStreamId | 0x80000000); // should ignore reserved bit buf.writeInt(statusCode); - delegate.readGoAwayFrame(lastGoodStreamId, statusCode); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); } @@ -1172,10 +1093,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -1191,11 +1110,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readHeadersFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readHeadersFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -1211,11 +1128,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readHeadersFrame(streamId, true); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readHeadersFrame(streamId, true); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -1231,11 +1146,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readHeadersFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readHeadersFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -1251,11 +1164,9 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); // should ignore reserved bit - delegate.readHeadersFrame(streamId, false); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readHeadersFrame(streamId, false); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); } @@ -1269,10 +1180,8 @@ public class SpdyFrameDecoderTest { ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -1288,10 +1197,8 @@ public class SpdyFrameDecoderTest { encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -1312,14 +1219,11 @@ public class SpdyFrameDecoderTest { for (int i = 0; i < 256; i ++) { headerBlock.writeInt(RANDOM.nextInt()); } - - delegate.readHeadersFrame(streamId, false); - delegate.readHeaderBlock(headerBlock.duplicate()); - delegate.readHeaderBlockEnd(); - replay(delegate); decoder.decode(buf); decoder.decode(headerBlock); - verify(delegate); + verify(delegate).readHeadersFrame(streamId, false); + verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); + verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); @@ -1339,10 +1243,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(deltaWindowSize); - delegate.readWindowUpdateFrame(streamId, deltaWindowSize); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); } @@ -1359,10 +1261,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(deltaWindowSize); - delegate.readWindowUpdateFrame(streamId, deltaWindowSize); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); buf.release(); } @@ -1380,10 +1280,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId | 0x80000000); // should ignore reserved bit buf.writeInt(deltaWindowSize | 0x80000000); // should ignore reserved bit - delegate.readWindowUpdateFrame(streamId, deltaWindowSize); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); buf.release(); } @@ -1401,10 +1299,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(deltaWindowSize); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } @@ -1422,10 +1318,8 @@ public class SpdyFrameDecoderTest { buf.writeInt(streamId); buf.writeInt(deltaWindowSize); - delegate.readFrameError((String) anyObject()); - replay(delegate); decoder.decode(buf); - verify(delegate); + verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); } diff --git a/codec/pom.xml b/codec/pom.xml index f677c115ca..f41efc8610 100644 --- a/codec/pom.xml +++ b/codec/pom.xml @@ -49,7 +49,12 @@ jzlib true - + + + org.mockito + mockito-core + + org.jboss.marshalling diff --git a/common/pom.xml b/common/pom.xml index fecba45a2b..1e6ba30edb 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -70,6 +70,10 @@ log4j-core test + + org.mockito + mockito-core + diff --git a/common/src/test/java/io/netty/util/concurrent/PromiseAggregatorTest.java b/common/src/test/java/io/netty/util/concurrent/PromiseAggregatorTest.java index a081124625..63bd01c1b9 100644 --- a/common/src/test/java/io/netty/util/concurrent/PromiseAggregatorTest.java +++ b/common/src/test/java/io/netty/util/concurrent/PromiseAggregatorTest.java @@ -16,9 +16,10 @@ package io.netty.util.concurrent; -import static org.easymock.EasyMock.*; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; import org.junit.Rule; import org.junit.Test; @@ -38,7 +39,7 @@ public class PromiseAggregatorTest { @Test public void testAddNullFuture() { @SuppressWarnings("unchecked") - Promise p = createStrictMock(Promise.class); + Promise p = mock(Promise.class); PromiseAggregator> a = new PromiseAggregator>(p); expectedException.expect(NullPointerException.class); @@ -48,85 +49,95 @@ public class PromiseAggregatorTest { @SuppressWarnings("unchecked") @Test public void testSucessfulNoPending() throws Exception { - Promise p = createStrictMock(Promise.class); + Promise p = mock(Promise.class); PromiseAggregator> a = new PromiseAggregator>(p); - Future future = createStrictMock(Future.class); - expect(p.setSuccess(null)).andReturn(p); - replay(future, p); + Future future = mock(Future.class); + when(p.setSuccess(null)).thenReturn(p); a.add(); a.operationComplete(future); - verify(future, p); + verifyNoMoreInteractions(future); + verify(p).setSuccess(null); } @SuppressWarnings("unchecked") @Test public void testSuccessfulPending() throws Exception { - Promise p = createStrictMock(Promise.class); + Promise p = mock(Promise.class); PromiseAggregator> a = new PromiseAggregator>(p); - Promise p1 = createStrictMock(Promise.class); - Promise p2 = createStrictMock(Promise.class); + Promise p1 = mock(Promise.class); + Promise p2 = mock(Promise.class); - expect(p1.addListener(a)).andReturn(p1); - expect(p2.addListener(a)).andReturn(p2); - expect(p1.isSuccess()).andReturn(true); - expect(p2.isSuccess()).andReturn(true); - expect(p.setSuccess(null)).andReturn(p); - replay(p1, p2, p); + when(p1.addListener(a)).thenReturn(p1); + when(p2.addListener(a)).thenReturn(p2); + when(p1.isSuccess()).thenReturn(true); + when(p2.isSuccess()).thenReturn(true); + when(p.setSuccess(null)).thenReturn(p); assertThat(a.add(p1, null, p2), is(a)); a.operationComplete(p1); a.operationComplete(p2); - verify(p1, p2, p); + verify(p1).addListener(a); + verify(p2).addListener(a); + verify(p1).isSuccess(); + verify(p2).isSuccess(); + verify(p).setSuccess(null); } @SuppressWarnings("unchecked") @Test public void testFailedFutureFailPending() throws Exception { - Promise p = createStrictMock(Promise.class); + Promise p = mock(Promise.class); PromiseAggregator> a = new PromiseAggregator>(p); - Promise p1 = createStrictMock(Promise.class); - Promise p2 = createStrictMock(Promise.class); - Throwable t = createStrictMock(Throwable.class); + Promise p1 = mock(Promise.class); + Promise p2 = mock(Promise.class); + Throwable t = mock(Throwable.class); - expect(p1.addListener(a)).andReturn(p1); - expect(p2.addListener(a)).andReturn(p2); - expect(p1.isSuccess()).andReturn(false); - expect(p1.cause()).andReturn(t); - expect(p.setFailure(t)).andReturn(p); - expect(p2.setFailure(t)).andReturn(p2); - replay(p1, p2, p); + when(p1.addListener(a)).thenReturn(p1); + when(p2.addListener(a)).thenReturn(p2); + when(p1.isSuccess()).thenReturn(false); + when(p1.cause()).thenReturn(t); + when(p.setFailure(t)).thenReturn(p); + when(p2.setFailure(t)).thenReturn(p2); a.add(p1, p2); a.operationComplete(p1); - verify(p1, p2, p); + + verify(p1).addListener(a); + verify(p2).addListener(a); + verify(p1).cause(); + verify(p).setFailure(t); + verify(p2).setFailure(t); } @SuppressWarnings("unchecked") @Test public void testFailedFutureNoFailPending() throws Exception { - Promise p = createStrictMock(Promise.class); + Promise p = mock(Promise.class); PromiseAggregator> a = new PromiseAggregator>(p, false); - Promise p1 = createStrictMock(Promise.class); - Promise p2 = createStrictMock(Promise.class); - Throwable t = createStrictMock(Throwable.class); + Promise p1 = mock(Promise.class); + Promise p2 = mock(Promise.class); + Throwable t = mock(Throwable.class); - expect(p1.addListener(a)).andReturn(p1); - expect(p2.addListener(a)).andReturn(p2); - expect(p1.isSuccess()).andReturn(false); - expect(p1.cause()).andReturn(t); - expect(p.setFailure(t)).andReturn(p); - replay(p1, p2, p); + when(p1.addListener(a)).thenReturn(p1); + when(p2.addListener(a)).thenReturn(p2); + when(p1.isSuccess()).thenReturn(false); + when(p1.cause()).thenReturn(t); + when(p.setFailure(t)).thenReturn(p); a.add(p1, p2); a.operationComplete(p1); - verify(p1, p2, p); - } + verify(p1).addListener(a); + verify(p2).addListener(a); + verify(p1).isSuccess(); + verify(p1).cause(); + verify(p).setFailure(t); + } } diff --git a/common/src/test/java/io/netty/util/concurrent/PromiseCombinerTest.java b/common/src/test/java/io/netty/util/concurrent/PromiseCombinerTest.java index b392585f59..17076d4141 100644 --- a/common/src/test/java/io/netty/util/concurrent/PromiseCombinerTest.java +++ b/common/src/test/java/io/netty/util/concurrent/PromiseCombinerTest.java @@ -61,7 +61,7 @@ public class PromiseCombinerTest { @Test public void testNullAggregatePromise() { combiner.finish(p1); - verify(p1).trySuccess(any(Void.class)); + verify(p1).trySuccess(null); } @Test(expected = NullPointerException.class) @@ -153,7 +153,7 @@ public class PromiseCombinerTest { } private void verifySuccess(Promise p) { - verify(p).trySuccess(any(Void.class)); + verify(p).trySuccess(null); } private void verifyNotCompleted(Promise p) { @@ -179,10 +179,10 @@ public class PromiseCombinerTest { @SuppressWarnings("unchecked") private void mockListener(final Promise p, final GenericFutureListenerConsumer consumer) { doAnswer(new Answer>() { - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "raw-types" }) @Override public Promise answer(InvocationOnMock invocation) throws Throwable { - consumer.accept(invocation.getArgumentAt(0, GenericFutureListener.class)); + consumer.accept((GenericFutureListener) invocation.getArgument(0)); return p; } }).when(p).addListener(any(GenericFutureListener.class)); diff --git a/common/src/test/java/io/netty/util/concurrent/PromiseNotifierTest.java b/common/src/test/java/io/netty/util/concurrent/PromiseNotifierTest.java index de477bf0ae..600e868c69 100644 --- a/common/src/test/java/io/netty/util/concurrent/PromiseNotifierTest.java +++ b/common/src/test/java/io/netty/util/concurrent/PromiseNotifierTest.java @@ -16,7 +16,7 @@ package io.netty.util.concurrent; -import static org.easymock.EasyMock.*; +import static org.mockito.Mockito.*; import org.junit.Rule; import org.junit.Test; @@ -43,49 +43,49 @@ public class PromiseNotifierTest { @Test public void testListenerSuccess() throws Exception { @SuppressWarnings("unchecked") - Promise p1 = createStrictMock(Promise.class); + Promise p1 = mock(Promise.class); @SuppressWarnings("unchecked") - Promise p2 = createStrictMock(Promise.class); + Promise p2 = mock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier> notifier = new PromiseNotifier>(p1, p2); @SuppressWarnings("unchecked") - Future future = createStrictMock(Future.class); - expect(future.isSuccess()).andReturn(true); - expect(future.get()).andReturn(null); - expect(p1.trySuccess(null)).andReturn(true); - expect(p2.trySuccess(null)).andReturn(true); - replay(p1, p2, future); + Future future = mock(Future.class); + when(future.isSuccess()).thenReturn(true); + when(future.get()).thenReturn(null); + when(p1.trySuccess(null)).thenReturn(true); + when(p2.trySuccess(null)).thenReturn(true); notifier.operationComplete(future); - verify(p1, p2); + verify(p1).trySuccess(null); + verify(p2).trySuccess(null); } @Test public void testListenerFailure() throws Exception { @SuppressWarnings("unchecked") - Promise p1 = createStrictMock(Promise.class); + Promise p1 = mock(Promise.class); @SuppressWarnings("unchecked") - Promise p2 = createStrictMock(Promise.class); + Promise p2 = mock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier> notifier = new PromiseNotifier>(p1, p2); @SuppressWarnings("unchecked") - Future future = createStrictMock(Future.class); - Throwable t = createStrictMock(Throwable.class); - expect(future.isSuccess()).andReturn(false); - expect(future.isCancelled()).andReturn(false); - expect(future.cause()).andReturn(t); - expect(p1.tryFailure(t)).andReturn(true); - expect(p2.tryFailure(t)).andReturn(true); - replay(p1, p2, future); + Future future = mock(Future.class); + Throwable t = mock(Throwable.class); + when(future.isSuccess()).thenReturn(false); + when(future.isCancelled()).thenReturn(false); + when(future.cause()).thenReturn(t); + when(p1.tryFailure(t)).thenReturn(true); + when(p2.tryFailure(t)).thenReturn(true); notifier.operationComplete(future); - verify(p1, p2); + verify(p1).tryFailure(t); + verify(p2).tryFailure(t); } } diff --git a/common/src/test/java/io/netty/util/internal/logging/CommonsLoggerTest.java b/common/src/test/java/io/netty/util/internal/logging/CommonsLoggerTest.java index bd4c3d562d..80ac351bf4 100644 --- a/common/src/test/java/io/netty/util/internal/logging/CommonsLoggerTest.java +++ b/common/src/test/java/io/netty/util/internal/logging/CommonsLoggerTest.java @@ -18,204 +18,169 @@ package io.netty.util.internal.logging; import org.apache.commons.logging.Log; import org.junit.Test; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; public class CommonsLoggerTest { private static final Exception e = new Exception(); @Test public void testIsTraceEnabled() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - expect(mock.isTraceEnabled()).andReturn(true); - replay(mock); + when(mockLog.isTraceEnabled()).thenReturn(true); - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); assertTrue(logger.isTraceEnabled()); - verify(mock); + + verify(mockLog).isTraceEnabled(); } @Test public void testIsDebugEnabled() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - expect(mock.isDebugEnabled()).andReturn(true); - replay(mock); + when(mockLog.isDebugEnabled()).thenReturn(true); - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); assertTrue(logger.isDebugEnabled()); - verify(mock); + + verify(mockLog).isDebugEnabled(); } @Test public void testIsInfoEnabled() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - expect(mock.isInfoEnabled()).andReturn(true); - replay(mock); + when(mockLog.isInfoEnabled()).thenReturn(true); - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); assertTrue(logger.isInfoEnabled()); - verify(mock); + + verify(mockLog).isInfoEnabled(); } @Test public void testIsWarnEnabled() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - expect(mock.isWarnEnabled()).andReturn(true); - replay(mock); + when(mockLog.isWarnEnabled()).thenReturn(true); - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); assertTrue(logger.isWarnEnabled()); - verify(mock); + + verify(mockLog).isWarnEnabled(); } @Test public void testIsErrorEnabled() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - expect(mock.isErrorEnabled()).andReturn(true); - replay(mock); + when(mockLog.isErrorEnabled()).thenReturn(true); - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); assertTrue(logger.isErrorEnabled()); - verify(mock); + + verify(mockLog).isErrorEnabled(); } @Test public void testTrace() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.trace("a"); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.trace("a"); - verify(mock); + + verify(mockLog).trace("a"); } @Test public void testTraceWithException() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.trace("a", e); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.trace("a", e); - verify(mock); + + verify(mockLog).trace("a", e); } @Test public void testDebug() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.debug("a"); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.debug("a"); - verify(mock); + + verify(mockLog).debug("a"); } @Test public void testDebugWithException() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.debug("a", e); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.debug("a", e); - verify(mock); + + verify(mockLog).debug("a", e); } @Test public void testInfo() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.info("a"); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.info("a"); - verify(mock); + + verify(mockLog).info("a"); } @Test public void testInfoWithException() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.info("a", e); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.info("a", e); - verify(mock); + + verify(mockLog).info("a", e); } @Test public void testWarn() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.warn("a"); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.warn("a"); - verify(mock); + + verify(mockLog).warn("a"); } @Test public void testWarnWithException() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.warn("a", e); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.warn("a", e); - verify(mock); + + verify(mockLog).warn("a", e); } @Test public void testError() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.error("a"); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.error("a"); - verify(mock); + + verify(mockLog).error("a"); } @Test public void testErrorWithException() { - Log mock = - createStrictMock(Log.class); + Log mockLog = mock(Log.class); - mock.error("a", e); - replay(mock); - - InternalLogger logger = new CommonsLogger(mock, "foo"); + InternalLogger logger = new CommonsLogger(mockLog, "foo"); logger.error("a", e); - verify(mock); + + verify(mockLog).error("a", e); } } diff --git a/common/src/test/java/io/netty/util/internal/logging/InternalLoggerFactoryTest.java b/common/src/test/java/io/netty/util/internal/logging/InternalLoggerFactoryTest.java index 0a9f85a3f7..209d47f209 100644 --- a/common/src/test/java/io/netty/util/internal/logging/InternalLoggerFactoryTest.java +++ b/common/src/test/java/io/netty/util/internal/logging/InternalLoggerFactoryTest.java @@ -19,27 +19,29 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import static org.easymock.EasyMock.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; public class InternalLoggerFactoryTest { private static final Exception e = new Exception(); private InternalLoggerFactory oldLoggerFactory; - private InternalLogger mock; + private InternalLogger mockLogger; @Before public void init() { oldLoggerFactory = InternalLoggerFactory.getDefaultFactory(); - InternalLoggerFactory mockFactory = createMock(InternalLoggerFactory.class); - mock = createStrictMock(InternalLogger.class); - expect(mockFactory.newInstance("mock")).andReturn(mock).anyTimes(); - replay(mockFactory); + + final InternalLoggerFactory mockFactory = mock(InternalLoggerFactory.class); + mockLogger = mock(InternalLogger.class); + when(mockFactory.newInstance("mock")).thenReturn(mockLogger); InternalLoggerFactory.setDefaultFactory(mockFactory); } @After public void destroy() { - reset(mock); + reset(mockLogger); InternalLoggerFactory.setDefaultFactory(oldLoggerFactory); } @@ -64,151 +66,116 @@ public class InternalLoggerFactoryTest { @Test public void testIsTraceEnabled() { - expect(mock.isTraceEnabled()).andReturn(true); - replay(mock); + when(mockLogger.isTraceEnabled()).thenReturn(true); InternalLogger logger = InternalLoggerFactory.getInstance("mock"); assertTrue(logger.isTraceEnabled()); - verify(mock); + verify(mockLogger).isTraceEnabled(); } @Test public void testIsDebugEnabled() { - expect(mock.isDebugEnabled()).andReturn(true); - replay(mock); + when(mockLogger.isDebugEnabled()).thenReturn(true); InternalLogger logger = InternalLoggerFactory.getInstance("mock"); assertTrue(logger.isDebugEnabled()); - verify(mock); + verify(mockLogger).isDebugEnabled(); } @Test public void testIsInfoEnabled() { - expect(mock.isInfoEnabled()).andReturn(true); - replay(mock); + when(mockLogger.isInfoEnabled()).thenReturn(true); InternalLogger logger = InternalLoggerFactory.getInstance("mock"); assertTrue(logger.isInfoEnabled()); - verify(mock); + verify(mockLogger).isInfoEnabled(); } @Test public void testIsWarnEnabled() { - expect(mock.isWarnEnabled()).andReturn(true); - replay(mock); + when(mockLogger.isWarnEnabled()).thenReturn(true); InternalLogger logger = InternalLoggerFactory.getInstance("mock"); assertTrue(logger.isWarnEnabled()); - verify(mock); + verify(mockLogger).isWarnEnabled(); } @Test public void testIsErrorEnabled() { - expect(mock.isErrorEnabled()).andReturn(true); - replay(mock); + when(mockLogger.isErrorEnabled()).thenReturn(true); InternalLogger logger = InternalLoggerFactory.getInstance("mock"); assertTrue(logger.isErrorEnabled()); - verify(mock); + verify(mockLogger).isErrorEnabled(); } @Test public void testTrace() { - mock.trace("a"); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.trace("a"); - verify(mock); + verify(mockLogger).trace("a"); } @Test public void testTraceWithException() { - mock.trace("a", e); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.trace("a", e); - verify(mock); + verify(mockLogger).trace("a", e); } @Test public void testDebug() { - mock.debug("a"); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.debug("a"); - verify(mock); + verify(mockLogger).debug("a"); } @Test public void testDebugWithException() { - mock.debug("a", e); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.debug("a", e); - verify(mock); + verify(mockLogger).debug("a", e); } @Test public void testInfo() { - mock.info("a"); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.info("a"); - verify(mock); + verify(mockLogger).info("a"); } @Test public void testInfoWithException() { - mock.info("a", e); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.info("a", e); - verify(mock); + verify(mockLogger).info("a", e); } @Test public void testWarn() { - mock.warn("a"); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.warn("a"); - verify(mock); + verify(mockLogger).warn("a"); } @Test public void testWarnWithException() { - mock.warn("a", e); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.warn("a", e); - verify(mock); + verify(mockLogger).warn("a", e); } @Test public void testError() { - mock.error("a"); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.error("a"); - verify(mock); + verify(mockLogger).error("a"); } @Test public void testErrorWithException() { - mock.error("a", e); - replay(mock); - - InternalLogger logger = InternalLoggerFactory.getInstance("mock"); + final InternalLogger logger = InternalLoggerFactory.getInstance("mock"); logger.error("a", e); - verify(mock); + verify(mockLogger).error("a", e); } } diff --git a/common/src/test/java/io/netty/util/internal/logging/Log4J2LoggerTest.java b/common/src/test/java/io/netty/util/internal/logging/Log4J2LoggerTest.java index 96ee4346c8..93cf453a7b 100644 --- a/common/src/test/java/io/netty/util/internal/logging/Log4J2LoggerTest.java +++ b/common/src/test/java/io/netty/util/internal/logging/Log4J2LoggerTest.java @@ -18,207 +18,208 @@ package io.netty.util.internal.logging; import org.junit.Test; import org.apache.logging.log4j.Logger; -import static org.easymock.EasyMock.createStrictMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; public class Log4J2LoggerTest { private static final Exception e = new Exception(); @Test public void testIsTraceEnabled() { - Logger mock = createStrictMock(Logger.class); - - expect(mock.getName()).andReturn("foo"); - expect(mock.isTraceEnabled()).andReturn(true); - replay(mock); + Logger mock = mock(Logger.class); + when(mock.getName()).thenReturn("foo"); + when(mock.isTraceEnabled()).thenReturn(true); InternalLogger logger = new Log4J2Logger(mock); assertTrue(logger.isTraceEnabled()); - verify(mock); + + verify(mock).getName(); + verify(mock).isTraceEnabled(); } @Test public void testIsDebugEnabled() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isDebugEnabled()).andReturn(true); - replay(mock); + when(mock.getName()).thenReturn("foo"); + when(mock.isDebugEnabled()).thenReturn(true); InternalLogger logger = new Log4J2Logger(mock); assertTrue(logger.isDebugEnabled()); - verify(mock); + + verify(mock).getName(); + verify(mock).isDebugEnabled(); } @Test public void testIsInfoEnabled() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isInfoEnabled()).andReturn(true); - replay(mock); + when(mock.getName()).thenReturn("foo"); + when(mock.isInfoEnabled()).thenReturn(true); InternalLogger logger = new Log4J2Logger(mock); assertTrue(logger.isInfoEnabled()); - verify(mock); + + verify(mock).getName(); + verify(mock).isInfoEnabled(); } @Test public void testIsWarnEnabled() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isWarnEnabled()).andReturn(true); - replay(mock); + when(mock.getName()).thenReturn("foo"); + when(mock.isWarnEnabled()).thenReturn(true); InternalLogger logger = new Log4J2Logger(mock); assertTrue(logger.isWarnEnabled()); - verify(mock); + + verify(mock).getName(); + verify(mock).isWarnEnabled(); } @Test public void testIsErrorEnabled() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isErrorEnabled()).andReturn(true); - replay(mock); + when(mock.getName()).thenReturn("foo"); + when(mock.isErrorEnabled()).thenReturn(true); InternalLogger logger = new Log4J2Logger(mock); assertTrue(logger.isErrorEnabled()); - verify(mock); + + verify(mock).getName(); + verify(mock).isErrorEnabled(); } @Test public void testTrace() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.trace("a"); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.trace("a"); - verify(mock); + + verify(mock).getName(); + verify(mock).trace("a"); } @Test public void testTraceWithException() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.trace("a", e); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.trace("a", e); - verify(mock); + + verify(mock).getName(); + verify(mock).trace("a", e); } @Test public void testDebug() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.debug("a"); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.debug("a"); - verify(mock); + + verify(mock).getName(); + verify(mock).debug("a"); } @Test public void testDebugWithException() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.debug("a", e); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.debug("a", e); - verify(mock); + + verify(mock).getName(); + verify(mock).debug("a", e); } @Test public void testInfo() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.info("a"); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.info("a"); - verify(mock); + + verify(mock).getName(); + verify(mock).info("a"); } @Test public void testInfoWithException() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.info("a", e); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.info("a", e); - verify(mock); + + verify(mock).getName(); + verify(mock).info("a", e); } @Test public void testWarn() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.warn("a"); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.warn("a"); - verify(mock); + + verify(mock).getName(); + verify(mock).warn("a"); } @Test public void testWarnWithException() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.warn("a", e); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.warn("a", e); - verify(mock); + + verify(mock).getName(); + verify(mock).warn("a", e); } @Test public void testError() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.error("a"); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.error("a"); - verify(mock); + + verify(mock).getName(); + verify(mock).error("a"); } @Test public void testErrorWithException() { - Logger mock = createStrictMock(Logger.class); + Logger mock = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.error("a", e); - replay(mock); + when(mock.getName()).thenReturn("foo"); InternalLogger logger = new Log4J2Logger(mock); logger.error("a", e); - verify(mock); + + verify(mock).getName(); + verify(mock).error("a", e); } } diff --git a/common/src/test/java/io/netty/util/internal/logging/Slf4JLoggerTest.java b/common/src/test/java/io/netty/util/internal/logging/Slf4JLoggerTest.java index 3eda5eed09..238bfef2b1 100644 --- a/common/src/test/java/io/netty/util/internal/logging/Slf4JLoggerTest.java +++ b/common/src/test/java/io/netty/util/internal/logging/Slf4JLoggerTest.java @@ -18,219 +18,209 @@ package io.netty.util.internal.logging; import org.junit.Test; import org.slf4j.Logger; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; public class Slf4JLoggerTest { private static final Exception e = new Exception(); @Test public void testIsTraceEnabled() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isTraceEnabled()).andReturn(true); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); + when(mockLogger.isTraceEnabled()).thenReturn(true); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); assertTrue(logger.isTraceEnabled()); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).isTraceEnabled(); } @Test public void testIsDebugEnabled() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isDebugEnabled()).andReturn(true); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); + when(mockLogger.isDebugEnabled()).thenReturn(true); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); assertTrue(logger.isDebugEnabled()); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).isDebugEnabled(); } @Test public void testIsInfoEnabled() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isInfoEnabled()).andReturn(true); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); + when(mockLogger.isInfoEnabled()).thenReturn(true); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); assertTrue(logger.isInfoEnabled()); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).isInfoEnabled(); } @Test public void testIsWarnEnabled() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isWarnEnabled()).andReturn(true); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); + when(mockLogger.isWarnEnabled()).thenReturn(true); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); assertTrue(logger.isWarnEnabled()); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).isWarnEnabled(); } @Test public void testIsErrorEnabled() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - expect(mock.isErrorEnabled()).andReturn(true); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); + when(mockLogger.isErrorEnabled()).thenReturn(true); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); assertTrue(logger.isErrorEnabled()); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).isErrorEnabled(); } @Test public void testTrace() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.trace("a"); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.trace("a"); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).trace("a"); } @Test public void testTraceWithException() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.trace("a", e); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.trace("a", e); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).trace("a", e); } @Test public void testDebug() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.debug("a"); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.debug("a"); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).debug("a"); } @Test public void testDebugWithException() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.debug("a", e); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.debug("a", e); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).debug("a", e); } @Test public void testInfo() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.info("a"); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.info("a"); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).info("a"); } @Test public void testInfoWithException() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.info("a", e); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.info("a", e); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).info("a", e); } @Test public void testWarn() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.warn("a"); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.warn("a"); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).warn("a"); } @Test public void testWarnWithException() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.warn("a", e); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.warn("a", e); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).warn("a", e); } @Test public void testError() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.error("a"); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.error("a"); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).error("a"); } @Test public void testErrorWithException() { - Logger mock = - createStrictMock(Logger.class); + Logger mockLogger = mock(Logger.class); - expect(mock.getName()).andReturn("foo"); - mock.error("a", e); - replay(mock); + when(mockLogger.getName()).thenReturn("foo"); - InternalLogger logger = new Slf4JLogger(mock); + InternalLogger logger = new Slf4JLogger(mockLogger); logger.error("a", e); - verify(mock); + + verify(mockLogger).getName(); + verify(mockLogger).error("a", e); } } diff --git a/handler/pom.xml b/handler/pom.xml index 24a789cf05..7156bebdde 100644 --- a/handler/pom.xml +++ b/handler/pom.xml @@ -65,6 +65,10 @@ alpn-api true + + org.mockito + mockito-core + diff --git a/pom.xml b/pom.xml index d554bdd323..ca27c30813 100644 --- a/pom.xml +++ b/pom.xml @@ -434,34 +434,10 @@ 1.3 test - - org.easymock - easymock - 3.4 - test - - - org.easymock - easymockclassextension - 3.2 - test - - - org.jmock - jmock-junit4 - 2.6.0 - test - - - junit - junit-dep - - - org.mockito mockito-core - 1.10.8 + 2.7.2 test @@ -534,26 +510,6 @@ hamcrest-library test - - org.easymock - easymock - test - - - org.easymock - easymockclassextension - test - - - org.jmock - jmock-junit4 - test - - - org.mockito - mockito-core - test - ch.qos.logback logback-classic diff --git a/testsuite/pom.xml b/testsuite/pom.xml index 294e923deb..30fe8425e2 100644 --- a/testsuite/pom.xml +++ b/testsuite/pom.xml @@ -74,21 +74,6 @@ hamcrest-library compile - - org.easymock - easymock - compile - - - org.easymock - easymockclassextension - compile - - - org.jmock - jmock-junit4 - compile - org.mockito mockito-core diff --git a/transport/pom.xml b/transport/pom.xml index 4e21a97abe..bfc6ba6e80 100644 --- a/transport/pom.xml +++ b/transport/pom.xml @@ -34,6 +34,10 @@ netty-buffer ${project.version} + + org.mockito + mockito-core + diff --git a/transport/src/test/java/io/netty/channel/AbstractChannelTest.java b/transport/src/test/java/io/netty/channel/AbstractChannelTest.java index 3a69155c1d..605a1f3287 100644 --- a/transport/src/test/java/io/netty/channel/AbstractChannelTest.java +++ b/transport/src/test/java/io/netty/channel/AbstractChannelTest.java @@ -17,64 +17,49 @@ package io.netty.channel; import java.net.SocketAddress; -import org.easymock.Capture; -import org.easymock.IAnswer; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; -import static org.easymock.EasyMock.*; + +import static org.mockito.Mockito.*; public class AbstractChannelTest { @Test public void ensureInitialRegistrationFiresActive() throws Throwable { - EventLoop eventLoop = createNiceMock(EventLoop.class); + EventLoop eventLoop = mock(EventLoop.class); // This allows us to have a single-threaded test - expect(eventLoop.inEventLoop()).andReturn(true).anyTimes(); + when(eventLoop.inEventLoop()).thenReturn(true); TestChannel channel = new TestChannel(); - ChannelInboundHandler handler = createMock(ChannelInboundHandler.class); - handler.handlerAdded(anyObject(ChannelHandlerContext.class)); expectLastCall(); - Capture throwable = catchHandlerExceptions(handler); - handler.channelRegistered(anyObject(ChannelHandlerContext.class)); - expectLastCall().once(); - handler.channelActive(anyObject(ChannelHandlerContext.class)); - expectLastCall().once(); - replay(handler, eventLoop); + ChannelInboundHandler handler = mock(ChannelInboundHandler.class); channel.pipeline().addLast(handler); registerChannel(eventLoop, channel); - checkForHandlerException(throwable); - verify(handler); + verify(handler).handlerAdded(any(ChannelHandlerContext.class)); + verify(handler).channelRegistered(any(ChannelHandlerContext.class)); + verify(handler).channelActive(any(ChannelHandlerContext.class)); } @Test public void ensureSubsequentRegistrationDoesNotFireActive() throws Throwable { - final EventLoop eventLoop = createNiceMock(EventLoop.class); + final EventLoop eventLoop = mock(EventLoop.class); // This allows us to have a single-threaded test - expect(eventLoop.inEventLoop()).andReturn(true).anyTimes(); - eventLoop.execute(anyObject(Runnable.class)); - expectLastCall().andAnswer(new IAnswer() { + when(eventLoop.inEventLoop()).thenReturn(true); + + doAnswer(new Answer() { @Override - public Object answer() throws Throwable { - ((Runnable) getCurrentArguments()[0]).run(); + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + ((Runnable) invocationOnMock.getArgument(0)).run(); return null; } - }).once(); + }).when(eventLoop).execute(any(Runnable.class)); final TestChannel channel = new TestChannel(); - ChannelInboundHandler handler = createMock(ChannelInboundHandler.class); - handler.handlerAdded(anyObject(ChannelHandlerContext.class)); expectLastCall(); - Capture throwable = catchHandlerExceptions(handler); - handler.channelRegistered(anyObject(ChannelHandlerContext.class)); - expectLastCall().times(2); // Should register twice - handler.channelActive(anyObject(ChannelHandlerContext.class)); - expectLastCall().once(); // Should only fire active once + ChannelInboundHandler handler = mock(ChannelInboundHandler.class); - handler.channelUnregistered(anyObject(ChannelHandlerContext.class)); - expectLastCall().once(); // Should register twice - - replay(handler, eventLoop); channel.pipeline().addLast(handler); registerChannel(eventLoop, channel); @@ -82,8 +67,12 @@ public class AbstractChannelTest { registerChannel(eventLoop, channel); - checkForHandlerException(throwable); - verify(handler); + verify(handler).handlerAdded(any(ChannelHandlerContext.class)); + + // Should register twice + verify(handler, times(2)) .channelRegistered(any(ChannelHandlerContext.class)); + verify(handler).channelActive(any(ChannelHandlerContext.class)); + verify(handler).channelUnregistered(any(ChannelHandlerContext.class)); } private static void registerChannel(EventLoop eventLoop, Channel channel) throws Exception { @@ -92,19 +81,6 @@ public class AbstractChannelTest { future.sync(); // Cause any exceptions to be thrown } - private static Capture catchHandlerExceptions(ChannelInboundHandler handler) throws Exception { - Capture throwable = new Capture(); - handler.exceptionCaught(anyObject(ChannelHandlerContext.class), capture(throwable)); - expectLastCall().anyTimes(); - return throwable; - } - - private static void checkForHandlerException(Capture throwable) throws Throwable { - if (throwable.hasCaptured()) { - throw throwable.getValue(); - } - } - private static class TestChannel extends AbstractChannel { private class TestUnsafe extends AbstractUnsafe { diff --git a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java index 84120702ad..a6e12da168 100644 --- a/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java +++ b/transport/src/test/java/io/netty/channel/CompleteChannelFutureTest.java @@ -15,24 +15,15 @@ */ package io.netty.channel; -import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import java.util.concurrent.TimeUnit; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; public class CompleteChannelFutureTest { - private final Channel channel = createMock(Channel.class); - private CompleteChannelFuture future; - - @Before - public void init() { - future = new CompleteChannelFutureImpl(channel); - } - @Test(expected = NullPointerException.class) public void shouldDisallowNullChannel() { new CompleteChannelFutureImpl(null); @@ -40,15 +31,19 @@ public class CompleteChannelFutureTest { @Test public void shouldNotDoAnythingOnRemove() throws Exception { - ChannelFutureListener l = createStrictMock(ChannelFutureListener.class); - replay(l); - + Channel channel = Mockito.mock(Channel.class); + CompleteChannelFuture future = new CompleteChannelFutureImpl(channel); + ChannelFutureListener l = Mockito.mock(ChannelFutureListener.class); future.removeListener(l); - verify(l); + Mockito.verifyNoMoreInteractions(l); + Mockito.verifyZeroInteractions(channel); } @Test public void testConstantProperties() throws InterruptedException { + Channel channel = Mockito.mock(Channel.class); + CompleteChannelFuture future = new CompleteChannelFutureImpl(channel); + assertSame(channel, future.channel()); assertTrue(future.isDone()); assertSame(future, future.await()); @@ -57,6 +52,7 @@ public class CompleteChannelFutureTest { assertSame(future, future.awaitUninterruptibly()); assertTrue(future.awaitUninterruptibly(1)); assertTrue(future.awaitUninterruptibly(1, TimeUnit.NANOSECONDS)); + Mockito.verifyZeroInteractions(channel); } private static class CompleteChannelFutureImpl extends CompleteChannelFuture { diff --git a/transport/src/test/java/io/netty/channel/FailedChannelFutureTest.java b/transport/src/test/java/io/netty/channel/FailedChannelFutureTest.java index 2e8070dd75..a0ffac9ff2 100644 --- a/transport/src/test/java/io/netty/channel/FailedChannelFutureTest.java +++ b/transport/src/test/java/io/netty/channel/FailedChannelFutureTest.java @@ -16,14 +16,14 @@ package io.netty.channel; import org.junit.Test; +import org.mockito.Mockito; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; public class FailedChannelFutureTest { @Test public void testConstantProperties() { - Channel channel = createMock(Channel.class); + Channel channel = Mockito.mock(Channel.class); Exception e = new Exception(); FailedChannelFuture future = new FailedChannelFuture(channel, null, e); @@ -33,6 +33,6 @@ public class FailedChannelFutureTest { @Test(expected = NullPointerException.class) public void shouldDisallowNullException() { - new FailedChannelFuture(createMock(Channel.class), null, null); + new FailedChannelFuture(Mockito.mock(Channel.class), null, null); } } diff --git a/transport/src/test/java/io/netty/channel/SucceededChannelFutureTest.java b/transport/src/test/java/io/netty/channel/SucceededChannelFutureTest.java index 9faa3a979f..949ecfeaeb 100644 --- a/transport/src/test/java/io/netty/channel/SucceededChannelFutureTest.java +++ b/transport/src/test/java/io/netty/channel/SucceededChannelFutureTest.java @@ -16,14 +16,14 @@ package io.netty.channel; import org.junit.Test; +import org.mockito.Mockito; -import static org.easymock.EasyMock.*; import static org.junit.Assert.*; public class SucceededChannelFutureTest { @Test public void testConstantProperties() { - Channel channel = createMock(Channel.class); + Channel channel = Mockito.mock(Channel.class); SucceededChannelFuture future = new SucceededChannelFuture(channel, null); assertTrue(future.isSuccess());