Skip to content

Commit

Permalink
opt test
Browse files Browse the repository at this point in the history
  • Loading branch information
清英 committed Apr 1, 2018
1 parent c58593d commit 3a8d6e1
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 172 deletions.
Expand Up @@ -51,16 +51,17 @@ public class ModuleManagerImpl implements ModuleManager, DisposableBean {
*/
private final ConcurrentHashMap<String, RuntimeModule> allModules = new ConcurrentHashMap();

RuntimeModule getRuntimeModule(String name) {
return allModules.get(name.toUpperCase());
private RuntimeModule getRuntimeModule(String name) {
RuntimeModule runtimeModule = allModules.get(name.toUpperCase());
return runtimeModule != null ? runtimeModule : new RuntimeModule();
}

@Override
public List<Module> getModules() {
List<Module> modules = Lists.newArrayList();

for (String name : allModules.keySet()) {
RuntimeModule runtimeModule = getRuntimeModule(name);
RuntimeModule runtimeModule = getRuntimeModule((String) name);
for (String version : runtimeModule.getModules().keySet()) {
modules.add(runtimeModule.getModules().get(version));
}
Expand All @@ -78,20 +79,20 @@ public Module find(String name) {
return find(name, defaultVersion);
}

private String getDefaultVersion(String name) {return getRuntimeModule(name).getDefaultVersion();}
private String getDefaultVersion(String name) {return getRuntimeModule((String) name).getDefaultVersion();}

@Override
public Module find(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
return getRuntimeModule(name).getModule(version);
return getRuntimeModule((String) name).getModule(version);
}

@Override
public void activeVersion(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
getRuntimeModule(name).setDefaultVersion(version);
getRuntimeModule((String) name).setDefaultVersion(version);
}

@Override
Expand All @@ -109,14 +110,23 @@ public Module register(Module module) {
LOGGER.info("register Module: {}-{}", name, version);
}

//same module and same version can not register
Module registeredModule = getRuntimeModule(name).getModule(version);
if (registeredModule != null) {
return null;
}

RuntimeModule runtimeModule = getRuntimeModule(name);
Module oldModule = null;
if (runtimeModule == null) {
//module frist register
if (runtimeModule.getModules().isEmpty()) {
runtimeModule = new RuntimeModule().withName(name).withDefaultVersion(version).addModule(module);
allModules.put(name.toUpperCase(), runtimeModule);
} else {
//the same module to register again
oldModule = runtimeModule.getDefaultModule();
runtimeModule.addModule(module).setDefaultVersion(version);
// remove module old version
if (oldModule != null && module.getModuleConfig().isNeedUnloadOldVersion() && !runtimeModule.getModules().isEmpty()) {
runtimeModule.getModules().remove(oldModule.getVersion());
}
Expand All @@ -131,14 +141,14 @@ public Module remove(String name) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Remove Module: {}", name);
}
return remove(name, getRuntimeModule(name).getDefaultVersion());
return remove(name, getRuntimeModule((String) name).getDefaultVersion());
}

@Override
public Module remove(String name, String version) {
checkNotNull(name, "module name is null");
checkNotNull(version, "module version is null");
return getRuntimeModule(name).getModules().remove(version);
return getRuntimeModule((String) name).getModules().remove(version);
}

@Override
Expand All @@ -159,7 +169,7 @@ public Map<String, String> getErrorModuleContext() {
Map<String, String> result = Maps.newHashMap();

for (String name : allModules.keySet()) {
RuntimeModule runtimeModule = getRuntimeModule(name);
RuntimeModule runtimeModule = getRuntimeModule((String) name);
result.put(name, runtimeModule.getErrorContext());
}

Expand Down
Expand Up @@ -26,6 +26,9 @@ public class RuntimeModule extends ToStringObject {
*/
private String errorContext;

/**
* all version module,key:version
*/
private ConcurrentHashMap<String, Module> modules = new ConcurrentHashMap();

public Module getModule(String version) {
Expand Down
Expand Up @@ -32,6 +32,8 @@

import java.util.ArrayList;

import static com.alipay.jarslink.api.impl.ModuleLoaderImplTest.buildModuleConfig;

/**
* JarsLink API入口,使用TITAN API必须继承AbstractModuleRefreshScheduler然后提供模块信息
*
Expand Down Expand Up @@ -59,7 +61,7 @@ public void init() {
@Test
public void shouldAddModule() {
//装载模块
abstractModuleRefreshSchedulerImpl.setModuleConfigs(ImmutableList.of(ModuleManagerTest.buildModuleConfig()));
abstractModuleRefreshSchedulerImpl.setModuleConfigs(ImmutableList.of(buildModuleConfig()));
Assert.assertEquals(1, abstractModuleRefreshSchedulerImpl.queryModuleConfigs().size());
abstractModuleRefreshSchedulerImpl.run();
Module demo = moduleManager.find("demo");
Expand All @@ -82,15 +84,15 @@ public void shouldAddModule() {
@Test
public void shouldUpdateModule() {
//装载模块
abstractModuleRefreshSchedulerImpl.setModuleConfigs(ImmutableList.of(ModuleManagerTest.buildModuleConfig
abstractModuleRefreshSchedulerImpl.setModuleConfigs(ImmutableList.of(buildModuleConfig
(true)));
Assert.assertEquals(1, abstractModuleRefreshSchedulerImpl.queryModuleConfigs().size());
abstractModuleRefreshSchedulerImpl.run();
Module demo = moduleManager.find("demo");
Assert.assertNotNull(demo.getAction("helloworld"));

//修改模块
ModuleConfig moduleConfig = ModuleManagerTest.buildModuleConfig(true);
ModuleConfig moduleConfig = buildModuleConfig(true);
moduleConfig.setVersion("1.1");
abstractModuleRefreshSchedulerImpl.setModuleConfigs(ImmutableList.of(moduleConfig));
abstractModuleRefreshSchedulerImpl.run();
Expand Down
Expand Up @@ -4,16 +4,21 @@
*/
package com.alipay.jarslink.api.impl;

import com.alipay.jarslink.api.Action;
import com.alipay.jarslink.api.Module;
import com.alipay.jarslink.api.ModuleConfig;
import com.alipay.jarslink.api.ModuleLoader;
import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static com.alipay.jarslink.api.impl.ModuleManagerTest.buildModuleConfig;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
*
Expand All @@ -24,6 +29,8 @@
@ContextConfiguration(locations = {"classpath*:META-INF/spring/jarslink.xml"})
public class ModuleLoaderImplTest {

public static final String JARSLINK_MODULE_DEMO = "jarslink-module-demo-1.0.0.jar";

@Autowired
private ModuleLoader moduleLoader;

Expand Down Expand Up @@ -58,12 +65,170 @@ public void shouldLoadMultiModule() {

}

@Test
public void shouldDoAction() {

Module module = loadModule();
String actionName = "helloworld";

//4.1:查找和执行Action
ModuleConfig moduleConfig = new ModuleConfig();
moduleConfig.setName("h");
moduleConfig.setEnabled(true);

ModuleConfig result = module.doAction(actionName, moduleConfig);
Assert.assertEquals(3, module.getActions().size());
Assert.assertNotNull(result);
Assert.assertEquals(result.getName(), moduleConfig.getName());
Assert.assertEquals(result.getEnabled(), moduleConfig.getEnabled());

//4.2:查找和执行Action
Action<ModuleConfig, ModuleConfig> action = module.getAction(actionName);
Assert.assertNotNull(action);
result = action.execute(moduleConfig);
Assert.assertNotNull(result);
Assert.assertEquals(result.getName(), moduleConfig.getName());
Assert.assertEquals(result.getEnabled(), moduleConfig.getEnabled());

//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
}

@Test
public void shouldDoOverrideAction() {
//测试注解action被xml中的action覆盖
ModuleConfig config = buildModuleConfig(true);
config.addScanPackage("com.alipay.jarslink.main").addScanPackage("com.alipay.jarslink.demo");

Module module = moduleLoader.load(config);
Action<String, String> action = null;
try {
action = module.getAction("overrideXmlAction");
} catch (Exception e) {
Assert.assertNotNull(e);
}
Assert.assertNull(action);
}

@Test
public void shouldDoAnnotationAction() {
//annotation方式加载,测试annotation中注入xml定义的action 同时测试annotation action和xmlaction同时加载
ModuleConfig config = buildModuleConfig(true);
config.addScanPackage("com.alipay.jarslink.main");
config.addScanPackage("com.alipay.jarslink.demo");

Module module = moduleLoader.load(config);
Assert.assertEquals(4, module.getActions().size());

//获取annotationAction 该action中注入xml中定义的xmlAction,使用xmlAction的实现
String result = module.doAction("annotationAction", "hello");
Assert.assertNotNull(result);
//实际结果为xmlAction返回,annotationAction作为代理
Assert.assertEquals(result, "xml:hello");

//4.2:查找和执行Action
Action<String, String> action = module.getAction("annotationAction");
Assert.assertNotNull(action);
result = action.execute("hello");
Assert.assertNotNull(result);
Assert.assertEquals(result, "xml:hello");

//查找和执行xml bean
result = module.doAction("xmlAction", "hello");
Assert.assertNotNull(result);
Assert.assertEquals(result, "xml:hello");

//4.2:查找和执行Action
action = module.getAction("xmlAction");
Assert.assertNotNull(action);
result = action.execute("hello");
Assert.assertNotNull(result);
Assert.assertEquals(result, "xml:hello");

//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
}

@Test
public void shouldDoXmlAction() {
//xml的方式加载
ModuleConfig config = buildModuleConfig(true);

Module module = moduleLoader.load(config);
Assert.assertEquals(3, module.getActions().size());
//4.2:查找annotation Action 因为xml没有配置该action所以不会被发现
Action<String, String> action = null;
try {
action = module.getAction("annotationAction");
} catch (NullPointerException e) {
//由于找不到action所以会抛异常
Assert.assertNotNull(e);
}
Assert.assertNull(action);

//查找和执行xml bean
String result = module.doAction("xmlAction", "hello");
Assert.assertNotNull(result);
Assert.assertEquals(result, "xml:hello");

//4.2:查找和执行Action
action = module.getAction("xmlAction");
Assert.assertNotNull(action);
result = action.execute("hello");
Assert.assertNotNull(result);
Assert.assertEquals(result, "xml:hello");

//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
}

/**
* 构建模块配置信息
*/
public static ModuleConfig buildModuleConfig() {
return buildModuleConfig(true);
}

public static ModuleConfig buildModuleConfig(boolean enabled) {
return buildModuleConfig("demo", "1.0.0.20170621", enabled);
}

public static ModuleConfig buildModuleConfig(String name, boolean enabled) {
return buildModuleConfig(name, "1.0.0.20170621", enabled);
}

public static ModuleConfig buildModuleConfig(String name, String version, boolean enabled) {
URL demoModule;
ModuleConfig moduleConfig = new ModuleConfig();
//通过该方法构建的配置都是使用注解形式扫描bean的
String scanBase = "com.alipay.jarslink.main";
moduleConfig.addScanPackage(scanBase);
moduleConfig.removeScanPackage(scanBase);
Map<String, Object> properties = new HashMap();
moduleConfig.withEnabled(enabled).withVersion("1.0.0.20170621").withOverridePackages(ImmutableList.of(
"com.alipay.jarslink.demo")).withProperties(properties);
demoModule = Thread.currentThread().getContextClassLoader().getResource(JARSLINK_MODULE_DEMO);

moduleConfig.setOverridePackages(ImmutableList.of("com.alipay.jarslink.demo"));

moduleConfig.setName(name);
moduleConfig.setEnabled(enabled);
moduleConfig.setVersion("1.0.0.20170621");
properties.put("url", "127.0.0.1");
moduleConfig.setProperties(properties);
moduleConfig.setModuleUrl(ImmutableList.of(demoModule));
return moduleConfig;
}

private Module loadModule() {
return moduleLoader.load(buildModuleConfig(true));
}

private Module loadModule(String name) {
return moduleLoader.load(buildModuleConfig(name, true));
return moduleLoader.load(buildModuleConfig(name, "1.0.0.20170621", true));
}

}

0 comments on commit 3a8d6e1

Please sign in to comment.