Skip to content

Commit 674f983

Browse files
author
孙健
committed
add relation by testing run
1 parent d5710ca commit 674f983

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

src/main/java/org/nlpcn/jcoder/util/Testing.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileNotFoundException;
55
import java.io.IOException;
66
import java.lang.reflect.Field;
7+
import java.lang.reflect.Method;
78
import java.nio.file.FileVisitResult;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
@@ -22,6 +23,9 @@
2223
import org.nlpcn.commons.lang.util.FileFinder;
2324
import org.nlpcn.commons.lang.util.IOUtil;
2425
import org.nlpcn.commons.lang.util.StringUtil;
26+
import org.nlpcn.commons.lang.util.tuples.KeyValue;
27+
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
28+
import org.nlpcn.jcoder.run.annotation.Execute;
2529
import org.nutz.http.Http;
2630
import org.nutz.ioc.Ioc;
2731
import org.nutz.ioc.impl.NutIoc;
@@ -52,12 +56,14 @@ public class Testing {
5256
* @return class c instance
5357
* @throws Exception
5458
*/
55-
public static <T> T instance(Class<T> c, String iocPath) throws Exception {
59+
public static <T> T instance(Class<T> c, String iocPath, Class<?>... relation) throws Exception {
5660
Ioc ioc = new NutIoc(new JsonLoader(iocPath));
5761

5862
StaticValue.setSystemIoc(ioc);
5963
StaticValue.setUserIoc(ioc);
6064

65+
relation(ioc, relation);
66+
6167
Mirror<?> mirror = Mirror.me(c);
6268
T obj = c.newInstance();
6369

@@ -75,7 +81,7 @@ public static <T> T instance(Class<T> c, String iocPath) throws Exception {
7581
return obj;
7682
}
7783

78-
public static <T> T instance(Class<T> c) throws Exception {
84+
public static <T> T instance(Class<T> c, Class<?>... relation) throws Exception {
7985

8086
File find = new File("src/test/resources/ioc.js");
8187

@@ -87,13 +93,64 @@ public static <T> T instance(Class<T> c) throws Exception {
8793
}
8894

8995
if (find != null && find.exists()) {
90-
return instance(c, find.getAbsolutePath());
96+
return instance(c, find.getAbsolutePath(), relation);
9197
} else {
9298
throw new FileNotFoundException("ioc.js not found in your classpath ");
9399
}
94100

95101
}
96102

103+
private static void relation(Ioc ioc, Class<?>... clas) throws InstantiationException, IllegalAccessException {
104+
if (clas == null || clas.length == 0) {
105+
return;
106+
}
107+
108+
for (Class<?> c : clas) {
109+
Mirror<?> mirror = Mirror.me(c);
110+
Object obj = c.newInstance();
111+
112+
for (Field field : mirror.getFields()) {
113+
Inject inject = field.getAnnotation(Inject.class);
114+
if (inject != null) {
115+
if (field.getType().equals(org.slf4j.Logger.class)) {
116+
mirror.setValue(obj, field, LoggerFactory.getLogger(c));
117+
} else {
118+
mirror.setValue(obj, field, ioc.get(field.getType(), StringUtil.isBlank(inject.value()) ? field.getName() : inject.value()));
119+
}
120+
}
121+
}
122+
123+
for (Method method : mirror.getMethods()) {
124+
if (method.getAnnotation(Execute.class) == null && method.getAnnotation(DefaultExecute.class) == null) {
125+
continue;
126+
}
127+
String key = c.getSimpleName() + "/" + method.getName();
128+
LOG.info("add relation " + key);
129+
TestingFilter.methods.put(key, KeyValue.with(method, obj));
130+
}
131+
}
132+
133+
}
134+
135+
/**
136+
* 释放关联类
137+
*
138+
* @param clas
139+
* @throws IllegalAccessException
140+
* @throws InstantiationException
141+
*/
142+
public static void unRelation(Class<?>... clas) {
143+
for (Class<?> c : clas) {
144+
Mirror<?> mirror = Mirror.me(c);
145+
for (Method method : mirror.getMethods()) {
146+
if (method.getAnnotation(Execute.class) == null && method.getAnnotation(DefaultExecute.class) == null) {
147+
continue;
148+
}
149+
TestingFilter.methods.remove(c.getSimpleName() + "/" + method.getName());
150+
}
151+
}
152+
}
153+
97154
/**
98155
* 对比本地代码和线上代码的不同
99156
*
@@ -178,7 +235,7 @@ public static void startServer(int port, String iocPath, String jcoderHome, Stri
178235
server.join();
179236

180237
}
181-
238+
182239
/**
183240
* test local api by server
184241
*
@@ -190,6 +247,5 @@ public static void startServer(int port, String iocPath, String jcoderHome, Stri
190247
public static void startServer(String iocPath, String[] packages) throws Exception {
191248
startServer(8080, iocPath, null, packages);
192249
}
193-
194-
250+
195251
}

src/main/java/org/nlpcn/jcoder/util/TestingFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.nutz.mvc.ActionInfo;
3131
import org.nutz.mvc.Mvcs;
3232
import org.nutz.mvc.NutFilter;
33-
import org.nutz.mvc.impl.processor.EncodingProcessor;
3433
import org.nutz.resource.Scans;
3534
import org.slf4j.Logger;
3635
import org.slf4j.LoggerFactory;
@@ -39,7 +38,7 @@ public class TestingFilter extends NutFilter {
3938

4039
private static final Logger LOG = LoggerFactory.getLogger(TestingFilter.class);
4140

42-
public static Map<String, KeyValue<Method, Object>> methods = null;
41+
public static Map<String, KeyValue<Method, Object>> methods = new HashMap<>();
4342

4443
public static void init(String... packages) throws IOException {
4544
Map<String, KeyValue<Method, Object>> tempMethods = new HashMap<>();

src/test/java/cn/com/infcn/api/test/TaskDemo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cn.com.infcn.api.test;
22

3-
import org.apache.log4j.Logger;
43
import org.nlpcn.jcoder.filter.TokenFilter;
54
import org.nlpcn.jcoder.run.annotation.DefaultExecute;
65
import org.nlpcn.jcoder.run.annotation.Single;
6+
import org.nlpcn.jcoder.util.Testing;
77
import org.nutz.ioc.loader.annotation.Inject;
88
import org.nutz.mvc.annotation.By;
99
import org.nutz.mvc.annotation.Filters;
10+
import org.slf4j.Logger;
1011

1112
@Single(true)
1213
public class TaskDemo {
@@ -24,6 +25,8 @@ public class TaskDemo {
2425
@DefaultExecute
2526
@Filters(@By(type = TokenFilter.class, args = { "false" }))
2627
public Object execute(String name, Integer age, Character sex) {
28+
log.info("12312312");
2729
return "name:" + name +" age:"+age+" sex:"+sex;
2830
}
31+
2932
}

0 commit comments

Comments
 (0)