diff --git a/pom.xml b/pom.xml index b86c59d..06d242d 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ spel-validator-javax spel-validator-jakarta spel-validator-test-report + spel-validator-test Spel Validator @@ -92,6 +93,11 @@ spel-validator-jakarta ${spel-validator.version} + + cn.sticki + spel-validator-test + ${spel-validator.version} + org.springframework spring-context diff --git a/spel-validator-core/pom.xml b/spel-validator-core/pom.xml index 42ed2b4..7389a47 100644 --- a/spel-validator-core/pom.xml +++ b/spel-validator-core/pom.xml @@ -37,6 +37,26 @@ org.slf4j slf4j-api + + + + + org.junit.jupiter + junit-jupiter + test + + + + org.glassfish + javax.el + test + + + + ch.qos.logback + logback-classic + test + \ No newline at end of file diff --git a/spel-validator-core/src/main/java/cn/sticki/spel/validator/core/parse/SpelParser.java b/spel-validator-core/src/main/java/cn/sticki/spel/validator/core/parse/SpelParser.java index 9371d63..e44d9b8 100644 --- a/spel-validator-core/src/main/java/cn/sticki/spel/validator/core/parse/SpelParser.java +++ b/spel-validator-core/src/main/java/cn/sticki/spel/validator/core/parse/SpelParser.java @@ -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; @@ -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); @@ -82,7 +83,7 @@ public static Object parse(String expression, Object rootObject) { * @throws SpelParserException 当表达式计算结果为null或者不是指定类型时抛出 */ @NotNull - public static T parse(String expression, Object rootObject, Class requiredType) { + public static T parse(@Language("spel") String expression, Object rootObject, Class requiredType) { Object any = parse(expression, rootObject); if (any == null) { throw new SpelParserException("Expression [" + expression + "] calculate result can not be null"); diff --git a/spel-validator-javax/pom.xml b/spel-validator-javax/pom.xml index 0b92bda..9548b2f 100644 --- a/spel-validator-javax/pom.xml +++ b/spel-validator-javax/pom.xml @@ -30,6 +30,11 @@ + + cn.sticki + spel-validator-test + + org.junit.jupiter junit-jupiter diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/ConstrainTest.java b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/ConstrainTest.java index 659cb4a..b410856 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/ConstrainTest.java +++ b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/ConstrainTest.java @@ -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; @@ -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"); - } - } diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/ExampleTestBean.java b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/ExampleTestBean.java index 31a6c7e..2c857f6 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/ExampleTestBean.java +++ b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/ExampleTestBean.java @@ -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; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelValidTestBean.java b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelValidTestBean.java index 0e057b2..c0ddd35 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelValidTestBean.java +++ b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelValidTestBean.java @@ -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; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/JavaxSpelValidator.java b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/JavaxSpelValidator.java new file mode 100644 index 0000000..4728d80 --- /dev/null +++ b/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/JavaxSpelValidator.java @@ -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 verifyObjectList) { + return INSTANCE.checkConstraintResult(verifyObjectList); + } + + /** + * 参数校验 + *

+ * 调用此方法会触发约束校验 + * + * @return 校验结果 + */ + @Override + public ObjectValidResult validate(Object obj, String[] spelGroups) { + // 如果对象没有使用 SpelValid 注解,则直接调用验证执行器进行验证 + // 这种情况下,只会验证本框架提供的约束注解 + if (!obj.getClass().isAnnotationPresent(SpelValid.class)) { + return SpelValidExecutor.validateObject(obj, spelGroups); + } + + // 通过 @Valid 的方式进行验证 + Set> validate = validator.validate(obj); + if (validate == null || validate.isEmpty()) { + return ObjectValidResult.EMPTY; + } + ObjectValidResult validResult = new ObjectValidResult(); + List list = validate.stream().map(JavaxSpelValidator::convert).collect(Collectors.toList()); + validResult.addFieldError(list); + return validResult; + } + + private static FieldError convert(ConstraintViolation violation) { + return FieldError.of(violation.getPropertyPath().toString(), violation.getMessage()); + } + +} diff --git a/spel-validator-test-report/pom.xml b/spel-validator-test-report/pom.xml index a877bfb..db734cf 100644 --- a/spel-validator-test-report/pom.xml +++ b/spel-validator-test-report/pom.xml @@ -33,6 +33,11 @@ cn.sticki spel-validator-javax + + + cn.sticki + spel-validator-test + diff --git a/spel-validator-test/pom.xml b/spel-validator-test/pom.xml new file mode 100644 index 0000000..9d06652 --- /dev/null +++ b/spel-validator-test/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + cn.sticki + spel-validator-root + 0.4.0-beta + + + spel-validator-test + + + + cn.sticki + spel-validator-core + + + + + org.junit.jupiter + junit-jupiter + test + + + + org.glassfish + javax.el + test + + + + ch.qos.logback + logback-classic + test + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ValidateUtil.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/AbstractSpelValidator.java similarity index 67% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ValidateUtil.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/AbstractSpelValidator.java index 8d38ed1..f12b926 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ValidateUtil.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/AbstractSpelValidator.java @@ -1,65 +1,37 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.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 lombok.extern.slf4j.Slf4j; -import javax.validation.ConstraintViolation; -import javax.validation.Validation; -import javax.validation.Validator; import java.util.Collection; import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; /** - * 测试验证工具类 + * 测试验证工具抽象类 * * @author 阿杆 * @version 1.0 * @since 2024/6/13 */ @Slf4j -public class ValidateUtil { - - @SuppressWarnings("resource") - private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); +public abstract class AbstractSpelValidator { /** * 参数校验 *

* 调用此方法会触发约束校验 * + * @param obj 待验证对象 + * @param spelGroups spel 分组参数 * @return 校验结果 */ - public static ObjectValidResult validate(Object obj, String[] spelGroups) { - // 如果对象没有使用 SpelValid 注解,则直接调用验证执行器进行验证 - // 这种情况下,只会验证本框架提供的约束注解 - if (!obj.getClass().isAnnotationPresent(SpelValid.class)) { - return SpelValidExecutor.validateObject(obj, spelGroups); - } - - // 通过 @Valid 的方式进行验证 - Set> validate = validator.validate(obj); - if (validate == null || validate.isEmpty()) { - return ObjectValidResult.EMPTY; - } - ObjectValidResult validResult = new ObjectValidResult(); - List list = validate.stream().map(ValidateUtil::convert).collect(Collectors.toList()); - validResult.addFieldError(list); - return validResult; - } - - private static FieldError convert(ConstraintViolation violation) { - return FieldError.of(violation.getPropertyPath().toString(), violation.getMessage()); - } + public abstract ObjectValidResult validate(Object obj, String[] spelGroups); /** * 验证约束结果是否符合预期 */ - public static boolean checkConstraintResult(List verifyObjectList) { + public boolean checkConstraintResult(List verifyObjectList) { int failCount = 0; for (VerifyObject verifyObject : verifyObjectList) { if (!checkConstraintResult(verifyObject)) { @@ -72,7 +44,7 @@ public static boolean checkConstraintResult(List verifyObjectList) /** * 验证约束结果是否符合预期 */ - public static boolean checkConstraintResult(VerifyObject verifyObject) { + public boolean checkConstraintResult(VerifyObject verifyObject) { Object object = verifyObject.getObject(); String[] spelGroups = verifyObject.getSpelGroups(); Collection verifyFailedFields = verifyObject.getVerifyFailedFields(); @@ -85,7 +57,7 @@ public static boolean checkConstraintResult(VerifyObject verifyObject) { int failCount = 0; try { // 执行约束校验 - ObjectValidResult validResult = ValidateUtil.validate(object, spelGroups); + ObjectValidResult validResult = validate(object, spelGroups); failCount += processVerifyResult(verifyFailedFields, ConstraintViolationSet.of(validResult.getErrors())); } catch (Exception e) { if (expectException) { @@ -120,7 +92,7 @@ public static boolean checkConstraintResult(VerifyObject verifyObject) { * @param violationSet 验证结果 * @return 验证失败的次数 */ - private static int processVerifyResult(Collection verifyFailedFields, ConstraintViolationSet violationSet) { + private int processVerifyResult(Collection verifyFailedFields, ConstraintViolationSet violationSet) { final String fieldNameLogKey = "fieldName"; int failCount = 0; // 检查结果是否符合预期 diff --git a/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BaseSpelValidator.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BaseSpelValidator.java new file mode 100644 index 0000000..714f5c5 --- /dev/null +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BaseSpelValidator.java @@ -0,0 +1,36 @@ +package cn.sticki.spel.validator.test.util; + +import cn.sticki.spel.validator.core.SpelValidExecutor; +import cn.sticki.spel.validator.core.result.ObjectValidResult; + +import java.util.List; + +/** + * 测试验证工具 + * + * @author 阿杆 + * @since 2024/10/31 + */ +public class BaseSpelValidator extends AbstractSpelValidator { + + private static final BaseSpelValidator INSTANCE = new BaseSpelValidator(); + + public static boolean check(List verifyObjectList) { + return INSTANCE.checkConstraintResult(verifyObjectList); + } + + /** + * 参数校验 + *

+ * 调用此方法会触发约束校验 + * + * @param obj 待验证对象 + * @param spelGroups spel 分组参数 + * @return 校验结果 + */ + @Override + public ObjectValidResult validate(Object obj, String[] spelGroups) { + return SpelValidExecutor.validateObject(obj, spelGroups); + } + +} diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/BeanUtil.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BeanUtil.java similarity index 89% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/BeanUtil.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BeanUtil.java index 9cdee1e..359ce67 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/BeanUtil.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/BeanUtil.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; import java.lang.invoke.SerializedLambda; import java.lang.reflect.Method; @@ -12,11 +12,6 @@ */ public class BeanUtil { - public static void main(String[] args) { - String e = getFieldName(Object::getClass); - System.out.println(e); - } - /** * 获取字段名称 */ diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ConstraintViolationSet.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ConstraintViolationSet.java similarity index 98% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ConstraintViolationSet.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ConstraintViolationSet.java index 5fd22b6..29d6514 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ConstraintViolationSet.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ConstraintViolationSet.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; import cn.sticki.spel.validator.core.result.FieldError; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ID.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ID.java similarity index 79% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ID.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ID.java index 441c9c4..c4e83fa 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/ID.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/ID.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; /** * ID接口,用于给测试用例标号,方便查看测试结果 diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/IGetter.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/IGetter.java similarity index 84% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/IGetter.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/IGetter.java index d7ce673..cf1c43a 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/IGetter.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/IGetter.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; /** * getter,用于获取对象的属性值 diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/LogContext.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/LogContext.java similarity index 97% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/LogContext.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/LogContext.java index f8cf489..b31e1e8 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/LogContext.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/LogContext.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyFailedField.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyFailedField.java similarity index 97% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyFailedField.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyFailedField.java index 03f690c..bcea7c2 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyFailedField.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyFailedField.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; import lombok.Data; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyObject.java b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyObject.java similarity index 98% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyObject.java rename to spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyObject.java index 4999e6b..e22734f 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/util/VerifyObject.java +++ b/spel-validator-test/src/main/java/cn/sticki/spel/validator/test/util/VerifyObject.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax.util; +package cn.sticki.spel.validator.test.util; import lombok.Data; import org.intellij.lang.annotations.Language; diff --git a/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/ConstrainTest.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/ConstrainTest.java new file mode 100644 index 0000000..ff6c20c --- /dev/null +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/ConstrainTest.java @@ -0,0 +1,94 @@ +package cn.sticki.spel.validator.core; + +import cn.sticki.spel.validator.core.bean.*; +import cn.sticki.spel.validator.test.util.BaseSpelValidator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * 约束类测试 + * + * @author 阿杆 + * @version 1.0 + * @since 2024/6/15 + */ +public class ConstrainTest { + + @Test + void testSpelAssert() { + boolean verified = BaseSpelValidator.check(SpelAssertTestBean.paramTestCase()); + boolean emptyTest = BaseSpelValidator.check(SpelAssertTestBean.emptyTestCase()); + + Assertions.assertTrue(verified); + Assertions.assertTrue(emptyTest); + } + + @Test + void testSpelNotBlank() { + boolean verified = BaseSpelValidator.check(SpelNotBlankTestBean.testCase()); + Assertions.assertTrue(verified); + } + + @Test + void testSpelNotEmpty() { + boolean verifiedParam = BaseSpelValidator.check(SpelNotEmptyTestBean.paramTestCase()); + boolean verifiedType = BaseSpelValidator.check(SpelNotEmptyTestBean.typeTestCase()); + + Assertions.assertTrue(verifiedParam, "spelNotEmpty param test failed"); + Assertions.assertTrue(verifiedType, "spelNotEmpty type test failed"); + } + + @Test + void testSpelNotNull() { + boolean paramTest = BaseSpelValidator.check(SpelNotNullTestBean.paramTestCase()); + boolean typeTest = BaseSpelValidator.check(SpelNotNullTestBean.typeTestCase()); + boolean repeatableTest = BaseSpelValidator.check(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 = BaseSpelValidator.check(SpelNullTestBean.paramTestCase()); + boolean typeTest = BaseSpelValidator.check(SpelNullTestBean.typeTestCase()); + boolean repeatableTest = BaseSpelValidator.check(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 = BaseSpelValidator.check(SpelSizeTestBean.paramTestCase()); + boolean repeatableTest = BaseSpelValidator.check(SpelSizeTestBean.repeatableTestCase()); + + Assertions.assertTrue(paramTest, "spelSize param test failed"); + Assertions.assertTrue(repeatableTest, "spelSize repeatable test failed"); + } + + @Test + void testSpelMin() { + boolean paramTest = BaseSpelValidator.check(SpelMinTestBean.paramTestCase()); + boolean repeatableTest = BaseSpelValidator.check(SpelMinTestBean.repeatableTestCase()); + boolean paramTest2 = BaseSpelValidator.check(SpelMinTestBean.paramTest2Case()); + boolean valueTypeTest = BaseSpelValidator.check(SpelMinTestBean.valueTypeTestCase()); + boolean notSupportTypeTest = BaseSpelValidator.check(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 = BaseSpelValidator.check(SpelMaxTestBean.paramTestCase()); + + Assertions.assertTrue(paramTest, "spelMax param test failed"); + } + +} diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/OtherTest.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/OtherTest.java similarity index 90% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/OtherTest.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/OtherTest.java index 68472da..490c2c9 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/OtherTest.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/OtherTest.java @@ -1,5 +1,6 @@ -package cn.sticki.spel.validator.javax; +package cn.sticki.spel.validator.core; +import cn.sticki.spel.validator.core.constrain.SpelAssert; import cn.sticki.spel.validator.core.exception.SpelArgumentException; import cn.sticki.spel.validator.core.exception.SpelParserException; import cn.sticki.spel.validator.core.manager.AnnotationMethodManager; @@ -17,7 +18,7 @@ import java.util.OptionalInt; /** - * 其他测试,无实际意义,用于提高代码覆盖率 + * 一些边边角角的测试 * * @author 阿杆 * @version 1.0 @@ -33,8 +34,11 @@ void testException() { @Test void testAnnotationMethodManager() { - Method abc = AnnotationMethodManager.get(SpelValid.class, "abc"); + Method abc = AnnotationMethodManager.get(SpelAssert.class, "abc"); Assertions.assertNull(abc); + + Method assertTrue = AnnotationMethodManager.get(SpelAssert.class, "assertTrue"); + Assertions.assertNotNull(assertTrue); } @Test diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/SpelParserTest.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/SpelParserTest.java similarity index 98% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/SpelParserTest.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/SpelParserTest.java index 4d0ee98..5cdfa98 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/SpelParserTest.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/SpelParserTest.java @@ -1,4 +1,4 @@ -package cn.sticki.spel.validator.javax; +package cn.sticki.spel.validator.core; import cn.sticki.spel.validator.core.exception.SpelParserException; import cn.sticki.spel.validator.core.parse.SpelParser; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelAssertTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelAssertTestBean.java similarity index 93% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelAssertTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelAssertTestBean.java index d6560ed..48bc7f1 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelAssertTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelAssertTestBean.java @@ -1,9 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelAssert; -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.Data; import java.util.ArrayList; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMaxTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMaxTestBean.java similarity index 87% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMaxTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMaxTestBean.java index f12bf85..2f6005c 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMaxTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMaxTestBean.java @@ -1,9 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelMax; -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; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMinTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMinTestBean.java similarity index 99% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMinTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMinTestBean.java index 52650f4..c01fe05 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelMinTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelMinTestBean.java @@ -1,9 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelMin; -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; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotBlankTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotBlankTestBean.java similarity index 86% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotBlankTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotBlankTestBean.java index d37a579..8e823a4 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotBlankTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotBlankTestBean.java @@ -1,9 +1,8 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelNotBlank; -import cn.sticki.spel.validator.javax.SpelValid; -import cn.sticki.spel.validator.javax.util.VerifyFailedField; -import cn.sticki.spel.validator.javax.util.VerifyObject; +import cn.sticki.spel.validator.test.util.VerifyFailedField; +import cn.sticki.spel.validator.test.util.VerifyObject; import lombok.Data; import java.util.ArrayList; @@ -17,7 +16,6 @@ * @since 2024/6/17 */ @Data -@SpelValid public class SpelNotBlankTestBean { @SpelNotBlank @@ -60,7 +58,6 @@ public static List testCase() { } @Data - @SpelValid public static class TestBean2 { @SpelNotBlank diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotEmptyTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotEmptyTestBean.java similarity index 92% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotEmptyTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotEmptyTestBean.java index 2417b2f..681e52a 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotEmptyTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotEmptyTestBean.java @@ -1,10 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelNotEmpty; -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; @@ -24,7 +23,6 @@ public class SpelNotEmptyTestBean { */ @Data @Builder - @SpelValid public static class ParamTestBean implements ID { private int id; @@ -85,7 +83,6 @@ public static List paramTestCase() { */ @Data @Builder - @SpelValid static class TypeTestBean { @SpelNotEmpty diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotNullTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotNullTestBean.java similarity index 93% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotNullTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotNullTestBean.java index 7c4b035..dd0803b 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNotNullTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNotNullTestBean.java @@ -1,10 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; 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; @@ -25,7 +24,7 @@ public class SpelNotNullTestBean { */ @Data @Builder - @SpelValid + // @SpelValid public static class ParamTestBean implements ID { private int id; @@ -85,7 +84,7 @@ public static List paramTestCase() { */ @Data @Builder - @SpelValid + // @SpelValid static class TypeTestBean { @SpelNotNull @@ -122,7 +121,7 @@ public static List typeTestCase() { */ @Data @Builder - @SpelValid + // @SpelValid static class RepeatableTestBean { boolean condition1; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNullTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNullTestBean.java similarity index 93% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNullTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNullTestBean.java index 897d75e..bb0007d 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelNullTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelNullTestBean.java @@ -1,10 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelNull; -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; @@ -25,7 +24,7 @@ public class SpelNullTestBean { */ @Data @Builder - @SpelValid + // @SpelValid public static class ParamTestBean implements ID { private int id; @@ -80,7 +79,7 @@ public static List paramTestCase() { */ @Data @Builder - @SpelValid + // @SpelValid static class TypeTestBean { @SpelNull @@ -116,7 +115,7 @@ public static List typeTestCase() { */ @Data @Builder - @SpelValid + // @SpelValid static class RepeatableTestBean { boolean condition1; diff --git a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelSizeTestBean.java b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelSizeTestBean.java similarity index 96% rename from spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelSizeTestBean.java rename to spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelSizeTestBean.java index 8c7597c..d71731c 100644 --- a/spel-validator-javax/src/test/java/cn/sticki/spel/validator/javax/bean/SpelSizeTestBean.java +++ b/spel-validator-test/src/test/java/cn/sticki/spel/validator/core/bean/SpelSizeTestBean.java @@ -1,10 +1,9 @@ -package cn.sticki.spel.validator.javax.bean; +package cn.sticki.spel.validator.core.bean; import cn.sticki.spel.validator.core.constrain.SpelSize; -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; @@ -24,7 +23,7 @@ public class SpelSizeTestBean { */ @Data @Builder - @SpelValid + // @SpelValid public static class ParamTestBean implements ID { private int id; @@ -149,7 +148,7 @@ public static List paramTestCase() { */ @Data @Builder - @SpelValid + // @SpelValid public static class RepeatableTestBean implements ID { private int id; diff --git a/spel-validator-core/src/test/resources/logback-test.xml b/spel-validator-test/src/test/resources/logback-test.xml similarity index 100% rename from spel-validator-core/src/test/resources/logback-test.xml rename to spel-validator-test/src/test/resources/logback-test.xml