From f186f2bc167f151dc15e025c03cca6da8c11c464 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Mon, 25 Jun 2018 14:31:23 +0800 Subject: [PATCH] [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 --- .../apache/dubbo/qos/command/impl/LsTest.java | 2 +- .../dubbo/qos/command/impl/OfflineTest.java | 54 +++++++++++++++++++ .../dubbo/qos/command/impl/OnlineTest.java | 46 ++++++++++++++++ .../dubbo/qos/command/impl/QuitTest.java | 18 +++++++ .../qos/command/impl/TestRegistryFactory.java | 14 +++++ .../qos/command/util/CommandHelperTest.java | 39 ++++++++++++++ .../org.apache.dubbo.registry.RegistryFactory | 1 + 7 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java create mode 100644 dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java create mode 100644 dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java create mode 100644 dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/TestRegistryFactory.java create mode 100644 dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java create mode 100644 dubbo-plugin/dubbo-qos/src/test/resources/META-INF/services/org.apache.dubbo.registry.RegistryFactory 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 a4c239a1d53..5e75e8626f7 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 00000000000..f77085d666f --- /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 00000000000..3f5e762cd0f --- /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 00000000000..bbfca51ce05 --- /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 00000000000..cd222e0aed8 --- /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 00000000000..338014c884c --- /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> 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 00000000000..a431677c9c1 --- /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