Fix telnet can not find method with enum type (#2803)
authorLiZhen <LiZhenNet@users.noreply.github.com>
Tue, 11 Dec 2018 02:40:22 +0000 (10:40 +0800)
committerIan Luo <ian.luo@gmail.com>
Tue, 11 Dec 2018 02:40:22 +0000 (10:40 +0800)
dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java
dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java
dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java
dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/telnet/InvokerTelnetHandlerTest.java

index 82591ab..b250c1e 100644 (file)
@@ -17,6 +17,7 @@
 package org.apache.dubbo.rpc.protocol.dubbo.telnet;
 
 import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.CompatibleTypeUtils;
 import org.apache.dubbo.common.utils.PojoUtils;
 import org.apache.dubbo.common.utils.ReflectUtils;
 import org.apache.dubbo.common.utils.StringUtils;
@@ -75,6 +76,14 @@ public class InvokeTelnetHandler implements TelnetHandler {
             }
 
             if (ReflectUtils.isPrimitive(arg.getClass())) {
+                if (arg instanceof String && type.isEnum()) {
+                    try {
+                        CompatibleTypeUtils.compatibleTypeConvert(arg, type);
+                    } catch (RuntimeException e) {
+                        return false;
+                    }
+                    continue;
+                }
                 if (!ReflectUtils.isPrimitive(type)) {
                     return false;
                 }
index e404af0..ed38344 100644 (file)
@@ -71,8 +71,8 @@ public class DemoServiceImpl implements DemoService {
             return Type.Lower;
         return types[0];
     }
-
-    public Type enumlength(Type type) {
+    
+    public Type getType(Type type) {
         return type;
     }
 
index a785330..dc63c8b 100644 (file)
@@ -108,6 +108,23 @@ public class InvokerTelnetHandlerTest {
         }
     }
 
+    @Test
+    public void testInvokeByPassingEnumValue() throws RemotingException {
+        mockInvoker = mock(Invoker.class);
+        given(mockInvoker.getInterface()).willReturn(DemoService.class);
+        given(mockInvoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:20886/demo"));
+        given(mockInvoker.invoke(any(Invocation.class))).willReturn(new RpcResult("ok"));
+        mockChannel = mock(Channel.class);
+        given(mockChannel.getAttribute("telnet.service")).willReturn(null);
+        given(mockChannel.getLocalAddress()).willReturn(NetUtils.toAddress("127.0.0.1:5555"));
+        given(mockChannel.getRemoteAddress()).willReturn(NetUtils.toAddress("127.0.0.1:20886"));
+
+        DubboProtocol.getDubboProtocol().export(mockInvoker);
+        String result = invoke.telnet(mockChannel, "getType(\"High\")");
+        assertTrue(result.contains("ok"));
+    }
+
+
     @SuppressWarnings("unchecked")
     @Test
     public void testInvokeAutoFindMethod() throws RemotingException {