forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerByteCodeScannerTest.java
More file actions
303 lines (260 loc) · 11.8 KB
/
ControllerByteCodeScannerTest.java
File metadata and controls
303 lines (260 loc) · 11.8 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package act.controller.bytecode;
import act.TestBase;
import act.app.AppByteCodeScanner;
import act.app.AppCodeScannerManager;
import act.app.TestingAppClassLoader;
import act.asm.Type;
import act.controller.meta.*;
import act.event.EventBus;
import act.job.AppJobManager;
import act.job.bytecode.JobByteCodeScanner;
import act.route.RouteSource;
import act.util.Files;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.verification.Times;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import playground.EmailBinder;
import testapp.controller.*;
import testapp.model.ModelController;
import testapp.model.ModelControllerWithAnnotation;
import java.io.File;
import java.util.List;
import static act.route.RouteSource.ACTION_ANNOTATION;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.osgl.http.H.Method.*;
public class ControllerByteCodeScannerTest extends TestBase {
private ControllerClassMetaInfoManager infoSrc;
private TestingAppClassLoader classLoader;
private AppCodeScannerManager scannerManager;
private AppJobManager jobManager;
private ControllerByteCodeScanner controllerScanner;
private JobByteCodeScanner jobScanner;
private EventBus eventBus;
private File base;
@Before
public void setup() throws Exception {
super.setup();
controllerScanner = new ControllerByteCodeScanner();
jobScanner = new JobByteCodeScanner();
scannerManager = mock(AppCodeScannerManager.class);
eventBus = mock(EventBus.class);
when(mockApp.eventBus()).thenReturn(eventBus);
jobManager = new AppJobManager(mockApp);
classLoader = new TestingAppClassLoader(mockApp);
when(mockApp.classLoader()).thenReturn(classLoader);
infoSrc = classLoader.controllerClassMetaInfoManager();
when(mockApp.classLoader()).thenReturn(classLoader);
when(mockApp.scannerManager()).thenReturn(scannerManager);
when(mockApp.jobManager()).thenReturn(jobManager);
when(mockAppConfig.possibleControllerClass(anyString())).thenReturn(true);
when(mockRouter.isActionMethod(anyString(), anyString())).thenReturn(false);
C.List<AppByteCodeScanner> scanners = $.cast(C.listOf(controllerScanner, jobScanner));
//C.List<AppByteCodeScanner> scanners = C.list(controllerScanner);
when(scannerManager.byteCodeScanners()).thenReturn(scanners);
controllerScanner.setApp(mockApp);
jobScanner.setApp(mockApp);
base = new File("./target/test-classes");
}
@Test
public void specificHttpMethodAnnotationShallNotRegisterOtherHttpMethodsInRouteTable() {
scan(WithAppContext.class);
String url = "/static_no_ret_no_param";
verifyRouting(url, "WithAppContext", "staticReturnStringNoParam", GET);
verifyNoRouting(url, "WithAppContext", "staticReturnStringNoParam", PUT, POST, DELETE);
}
@Test
public void testNotRoutedActionScan() {
scan(WithAppContext.class);
ActionMethodMetaInfo action = action("WithAppContext", "foo");
assertNull(action);
}
@Test
public void testRoutedActionScan() {
when(mockRouter.isActionMethod("testapp.controller.WithAppContext", "foo")).thenReturn(true);
scan(WithAppContext.class);
ActionMethodMetaInfo action = action("WithAppContext", "foo");
assertNotNull(action);
}
@Test
public void testControllerNotInControllerPackage() {
scan(ModelController.class);
ActionMethodMetaInfo action = action("ModelController", "handle");
assertNull(action);
}
@Test
public void testControllerNotInControllerPackageWithAnnotation() {
scan(ModelControllerWithAnnotation.class);
ActionMethodMetaInfo action = action(ModelControllerWithAnnotation.class, "handle");
assertNotNull(action);
}
@Test
public void controllerContextPathShallBeAppendToActionPath() {
scan(WithContextPath.class);
verify(mockRouter).addMapping(GET, "/foo/bar", "testapp.controller.WithContextPath.bar", RouteSource.ACTION_ANNOTATION);
}
public void verifyWithAppContextNoReturnNoParam() {
String url = "/no_ret_no_param";
verifyRouting(url, "WithAppContext", "noReturnNoParam", GET, PUT);
ActionMethodMetaInfo action = action("WithAppContext", "noReturnNoParam");
System.out.println(action);
// verify app context injection
assertFieldAppCtxInject(action, "ctx");
// verify return type
eq(Type.VOID_TYPE, action.returnType());
// verify params
assertNoParam(action);
// verify interceptors
InterceptorMethodMetaInfo setup = interceptor(action, InterceptorType.BEFORE, "WithAppContext", "setup");
assertFieldAppCtxInject(setup, "ctx");
assertNoParam(setup);
InterceptorMethodMetaInfo ff_f1 = interceptor(action, InterceptorType.FINALLY, "FilterF", "f1");
assertLocalAppCtxInject(ff_f1);
assertNoParam(ff_f1);
}
public void verifyWithAppContextStaticNoReturnNoParam() {
String url = "/static_no_ret_no_param";
verifyRouting(url, "WithAppContext", "staticReturnStringNoParam", GET);
ActionMethodMetaInfo action = action("WithAppContext", "staticReturnStringNoParam");
// verify app context injection
ActContextInjection actContextInjection = $.cast(action.appContextInjection());
same(ActContextInjection.InjectType.LOCAL, actContextInjection.injectVia());
// verify return type
eq(Type.getType(String.class), action.returnType());
// verify params
same(0, action.paramCount());
}
@Test
public void testInheritedInterceptor() throws Exception {
scan(ControllerWithInheritedInterceptor.class);
assertNotNull(infoSrc.controllerMetaInfo(FilterA.class.getName()));
assertNotNull(infoSrc.controllerMetaInfo(FilterB.class.getName()));
ControllerClassMetaInfo info = infoSrc.controllerMetaInfo(ControllerWithInheritedInterceptor.class.getName());
assertHasInterceptor("FilterA", "afterP10", info.afterInterceptors(mockApp));
}
@Test
public void testParamAnnotations() {
scan(ParamWithAnnotationController.class);
ControllerClassMetaInfo info = infoSrc.controllerMetaInfo(ParamWithAnnotationController.class.getName());
assertNotNull(info);
ActionMethodMetaInfo bindNameChanged = info.action("bindNameChanged");
assertNotNull(bindNameChanged);
ParamMetaInfo param = bindNameChanged.param(0);
assertNotNull(param);
eq("bar", param.bindName());
ActionMethodMetaInfo defValPresented = info.action("defValPresented");
assertNotNull(defValPresented);
param = defValPresented.param(0);
assertNotNull(param);
eq(5, param.defVal(Integer.class));
ActionMethodMetaInfo binderRequired = info.action("binderRequired");
assertNotNull(binderRequired);
param = binderRequired.param(0);
assertNotNull(param);
assertNotNull(param.bindAnnoInfo());
eq(EmailBinder.class, param.bindAnnoInfo().binder(mockActionContext).getClass());
}
@Test
public void testHelloWorldApp() {
scan(HelloWorldApp.class);
verifyNoRouting("/hello", "testapp.controller.HelloWorldApp", "sayHello", H.Method.GET);
}
private void scan(Class<?> c) {
List<File> files = Files.filter(base, _F.SAFE_CLASS);
for (File file : files) {
classLoader.preloadClassFile(base, file);
}
classLoader.scan();
infoSrc.mergeActionMetaInfo(mockApp);
}
private void assertHasInterceptor(String className, String actionName, List<InterceptorMethodMetaInfo> list) {
if (!className.contains(".")) {
className = "testapp.controller." + className;
}
for (InterceptorMethodMetaInfo info : list) {
if (S.eq(info.name(), actionName)) {
ControllerClassMetaInfo cinfo = info.classInfo();
if (S.eq(cinfo.className(), className)) {
return;
}
}
}
fail("The list does not contains the interceptor: %s.%s", className, actionName);
}
private void verifyRouting(String url, String controller, String action, H.Method... methods) {
for (H.Method method : methods) {
verify(mockRouter).addMapping(method, url, "testapp.controller." + controller + "." + action, ACTION_ANNOTATION);
}
}
private void verifyNoRouting(String url, String controller, String action, H.Method... methods) {
for (H.Method method : methods) {
verify(mockRouter, new Times(0)).addMapping(method, url, "testapp.controller." + controller + "." + action, ACTION_ANNOTATION);
}
}
private ControllerClassMetaInfo controller(String className) {
return infoSrc.controllerMetaInfo("testapp.controller." + className);
}
private ControllerClassMetaInfo controller(Class<?> c) {
return infoSrc.controllerMetaInfo(c.getName());
}
private ActionMethodMetaInfo action(String controller, String action) {
ControllerClassMetaInfo cinfo = controller(controller);
return null == cinfo ? null : cinfo.action(action);
}
private ActionMethodMetaInfo action(Class<?> c, String action) {
ControllerClassMetaInfo cinfo = controller(c);
return null == cinfo ? null : cinfo.action(action);
}
private InterceptorMethodMetaInfo interceptor(ActionMethodMetaInfo action, InterceptorType interceptorType, String className, String methodName) {
switch (interceptorType) {
case BEFORE:
return interceptor(action.beforeList(), className, methodName);
case AFTER:
return interceptor(action.afterList(), className, methodName);
case CATCH:
return interceptor(action.catchList(), className, methodName);
case FINALLY:
return interceptor(action.finallyList(), className, methodName);
default:
throw E.unexpected("unknown interceptor type: %s", interceptorType);
}
}
private InterceptorMethodMetaInfo interceptor(List<? extends InterceptorMethodMetaInfo> list, String className, String methodName) {
String fullName = S.join(".", "testapp.controller", className, methodName);
for (InterceptorMethodMetaInfo info : list) {
if (S.eq(fullName, info.fullName())) {
return info;
}
}
return null;
}
private void assertFieldAppCtxInject(HandlerMethodMetaInfo action, String fieldName) {
ActContextInjection.FieldActContextInjection appContextInjection = $.cast(action.appContextInjection());
eq(fieldName, appContextInjection.fieldName());
}
private void assertParamAppCtxInject(HandlerMethodMetaInfo action, int index) {
ActContextInjection.ParamAppContextInjection appContextInjection = $.cast(action.appContextInjection());
same(index, appContextInjection.paramIndex());
}
private void assertLocalAppCtxInject(HandlerMethodMetaInfo action) {
same(ActContextInjection.InjectType.LOCAL, action.appContextInjection().injectVia());
}
private void assertNoParam(HandlerMethodMetaInfo action) {
same(0, action.paramCount());
}
private static enum _F {
;
static $.Predicate<String> SYS_CLASS_NAME = new $.Predicate<String>() {
@Override
public boolean test(String s) {
return s.startsWith("java") || s.startsWith("org.osgl.");
}
};
static $.Predicate<String> SAFE_CLASS = S.F.endsWith(".class").and(SYS_CLASS_NAME.negate());
}
}