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 (!StringUtils.isEmpty(service)) { return "Invalid parameter " + part; } service = part; } } } else { service = (String) channel.getAttribute(ChangeTelnetHandler.SERVICE_KEY); if (StringUtils.isNotEmpty(service)) { buf.append("Use default service ").append(service).append(".\r\n"); } } if (StringUtils.isEmpty(service)) { printAllServices(buf, detail); } else { printSpecifiedService(service, buf, detail); if (buf.length() == 0) { buf.append("No such service: ").append(service); } } return buf.toString(); }  @Override String telnet(Channel channel, String message);  }
@Test public void testListDetailService() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); registerProvider(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); String result = list.telnet(mockChannel, "-l DemoService"); for (Method method : DemoService.class.getMethods()) { assertTrue(result.contains(ReflectUtils.getName(method))); } }
@Test public void testListService() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); registerProvider(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); String result = list.telnet(mockChannel, "DemoService"); for (Method method : DemoService.class.getMethods()) { assertTrue(result.contains(method.getName())); } }
@Test public void testList() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); registerProvider(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); String result = list.telnet(mockChannel, ""); assertEquals("PROVIDER:\r\norg.apache.dubbo.qos.legacy.service.DemoService\r\n", result); }
@Test public void testListDetail() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(null); registerProvider(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); String result = list.telnet(mockChannel, "-l"); assertEquals("PROVIDER:\r\norg.apache.dubbo.qos.legacy.service.DemoService -> published: N\r\n", result); }
@Test public void testListDefault() throws RemotingException { mockChannel = mock(Channel.class); given(mockChannel.getAttribute("telnet.service")).willReturn(DemoService.class.getName()); registerProvider(DemoService.class.getName(), new DemoServiceImpl(), DemoService.class); String result = list.telnet(mockChannel, ""); assertTrue(result.startsWith("Use default service org.apache.dubbo.qos.legacy.service.DemoService.\r\n" + "org.apache.dubbo.qos.legacy.service.DemoService (as provider):\r\n")); for (Method method : DemoService.class.getMethods()) { assertTrue(result.contains(method.getName())); } }
@Test public void testInvalidMessage() 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); }