Skip to content

Commit

Permalink
test: 转移测试工具代码,转移测试用例
Browse files Browse the repository at this point in the history
由于test模块和core模块的测试用例产生了循环依赖,故转移core模块的测试用例到test模块。
  • Loading branch information
stick-i committed Oct 31, 2024
1 parent de5ec3d commit cb7c5c6
Show file tree
Hide file tree
Showing 31 changed files with 379 additions and 192 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<module>spel-validator-javax</module>
<module>spel-validator-jakarta</module>
<module>spel-validator-test-report</module>
<module>spel-validator-test</module>
</modules>

<name>Spel Validator</name>
Expand Down Expand Up @@ -92,6 +93,11 @@
<artifactId>spel-validator-jakarta</artifactId>
<version>${spel-validator.version}</version>
</dependency>
<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-test</artifactId>
<version>${spel-validator.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
Expand Down
20 changes: 20 additions & 0 deletions spel-validator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</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>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.sticki.spel.validator.core.exception.SpelParserException;
import lombok.extern.slf4j.Slf4j;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
Expand Down Expand Up @@ -59,7 +60,7 @@ private static void init() {
* @return 表达式计算结果。若为基本数据类型,则会自动转为包装类型。
*/
@Nullable
public static Object parse(String expression, Object rootObject) {
public static Object parse(@Language("spel") String expression, Object rootObject) {
try {
log.debug("======> Parse expression [{}]", expression);
Expression parsed = expressionCache.computeIfAbsent(expression, parser::parseExpression);
Expand All @@ -82,7 +83,7 @@ public static Object parse(String expression, Object rootObject) {
* @throws SpelParserException 当表达式计算结果为null或者不是指定类型时抛出
*/
@NotNull
public static <T> T parse(String expression, Object rootObject, Class<T> requiredType) {
public static <T> T parse(@Language("spel") String expression, Object rootObject, Class<T> requiredType) {
Object any = parse(expression, rootObject);
if (any == null) {
throw new SpelParserException("Expression [" + expression + "] calculate result can not be null");
Expand Down
5 changes: 5 additions & 0 deletions spel-validator-javax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

<!-- ===== for test ===== -->

<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-test</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cn.sticki.spel.validator.javax;

import cn.sticki.spel.validator.javax.bean.*;
import cn.sticki.spel.validator.javax.util.ValidateUtil;
import cn.sticki.spel.validator.javax.bean.ExampleTestBean;
import cn.sticki.spel.validator.javax.bean.SpelValidTestBean;
import cn.sticki.spel.validator.javax.util.JavaxSpelValidator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -19,94 +20,17 @@ public class ConstrainTest {
*/
@Test
void testExample() {
boolean verified = ValidateUtil.checkConstraintResult(ExampleTestBean.testCase());
boolean verified = JavaxSpelValidator.check(ExampleTestBean.testCase());
Assertions.assertTrue(verified);

boolean innerTest = ValidateUtil.checkConstraintResult(ExampleTestBean.innerTestCase());
boolean innerTest = JavaxSpelValidator.check(ExampleTestBean.innerTestCase());
Assertions.assertTrue(innerTest);
}

@Test
void testSpelValid() {
boolean verified = ValidateUtil.checkConstraintResult(SpelValidTestBean.paramTestCase());
boolean verified = JavaxSpelValidator.check(SpelValidTestBean.paramTestCase());
Assertions.assertTrue(verified);
}

@Test
void testSpelAssert() {
boolean verified = ValidateUtil.checkConstraintResult(SpelAssertTestBean.paramTestCase());
boolean emptyTest = ValidateUtil.checkConstraintResult(SpelAssertTestBean.emptyTestCase());

Assertions.assertTrue(verified);
Assertions.assertTrue(emptyTest);
}

@Test
void testSpelNotBlank() {
boolean verified = ValidateUtil.checkConstraintResult(SpelNotBlankTestBean.testCase());
Assertions.assertTrue(verified);
}

@Test
void testSpelNotEmpty() {
boolean verifiedParam = ValidateUtil.checkConstraintResult(SpelNotEmptyTestBean.paramTestCase());
boolean verifiedType = ValidateUtil.checkConstraintResult(SpelNotEmptyTestBean.typeTestCase());

Assertions.assertTrue(verifiedParam, "spelNotEmpty param test failed");
Assertions.assertTrue(verifiedType, "spelNotEmpty type test failed");
}

@Test
void testSpelNotNull() {
boolean paramTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.paramTestCase());
boolean typeTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.typeTestCase());
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelNotNullTestBean.repeatableTestCase());

Assertions.assertTrue(paramTest, "spelNotNull param test failed");
Assertions.assertTrue(typeTest, "spelNotNull type test failed");
Assertions.assertTrue(repeatableTest, "spelNotNull repeatable test failed");
}

@Test
void testSpelNull() {
boolean paramTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.paramTestCase());
boolean typeTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.typeTestCase());
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelNullTestBean.repeatableTestCase());

Assertions.assertTrue(paramTest, "spelNull param test failed");
Assertions.assertTrue(typeTest, "spelNull type test failed");
Assertions.assertTrue(repeatableTest, "spelNull repeatable test failed");
}

@Test
void testSpelSize() {
boolean paramTest = ValidateUtil.checkConstraintResult(SpelSizeTestBean.paramTestCase());
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelSizeTestBean.repeatableTestCase());

Assertions.assertTrue(paramTest, "spelSize param test failed");
Assertions.assertTrue(repeatableTest, "spelSize repeatable test failed");
}

@Test
void testSpelMin() {
boolean paramTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.paramTestCase());
boolean repeatableTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.repeatableTestCase());
boolean paramTest2 = ValidateUtil.checkConstraintResult(SpelMinTestBean.paramTest2Case());
boolean valueTypeTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.valueTypeTestCase());
boolean notSupportTypeTest = ValidateUtil.checkConstraintResult(SpelMinTestBean.notSupportTypeTestCase());

Assertions.assertTrue(paramTest, "spelMin param test failed");
Assertions.assertTrue(repeatableTest, "spelMin repeatable test failed");
Assertions.assertTrue(paramTest2, "spelMin param test2 failed");
Assertions.assertTrue(valueTypeTest, "spelMin valueType test failed");
Assertions.assertTrue(notSupportTypeTest, "spelMin notSupportType test failed");
}

@Test
void testSpelMax() {
boolean paramTest = ValidateUtil.checkConstraintResult(SpelMaxTestBean.paramTestCase());

Assertions.assertTrue(paramTest, "spelMax param test failed");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import cn.sticki.spel.validator.core.constrain.SpelAssert;
import cn.sticki.spel.validator.core.constrain.SpelNotNull;
import cn.sticki.spel.validator.javax.SpelValid;
import cn.sticki.spel.validator.javax.util.ID;
import cn.sticki.spel.validator.javax.util.VerifyFailedField;
import cn.sticki.spel.validator.javax.util.VerifyObject;
import cn.sticki.spel.validator.test.util.ID;
import cn.sticki.spel.validator.test.util.VerifyFailedField;
import cn.sticki.spel.validator.test.util.VerifyObject;
import lombok.Builder;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import cn.sticki.spel.validator.core.constrain.SpelNotNull;
import cn.sticki.spel.validator.javax.SpelValid;
import cn.sticki.spel.validator.javax.util.ID;
import cn.sticki.spel.validator.javax.util.VerifyFailedField;
import cn.sticki.spel.validator.javax.util.VerifyObject;
import cn.sticki.spel.validator.test.util.ID;
import cn.sticki.spel.validator.test.util.VerifyFailedField;
import cn.sticki.spel.validator.test.util.VerifyObject;
import lombok.Builder;
import lombok.Data;

Expand Down
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());
}

}
5 changes: 5 additions & 0 deletions spel-validator-test-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-javax</artifactId>
</dependency>

<dependency>
<groupId>cn.sticki</groupId>
<artifactId>spel-validator-test</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
64 changes: 64 additions & 0 deletions spel-validator-test/pom.xml
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>
Loading

0 comments on commit cb7c5c6

Please sign in to comment.