EchoFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException { if (inv.getMethodName().equals($ECHO) && inv.getArguments() != null && inv.getArguments().length == 1) { return AsyncRpcResult.newDefaultAsyncResult(inv.getArguments()[0], inv); } return invoker.invoke(inv); }  @Override Result invoke(Invoker<?> invoker, Invocation inv);  }
@SuppressWarnings("unchecked") @Test public void testEcho() { Invocation invocation = mock(Invocation.class); given(invocation.getMethodName()).willReturn("$echo"); given(invocation.getParameterTypes()).willReturn(new Class<?>[]{Enum.class}); given(invocation.getArguments()).willReturn(new Object[]{"hello"}); given(invocation.getObjectAttachments()).willReturn(null); Invoker<DemoService> invoker = mock(Invoker.class); given(invoker.isAvailable()).willReturn(true); given(invoker.getInterface()).willReturn(DemoService.class); AppResponse result = new AppResponse(); result.setValue("High"); given(invoker.invoke(invocation)).willReturn(result); URL url = URL.valueOf("test: given(invoker.getUrl()).willReturn(url); Result filterResult = echoFilter.invoke(invoker, invocation); assertEquals("hello", filterResult.getValue()); }
@SuppressWarnings("unchecked") @Test public void testNonEcho() { Invocation invocation = mock(Invocation.class); given(invocation.getMethodName()).willReturn("echo"); given(invocation.getParameterTypes()).willReturn(new Class<?>[]{Enum.class}); given(invocation.getArguments()).willReturn(new Object[]{"hello"}); given(invocation.getObjectAttachments()).willReturn(null); Invoker<DemoService> invoker = mock(Invoker.class); given(invoker.isAvailable()).willReturn(true); given(invoker.getInterface()).willReturn(DemoService.class); AppResponse result = new AppResponse(); result.setValue("High"); given(invoker.invoke(invocation)).willReturn(result); URL url = URL.valueOf("test: given(invoker.getUrl()).willReturn(url); Result filterResult = echoFilter.invoke(invoker, invocation); assertEquals("High", filterResult.getValue()); }