You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/06/21 13:29:45 UTC
[incubator-dubbo] branch master updated: #1689: Enhance the test
coverage part-10 : dubbo-plugin module (#1976)
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 cc79b22 #1689: Enhance the test coverage part-10 : dubbo-plugin module (#1976)
cc79b22 is described below
commit cc79b22e2b02276d0d016da25d8f4da8557ebb77
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Thu Jun 21 21:29:23 2018 +0800
#1689: Enhance the test coverage part-10 : dubbo-plugin module (#1976)
---
.../apache/dubbo/qos/command/GreetingCommand.java | 4 ++
.../command/decoder/HttpCommandDecoderTest.java | 48 +++++++++++++++++
.../command/decoder/TelnetCommandDecoderTest.java | 19 +++++++
.../apache/dubbo/qos/command/impl/HelpTest.java | 32 ++++++++++++
.../org/apache/dubbo/qos/command/impl/LsTest.java | 61 ++++++++++++++++++++++
5 files changed, 164 insertions(+)
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
index 8c515ee..ff49804 100644
--- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
@@ -17,6 +17,10 @@
package org.apache.dubbo.qos.command;
+import org.apache.dubbo.qos.command.annotation.Cmd;
+
+
+@Cmd(name = "greeting", summary = "greeting message", example = {"greeting dubbo",})
public class GreetingCommand implements BaseCommand {
@Override
public String execute(CommandContext commandContext, String[] args) {
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java
new file mode 100644
index 0000000..36d6650
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java
@@ -0,0 +1,48 @@
+package org.apache.dubbo.qos.command.decoder;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.handler.codec.http.FullHttpRequest;
+import io.netty.handler.codec.http.HttpHeaders;
+import io.netty.handler.codec.http.HttpMethod;
+import io.netty.handler.codec.http.HttpRequest;
+import org.apache.dubbo.qos.command.CommandContext;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.hamcrest.Matchers.arrayContaining;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class HttpCommandDecoderTest {
+ @Test
+ public void decodeGet() throws Exception {
+ HttpRequest request = mock(HttpRequest.class);
+ when(request.getUri()).thenReturn("localhost:80/test");
+ when(request.getMethod()).thenReturn(HttpMethod.GET);
+ CommandContext context = HttpCommandDecoder.decode(request);
+ assertThat(context.getCommandName(), equalTo("test"));
+ assertThat(context.isHttp(), is(true));
+ when(request.getUri()).thenReturn("localhost:80/test?a=b&c=d");
+ context = HttpCommandDecoder.decode(request);
+ assertThat(context.getArgs(), arrayContaining("b", "d"));
+ }
+
+ @Test
+ public void decodePost() throws Exception {
+ FullHttpRequest request = mock(FullHttpRequest.class);
+ when(request.getUri()).thenReturn("localhost:80/test");
+ when(request.getMethod()).thenReturn(HttpMethod.POST);
+ when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS);
+ ByteBuf buf = Unpooled.copiedBuffer("a=b&c=d", StandardCharsets.UTF_8);
+ when(request.content()).thenReturn(buf);
+ CommandContext context = HttpCommandDecoder.decode(request);
+ assertThat(context.getCommandName(), equalTo("test"));
+ assertThat(context.isHttp(), is(true));
+ assertThat(context.getArgs(), arrayContaining("b", "d"));
+ }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java
new file mode 100644
index 0000000..e9aed83
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java
@@ -0,0 +1,19 @@
+package org.apache.dubbo.qos.command.decoder;
+
+import org.apache.dubbo.qos.command.CommandContext;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.arrayContaining;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class TelnetCommandDecoderTest {
+ @Test
+ public void testDecode() throws Exception {
+ CommandContext context = TelnetCommandDecoder.decode("test a b");
+ assertThat(context.getCommandName(), equalTo("test"));
+ assertThat(context.isHttp(), is(false));
+ assertThat(context.getArgs(), arrayContaining("a", "b"));
+ }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java
new file mode 100644
index 0000000..180a144
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java
@@ -0,0 +1,32 @@
+package org.apache.dubbo.qos.command.impl;
+
+import org.apache.dubbo.qos.command.CommandContext;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+
+public class HelpTest {
+ @Test
+ public void testMainHelp() throws Exception {
+ Help help = new Help();
+ String output = help.execute(Mockito.mock(CommandContext.class), null);
+ assertThat(output, containsString("greeting"));
+ assertThat(output, containsString("help"));
+ assertThat(output, containsString("ls"));
+ assertThat(output, containsString("online"));
+ assertThat(output, containsString("offline"));
+ assertThat(output, containsString("quit"));
+ }
+
+ @Test
+ public void testGreeting() throws Exception {
+ Help help = new Help();
+ String output = help.execute(Mockito.mock(CommandContext.class), new String[]{"greeting"});
+ assertThat(output, containsString("COMMAND NAME"));
+ assertThat(output, containsString("greeting"));
+ assertThat(output, containsString("EXAMPLE"));
+ assertThat(output, containsString("greeting dubbo"));
+ }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
new file mode 100644
index 0000000..a4c239a
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
@@ -0,0 +1,61 @@
+package org.apache.dubbo.qos.command.impl;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.config.model.ApplicationModel;
+import org.apache.dubbo.config.model.ConsumerModel;
+import org.apache.dubbo.config.model.ProviderModel;
+import org.apache.dubbo.qos.command.CommandContext;
+import org.apache.dubbo.registry.integration.RegistryDirectory;
+import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
+import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
+import org.apache.dubbo.rpc.Invoker;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.util.Map;
+
+import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class LsTest {
+ @Test
+ public void testExecute() throws Exception {
+ ConsumerModel consumerModel = mock(ConsumerModel.class);
+ when(consumerModel.getServiceName()).thenReturn("org.apache.dubbo.FooService");
+ ProviderModel providerModel = mock(ProviderModel.class);
+ when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
+ ApplicationModel.initConsumerModel("org.apache.dubbo.FooService", consumerModel);
+ ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);
+
+ Invoker providerInvoker = mock(Invoker.class);
+ URL registryUrl = mock(URL.class);
+ when(registryUrl.toFullString()).thenReturn("registry://localhost:8080");
+ URL providerUrl = mock(URL.class);
+ when(providerUrl.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
+ when(providerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.BarService");
+ when(providerInvoker.getUrl()).thenReturn(providerUrl);
+ ProviderConsumerRegTable.registerProvider(providerInvoker, registryUrl, providerUrl);
+ for (ProviderInvokerWrapper wrapper : getProviderInvoker("org.apache.dubbo.BarService")) {
+ wrapper.setReg(true);
+ }
+
+ Invoker consumerInvoker = mock(Invoker.class);
+ URL consumerUrl = mock(URL.class);
+ when(consumerUrl.getServiceKey()).thenReturn("org.apache.dubbo.FooService");
+ when(consumerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.FooService");
+ when(consumerInvoker.getUrl()).thenReturn(consumerUrl);
+ RegistryDirectory directory = mock(RegistryDirectory.class);
+ Map invokers = Mockito.mock(Map.class);
+ when(invokers.size()).thenReturn(100);
+ when(directory.getUrlInvokerMap()).thenReturn(invokers);
+ ProviderConsumerRegTable.registerConsumer(consumerInvoker, registryUrl, consumerUrl, directory);
+
+ Ls ls = new Ls();
+ String output = ls.execute(mock(CommandContext.class), null);
+ assertThat(output, containsString("org.apache.dubbo.FooService|100"));
+ assertThat(output, containsString("org.apache.dubbo.BarService| Y"));
+ }
+}