diff --git a/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java index e8d41137c0..bbe25879e6 100644 --- a/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java +++ b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java @@ -58,15 +58,15 @@ public class InvokerTelnetHandlerTest { public void testInvokeDefaultSService() throws RemotingException { 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:20885/demo")).anyTimes(); + EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20883/demo")).anyTimes(); EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes(); mockChannel = EasyMock.createMock(Channel.class); EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService").anyTimes(); EasyMock.expect(mockChannel.getLocalAddress()).andReturn(NetUtils.toAddress("127.0.0.1:5555")).anyTimes(); - EasyMock.expect(mockChannel.getRemoteAddress()).andReturn(NetUtils.toAddress("127.0.0.1:20885")).anyTimes(); + EasyMock.expect(mockChannel.getRemoteAddress()).andReturn(NetUtils.toAddress("127.0.0.1:20883")).anyTimes(); EasyMock.replay(mockChannel, mockInvoker); DubboProtocol.getDubboProtocol().export(mockInvoker); - String result = invoke.telnet(mockChannel, "DemoService.echo(\"hello\")"); + String result = invoke.telnet(mockChannel, "DemoService.echo(\"ok\")"); assertTrue(result.contains("Use default service com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService.\r\n\"ok\"\r\n")); EasyMock.reset(mockChannel, mockInvoker); } @@ -76,15 +76,15 @@ public class InvokerTelnetHandlerTest { public void testInvokeAutoFindMethod() throws RemotingException { 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:20885/demo")).anyTimes(); + EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20883/demo")).anyTimes(); EasyMock.expect(mockInvoker.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes(); mockChannel = EasyMock.createMock(Channel.class); EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes(); EasyMock.expect(mockChannel.getLocalAddress()).andReturn(NetUtils.toAddress("127.0.0.1:5555")).anyTimes(); - EasyMock.expect(mockChannel.getRemoteAddress()).andReturn(NetUtils.toAddress("127.0.0.1:20885")).anyTimes(); + EasyMock.expect(mockChannel.getRemoteAddress()).andReturn(NetUtils.toAddress("127.0.0.1:20883")).anyTimes(); EasyMock.replay(mockChannel, mockInvoker); DubboProtocol.getDubboProtocol().export(mockInvoker); - String result = invoke.telnet(mockChannel, "echo(\"hello\")"); + String result = invoke.telnet(mockChannel, "echo(\"ok\")"); assertTrue(result.contains("ok")); EasyMock.reset(mockChannel, mockInvoker); } diff --git a/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java new file mode 100644 index 0000000000..97d3fc586c --- /dev/null +++ b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/LogTelnetHandlerTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 1999-2101 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 static org.junit.Assert.assertTrue; + +import org.easymock.EasyMock; +import org.junit.Test; + +import com.alibaba.dubbo.remoting.Channel; +import com.alibaba.dubbo.remoting.RemotingException; +import com.alibaba.dubbo.remoting.telnet.TelnetHandler; + +/** + * LogTelnetHandlerTest.java + * + * @author tony.chenl + */ +public class LogTelnetHandlerTest { + + private static TelnetHandler log = new LogTelnetHandler(); + private Channel mockChannel; + + @Test + public void testChangeLogLevel() throws RemotingException { + mockChannel = EasyMock.createMock(Channel.class); + EasyMock.replay(mockChannel); + String result = log.telnet(mockChannel, "error"); + assertTrue(result.contains("\r\nCURRENT LOG LEVEL:ERROR")); + String result2 = log.telnet(mockChannel, "warn"); + assertTrue(result2.contains("\r\nCURRENT LOG LEVEL:WARN")); + EasyMock.reset(mockChannel); + } + + @Test + public void testPrintLog() throws RemotingException { + mockChannel = EasyMock.createMock(Channel.class); + EasyMock.replay(mockChannel); + String result = log.telnet(mockChannel, "100"); + assertTrue(result.contains("CURRENT LOG APPENDER")); + EasyMock.reset(mockChannel); + } + +} diff --git a/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java new file mode 100644 index 0000000000..b541714cc8 --- /dev/null +++ b/dubbo-rpc-default/src/test/java/com/alibaba/dubbo/rpc/protocol/dubbo/telnet/PortTelnetHandlerTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 1999-2101 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.easymock.EasyMock; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.utils.NetUtils; +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; + +/** + * PortTelnetHandlerTest.java + * + * @author tony.chenl + */ +public class PortTelnetHandlerTest { + + private static TelnetHandler port = new PortTelnetHandler(); + private Invoker 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 RemotingException { + ExchangeClient client1 = Exchangers.connect("dubbo://127.0.0.1:20887/demo"); + ExchangeClient client2 = Exchangers.connect("dubbo://127.0.0.1:20887/demo"); + String result = port.telnet(null, "-l 20887"); + assertTrue(result.contains(client1.getLocalAddress().toString())); + assertTrue(result.contains(client2.getLocalAddress().toString())); + + } + + @Test + public void testListDetail() throws RemotingException { + String result = port.telnet(null, "-l"); + assertEquals("dubbo://" + NetUtils.getLocalHost() + ":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); + } + +}