44import java .io .FileNotFoundException ;
55import java .io .IOException ;
66import java .lang .reflect .Field ;
7+ import java .lang .reflect .Method ;
78import java .nio .file .FileVisitResult ;
89import java .nio .file .Files ;
910import java .nio .file .Path ;
2223import org .nlpcn .commons .lang .util .FileFinder ;
2324import org .nlpcn .commons .lang .util .IOUtil ;
2425import 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 ;
2529import org .nutz .http .Http ;
2630import org .nutz .ioc .Ioc ;
2731import 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}
0 commit comments