1
1
package cn .sticki .spel .validator .javax .util ;
2
2
3
3
import lombok .extern .slf4j .Slf4j ;
4
- import org .slf4j .MDC ;
5
4
6
5
import javax .validation .ConstraintViolation ;
7
6
import javax .validation .Validation ;
8
7
import javax .validation .Validator ;
9
- import java .util .*;
10
- import java .util .stream .Collectors ;
8
+ import java .util .Collection ;
9
+ import java .util .List ;
10
+ import java .util .Set ;
11
11
12
12
/**
13
13
* 测试验证工具类
@@ -55,25 +55,14 @@ public static boolean checkConstraintResult(VerifyObject verifyObject) {
55
55
boolean expectException = verifyObject .isExpectException ();
56
56
57
57
// 设置日志上下文
58
- String className = object .getClass ().getSimpleName ();
59
- Class <?> enclosingClass = object .getClass ().getEnclosingClass ();
60
- if (enclosingClass != null ) {
61
- className = enclosingClass .getSimpleName () + "." + className ;
62
- }
63
-
64
- MDC .put ("className" , className );
65
- MDC .put ("fullClassName" , abbreviate (object .getClass ().getName ()));
66
- if (object instanceof ID ) {
67
- MDC .put ("id" , String .valueOf (((ID ) object ).getId ()));
68
- }
69
-
58
+ LogContext .setValidateObject (object );
70
59
log .info ("Start checking object: {}" , object );
71
60
72
61
int failCount = 0 ;
73
62
try {
74
63
// 执行约束校验
75
64
Set <ConstraintViolation <Object >> validate = ValidateUtil .validate (object );
76
- failCount += calcFailCount (verifyFailedFields , ViolationSet .of (validate ));
65
+ failCount += processVerifyResult (verifyFailedFields , ConstraintViolationSet .of (validate ));
77
66
} catch (Exception e ) {
78
67
if (expectException ) {
79
68
log .info ("Passed, Capture exception {}, message: {}" , e .getClass (), e .getMessage ());
@@ -95,30 +84,32 @@ public static boolean checkConstraintResult(VerifyObject verifyObject) {
95
84
log .error ("Verification end, number of failures: {}" , failCount );
96
85
}
97
86
log .info ("------------------------------------------------------------------------" );
98
- MDC . clear ();
87
+ LogContext . clearValidateObject ();
99
88
100
89
return failCount == 0 ;
101
90
}
102
91
103
92
/**
104
- * 计算验证字段约束的失败次数
93
+ * 处理验证结果
105
94
*
106
95
* @param verifyFailedFields 预期失败字段
107
96
* @param violationSet 验证结果
97
+ * @return 验证失败的次数
108
98
*/
109
- private static int calcFailCount (Collection <VerifyFailedField > verifyFailedFields , ViolationSet violationSet ) {
99
+ private static int processVerifyResult (Collection <VerifyFailedField > verifyFailedFields , ConstraintViolationSet violationSet ) {
100
+ final String fieldNameLogKey = "fieldName" ;
110
101
int failCount = 0 ;
111
102
// 检查结果是否符合预期
112
103
for (VerifyFailedField verifyFailedField : verifyFailedFields ) {
113
104
String fieldName = verifyFailedField .getName ();
114
- MDC . put ( "fieldName" , fieldName );
105
+ LogContext . set ( fieldNameLogKey , fieldName );
115
106
String message = verifyFailedField .getMessage ();
116
107
117
108
log .info ("Expected exception information: {}" , message == null ? "ignore" : message );
118
109
119
110
boolean fieldMatch = false , find = false ;
120
111
121
- ConstraintViolation < Object > violation = violationSet .getAndRemove (fieldName , message );
112
+ VerifyFailedField violation = violationSet .getAndRemove (fieldName , message );
122
113
if (violation != null ) {
123
114
find = true ;
124
115
log .info ("Real exception information: {}" , violation .getMessage ());
@@ -141,75 +132,14 @@ private static int calcFailCount(Collection<VerifyFailedField> verifyFailedField
141
132
}
142
133
}
143
134
144
- MDC .remove ("fieldName" );
135
+ LogContext .remove (fieldNameLogKey );
145
136
// 被忽略的字段
146
- for (ConstraintViolation < Object > violation : violationSet .getAll ()) {
147
- log .error ("Field [{}] is ignored" , violation .getPropertyPath (). toString ());
137
+ for (VerifyFailedField violation : violationSet .getAll ()) {
138
+ log .error ("Field [{}] is ignored" , violation .getName ());
148
139
failCount ++;
149
140
}
150
141
return failCount ;
151
142
}
152
143
153
- public static String abbreviate (String className ) {
154
- String [] parts = className .split ("\\ ." );
155
- StringBuilder abbreviated = new StringBuilder ();
156
- for (int i = 0 ; i < parts .length - 1 ; i ++) {
157
- abbreviated .append (parts [i ].charAt (0 )).append ("." );
158
- }
159
- abbreviated .append (parts [parts .length - 1 ]);
160
- return abbreviated .toString ();
161
- }
162
-
163
- static class ViolationSet {
164
-
165
- private final Map <String , List <ConstraintViolation <Object >>> violationMap ;
166
-
167
- public ViolationSet (Set <ConstraintViolation <Object >> validate ) {
168
- if (validate == null || validate .isEmpty ()) {
169
- violationMap = Collections .emptyMap ();
170
- return ;
171
- }
172
- violationMap = validate .stream ().collect (
173
- Collectors .groupingBy (violation -> violation .getPropertyPath ().toString ())
174
- );
175
- }
176
-
177
- public static ViolationSet of (Set <ConstraintViolation <Object >> validate ) {
178
- return new ViolationSet (validate );
179
- }
180
-
181
- /**
182
- * 根据字段和期望的错误信息来获取字段约束结果
183
- *
184
- * @param fieldName 字段名
185
- * @param expectMessage 期望的错误信息
186
- * @return 字段约束结果,当 expectMessage 不为null时,会优先匹配具有相同message的数据
187
- */
188
- public ConstraintViolation <Object > getAndRemove (String fieldName , String expectMessage ) {
189
- List <ConstraintViolation <Object >> violationList = violationMap .get (fieldName );
190
- if (violationList == null || violationList .isEmpty ()) {
191
- return null ;
192
- }
193
- if (violationList .size () == 1 || expectMessage == null ) {
194
- ConstraintViolation <Object > violation = violationList .get (0 );
195
- violationMap .remove (fieldName );
196
- return violation ;
197
- }
198
- // 当存在多个约束时,优先匹配具有相同message的数据
199
- for (ConstraintViolation <Object > violation : violationList ) {
200
- if (expectMessage .equals (violation .getMessage ())) {
201
- violationList .remove (violation );
202
- return violation ;
203
- }
204
- }
205
-
206
- return violationList .remove (0 );
207
- }
208
-
209
- public Set <ConstraintViolation <Object >> getAll () {
210
- return violationMap .values ().stream ().flatMap (List ::stream ).collect (Collectors .toSet ());
211
- }
212
-
213
- }
214
144
215
145
}
0 commit comments