Skip to content

Commit

Permalink
test: 添加注解SpelMax测试用例,添加SpelParser解析器测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
stick-i committed Oct 12, 2024
1 parent 089f6f1 commit 7c9b06e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/main/java/cn/sticki/validator/spel/parse/SpelParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ParseException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

Expand Down Expand Up @@ -59,9 +57,8 @@ public static Object parse(String expression, Object rootObject) {
Object value = parsed.getValue(context, rootObject, Object.class);
log.debug("======> Parse result [{}]", value);
return value;
} catch (ParseException | EvaluationException e) {
log.error("Parse expression error, expression [{}], message [{}]", expression, e.getMessage());
throw new SpelParserException(e);
} catch (RuntimeException e) {
throw new SpelParserException("Parse expression error, expression [" + expression + "], message [" + e.getMessage() + "]", e);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/cn/sticki/validator/spel/ConstrainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ void testSpelMin() {
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");
}
}
47 changes: 47 additions & 0 deletions src/test/java/cn/sticki/validator/spel/SpelParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.sticki.validator.spel;

import cn.sticki.validator.spel.exception.SpelParserException;
import cn.sticki.validator.spel.parse.SpelParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* SpelParser 测试
*
* @author 阿杆
* @version 1.0
* @since 2024/10/12
*/
public class SpelParserTest {

@Test
void testParse() {
Integer num = SpelParser.parse("1+1", null, Integer.class);
Assertions.assertEquals(2, (int) num);

String str = SpelParser.parse("'hello' + 'world'", null, String.class);
Assertions.assertEquals("helloworld", str);

Boolean bool = SpelParser.parse("true && false", null, Boolean.class);
Assertions.assertFalse(bool);

Double decimal = SpelParser.parse("1.1 + 2.2", null, Double.class);
Assertions.assertEquals(3.3, decimal, 0.0001);

// ----- 异常情况 ------

// 表达式结果为空
Integer nullValue = null;
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("#this", nullValue, Integer.class));

// 表达式计算结果类型不匹配
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("1+1", null, String.class));

// 表达式计算异常
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("1/0", null));

// 表达式解析异常
Assertions.assertThrows(SpelParserException.class, () -> SpelParser.parse("#this.aaa", null));
}

}
73 changes: 73 additions & 0 deletions src/test/java/cn/sticki/validator/spel/bean/SpelMaxTestBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cn.sticki.validator.spel.bean;

import cn.sticki.validator.spel.SpelValid;
import cn.sticki.validator.spel.VerifyFailedField;
import cn.sticki.validator.spel.VerifyObject;
import cn.sticki.validator.spel.constrain.SpelMax;
import cn.sticki.validator.spel.util.ID;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
* SpelMax 测试用例
* <p>
* 由于 SpelMax 和 SpelMax 的实现方式相同,故SpelMax仅进行简单测试
*
* @author 阿杆
* @version 1.0
* @since 2024/10/12
*/
public class SpelMaxTestBean {

/**
* 参数测试
*/
@Data
@Builder
@SpelValid
public static class ParamTestBean implements ID {

private int id;

private int max;

@SpelMax(value = "#this.max")
private Long testLong;

}

/**
* 参数测试
*/
public static List<VerifyObject> paramTestCase() {
ArrayList<VerifyObject> result = new ArrayList<>();

// null
result.add(VerifyObject.of(
ParamTestBean.builder().id(1).max(1).build(),
VerifyFailedField.of()
));

// 大于最大值
result.add(VerifyObject.of(
ParamTestBean.builder().id(2).max(5).testLong(6L).build(),
VerifyFailedField.of(SpelMaxTestBean.ParamTestBean::getTestLong)
));

// 等于最大值
result.add(VerifyObject.of(
ParamTestBean.builder().id(3).max(5).testLong(5L).build()
));

// 小于最大值
result.add(VerifyObject.of(
ParamTestBean.builder().id(4).max(5).testLong(-123L).build()
));

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.List;

/**
* 功能:SpelMin 测试用例
* SpelMin 测试用例
*
* @author oddfar、阿杆
* @since 2024/8/25
Expand Down

0 comments on commit 7c9b06e

Please sign in to comment.