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 f186f2b [dubbo-1689]: Enhance the test coverage part-10 :
dubbo-plugin module (#1980)
f186f2b is described below
commit f186f2bc167f151dc15e025c03cca6da8c11c464
Author: Ian Luo <[email protected]>
AuthorDate: Mon Jun 25 14:31:23 2018 +0800
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module
(#1980)
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* #1689: Enhance the test coverage part-10 : dubbo-plugin module
* fix unit test failure
---
.../org/apache/dubbo/qos/command/impl/LsTest.java | 2 +-
.../command/impl/{LsTest.java => OfflineTest.java} | 39 +++++++++-----------
.../command/impl/{LsTest.java => OnlineTest.java} | 41 +++++++---------------
.../apache/dubbo/qos/command/impl/QuitTest.java | 18 ++++++++++
.../qos/command/impl/TestRegistryFactory.java | 14 ++++++++
.../dubbo/qos/command/util/CommandHelperTest.java | 39 ++++++++++++++++++++
.../org.apache.dubbo.registry.RegistryFactory | 1 +
7 files changed, 102 insertions(+), 52 deletions(-)
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
index a4c239a..5e75e86 100644
---
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
@@ -32,7 +32,7 @@ public class LsTest {
Invoker providerInvoker = mock(Invoker.class);
URL registryUrl = mock(URL.class);
-
when(registryUrl.toFullString()).thenReturn("registry://localhost:8080");
+ when(registryUrl.toFullString()).thenReturn("test://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");
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/OfflineTest.java
similarity index 54%
copy from
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
copy to
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java
index a4c239a..f77085d 100644
---
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/OfflineTest.java
@@ -2,37 +2,32 @@ 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.Registry;
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.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public class LsTest {
+public class OfflineTest {
@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");
+ when(registryUrl.toFullString()).thenReturn("test://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");
@@ -42,20 +37,18 @@ public class LsTest {
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);
+ Registry registry = mock(Registry.class);
+ TestRegistryFactory.registry = registry;
+
+ Offline offline = new Offline();
+ String output = offline.execute(mock(CommandContext.class), new
String[]{"org.apache.dubbo.BarService"});
+ assertThat(output, containsString("OK"));
+ Mockito.verify(registry).unregister(providerUrl);
+ for (ProviderInvokerWrapper wrapper :
getProviderInvoker("org.apache.dubbo.BarService")) {
+ assertThat(wrapper.isReg(), is(false));
+ }
- 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"));
+ output = offline.execute(mock(CommandContext.class), new
String[]{"org.apache.dubbo.FooService"});
+ assertThat(output, containsString("service not found"));
}
}
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/OnlineTest.java
similarity index 50%
copy from
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
copy to
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java
index a4c239a..3f5e762 100644
---
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/OnlineTest.java
@@ -2,60 +2,45 @@ 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.Registry;
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.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public class LsTest {
+public class OnlineTest {
@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");
+ when(registryUrl.toFullString()).thenReturn("test://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);
+ Registry registry = mock(Registry.class);
+ TestRegistryFactory.registry = registry;
- 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"));
+ Online online = new Online();
+ String output = online.execute(mock(CommandContext.class), new
String[]{"org.apache.dubbo.BarService"});
+ assertThat(output, equalTo("OK"));
+ for (ProviderInvokerWrapper wrapper :
getProviderInvoker("org.apache.dubbo.BarService")) {
+ assertTrue(wrapper.isReg());
+ }
}
}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java
new file mode 100644
index 0000000..bbfca51
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java
@@ -0,0 +1,18 @@
+package org.apache.dubbo.qos.command.impl;
+
+import org.apache.dubbo.qos.command.CommandContext;
+import org.apache.dubbo.qos.common.QosConstants;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class QuitTest {
+ @Test
+ public void testExecute() throws Exception {
+ Quit quit = new Quit();
+ String output = quit.execute(Mockito.mock(CommandContext.class), null);
+ assertThat(output, equalTo(QosConstants.CLOSE));
+ }
+}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/TestRegistryFactory.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/TestRegistryFactory.java
new file mode 100644
index 0000000..cd222e0
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/TestRegistryFactory.java
@@ -0,0 +1,14 @@
+package org.apache.dubbo.qos.command.impl;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.RegistryFactory;
+
+public class TestRegistryFactory implements RegistryFactory {
+ static Registry registry;
+
+ @Override
+ public Registry getRegistry(URL url) {
+ return registry;
+ }
+}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
new file mode 100644
index 0000000..338014c
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
@@ -0,0 +1,39 @@
+package org.apache.dubbo.qos.command.util;
+
+import org.apache.dubbo.qos.command.GreetingCommand;
+import org.apache.dubbo.qos.command.impl.Help;
+import org.apache.dubbo.qos.command.impl.Ls;
+import org.apache.dubbo.qos.command.impl.Offline;
+import org.apache.dubbo.qos.command.impl.Online;
+import org.apache.dubbo.qos.command.impl.Quit;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class CommandHelperTest {
+ @Test
+ public void testHasCommand() throws Exception {
+ assertTrue(CommandHelper.hasCommand("greeting"));
+ assertFalse(CommandHelper.hasCommand("not-exiting"));
+ }
+
+ @Test
+ public void testGetAllCommandClass() throws Exception {
+ List<Class<?>> classes = CommandHelper.getAllCommandClass();
+ assertThat(classes, containsInAnyOrder(GreetingCommand.class,
Help.class, Ls.class, Offline.class, Online.class, Quit.class));
+ }
+
+ @Test
+ public void testGetCommandClass() throws Exception {
+ assertThat(CommandHelper.getCommandClass("greeting"),
equalTo(GreetingCommand.class));
+ assertNull(CommandHelper.getCommandClass("not-exiting"));
+ }
+}
diff --git
a/dubbo-plugin/dubbo-qos/src/test/resources/META-INF/services/org.apache.dubbo.registry.RegistryFactory
b/dubbo-plugin/dubbo-qos/src/test/resources/META-INF/services/org.apache.dubbo.registry.RegistryFactory
new file mode 100644
index 0000000..a431677
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/resources/META-INF/services/org.apache.dubbo.registry.RegistryFactory
@@ -0,0 +1 @@
+test=org.apache.dubbo.qos.command.impl.TestRegistryFactory
\ No newline at end of file