修改测试类。

pull/4/head
ZhouXY108 2024-08-07 16:54:20 +08:00
parent 92e142dcf3
commit a71d75029e
1 changed files with 56 additions and 54 deletions

View File

@ -21,18 +21,37 @@ import xyz.zhouxy.plusone.validator.map.MapValidator;
public // public //
class MapValidatorTests { class MapValidatorTests {
private static final String USERNAME = "username"; private static final MapValidator<String, Object> validator = ParamsValidator.INSTANCE;
private static final String ACCOUNT = "account";
private static final String PASSWORD = "password";
private static final String PASSWORD2 = "password2";
private static final String AGE = "age";
private static final String BOOLEAN = "boolean";
private static final String ROLE_LIST = "roleList";
private static final MapValidator<String, Object> validator = new MapValidator<String, Object>( @Test
new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST }) { void testValidateAndCopy() {
{ Map<String, Object> params = new HashMap<>();
params.put(ParamsValidator.USERNAME, "ZhouXY");
params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn");
params.put(ParamsValidator.PASSWORD, "99Code108");
params.put(ParamsValidator.PASSWORD2, "99Code108");
params.put(ParamsValidator.AGE, 18);
params.put(ParamsValidator.BOOLEAN, true);
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", ""));
Map<String, Object> validedParams = validator.validateAndCopy(params);
System.out.println(validedParams);
}
}
class ParamsValidator extends MapValidator<String, Object> {
public static final String USERNAME = "username";
public static final String ACCOUNT = "account";
public static final String PASSWORD = "password";
public static final String PASSWORD2 = "password2";
public static final String AGE = "age";
public static final String BOOLEAN = "boolean";
public static final String ROLE_LIST = "roleList";
public static final ParamsValidator INSTANCE = new ParamsValidator();
private ParamsValidator() {
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
checkValue(USERNAME, String.class).withRule( checkValue(USERNAME, String.class).withRule(
PredicateTools.from(StringUtils::isNotBlank) PredicateTools.from(StringUtils::isNotBlank)
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)), .and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
@ -65,22 +84,5 @@ class MapValidatorTests {
.withRule(CollectionTools::isNotEmpty, "角色列表不能为空!") .withRule(CollectionTools::isNotEmpty, "角色列表不能为空!")
.withRule(l -> l.stream().allMatch(StringUtils::isNotBlank), .withRule(l -> l.stream().allMatch(StringUtils::isNotBlank),
() -> new IllegalArgumentException("角色标识不能为空!")); () -> new IllegalArgumentException("角色标识不能为空!"));
}
};
@Test
void testValidateAndCopy() {
Map<String, Object> params = new HashMap<>();
params.put("username", "ZhouXY");
params.put("account", "zhouxy@code108.cn");
params.put("password", "99Code108");
params.put("password2", "99Code108");
params.put("age", 18);
params.put("boolean", true);
params.put("roleList", Arrays.asList("admin", ""));
Map<String, Object> validedParams = validator.validateAndCopy(params);
System.out.println(validedParams);
} }
} }