From d4f074186dcc86f258bc4af10b6d816bda9fee3b Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 4 May 2014 21:09:58 +0200 Subject: [PATCH] [#2454] Correctly return null when DefaultChannelPipeline.firstContext() is called on empty pipeline Motivation: DefaultChannelPipeline.firstContext() should return null when the ipeline is empty. This is not the case atm. Modification: Fix incorrect check in DefaultChannelPipeline.firstContext() and add unit tests. Result: Correctly return null when DefaultChannelPipeline.firstContext() is called on empty pipeline. --- .../netty/channel/DefaultChannelPipeline.java | 2 +- .../channel/DefaultChannelPipelineTest.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java index b8a9685082..bc8cbac538 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java @@ -568,7 +568,7 @@ final class DefaultChannelPipeline implements ChannelPipeline { @Override public ChannelHandlerContext firstContext() { DefaultChannelHandlerContext first = head.next; - if (first == head) { + if (first == tail) { return null; } return head.next; diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java index 35bd97d1d7..e9437632cc 100644 --- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java @@ -511,6 +511,30 @@ public class DefaultChannelPipelineTest { assertEquals(0, buffer.refCnt()); } + @Test + public void testFirstContextEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.firstContext()); + } + + @Test + public void testLastContextEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.lastContext()); + } + + @Test + public void testFirstHandlerEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.first()); + } + + @Test + public void testLastHandlerEmptyPipeline() throws Exception { + ChannelPipeline pipeline = new LocalChannel().pipeline(); + assertNull(pipeline.last()); + } + private static int next(DefaultChannelHandlerContext ctx) { DefaultChannelHandlerContext next = ctx.next; if (next == null) {