This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new a1b301d [dubbo-1689]: Enhance the test coverage part-10 :
dubbo-plugin module (#2001)
a1b301d is described below
commit a1b301d50fcfe84fb6cb0ee01fa9b2c23b3c29d5
Author: Ian Luo <[email protected]>
AuthorDate: Thu Jun 28 16:18:31 2018 +0800
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module
(#2001)
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* fix unit test failure
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
---
.../server/handler/LocalHostPermitHandlerTest.java | 40 ++++++++++++++++
.../qos/server/handler/QosProcessHandlerTest.java | 51 ++++++++++++++++++++
.../server/handler/TelnetProcessHandlerTest.java | 56 ++++++++++++++++++++++
3 files changed, 147 insertions(+)
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java
new file mode 100644
index 0000000..9869572
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java
@@ -0,0 +1,40 @@
+package org.apache.dubbo.qos.server.handler;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class LocalHostPermitHandlerTest {
+ @Test
+ public void testHandlerAdded() throws Exception {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+ Channel channel = mock(Channel.class);
+ when(context.channel()).thenReturn(channel);
+ InetAddress addr = mock(InetAddress.class);
+ when(addr.isLoopbackAddress()).thenReturn(false);
+ InetSocketAddress address = new InetSocketAddress(addr, 12345);
+ when(channel.remoteAddress()).thenReturn(address);
+ ChannelFuture future = mock(ChannelFuture.class);
+ when(context.writeAndFlush(any(ByteBuf.class))).thenReturn(future);
+ LocalHostPermitHandler handler = new LocalHostPermitHandler(false);
+ handler.handlerAdded(context);
+ ArgumentCaptor<ByteBuf> captor =
ArgumentCaptor.forClass(ByteBuf.class);
+ verify(context).writeAndFlush(captor.capture());
+ assertThat(new String(captor.getValue().array()),
containsString("Foreign Ip Not Permitted"));
+ verify(future).addListener(ChannelFutureListener.CLOSE);
+ }
+}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java
new file mode 100644
index 0000000..5199c70
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java
@@ -0,0 +1,51 @@
+package org.apache.dubbo.qos.server.handler;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelPipeline;
+import io.netty.handler.codec.LineBasedFrameDecoder;
+import io.netty.handler.codec.http.HttpObjectAggregator;
+import io.netty.handler.codec.http.HttpServerCodec;
+import io.netty.handler.codec.string.StringDecoder;
+import io.netty.handler.codec.string.StringEncoder;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+
+public class QosProcessHandlerTest {
+ @Test
+ public void testDecodeHttp() throws Exception {
+ ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'G'});
+ ChannelHandlerContext context =
Mockito.mock(ChannelHandlerContext.class);
+ ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
+ Mockito.when(context.pipeline()).thenReturn(pipeline);
+ QosProcessHandler handler = new QosProcessHandler("welcome", false);
+ handler.decode(context, buf, Collections.emptyList());
+ verify(pipeline).addLast(any(HttpServerCodec.class));
+ verify(pipeline).addLast(any(HttpObjectAggregator.class));
+ verify(pipeline).addLast(any(HttpProcessHandler.class));
+ verify(pipeline).remove(handler);
+ }
+
+ @Test
+ public void testDecodeTelnet() throws Exception {
+ ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'A'});
+ ChannelHandlerContext context =
Mockito.mock(ChannelHandlerContext.class);
+ ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
+ Mockito.when(context.pipeline()).thenReturn(pipeline);
+ QosProcessHandler handler = new QosProcessHandler("welcome", false);
+ handler.decode(context, buf, Collections.emptyList());
+ verify(pipeline).addLast(any(LineBasedFrameDecoder.class));
+ verify(pipeline).addLast(any(StringDecoder.class));
+ verify(pipeline).addLast(any(StringEncoder.class));
+ verify(pipeline).addLast(any(TelnetProcessHandler.class));
+ verify(pipeline).remove(handler);
+ }
+
+
+}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java
new file mode 100644
index 0000000..ab3174e
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java
@@ -0,0 +1,56 @@
+package org.apache.dubbo.qos.server.handler;
+
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class TelnetProcessHandlerTest {
+ @Test
+ public void testPrompt() throws Exception {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+ TelnetProcessHandler handler = new TelnetProcessHandler();
+ handler.channelRead0(context, "");
+ verify(context).writeAndFlush(QosProcessHandler.prompt);
+ }
+
+ @Test
+ public void testBye() throws Exception {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+ TelnetProcessHandler handler = new TelnetProcessHandler();
+ ChannelFuture future = mock(ChannelFuture.class);
+ when(context.writeAndFlush("BYE!\n")).thenReturn(future);
+ handler.channelRead0(context, "quit");
+ verify(future).addListener(ChannelFutureListener.CLOSE);
+ }
+
+ @Test
+ public void testUnknownCommand() throws Exception {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+ TelnetProcessHandler handler = new TelnetProcessHandler();
+ handler.channelRead0(context, "unknown");
+ ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
+ verify(context, Mockito.atLeastOnce()).writeAndFlush(captor.capture());
+ assertThat(captor.getAllValues(), contains("unknown :no such command",
"\r\ndubbo>"));
+ }
+
+ @Test
+ public void testGreeting() throws Exception {
+ ChannelHandlerContext context = mock(ChannelHandlerContext.class);
+ TelnetProcessHandler handler = new TelnetProcessHandler();
+ handler.channelRead0(context, "greeting");
+ ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
+ verify(context).writeAndFlush(captor.capture());
+ assertThat(captor.getValue(), containsString("greeting"));
+ assertThat(captor.getValue(), containsString("dubbo>"));
+ }
+}