/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed 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 com.alibaba.dubbo.rpc.protocol.dubbo.telnet;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.exchange.ExchangeClient;
import com.alibaba.dubbo.remoting.exchange.Exchangers;
import com.alibaba.dubbo.remoting.telnet.TelnetHandler;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService;
import com.alibaba.dubbo.rpc.protocol.dubbo.support.ProtocolUtils;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * PortTelnetHandlerTest.java
 *
 * @author tony.chenl
 */
public class PortTelnetHandlerTest {

    private static TelnetHandler port = new PortTelnetHandler();
    private Invoker<DemoService> mockInvoker;

    @SuppressWarnings("unchecked")
    @Before
    public void before() {
        mockInvoker = EasyMock.createMock(Invoker.class);
        EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
        EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20887/demo")).anyTimes();
        EasyMock.replay(mockInvoker);
        DubboProtocol.getDubboProtocol().export(mockInvoker);
    }

    @After
    public void after() {
        EasyMock.reset(mockInvoker);
        ProtocolUtils.closeAll();
    }

    @Test
    public void testListClient() throws Exception {
        ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
        ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:20887/demo");
        Thread.sleep(5000);
        String result = port.telnet(null, "-l 20887");
        String client1Addr = client1.getLocalAddress().toString();
        String client2Addr = client2.getLocalAddress().toString();
        System.out.printf("Result: %s %n", result);
        System.out.printf("Client 1 Address %s %n", client1Addr);
        System.out.printf("Client 2 Address %s %n", client2Addr);
        assertTrue(result.contains(client1Addr));
        assertTrue(result.contains(client2Addr));

    }

    @Test
    public void testListDetail() throws RemotingException {
        String result = port.telnet(null, "-l");
        assertEquals("dubbo://127.0.0.1:20887", result);
    }

    @Test
    public void testListAllPort() throws RemotingException {
        String result = port.telnet(null, "");
        assertEquals("20887", result);
    }

    @Test
    public void testErrorMessage() throws RemotingException {
        String result = port.telnet(null, "a");
        assertEquals("Illegal port a, must be integer.", result);
    }

    @Test
    public void testNoPort() throws RemotingException {
        String result = port.telnet(null, "-l 20880");
        assertEquals("No such port 20880", result);
    }

}
@@ -2165,32 +2165,158 @@
 ll();%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * NAT%E7%BD%91%E7%BB%9C%E5%9C%BA%E6%99%AF%EF%BC%8Cserver channel.getRemoteAddress()%E5%8F%AF%E8%83%BD%E6%8B%BF%E5%88%B0%E7%9A%84%E6%98%AF%E7%BD%91%E5%85%B3%E6%88%96NAT%E8%BD%AC%E6%8D%A2%E5%90%8E%E7%9A%84%E5%9C%B0%E5%9D%80%0D%0A     * %E5%8F%AA%E5%88%A4%E6%96%AD%E7%AB%AF%E5%8F%A3%0D%0A     * @throws Exception%0D%0A     */%0D%0A
     @Test%0D%0A    p
@@ -2982,70 +2982,150 @@
 ins(
-client1Addr));%0D%0A        assertTrue(result.contains(client2Addr
+String.valueOf(client1.getLocalAddress().getPort())));%0D%0A        assertTrue(result.contains(String.valueOf(client2.getLocalAddress().getPort())
 ));%0D

Fix unit test PortTelnetHandlerTest