Skip to content

Commit 1fda4ba

Browse files
committed
优化启动时自检,以免用户对异常日志困惑;移除已废弃的 StructureUtil 和 Test 的相关代码
1 parent ad6996f commit 1fda4ba

6 files changed

Lines changed: 144 additions & 137 deletions

File tree

src/main/java/apijson/framework/APIJSONApplication.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.rmi.ServerException;
1818

19+
import apijson.Log;
1920
import apijson.NotNull;
2021

2122

@@ -24,7 +25,8 @@
2425
* @author Lemon
2526
*/
2627
public class APIJSONApplication {
27-
28+
public static final String TAG = "APIJSONApplication";
29+
2830
@NotNull
2931
public static APIJSONCreator DEFAULT_APIJSON_CREATOR;
3032
static {
@@ -34,34 +36,34 @@ public class APIJSONApplication {
3436

3537
/**初始化,加载所有配置并校验
3638
* @return
37-
* @throws ServerException
39+
* @throws Exception
3840
*/
39-
public static void init() throws ServerException {
41+
public static void init() throws Exception {
4042
init(true, DEFAULT_APIJSON_CREATOR);
4143
}
4244
/**初始化,加载所有配置并校验
4345
* @param shutdownWhenServerError
4446
* @return
45-
* @throws ServerException
47+
* @throws Exception
4648
*/
47-
public static void init(boolean shutdownWhenServerError) throws ServerException {
49+
public static void init(boolean shutdownWhenServerError) throws Exception {
4850
init(shutdownWhenServerError, DEFAULT_APIJSON_CREATOR);
4951
}
5052
/**初始化,加载所有配置并校验
5153
* @param creator
5254
* @return
53-
* @throws ServerException
55+
* @throws Exception
5456
*/
55-
public static void init(@NotNull APIJSONCreator creator) throws ServerException {
57+
public static void init(@NotNull APIJSONCreator creator) throws Exception {
5658
init(true, creator);
5759
}
5860
/**初始化,加载所有配置并校验
5961
* @param shutdownWhenServerError
6062
* @param creator
6163
* @return
62-
* @throws ServerException
64+
* @throws Exception
6365
*/
64-
public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator creator) throws ServerException {
66+
public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator creator) throws Exception {
6567
System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 开始启动 >>>>>>>>>>>>>>>>>>>>>>>>\n");
6668
DEFAULT_APIJSON_CREATOR = creator;
6769

@@ -75,8 +77,11 @@ public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator
7577
try {
7678
APIJSONVerifier.initAccess(shutdownWhenServerError, creator);
7779
}
78-
catch (Exception e) {
80+
catch (Throwable e) {
7981
e.printStackTrace();
82+
if (shutdownWhenServerError) {
83+
onServerError("权限校验配置 初始化失败!", shutdownWhenServerError);
84+
}
8085
}
8186
System.out.println("\n完成初始化: 权限校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
8287

@@ -86,17 +91,23 @@ public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator
8691
try {
8792
APIJSONFunctionParser.init(shutdownWhenServerError, creator);
8893
}
89-
catch (Exception e) {
94+
catch (Throwable e) {
9095
e.printStackTrace();
96+
if (shutdownWhenServerError) {
97+
onServerError("远程函数配置 初始化失败!", shutdownWhenServerError);
98+
}
9199
}
92100
System.out.println("\n完成初始化: 远程函数配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
93101

94102
System.out.println("开始测试: 远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
95103
try {
96104
APIJSONFunctionParser.test();
97105
}
98-
catch (Exception e) {
106+
catch (Throwable e) {
99107
e.printStackTrace();
108+
if (shutdownWhenServerError) {
109+
onServerError("远程函数配置 测试失败!", shutdownWhenServerError);
110+
}
100111
}
101112
System.out.println("\n完成测试: 远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
102113

@@ -106,17 +117,23 @@ public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator
106117
try {
107118
APIJSONVerifier.initRequest(shutdownWhenServerError, creator);
108119
}
109-
catch (Exception e) {
120+
catch (Throwable e) {
110121
e.printStackTrace();
122+
if (shutdownWhenServerError) {
123+
onServerError("请求结构校验配置 初始化失败!", shutdownWhenServerError);
124+
}
111125
}
112126
System.out.println("\n完成初始化: 请求结构校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
113127

114128
System.out.println("\n\n\n开始测试: Request 和 Response 的数据结构校验 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
115129
try {
116130
APIJSONVerifier.testStructure();
117131
}
118-
catch (Exception e) {
132+
catch (Throwable e) {
119133
e.printStackTrace();
134+
if (shutdownWhenServerError) {
135+
onServerError("Request 和 Response 的数据结构校验 测试失败!", shutdownWhenServerError);
136+
}
120137
}
121138
System.out.println("\n完成测试: Request 和 Response 的数据结构校验 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
122139

@@ -125,5 +142,15 @@ public static void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator
125142

126143
System.out.println("\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 启动完成,试试调用自动化 API 吧 ^_^ >>>>>>>>>>>>>>>>>>>>>>>>\n");
127144
}
145+
146+
private static void onServerError(String msg, boolean shutdown) throws ServerException {
147+
Log.e(TAG, "\n启动时自检测试未通过!原因:\n" + msg);
148+
149+
if (shutdown) {
150+
System.exit(1);
151+
} else {
152+
throw new ServerException(msg);
153+
}
154+
}
128155

129156
}

src/main/java/apijson/framework/APIJSONConstant.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
package apijson.framework;
1616

17-
18-
/**APIJSON 常量类
19-
* @author Lemon
20-
*/
2117
import apijson.JSONResponse;
2218
import apijson.orm.JSONRequest;
2319
import apijson.orm.Visitor;
@@ -33,9 +29,12 @@
3329
import apijson.orm.model.SysColumn;
3430
import apijson.orm.model.SysTable;
3531
import apijson.orm.model.Table;
36-
import apijson.orm.model.Test;
3732
import apijson.orm.model.TestRecord;
3833

34+
35+
/**APIJSON 常量类
36+
* @author Lemon
37+
*/
3938
public class APIJSONConstant {
4039

4140
public static final String DEFAULTS = "defaults";
@@ -66,7 +65,6 @@ public class APIJSONConstant {
6665
public static final String SYS_COLUMN_;
6766
public static final String SYS_TABLE_;
6867
public static final String TABLE_;
69-
public static final String TEST_;
7068
public static final String TEST_RECORD_;
7169

7270
public static final String VISITOR_;
@@ -84,7 +82,6 @@ public class APIJSONConstant {
8482
SYS_COLUMN_ = SysColumn.class.getSimpleName();
8583
SYS_TABLE_ = SysTable.class.getSimpleName();
8684
TABLE_ = Table.class.getSimpleName();
87-
TEST_ = Test.class.getSimpleName();
8885
TEST_RECORD_ = TestRecord.class.getSimpleName();
8986

9087
VISITOR_ = Visitor.class.getSimpleName();

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package apijson.framework;
1616

1717
import static apijson.framework.APIJSONConstant.FUNCTION_;
18-
import static apijson.framework.APIJSONConstant.REQUEST_;
1918

2019
import java.io.IOException;
2120
import java.rmi.ServerException;
@@ -235,18 +234,28 @@ public static void test(APIJSONFunctionParser function) throws Exception {
235234

236235
request.put("key", "key");
237236
JSONObject object = new JSONObject();
238-
object.put("key", true);
237+
object.put("key", "success");
239238
request.put("object", object);
240239

241240
if (function == null) {
242241
function = new APIJSONFunctionParser(null, null, 1, null, null);
243242
}
244243

245-
Log.i(TAG, "plus(1,-2) = " + function.invoke("plus(i0,i1)", request));
244+
// 等数据库 Function 表加上 plus 配置再过两个以上迭代(应该是到 5.0)后再取消注释
245+
// Log.i(TAG, "plus(1,-2) = " + function.invoke("plus(i0,i1)", request));
246+
// AssertUtil.assertEqual(-1, function.invoke("plus(i0,i1)", request));
247+
246248
Log.i(TAG, "count([1,2,4,10]) = " + function.invoke("countArray(array)", request));
249+
AssertUtil.assertEqual(4, function.invoke("countArray(array)", request));
250+
247251
Log.i(TAG, "isContain([1,2,4,10], 10) = " + function.invoke("isContain(array,id)", request));
252+
AssertUtil.assertEqual(true, function.invoke("isContain(array,id)", request));
253+
248254
Log.i(TAG, "getFromArray([1,2,4,10], 0) = " + function.invoke("getFromArray(array,@position)", request));
249-
Log.i(TAG, "getFromObject({key:true}, key) = " + function.invoke("getFromObject(object,key)", request));
255+
AssertUtil.assertEqual(1, function.invoke("getFromArray(array,@position)", request));
256+
257+
Log.i(TAG, "getFromObject({key:\"success\"}, key) = " + function.invoke("getFromObject(object,key)", request));
258+
AssertUtil.assertEqual("success", function.invoke("getFromObject(object,key)", request));
250259

251260
}
252261

src/main/java/apijson/framework/APIJSONVerifier.java

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
147147
}
148148
APIJSON_CREATOR = creator;
149149

150-
150+
151151
boolean isAll = table == null || table.isEmpty();
152152

153153
JSONObject access = isAll ? new JSONRequest() : table;
@@ -160,7 +160,7 @@ public static JSONObject initAccess(boolean shutdownWhenServerError, APIJSONCrea
160160
JSONRequest request = new JSONRequest();
161161
request.putAll(accessItem.toArray(0, 0, ACCESS_));
162162

163-
163+
164164
JSONObject response = creator.createParser().setMethod(RequestMethod.GET).setNeedVerify(false).parseResponse(request);
165165
if (JSONResponse.isSuccess(response) == false) {
166166
Log.e(TAG, "\n\n\n\n\n !!!! 查询权限配置异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n");
@@ -277,7 +277,7 @@ public static JSONObject initRequest(boolean shutdownWhenServerError, APIJSONCre
277277
}
278278
APIJSON_CREATOR = creator;
279279

280-
280+
281281
boolean isAll = table == null || table.isEmpty();
282282

283283
JSONRequest requestItem = new JSONRequest();
@@ -380,61 +380,79 @@ public int compare(Integer o1, Integer o2) {
380380
}
381381

382382

383-
public static void test() {
383+
public static void test() throws Exception {
384384
testStructure();
385385
}
386386

387-
static final String requestString = "{\"Comment\":{\"REFUSE\": \"id\", \"MUST\": \"userId,momentId,content\"}, \"INSERT\":{\"Comment:to\":{}}}";
388-
static final String responseString = "{\"User\":{\"REMOVE\": \"phone\", \"REPLACE\":{\"sex\":2}, \"INSERT\":{\"name\":\"api\"}}, \"UPDATE\":{\"Comment:to\":{}}}";
387+
static final String requestConfig = "{\"Comment\":{\"REFUSE\": \"id\", \"MUST\": \"userId,momentId,content\"}, \"INSERT\":{\"@role\":\"OWNER\"}}";
388+
static final String responseConfig = "{\"User\":{\"REMOVE\": \"phone\", \"REPLACE\":{\"sex\":2}, \"INSERT\":{\"name\":\"api\"}, \"UPDATE\":{\"verifyURLList-()\":\"verifyURLList(pictureList)\"}}}";
389389
/**
390390
* 测试 Request 和 Response 的数据结构校验
391+
* @throws Exception
391392
*/
392-
public static void testStructure() {
393+
public static void testStructure() throws Exception {
393394
JSONObject request;
394395
try {
395396
request = JSON.parseObject("{\"Comment\":{\"userId\":0}}");
396-
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestString), request, APIJSON_CREATOR));
397-
} catch (Exception e) {
398-
e.printStackTrace();
397+
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestConfig), request, APIJSON_CREATOR));
398+
} catch (Throwable e) {
399+
if (e instanceof IllegalArgumentException == false || "POST请求,Comment 里面不能缺少 momentId 等[userId,momentId,content]内的任何字段!".equals(e.getMessage()) == false) {
400+
throw e;
401+
}
402+
Log.d(TAG, "测试 Operation.MUST 校验缺少字段:成功");
399403
}
400-
try {
401-
request = JSON.parseObject("{\"Comment\":{\"userId\":0, \"momentId\":0, \"content\":\"apijson\"}}");
402-
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestString), request, APIJSON_CREATOR));
403-
} catch (Exception e) {
404-
e.printStackTrace();
404+
try {
405+
request = JSON.parseObject("{\"Comment\":{\"id\":0, \"userId\":0, \"momentId\":0, \"content\":\"apijson\"}}");
406+
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestConfig), request, APIJSON_CREATOR));
407+
} catch (Throwable e) {
408+
if (e instanceof IllegalArgumentException == false || "POST请求,/Comment 不能传 id !".equals(e.getMessage()) == false) {
409+
throw e;
410+
}
411+
Log.d(TAG, "测试 Operation.REFUSE 校验不允许传字段:成功");
405412
}
406413
try {
407-
request = JSON.parseObject("{\"Comment\":{\"id\":0, \"userId\":0, \"momentId\":0, \"content\":\"apijson\"}}");
408-
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestString), request, APIJSON_CREATOR));
409-
} catch (Exception e) {
410-
e.printStackTrace();
414+
request = JSON.parseObject("{\"Comment\":{\"userId\":0, \"momentId\":0, \"content\":\"apijson\"}}");
415+
Log.d(TAG, "test verifyRequest = " + AbstractVerifier.verifyRequest(RequestMethod.POST, "", JSON.parseObject(requestConfig), request, APIJSON_CREATOR));
416+
AssertUtil.assertEqual("OWNER", request.getString("@role"));
417+
Log.d(TAG, "测试 Operation.INSERT 不存在字段时插入:成功");
418+
} catch (Throwable e) {
419+
throw e;
411420
}
412421

413422

423+
414424
JSONObject response;
415425
try {
416426
response = JSON.parseObject("{\"User\":{\"userId\":0}}");
417-
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseString), response, APIJSON_CREATOR, null));
418-
} catch (Exception e) {
419-
e.printStackTrace();
427+
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseConfig), response, APIJSON_CREATOR, null));
428+
AssertUtil.assertEqual("verifyURLList(pictureList)", response.getJSONObject("User").getString("verifyURLList-()"));
429+
Log.d(TAG, "测试 Operation.UPDATE 强制插入/替换:成功");
430+
} catch (Throwable e) {
431+
throw e;
420432
}
421433
try {
422434
response = JSON.parseObject("{\"User\":{\"userId\":0, \"phone\":\"12345678\"}}");
423-
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseString), response, APIJSON_CREATOR, null));
424-
} catch (Exception e) {
425-
e.printStackTrace();
435+
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseConfig), response, APIJSON_CREATOR, null));
436+
AssertUtil.assertEqual(null, response.getJSONObject("User").get("phone"));
437+
Log.d(TAG, "测试 Operation.REMOVE 强制移除:成功");
438+
} catch (Throwable e) {
439+
throw e;
426440
}
427441
try {
428442
response = JSON.parseObject("{\"User\":{\"userId\":0, \"phone\":\"12345678\", \"sex\":1}}");
429-
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseString), response, APIJSON_CREATOR, null));
430-
} catch (Exception e) {
431-
e.printStackTrace();
443+
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseConfig), response, APIJSON_CREATOR, null));
444+
AssertUtil.assertEqual("api", response.getJSONObject("User").get("name"));
445+
Log.d(TAG, "测试 Operation.INSERT 不存在字段时插入:成功");
446+
} catch (Throwable e) {
447+
throw e;
432448
}
433449
try {
434450
response = JSON.parseObject("{\"User\":{\"id\":0, \"name\":\"tommy\", \"phone\":\"12345678\", \"sex\":1}}");
435-
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseString), response, APIJSON_CREATOR, null));
436-
} catch (Exception e) {
437-
e.printStackTrace();
451+
Log.d(TAG, "test verifyResponse = " + AbstractVerifier.verifyResponse(RequestMethod.GET, "", JSON.parseObject(responseConfig), response, APIJSON_CREATOR, null));
452+
AssertUtil.assertEqual(2, response.getJSONObject("User").get("sex"));
453+
Log.d(TAG, "测试 Operation.REPLACE 存在字段时替换:成功");
454+
} catch (Throwable e) {
455+
throw e;
438456
}
439457

440458
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON)
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.*/
14+
15+
package apijson.framework;
16+
17+
18+
/**简单断言工具类,不用额外引入 JUnit 等库
19+
* @author Lemon
20+
*/
21+
public class AssertUtil {
22+
23+
public static void assertEqual(Object a, Object b) {
24+
assertEqual(a, b, null);
25+
}
26+
27+
public static void assertEqual(Object a, Object b, String errorMessage) {
28+
if (a == b) {
29+
return;
30+
}
31+
32+
if (a == null || b == null || a.equals(b) == false) {
33+
throw new AssertionError(errorMessage == null ? "assert fail: a != b" : errorMessage);
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)