beiwei30 closed pull request #1980: [dubbo-1689]: Enhance the test coverage
part-10 : dubbo-plugin module
URL: https://github.com/apache/incubator-dubbo/pull/1980
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 a4c239a1d5..5e75e8626f 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 void testExecute() throws Exception {
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/OfflineTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java
new file mode 100644
index 0000000000..f77085d666
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java
@@ -0,0 +1,54 @@
+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.ProviderModel;
+import org.apache.dubbo.qos.command.CommandContext;
+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 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 OfflineTest {
+ @Test
+ public void testExecute() throws Exception {
+ ProviderModel providerModel = mock(ProviderModel.class);
+
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
+ ApplicationModel.initProviderModel("org.apache.dubbo.BarService",
providerModel);
+
+ Invoker providerInvoker = mock(Invoker.class);
+ URL registryUrl = mock(URL.class);
+ 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);
+ }
+
+ 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));
+ }
+
+ 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/OnlineTest.java
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java
new file mode 100644
index 0000000000..3f5e762cd0
--- /dev/null
+++
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java
@@ -0,0 +1,46 @@
+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.ProviderModel;
+import org.apache.dubbo.qos.command.CommandContext;
+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 static
org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
+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 OnlineTest {
+ @Test
+ public void testExecute() throws Exception {
+ ProviderModel providerModel = mock(ProviderModel.class);
+
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
+ ApplicationModel.initProviderModel("org.apache.dubbo.BarService",
providerModel);
+
+ Invoker providerInvoker = mock(Invoker.class);
+ URL registryUrl = mock(URL.class);
+ 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);
+
+ Registry registry = mock(Registry.class);
+ TestRegistryFactory.registry = registry;
+
+ 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 0000000000..bbfca51ce0
--- /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 0000000000..cd222e0aed
--- /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 0000000000..338014c884
--- /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 0000000000..a431677c9c
--- /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
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]