Skip to content

Commit cb7c5c6

Browse files
committed
test: 转移测试工具代码,转移测试用例
由于test模块和core模块的测试用例产生了循环依赖,故转移core模块的测试用例到test模块。
1 parent de5ec3d commit cb7c5c6

31 files changed

+379
-192
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>spel-validator-javax</module>
1616
<module>spel-validator-jakarta</module>
1717
<module>spel-validator-test-report</module>
18+
<module>spel-validator-test</module>
1819
</modules>
1920

2021
<name>Spel Validator</name>
@@ -92,6 +93,11 @@
9293
<artifactId>spel-validator-jakarta</artifactId>
9394
<version>${spel-validator.version}</version>
9495
</dependency>
96+
<dependency>
97+
<groupId>cn.sticki</groupId>
98+
<artifactId>spel-validator-test</artifactId>
99+
<version>${spel-validator.version}</version>
100+
</dependency>
95101
<dependency>
96102
<groupId>org.springframework</groupId>
97103
<artifactId>spring-context</artifactId>

spel-validator-core/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@
3737
<groupId>org.slf4j</groupId>
3838
<artifactId>slf4j-api</artifactId>
3939
</dependency>
40+
41+
<!-- ===== for test ===== -->
42+
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.glassfish</groupId>
51+
<artifactId>javax.el</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>ch.qos.logback</groupId>
57+
<artifactId>logback-classic</artifactId>
58+
<scope>test</scope>
59+
</dependency>
4060
</dependencies>
4161

4262
</project>

spel-validator-core/src/main/java/cn/sticki/spel/validator/core/parse/SpelParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.sticki.spel.validator.core.exception.SpelParserException;
44
import lombok.extern.slf4j.Slf4j;
5+
import org.intellij.lang.annotations.Language;
56
import org.jetbrains.annotations.NotNull;
67
import org.jetbrains.annotations.Nullable;
78
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
@@ -59,7 +60,7 @@ private static void init() {
5960
* @return 表达式计算结果。若为基本数据类型,则会自动转为包装类型。
6061
*/
6162
@Nullable
62-
public static Object parse(String expression, Object rootObject) {
63+
public static Object parse(@Language("spel") String expression, Object rootObject) {
6364
try {
6465
log.debug("======> Parse expression [{}]", expression);
6566
Expression parsed = expressionCache.computeIfAbsent(expression, parser::parseExpression);
@@ -82,7 +83,7 @@ public static Object parse(String expression, Object rootObject) {
8283
* @throws SpelParserException 当表达式计算结果为null或者不是指定类型时抛出
8384
*/
8485
@NotNull
85-
public static <T> T parse(String expression, Object rootObject, Class<T> requiredType) {
86+
public static <T> T parse(@Language("spel") String expression, Object rootObject, Class<T> requiredType) {
8687
Object any = parse(expression, rootObject);
8788
if (any == null) {
8889
throw new SpelParserException("Expression [" + expression + "] calculate result can not be null");

spel-validator-javax/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131
<!-- ===== for test ===== -->
3232

33+
<dependency>
34+
<groupId>cn.sticki</groupId>
35+
<artifactId>spel-validator-test</artifactId>
36+
</dependency>
37+
3338
<dependency>
3439
<groupId>org.junit.jupiter</groupId>
3540
<artifactId>junit-jupiter</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package cn.sticki.spel.validator.javax;
22

3-
import cn.sticki.spel.validator.javax.bean.*;
4-
import cn.sticki.spel.validator.javax.util.ValidateUtil;
3+
import cn.sticki.spel.validator.javax.bean.ExampleTestBean;
4+
import cn.sticki.spel.validator.javax.bean.SpelValidTestBean;
5+
import cn.sticki.spel.validator.javax.util.JavaxSpelValidator;
56
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
78

@@ -19,94 +20,17 @@ public class ConstrainTest {
1920
*/
2021
@Test
2122
void testExample() {
22-
boolean verified = ValidateUtil.checkConstraintResult(ExampleTestBean.testCase());
23+
boolean verified = JavaxSpelValidator.check(ExampleTestBean.testCase());
2324
Assertions.assertTrue(verified);
2425

25-
boolean innerTest = ValidateUtil.checkConstraintResult(ExampleTestBean.innerTestCase());
26+
boolean innerTest = JavaxSpelValidator.check(ExampleTestBean.innerTestCase());
2627
Assertions.assertTrue(innerTest);
2728
}
2829

2930
@Test
3031
void testSpelValid() {
31-
boolean verified = ValidateUtil.checkConstraintResult(SpelValidTestBean.paramTestCase());
32+
boolean verified = JavaxSpelValidator.check(SpelValidTestBean.paramTestCase());
3233
Assertions.assertTrue(verified);
3334
}
3435

35-
@Test
36-
void testSpelAssert() {
37-
boolean verified = ValidateUtil.checkConstraintResult(SpelAssertTestBean.paramTestCase());
38-
boolean emptyTest = ValidateUtil.checkConstraintResult(SpelAssertTestBean.emptyTestCase());
39-
40-
Assertions.assertTrue(verified);
41-
Assertions.assertTrue(emptyTest);
42-
}
43-
44-
@Test
45-
void testSpelNotBlank() {
46-
boolean verified = ValidateUtil.checkConstraintResult(SpelNotBlankTestBean.testCase());
47-
Assertions.assertTrue(verified);
48-
}
49-
50-
@Test
51-
void testSpelNotEmpty() {
52-
boolean verifiedParam = ValidateUtil.checkConstraintResult(SpelNotEmptyTestBean.paramTestCase());
53-
boolean verifiedType = ValidateUtil.checkConstraintResult(SpelNotEmptyTestBean.typeTestCase());
54-
55-
Assertions.assertTrue(verifiedParam, "spelNotEmpty param test failed");
56-
Assertions.assertTrue(verifiedType, "spelNotEmpty type test failed");
57-
}
58-
59-
@Test
60-
void testSpelNotNull() {
61-
boolean paramTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.paramTestCase());
62-
boolean typeTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.typeTestCase());
63-
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.repeatableTestCase());
64-
65-
Assertions.assertTrue(paramTest, "spelNotNull param test failed");
66-
Assertions.assertTrue(typeTest, "spelNotNull type test failed");
67-
Assertions.assertTrue(repeatableTest, "spelNotNull repeatable test failed");
68-
}
69-
70-
@Test
71-
void testSpelNull() {
72-
boolean paramTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.paramTestCase());
73-
boolean typeTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.typeTestCase());
74-
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.repeatableTestCase());
75-
76-
Assertions.assertTrue(paramTest, "spelNull param test failed");
77-
Assertions.assertTrue(typeTest, "spelNull type test failed");
78-
Assertions.assertTrue(repeatableTest, "spelNull repeatable test failed");
79-
}
80-
81-
@Test
82-
void testSpelSize() {
83-
boolean paramTest = ValidateUtil.checkConstraintResult(SpelSizeTestBean.paramTestCase());
84-
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelSizeTestBean.repeatableTestCase());
85-
86-
Assertions.assertTrue(paramTest, "spelSize param test failed");
87-
Assertions.assertTrue(repeatableTest, "spelSize repeatable test failed");
88-
}
89-
90-
@Test
91-
void testSpelMin() {
92-
boolean paramTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.paramTestCase());
93-
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.repeatableTestCase());
94-
boolean paramTest2 = ValidateUtil.checkConstraintResult(SpelMinTestBean.paramTest2Case());
95-
boolean valueTypeTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.valueTypeTestCase());
96-
boolean notSupportTypeTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.notSupportTypeTestCase());
97-
98-
Assertions.assertTrue(paramTest, "spelMin param test failed");
99-
Assertions.assertTrue(repeatableTest, "spelMin repeatable test failed");
100-
Assertions.assertTrue(paramTest2, "spelMin param test2 failed");
101-
Assertions.assertTrue(valueTypeTest, "spelMin valueType test failed");
102-
Assertions.assertTrue(notSupportTypeTest, "spelMin notSupportType test failed");
103-
}
104-
105-
@Test
106-
void testSpelMax() {
107-
boolean paramTest = ValidateUtil.checkConstraintResult(SpelMaxTestBean.paramTestCase());
108-
109-
Assertions.assertTrue(paramTest, "spelMax param test failed");
110-
}
111-
11236
}

spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/ExampleTestBean.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import cn.sticki.spel.validator.core.constrain.SpelAssert;
44
import cn.sticki.spel.validator.core.constrain.SpelNotNull;
55
import cn.sticki.spel.validator.javax.SpelValid;
6-
import cn.sticki.spel.validator.javax.util.ID;
7-
import cn.sticki.spel.validator.javax.util.VerifyFailedField;
8-
import cn.sticki.spel.validator.javax.util.VerifyObject;
6+
import cn.sticki.spel.validator.test.util.ID;
7+
import cn.sticki.spel.validator.test.util.VerifyFailedField;
8+
import cn.sticki.spel.validator.test.util.VerifyObject;
99
import lombok.Builder;
1010
import lombok.Data;
1111

spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelValidTestBean.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import cn.sticki.spel.validator.core.constrain.SpelNotNull;
44
import cn.sticki.spel.validator.javax.SpelValid;
5-
import cn.sticki.spel.validator.javax.util.ID;
6-
import cn.sticki.spel.validator.javax.util.VerifyFailedField;
7-
import cn.sticki.spel.validator.javax.util.VerifyObject;
5+
import cn.sticki.spel.validator.test.util.ID;
6+
import cn.sticki.spel.validator.test.util.VerifyFailedField;
7+
import cn.sticki.spel.validator.test.util.VerifyObject;
88
import lombok.Builder;
99
import lombok.Data;
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.sticki.spel.validator.javax.util;
2+
3+
import cn.sticki.spel.validator.core.SpelValidExecutor;
4+
import cn.sticki.spel.validator.core.result.FieldError;
5+
import cn.sticki.spel.validator.core.result.ObjectValidResult;
6+
import cn.sticki.spel.validator.javax.SpelValid;
7+
import cn.sticki.spel.validator.test.util.AbstractSpelValidator;
8+
import cn.sticki.spel.validator.test.util.VerifyObject;
9+
import lombok.extern.slf4j.Slf4j;
10+
11+
import javax.validation.ConstraintViolation;
12+
import javax.validation.Validation;
13+
import javax.validation.Validator;
14+
import java.util.List;
15+
import java.util.Set;
16+
import java.util.stream.Collectors;
17+
18+
/**
19+
* 测试验证工具类
20+
*
21+
* @author 阿杆
22+
* @version 1.0
23+
* @since 2024/6/13
24+
*/
25+
@Slf4j
26+
public class JavaxSpelValidator extends AbstractSpelValidator {
27+
28+
private static final JavaxSpelValidator INSTANCE = new JavaxSpelValidator();
29+
30+
@SuppressWarnings("resource")
31+
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
32+
33+
/**
34+
* 验证约束结果是否符合预期
35+
*/
36+
public static boolean check(List<VerifyObject> verifyObjectList) {
37+
return INSTANCE.checkConstraintResult(verifyObjectList);
38+
}
39+
40+
/**
41+
* 参数校验
42+
* <p>
43+
* 调用此方法会触发约束校验
44+
*
45+
* @return 校验结果
46+
*/
47+
@Override
48+
public ObjectValidResult validate(Object obj, String[] spelGroups) {
49+
// 如果对象没有使用 SpelValid 注解,则直接调用验证执行器进行验证
50+
// 这种情况下,只会验证本框架提供的约束注解
51+
if (!obj.getClass().isAnnotationPresent(SpelValid.class)) {
52+
return SpelValidExecutor.validateObject(obj, spelGroups);
53+
}
54+
55+
// 通过 @Valid 的方式进行验证
56+
Set<ConstraintViolation<Object>> validate = validator.validate(obj);
57+
if (validate == null || validate.isEmpty()) {
58+
return ObjectValidResult.EMPTY;
59+
}
60+
ObjectValidResult validResult = new ObjectValidResult();
61+
List<FieldError> list = validate.stream().map(JavaxSpelValidator::convert).collect(Collectors.toList());
62+
validResult.addFieldError(list);
63+
return validResult;
64+
}
65+
66+
private static FieldError convert(ConstraintViolation<Object> violation) {
67+
return FieldError.of(violation.getPropertyPath().toString(), violation.getMessage());
68+
}
69+
70+
}

spel-validator-test-report/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<groupId>cn.sticki</groupId>
3434
<artifactId>spel-validator-javax</artifactId>
3535
</dependency>
36+
37+
<dependency>
38+
<groupId>cn.sticki</groupId>
39+
<artifactId>spel-validator-test</artifactId>
40+
</dependency>
3641
</dependencies>
3742

3843
<build>

spel-validator-test/pom.xml

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>cn.sticki</groupId>
8+
<artifactId>spel-validator-root</artifactId>
9+
<version>0.4.0-beta</version>
10+
</parent>
11+
12+
<artifactId>spel-validator-test</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.sticki</groupId>
17+
<artifactId>spel-validator-core</artifactId>
18+
</dependency>
19+
<!-- ===== for test ===== -->
20+
21+
<dependency>
22+
<groupId>org.junit.jupiter</groupId>
23+
<artifactId>junit-jupiter</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.glassfish</groupId>
29+
<artifactId>javax.el</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>ch.qos.logback</groupId>
35+
<artifactId>logback-classic</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<!-- <dependency> -->
39+
<!-- <groupId>org.hibernate.validator</groupId> -->
40+
<!-- <artifactId>hibernate-validator</artifactId> -->
41+
<!-- </dependency> -->
42+
43+
<!-- ===== for test ===== -->
44+
45+
<!-- <dependency> -->
46+
<!-- <groupId>org.junit.jupiter</groupId> -->
47+
<!-- <artifactId>junit-jupiter</artifactId> -->
48+
<!-- <scope>test</scope> -->
49+
<!-- </dependency> -->
50+
51+
<!-- <dependency> -->
52+
<!-- <groupId>org.glassfish</groupId> -->
53+
<!-- <artifactId>javax.el</artifactId> -->
54+
<!-- <scope>test</scope> -->
55+
<!-- </dependency> -->
56+
57+
<!-- <dependency> -->
58+
<!-- <groupId>ch.qos.logback</groupId> -->
59+
<!-- <artifactId>logback-classic</artifactId> -->
60+
<!-- <scope>test</scope> -->
61+
<!-- </dependency> -->
62+
</dependencies>
63+
64+
</project>

0 commit comments

Comments
 (0)