-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
7 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OfflineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/OnlineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/QuitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/TestRegistryFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...o-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../dubbo-qos/src/test/resources/META-INF/services/org.apache.dubbo.registry.RegistryFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test=org.apache.dubbo.qos.command.impl.TestRegistryFactory |