/*
 * 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.local;

import org.junit.Assert;
import org.junit.Test;

import io.netty.bootstrap.ClientBootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.Channels;
import io.netty.channel.ChannelEvent;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPipelineFactory;
import io.netty.channel.SimpleChannelUpstreamHandler;
import io.netty.channel.local.DefaultLocalClientChannelFactory;
import io.netty.channel.local.DefaultLocalServerChannelFactory;
import io.netty.channel.local.LocalAddress;

public class LocalAddressTest {
    private static String LOCAL_ADDR_ID = "test.id";

    @Test
    public void localConnectOK()
         throws Exception {

        ClientBootstrap cb = new ClientBootstrap(new DefaultLocalClientChannelFactory());
        ServerBootstrap sb = new ServerBootstrap(new DefaultLocalServerChannelFactory());

        cb.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline()
                throws Exception {

                ChannelPipeline pipeline = Channels.pipeline();

                pipeline.addLast("test.handler", new TestHandler());
                return pipeline;
            }
        });

        sb.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline()
                throws Exception {

                ChannelPipeline pipeline = Channels.pipeline();

                pipeline.addLast("test.handler", new TestHandler());
                return pipeline;
            }
        });

        LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);

        // Start server
        sb.bind(addr);
        
        // Connect to the server
        ChannelFuture connectFuture = cb.connect(addr);
        connectFuture.awaitUninterruptibly();

        // Send a message event up the pipeline.
        Channels.fireMessageReceived(connectFuture.channel(), "Hello, World");

        // Close the channel
        connectFuture.channel().close();

        // Wait until the connection is closed, or the connection attempt fails
        connectFuture.channel().getCloseFuture().awaitUninterruptibly();
        
        sb.releaseExternalResources();
        cb.releaseExternalResources();

        Assert.assertTrue(String.format("Expected null, got channel '%s' for local address '%s'", LocalChannelRegistry.getChannel(addr), addr), LocalChannelRegistry.getChannel(addr) == null);
    }

    @Test
    public void localConnectAgain()
        throws Exception {

        ClientBootstrap cb = new ClientBootstrap(new DefaultLocalClientChannelFactory());
        ServerBootstrap sb = new ServerBootstrap(new DefaultLocalServerChannelFactory());

        cb.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline()
                throws Exception {

                ChannelPipeline pipeline = Channels.pipeline();

                pipeline.addLast("test.handler", new TestHandler());
                return pipeline;
            }
        });

        sb.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline()
                throws Exception {

                ChannelPipeline pipeline = Channels.pipeline();

                pipeline.addLast("test.handler", new TestHandler());
                return pipeline;
            }
        });

        LocalAddress addr = new LocalAddress(LOCAL_ADDR_ID);

        // Start server
        sb.bind(addr);

        // Connect to the server
        ChannelFuture connectFuture = cb.connect(addr);
        connectFuture.awaitUninterruptibly();

        // Send a message event up the pipeline.
        Channels.fireMessageReceived(connectFuture.channel(), "Hello, World");

        // Close the channel
        connectFuture.channel().close();

        // Wait until the connection is closed, or the connection attempt fails
        connectFuture.channel().getCloseFuture().awaitUninterruptibly();

        
        sb.releaseExternalResources();
        cb.releaseExternalResources();

        Assert.assertTrue(String.format("Expected null, got channel '%s' for local address '%s'", LocalChannelRegistry.getChannel(addr), addr), LocalChannelRegistry.getChannel(addr) == null);
     }

    public static class TestHandler
        extends SimpleChannelUpstreamHandler {

        public TestHandler() {
        }

        @Override
        public void handleUpstream(ChannelHandlerContext ctx,
                                                     ChannelEvent e)
            throws Exception {

            System.err.println(String.format("Received upstream event '%s'", e));
        }
    }
}
@@ -663,57 +663,8 @@
 l;%0A%0A
-import org.junit.Assert;%0Aimport org.junit.Test;%0A%0A
 impo
@@ -685,22 +685,16 @@
 otstrap.
-Client
 Bootstra
@@ -770,16 +770,22 @@
 .Channel
+Future
 ;%0Aimport
@@ -813,46 +813,21 @@
 nnel
-s;%0Aimport io.netty.channel.ChannelEven
+HandlerContex
 t;%0Ai
@@ -848,37 +848,39 @@
 .channel.Channel
-Futur
+Pipelin
 e;%0Aimport io.net
@@ -886,103 +886,103 @@
 tty.
-channel.ChannelHandlerContext;%0Aimport io.netty.channel.ChannelPipeline;%0Aimport io.netty
+logging.InternalLogger;%0Aimport io.netty.logging.InternalLoggerFactory;%0A%0Aimport java.nio
 .channel
 .Cha
@@ -973,32 +973,33 @@
 .nio.channel
+s
 .Channel
 PipelineFact
@@ -990,77 +990,35 @@
 nnel
-PipelineFactory;%0Aimport io.netty.channel.SimpleChannelUpstreamHandl
+s;%0A%0Aimport org.junit.Ass
 er
+t
 ;%0Aim
@@ -1026,205 +1026,178 @@
 ort 
-io.netty.channel.local.DefaultLocalClientChannelFactory;%0Aimport io.netty.channel.local.DefaultLocalServerChannelFactory;%0Aimport io.netty.channel.local.LocalAddress;%0A%0Apublic class LocalAddressTest %7B
+org.junit.Test;%0A%0Apublic class LocalAddressTest %7B%0A%0A    private static final InternalLogger logger =%0A            InternalLoggerFactory.getInstance(LocalAddressTest.class);%0A
 %0A   
@@ -1319,38 +1319,32 @@
 tion %7B%0A%0A        
-Client
 Bootstrap cb = n
@@ -1338,38 +1338,32 @@
 tstrap cb = new 
-Client
 Bootstrap(new De
@@ -1360,46 +1360,8 @@
 rap(
-new DefaultLocalClientChannelFactory()
 );%0A 
@@ -1412,46 +1412,8 @@
 rap(
-new DefaultLocalServerChannelFactory()
 );%0A%0A
@@ -2245,24 +2245,16 @@
 (addr);%0A
-        
 %0A       
@@ -2735,24 +2735,16 @@
 ibly();%0A
-        
 %0A       
@@ -4593,24 +4593,16 @@
 bly();%0A%0A
-        
 %0A       
@@ -5196,26 +5196,19 @@
     
-System.err.println
+logger.info
 (Str

Use logger instead of System.err