|
|
|
@ -11,7 +11,9 @@ import org.hibernate.validator.constraints.Length; |
|
|
|
|
|
|
|
|
|
import javax.validation.constraints.Min; |
|
|
|
|
import javax.validation.constraints.NotBlank; |
|
|
|
|
import javax.validation.constraints.NotNull; |
|
|
|
|
import javax.validation.constraints.Pattern; |
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author tanghc |
|
|
|
@ -24,10 +26,17 @@ public class ValidatorTest extends TestCase { |
|
|
|
|
* 测试JSR-303注解校验顺序,校验顺序: Group1~GroupN |
|
|
|
|
*/ |
|
|
|
|
public void testValidate() { |
|
|
|
|
serviceParamValidator.validateBizParam(new User("1", 1)); |
|
|
|
|
serviceParamValidator.validateBizParam(new User("Jim", 30)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void testField() { |
|
|
|
|
Manager manager = new Manager("Jim", 22); |
|
|
|
|
Store store = new Store("仓库A", manager); |
|
|
|
|
Goods goods = new Goods("Apple", new BigDecimal(50000), store); |
|
|
|
|
serviceParamValidator.validateBizParam(goods); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
private static class User { |
|
|
|
@ -36,7 +45,7 @@ public class ValidatorTest extends TestCase { |
|
|
|
|
@NotBlank(message = "NotBlank", groups = Group1.class) |
|
|
|
|
// 优先校验Group2
|
|
|
|
|
// 可交换下面Group2,Group3,看下校验顺序
|
|
|
|
|
@Length(min = 2, max = 20, message = "length must 10~20", groups = Group2.class) |
|
|
|
|
@Length(min = 2, max = 20, message = "length must 2~20", groups = Group2.class) |
|
|
|
|
@Pattern(regexp = "[a-zA-Z]*", message = "name must letters", groups = Group3.class) |
|
|
|
|
private String name; |
|
|
|
|
|
|
|
|
@ -45,4 +54,36 @@ public class ValidatorTest extends TestCase { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
static class Goods { |
|
|
|
|
@NotBlank(message = "商品名称不能为空") |
|
|
|
|
private String goodsName; |
|
|
|
|
|
|
|
|
|
@Min(value = 1, message = "商品价格最小值为1") |
|
|
|
|
private BigDecimal price; |
|
|
|
|
|
|
|
|
|
@NotNull(message = "仓库不能为空") |
|
|
|
|
private Store store; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
static class Store { |
|
|
|
|
@NotBlank(message = "库存名称不能为空") |
|
|
|
|
private String storeName; |
|
|
|
|
|
|
|
|
|
@NotNull(message = "管理员不能为空") |
|
|
|
|
private Manager manager; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
static class Manager { |
|
|
|
|
@NotBlank(message = "管理员姓名不能为空") |
|
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
private int age; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|