Skip to content

Commit 102e2e3

Browse files
committed
单元测试:引入 UnitAuto-机器学习零代码单元测试 库
1 parent 5683b20 commit 102e2e3

File tree

6 files changed

+203
-1010
lines changed

6 files changed

+203
-1010
lines changed

.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@
2828
</attributes>
2929
</classpathentry>
3030
<classpathentry kind="lib" path="libs/apijson-orm-4.2.3.jar"/>
31+
<classpathentry kind="lib" path="libs/unitauto-2.5.0.jar"/>
32+
<classpathentry kind="lib" path="libs/unitauto-jar-2.5.0.jar"/>
3133
<classpathentry kind="output" path="target/classes"/>
3234
</classpath>

libs/unitauto-2.5.0.jar

77.6 KB
Binary file not shown.

libs/unitauto-jar-2.5.0.jar

5.1 KB
Binary file not shown.

src/main/java/apijson/framework/APIJSONController.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
import static apijson.framework.APIJSONConstant.VISITOR_;
3131
import static apijson.framework.APIJSONConstant.VISITOR_ID;
3232

33+
import java.lang.reflect.Method;
3334
import java.rmi.ServerException;
3435

36+
import javax.servlet.AsyncContext;
37+
import javax.servlet.ServletResponse;
38+
import javax.servlet.http.HttpServletRequest;
3539
import javax.servlet.http.HttpSession;
3640

3741
import com.alibaba.fastjson.JSONObject;
@@ -42,9 +46,11 @@
4246
import apijson.StringUtil;
4347
import apijson.orm.Parser;
4448
import apijson.orm.Visitor;
49+
import unitauto.MethodUtil;
50+
import unitauto.MethodUtil.InterfaceProxy;
4551

4652

47-
/**request controller
53+
/**APIJSON base controller,建议在子项目被 @RestController 注解的类继承它或通过它的实例调用相关方法
4854
* <br > 全通过 HTTP POST 来请求:
4955
* <br > 1.减少代码 - 客户端无需写 HTTP GET, HTTP PUT 等各种方式的请求代码
5056
* <br > 2.提高性能 - 无需 URL encode 和 decode
@@ -223,12 +229,44 @@ public Object logout(@NotNull HttpSession session) {
223229
}
224230

225231

226-
public JSONObject invokeMethod(String request) {
227-
return MethodUtil.invokeMethod(request);
228-
}
229232

230233
public JSONObject listMethod(String request) {
231234
return MethodUtil.listMethod(request);
232235
}
236+
237+
public void invokeMethod(String request, HttpServletRequest servletRequest) {
238+
AsyncContext asyncContext = servletRequest.startAsync();
239+
240+
MethodUtil.Listener<JSONObject> listener = new MethodUtil.Listener<JSONObject>() {
241+
242+
@Override
243+
public void complete(JSONObject data, Method method, InterfaceProxy proxy, Object... extras) throws Exception {
244+
ServletResponse servletResponse = asyncContext.getResponse();
245+
if (servletResponse.isCommitted()) {
246+
Log.w(TAG, "invokeMethod listener.complete servletResponse.isCommitted() >> return;");
247+
return;
248+
}
249+
250+
servletResponse.setCharacterEncoding(servletRequest.getCharacterEncoding());
251+
servletResponse.setContentType(servletRequest.getContentType());
252+
servletResponse.getWriter().println(data);
253+
asyncContext.complete();
254+
}
255+
};
256+
257+
try {
258+
MethodUtil.invokeMethod(request, null, listener);
259+
}
260+
catch (Exception e) {
261+
Log.e(TAG, "invokeMethod try { JSONObject req = JSON.parseObject(request); ... } catch (Exception e) { \n" + e.getMessage());
262+
try {
263+
listener.complete(MethodUtil.JSON_CALLBACK.newErrorResult(e));
264+
}
265+
catch (Exception e1) {
266+
e1.printStackTrace();
267+
asyncContext.complete();
268+
}
269+
}
270+
}
233271

234272
}

0 commit comments

Comments
 (0)