提交 telnet 单元测试。

git-svn-id: http://code.alibabatech.com/svn/dubbo/trunk@189 1a56cb94-b969-4eaa-88fa-be21384802f2
This commit is contained in:
tony.chenl 2011-11-03 15:29:58 +00:00
parent c081c1eb19
commit 1d662b2ba8
4 changed files with 341 additions and 1 deletions

View File

@ -26,6 +26,7 @@ import org.junit.Test;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.exchange.ExchangeServer;
import com.alibaba.dubbo.remoting.telnet.TelnetHandler;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
@ -57,7 +58,7 @@ public class ChangeTelnetHandlerTest {
mockChannel.removeAttribute("telnet.service");
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(mockInvoker.getInterface()).andReturn(DemoService.class).anyTimes();
EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20880/demo")).anyTimes();
EasyMock.expect(mockInvoker.getUrl()).andReturn(URL.valueOf("dubbo://127.0.0.1:20883/demo")).anyTimes();
EasyMock.replay(mockChannel, mockInvoker);
}
@ -68,6 +69,9 @@ public class ChangeTelnetHandlerTest {
@After
public void after() {
for (ExchangeServer server : DubboProtocol.getDubboProtocol().getServers()) {
server.close();
}
EasyMock.reset(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().destroy();
}

View File

@ -0,0 +1,75 @@
/*
* 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 org.easymock.EasyMock;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.RemotingException;
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;
/**
* CountTelnetHandlerTest.java
*
* @author tony.chenl
*/
public class CurrentTelnetHandlerTest {
private static TelnetHandler count = new CurrentTelnetHandler();
private Channel mockChannel;
@After
public void after() {
EasyMock.reset(mockChannel);
}
@Test
public void testService() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService").anyTimes();
EasyMock.replay(mockChannel);
String result = count.telnet(mockChannel, "");
assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService", result);
}
@Test
public void testSlash() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel);
String result = count.telnet(mockChannel, "");
assertEquals("/", result);
}
@Test
public void testMessageError() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel);
String result = count.telnet(mockChannel, "test");
assertEquals("Unsupported parameter test for pwd.", result);
}
}

View File

@ -0,0 +1,102 @@
/*
* 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.Test;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.telnet.TelnetHandler;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcResult;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService;
/**
* CountTelnetHandlerTest.java
*
* @author tony.chenl
*/
public class InvokerTelnetHandlerTest {
private static TelnetHandler invoke = new InvokeTelnetHandler();
private Channel mockChannel;
private Invoker<DemoService> mockInvoker;
@SuppressWarnings("unchecked")
@Test
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.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.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = invoke.telnet(mockChannel, "DemoService.echo(\"hello\")");
assertTrue(result.contains("Use default service com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService.\r\n\"ok\"\r\n"));
EasyMock.reset(mockChannel, mockInvoker);
}
@SuppressWarnings("unchecked")
@Test
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.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.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = invoke.telnet(mockChannel, "echo(\"hello\")");
assertTrue(result.contains("ok"));
EasyMock.reset(mockChannel, mockInvoker);
}
@Test
public void testMessageNull() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel);
String result = invoke.telnet(mockChannel, null);
assertEquals("Please input method name, eg: \r\ninvoke xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})\r\ninvoke com.xxx.XxxService.xxxMethod(1234, \"abcd\", {\"prop\" : \"value\"})",
result);
EasyMock.reset(mockChannel);
}
@Test
public void testInvaildMessage() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel);
String result = invoke.telnet(mockChannel, "(");
assertEquals("Invalid parameters, format: service.method(args)", result);
EasyMock.reset(mockChannel);
}
}

View File

@ -0,0 +1,159 @@
/*
* 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 java.lang.reflect.Method;
import org.easymock.EasyMock;
import org.junit.BeforeClass;
import org.junit.Test;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.common.utils.ReflectUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.telnet.TelnetHandler;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcResult;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService;
/**
* CountTelnetHandlerTest.java
*
* @author tony.chenl
*/
public class ListTelnetHandlerTest {
private static TelnetHandler list = new ListTelnetHandler();
private Channel mockChannel;
private Invoker<DemoService> mockInvoker;
private static String detailMethods;
private static String methodsName;
@BeforeClass
public static void setUp() {
StringBuilder buf = new StringBuilder();
StringBuilder buf2 = new StringBuilder();
Method[] methods = DemoService.class.getMethods();
for (Method method : methods) {
if (buf.length() > 0) {
buf.append("\r\n");
}
if (buf2.length() > 0) {
buf2.append("\r\n");
}
buf2.append(method.getName());
buf.append(ReflectUtils.getName(method));
}
detailMethods = buf.toString();
methodsName = buf2.toString();
}
@SuppressWarnings("unchecked")
@Test
public void testListDetailService() 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.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.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = list.telnet(mockChannel, "-l DemoService");
assertEquals(detailMethods, result);
EasyMock.reset(mockChannel, mockInvoker);
}
@SuppressWarnings("unchecked")
@Test
public void testListService() 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.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.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = list.telnet(mockChannel, "DemoService");
assertEquals(methodsName, result);
EasyMock.reset(mockChannel, mockInvoker);
}
@Test
public void testList() 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.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = list.telnet(mockChannel, "");
assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService", result);
EasyMock.reset(mockChannel);
}
@Test
public void testListDetail() 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.invoke((Invocation) EasyMock.anyObject())).andReturn(new RpcResult("ok")).anyTimes();
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = list.telnet(mockChannel, "-l");
assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService -> dubbo://" + NetUtils.getLocalHost()
+ ":20885/demo", result);
EasyMock.reset(mockChannel);
}
@Test
public void testListDefault() 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.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.replay(mockChannel, mockInvoker);
DubboProtocol.getDubboProtocol().export(mockInvoker);
String result = list.telnet(mockChannel, "");
assertEquals("Use default service com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService.\r\n\r\n"
+ methodsName, result);
EasyMock.reset(mockChannel);
}
@Test
public void testInvaildMessage() throws RemotingException {
mockChannel = EasyMock.createMock(Channel.class);
EasyMock.expect(mockChannel.getAttribute("telnet.service")).andReturn(null).anyTimes();
EasyMock.replay(mockChannel);
String result = list.telnet(mockChannel, "xx");
assertEquals("No such service xx", result);
EasyMock.reset(mockChannel);
}
}