From 5edc4e16aae9196bd02a645b12c236060491cc5c Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 6 May 2010 07:00:52 +0000 Subject: [PATCH] * StaticChannelPipeline's constructor should stop when a null is encountered * Added a test case for StaticChannelPipeline --- .../netty/channel/StaticChannelPipeline.java | 27 +++++-- .../channel/StaticChannelPipelineTest.java | 73 +++++++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/test/java/org/jboss/netty/channel/StaticChannelPipelineTest.java diff --git a/src/main/java/org/jboss/netty/channel/StaticChannelPipeline.java b/src/main/java/org/jboss/netty/channel/StaticChannelPipeline.java index 3523b3d51f..775b67df20 100644 --- a/src/main/java/org/jboss/netty/channel/StaticChannelPipeline.java +++ b/src/main/java/org/jboss/netty/channel/StaticChannelPipeline.java @@ -61,16 +61,29 @@ public class StaticChannelPipeline implements ChannelPipeline { throw new IllegalArgumentException("no handlers specified"); } - contexts = new StaticChannelHandlerContext[handlers.length]; - lastIndex = contexts.length - 1; - - for (int i = 0; i < contexts.length; i ++) { - ChannelHandler h = handlers[i]; + // Get the number of first non-null handlers. + StaticChannelHandlerContext[] contexts = + new StaticChannelHandlerContext[handlers.length]; + int nContexts; + for (nContexts = 0; nContexts < contexts.length; nContexts ++) { + ChannelHandler h = handlers[nContexts]; if (h == null) { - // FIXME: Should just break the loop on null - throw new NullPointerException("handlers[" + i + ']'); + break; } + } + if (nContexts == contexts.length) { + this.contexts = contexts; + lastIndex = contexts.length - 1; + } else { + this.contexts = contexts = + new StaticChannelHandlerContext[nContexts]; + lastIndex = nContexts - 1; + } + + // Initialize the first non-null handlers only. + for (int i = 0; i < nContexts; i ++) { + ChannelHandler h = handlers[i]; String name = ConversionUtil.toString(i); StaticChannelHandlerContext ctx = new StaticChannelHandlerContext(i, name, h); diff --git a/src/test/java/org/jboss/netty/channel/StaticChannelPipelineTest.java b/src/test/java/org/jboss/netty/channel/StaticChannelPipelineTest.java new file mode 100644 index 0000000000..b9b1d6b990 --- /dev/null +++ b/src/test/java/org/jboss/netty/channel/StaticChannelPipelineTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2010 Red Hat, Inc. + * + * Red Hat licenses this file to you under the Apache License, version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package org.jboss.netty.channel; + +import java.util.Map; + +import junit.framework.Assert; + +import org.junit.Test; + +/** + * @author The Netty Project + * @author Trustin Lee + * @version $Rev$, $Date$ + */ +public class StaticChannelPipelineTest { + + @Test + public void testConstructionWithoutNull() { + StaticChannelPipeline p = new StaticChannelPipeline(new A(), new B()); + Map m = p.toMap(); + Assert.assertEquals(2, m.size()); + Assert.assertTrue(m.get("0") instanceof A); + Assert.assertTrue(m.get("1") instanceof B); + } + + @Test + public void testConstructionWithNull1() { + StaticChannelPipeline p = new StaticChannelPipeline(null, new A(), new B()); + Map m = p.toMap(); + Assert.assertEquals(0, m.size()); + + } + + @Test + public void testConstructionWithNull2() { + StaticChannelPipeline p = new StaticChannelPipeline(new A(), null, new B()); + Map m = p.toMap(); + Assert.assertEquals(1, m.size()); + Assert.assertTrue(m.get("0") instanceof A); + + } + + @Test + public void testConstructionWithNull() { + StaticChannelPipeline p = new StaticChannelPipeline(new A(), new B(), null); + Map m = p.toMap(); + Assert.assertEquals(2, m.size()); + Assert.assertTrue(m.get("0") instanceof A); + Assert.assertTrue(m.get("1") instanceof B); + } + + static final class A extends SimpleChannelHandler { + // Dummy + } + + static final class B extends SimpleChannelHandler { + // Dummy + } +}