/*
 * Copyright 2011 The Netty Project
 *
 * The Netty Project 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 io.netty.channel;

import static org.junit.Assert.*;
import io.netty.channel.local.LocalChannel;

import org.junit.Test;

public class DefaultChannelPipelineTest {
    @Test
    public void testReplaceChannelHandler() {
        DefaultChannelPipeline pipeline = new DefaultChannelPipeline(new LocalChannel());

        ChannelHandler handler1 = newHandler();
        pipeline.addLast("handler1", handler1);
        pipeline.addLast("handler2", handler1);
        pipeline.addLast("handler3", handler1);
        assertTrue(pipeline.get("handler1") == handler1);
        assertTrue(pipeline.get("handler2") == handler1);
        assertTrue(pipeline.get("handler3") == handler1);

        ChannelHandler newHandler1 = newHandler();
        pipeline.replace("handler1", "handler1", newHandler1);
        assertTrue(pipeline.get("handler1") == newHandler1);

        ChannelHandler newHandler3 = newHandler();
        pipeline.replace("handler3", "handler3", newHandler3);
        assertTrue(pipeline.get("handler3") == newHandler3);

        ChannelHandler newHandler2 = newHandler();
        pipeline.replace("handler2", "handler2", newHandler2);
        assertTrue(pipeline.get("handler2") == newHandler2);
    }

    private static ChannelHandler newHandler() {
        return new ChannelHandlerAdapter<Byte, Byte>() {
            @Override
            public ChannelBufferHolder<Byte> newInboundBuffer(
                    ChannelInboundHandlerContext<Byte> ctx) throws Exception {
                return ChannelBufferHolders.byteBuffer();
            }

            @Override
            public ChannelBufferHolder<Byte> newOutboundBuffer(
                    ChannelOutboundHandlerContext<Byte> ctx) throws Exception {
                return ChannelBufferHolders.byteBuffer();
            }
        };
    }
}

@@ -687,16 +687,65 @@
 sert.*;%0A
+import io.netty.channel.ChannelHandler.Sharable;%0A
 import i
@@ -1963,16 +1963,97 @@
 urn new 
+TestHandler();%0A    %7D%0A%0A    @Sharable%0A    private static class TestHandler extends 
 ChannelH
@@ -2077,18 +2077,16 @@
 e, Byte%3E
-()
  %7B%0A     
@@ -2080,36 +2080,32 @@
 Byte%3E %7B%0A        
- 
-   
 @Override%0A      
@@ -2090,36 +2090,32 @@
       @Override%0A
-    
         public C
@@ -2169,28 +2169,24 @@
             
- 
-   
 ChannelInbou
@@ -2236,36 +2236,32 @@
 n %7B%0A            
-    
 return ChannelBu
@@ -2294,27 +2294,19 @@
         
- 
-   %7D%0A%0A    
+%7D%0A%0A
         
@@ -2319,28 +2319,24 @@
 ide%0A        
-  
-  
 public Chann
@@ -2367,36 +2367,32 @@
 OutboundBuffer(%0A
-    
                 
@@ -2455,36 +2455,32 @@
 n %7B%0A            
-  
-  
 return ChannelBu
@@ -2517,29 +2517,15 @@
     
-    %7D%0A   
+%7D%0A
     
- 
 %7D;%0A
-    %7D%0A
 %7D%0A

Fix a test failure