ListTelnetHandler implements TelnetHandler { @Override public String telnet(Channel channel, String message) { StringBuilder buf = new StringBuilder(); String service = null; boolean detail = false; if (message.length() > 0) { String[] parts = message.split("\\s+"); for (String part : parts) { if ("-l".equals(part)) { detail = true; } else { if (service != null && service.length() > 0) { return "Invaild parameter " + part; } service = part; } } } else { service = (String) channel.getAttribute(ChangeTelnetHandler.SERVICE_KEY); if (service != null && service.length() > 0) { buf.append("Use default service " + service + ".\r\n"); } } if (service == null || service.length() == 0) { for (Exporter<?> exporter : DubboProtocol.getDubboProtocol().getExporters()) { if (buf.length() > 0) { buf.append("\r\n"); } buf.append(exporter.getInvoker().getInterface().getName()); if (detail) { buf.append(" -> "); buf.append(exporter.getInvoker().getUrl()); } } } else { Invoker<?> invoker = null; for (Exporter<?> exporter : DubboProtocol.getDubboProtocol().getExporters()) { if (service.equals(exporter.getInvoker().getInterface().getSimpleName()) || service.equals(exporter.getInvoker().getInterface().getName()) || service.equals(exporter.getInvoker().getUrl().getPath())) { invoker = exporter.getInvoker(); break; } } if (invoker != null) { Method[] methods = invoker.getInterface().getMethods(); for (Method method : methods) { if (buf.length() > 0) { buf.append("\r\n"); } if (detail) { buf.append(ReflectUtils.getName(method)); } else { buf.append(method.getName()); } } } else { buf.append("No such service " + service); } } return buf.toString(); }  @Override String telnet(Channel channel, String message);  }
@SuppressWarnings("unchecked") @Test public void testListDetailService() throws RemotingException { mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo: given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService"); DubboProtocol.getDubboProtocol().export(mockInvoker); String result = list.telnet(mockChannel, "-l DemoService"); assertEquals(detailMethods, result); }
@SuppressWarnings("unchecked") @Test public void testListService() throws RemotingException { mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo: given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService"); DubboProtocol.getDubboProtocol().export(mockInvoker); String result = list.telnet(mockChannel, "DemoService"); assertEquals(methodsName, result); }
@SuppressWarnings("unchecked") @Test public void testList() throws RemotingException { mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo: given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); DubboProtocol.getDubboProtocol().export(mockInvoker); String result = list.telnet(mockChannel, ""); assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService", result); }
@SuppressWarnings("unchecked") @Test public void testListDetail() throws RemotingException { int port = NetUtils.getAvailablePort(); mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo: given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); DubboProtocol.getDubboProtocol().export(mockInvoker); String result = list.telnet(mockChannel, "-l"); assertEquals("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService -> dubbo: }
@SuppressWarnings("unchecked") @Test public void testListDefault() throws RemotingException { mockInvoker = mock(Invoker.class); given(mockInvoker.getInterface()).willReturn(DemoService.class); given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo: given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok")); mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn("com.alibaba.dubbo.rpc.protocol.dubbo.support.DemoService"); 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); }
@Test public void testInvaildMessage() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); String result = list.telnet(mockChannel, "xx"); assertEquals("No such service xx", result); }