Skip to content

Commit

Permalink
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module (
Browse files Browse the repository at this point in the history
…#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
beiwei30 authored Jun 25, 2018
1 parent 590bbb2 commit f186f2b
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
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"));
}
}
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());
}
}
}
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));
}
}
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;
}
}
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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test=org.apache.dubbo.qos.command.impl.TestRegistryFactory

0 comments on commit f186f2b

Please sign in to comment.