* StaticChannelPipeline's constructor should stop when a null is encountered

* Added a test case for StaticChannelPipeline
This commit is contained in:
Trustin Lee 2010-05-06 07:00:52 +00:00
parent 56cdf73cb9
commit 5edc4e16aa
2 changed files with 93 additions and 7 deletions

View File

@ -61,16 +61,29 @@ public class StaticChannelPipeline implements ChannelPipeline {
throw new IllegalArgumentException("no handlers specified"); throw new IllegalArgumentException("no handlers specified");
} }
contexts = new StaticChannelHandlerContext[handlers.length]; // Get the number of first non-null handlers.
lastIndex = contexts.length - 1; StaticChannelHandlerContext[] contexts =
new StaticChannelHandlerContext[handlers.length];
for (int i = 0; i < contexts.length; i ++) { int nContexts;
ChannelHandler h = handlers[i]; for (nContexts = 0; nContexts < contexts.length; nContexts ++) {
ChannelHandler h = handlers[nContexts];
if (h == null) { if (h == null) {
// FIXME: Should just break the loop on null break;
throw new NullPointerException("handlers[" + i + ']');
} }
}
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); String name = ConversionUtil.toString(i);
StaticChannelHandlerContext ctx = StaticChannelHandlerContext ctx =
new StaticChannelHandlerContext(i, name, h); new StaticChannelHandlerContext(i, name, h);

View File

@ -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 <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
* @version $Rev$, $Date$
*/
public class StaticChannelPipelineTest {
@Test
public void testConstructionWithoutNull() {
StaticChannelPipeline p = new StaticChannelPipeline(new A(), new B());
Map<String, ChannelHandler> 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<String, ChannelHandler> m = p.toMap();
Assert.assertEquals(0, m.size());
}
@Test
public void testConstructionWithNull2() {
StaticChannelPipeline p = new StaticChannelPipeline(new A(), null, new B());
Map<String, ChannelHandler> 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<String, ChannelHandler> 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
}
}