Skip to content

Commit

Permalink
DUBBO-627 添加单元测试,覆盖 export 时是否能把 generic 的配置发布到 registry 上
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Jan 11, 2013
1 parent b90c3b3 commit 3416402
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.containsString;

import java.util.Collection;
import java.util.List;

import org.junit.Test;
Expand Down Expand Up @@ -50,6 +51,10 @@
import com.alibaba.dubbo.config.spring.filter.MockFilter;
import com.alibaba.dubbo.config.spring.impl.DemoServiceImpl;
import com.alibaba.dubbo.config.spring.impl.HelloServiceImpl;
import com.alibaba.dubbo.config.spring.registry.MockRegistry;
import com.alibaba.dubbo.config.spring.registry.MockRegistryFactory;
import com.alibaba.dubbo.registry.Registry;
import com.alibaba.dubbo.registry.RegistryFactory;
import com.alibaba.dubbo.registry.RegistryService;
import com.alibaba.dubbo.rpc.Exporter;
import com.alibaba.dubbo.rpc.Filter;
Expand Down Expand Up @@ -943,7 +948,7 @@ public void testReferGenericExport() throws Exception {
public void testGenericServiceConfig() throws Exception {
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
service.setApplication(new ApplicationConfig("test"));
service.setRegistry(new RegistryConfig("N/A"));
service.setRegistry(new RegistryConfig("mock://localhost"));
service.setInterface(DemoService.class.getName());
service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
service.setRef(new GenericService(){
Expand All @@ -954,9 +959,12 @@ public void testGenericServiceConfig() throws Exception {
});
try {
service.export();
URL url = service.getExportedUrls().get(0);
Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
MockRegistry registry = (MockRegistry)collection.iterator().next();
URL url = registry.getRegistered().get(0);
Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
} finally {
MockRegistryFactory.cleanCachedRegistry();
service.unexport();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.alibaba.dubbo.config.spring.registry;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.registry.NotifyListener;
import com.alibaba.dubbo.registry.Registry;

/**
* @author <a href="mailto:gang.lvg@taobao.com">kimi</a>
*/
public class MockRegistry implements Registry {

private URL url;

private List<URL> registered = new ArrayList<URL>();

private List<URL> subscribered = new ArrayList<URL>();

public List<URL> getRegistered() {
return registered;
}

public List<URL> getSubscribered() {
return subscribered;
}

public MockRegistry(URL url) {
if (url == null) {
throw new NullPointerException();
}
this.url = url;
}

public URL getUrl() {
return url;
}

public boolean isAvailable() {
return true;
}

public void destroy() {

}

public void register(URL url) {
registered.add(url);
}

public void unregister(URL url) {
registered.remove(url);
}

public void subscribe(URL url, NotifyListener listener) {
subscribered.add(url);
}

public void unsubscribe(URL url, NotifyListener listener) {
subscribered.remove(url);
}

public List<URL> lookup(URL url) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.dubbo.config.spring.registry;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.registry.Registry;
import com.alibaba.dubbo.registry.RegistryFactory;

/**
* @author <a href="mailto:gang.lvg@taobao.com">kimi</a>
*/
public class MockRegistryFactory implements RegistryFactory {

private static final Map<URL, Registry> registries = new HashMap<URL, Registry>();

public Registry getRegistry(URL url) {
MockRegistry registry = new MockRegistry(url);
registries.put(url, registry);
return registry;
}

public static Collection<Registry> getCachedRegistry() {
return registries.values();
}

public static void cleanCachedRegistry() {
registries.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock=com.alibaba.dubbo.config.spring.registry.MockRegistryFactory

0 comments on commit 3416402

Please sign in to comment.