-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
由于test模块和core模块的测试用例产生了循环依赖,故转移core模块的测试用例到test模块。
- Loading branch information
Showing
31 changed files
with
379 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/JavaxSpelValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cn.sticki.spel.validator.javax.util; | ||
|
||
import cn.sticki.spel.validator.core.SpelValidExecutor; | ||
import cn.sticki.spel.validator.core.result.FieldError; | ||
import cn.sticki.spel.validator.core.result.ObjectValidResult; | ||
import cn.sticki.spel.validator.javax.SpelValid; | ||
import cn.sticki.spel.validator.test.util.AbstractSpelValidator; | ||
import cn.sticki.spel.validator.test.util.VerifyObject; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* 测试验证工具类 | ||
* | ||
* @author 阿杆 | ||
* @version 1.0 | ||
* @since 2024/6/13 | ||
*/ | ||
@Slf4j | ||
public class JavaxSpelValidator extends AbstractSpelValidator { | ||
|
||
private static final JavaxSpelValidator INSTANCE = new JavaxSpelValidator(); | ||
|
||
@SuppressWarnings("resource") | ||
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); | ||
|
||
/** | ||
* 验证约束结果是否符合预期 | ||
*/ | ||
public static boolean check(List<VerifyObject> verifyObjectList) { | ||
return INSTANCE.checkConstraintResult(verifyObjectList); | ||
} | ||
|
||
/** | ||
* 参数校验 | ||
* <p> | ||
* 调用此方法会触发约束校验 | ||
* | ||
* @return 校验结果 | ||
*/ | ||
@Override | ||
public ObjectValidResult validate(Object obj, String[] spelGroups) { | ||
// 如果对象没有使用 SpelValid 注解,则直接调用验证执行器进行验证 | ||
// 这种情况下,只会验证本框架提供的约束注解 | ||
if (!obj.getClass().isAnnotationPresent(SpelValid.class)) { | ||
return SpelValidExecutor.validateObject(obj, spelGroups); | ||
} | ||
|
||
// 通过 @Valid 的方式进行验证 | ||
Set<ConstraintViolation<Object>> validate = validator.validate(obj); | ||
if (validate == null || validate.isEmpty()) { | ||
return ObjectValidResult.EMPTY; | ||
} | ||
ObjectValidResult validResult = new ObjectValidResult(); | ||
List<FieldError> list = validate.stream().map(JavaxSpelValidator::convert).collect(Collectors.toList()); | ||
validResult.addFieldError(list); | ||
return validResult; | ||
} | ||
|
||
private static FieldError convert(ConstraintViolation<Object> violation) { | ||
return FieldError.of(violation.getPropertyPath().toString(), violation.getMessage()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>cn.sticki</groupId> | ||
<artifactId>spel-validator-root</artifactId> | ||
<version>0.4.0-beta</version> | ||
</parent> | ||
|
||
<artifactId>spel-validator-test</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>cn.sticki</groupId> | ||
<artifactId>spel-validator-core</artifactId> | ||
</dependency> | ||
<!-- ===== for test ===== --> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.glassfish</groupId> | ||
<artifactId>javax.el</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- <dependency> --> | ||
<!-- <groupId>org.hibernate.validator</groupId> --> | ||
<!-- <artifactId>hibernate-validator</artifactId> --> | ||
<!-- </dependency> --> | ||
|
||
<!-- ===== for test ===== --> | ||
|
||
<!-- <dependency> --> | ||
<!-- <groupId>org.junit.jupiter</groupId> --> | ||
<!-- <artifactId>junit-jupiter</artifactId> --> | ||
<!-- <scope>test</scope> --> | ||
<!-- </dependency> --> | ||
|
||
<!-- <dependency> --> | ||
<!-- <groupId>org.glassfish</groupId> --> | ||
<!-- <artifactId>javax.el</artifactId> --> | ||
<!-- <scope>test</scope> --> | ||
<!-- </dependency> --> | ||
|
||
<!-- <dependency> --> | ||
<!-- <groupId>ch.qos.logback</groupId> --> | ||
<!-- <artifactId>logback-classic</artifactId> --> | ||
<!-- <scope>test</scope> --> | ||
<!-- </dependency> --> | ||
</dependencies> | ||
|
||
</project> |
Oops, something went wrong.