-
Notifications
You must be signed in to change notification settings - Fork 706
/
ModuleLoaderImplTest.java
234 lines (196 loc) · 8.31 KB
/
ModuleLoaderImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Alipay.com Inc.
* Copyright (c) 2004-2018 All Rights Reserved.
*/
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 java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author tengfei.fang
* @version $Id: ModuleLoaderImplTest.java, v 0.1 2018年04月01日 12:12 PM tengfei.fang Exp $
*/
@RunWith(SpringJUnit4ClassRunner.class)
@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;
@Test
public void shouldLoadModule() {
//1:加载模块
Module module = loadModule();
Assert.assertEquals("demo", module.getName());
Assert.assertEquals("1.0.0.20170621", module.getVersion());
Assert.assertEquals(3, module.getActions().size());
Assert.assertNotNull(module);
Assert.assertNotNull(module.getCreation());
Assert.assertNotNull(module.getChildClassLoader());
//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
Assert.assertNotNull(module.getChildClassLoader());
}
@Test
public void shouldLoadMultiModule() {
for (int i = 0; i < 10; i++) {
//1:加载模块
String name = "demo" + i;
Module module = loadModule(name);
Assert.assertNotNull(module);
//卸载模块
moduleLoader.unload(module);
Assert.assertTrue(module.getActions().isEmpty());
}
}
@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, "1.0.0.20170621", true));
}
}