zhouhao 6 年 前
コミット
014b06a5f1

+ 18 - 0
hsweb-commons/hsweb-commons-bean/pom.xml

@@ -30,5 +30,23 @@
             <artifactId>spring-boot-starter</artifactId>
             <optional>true</optional>
         </dependency>
+
+        <dependency>
+            <groupId>org.spockframework</groupId>
+            <artifactId>spock-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.athaydes</groupId>
+            <artifactId>spock-reports</artifactId>
+            <version>1.2.13</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.glassfish</groupId>
+            <artifactId>javax.el</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

+ 43 - 0
hsweb-commons/hsweb-commons-bean/src/test/groovy/org/hswebframework/web/commons/bean/BeanValidatorTest.groovy

@@ -0,0 +1,43 @@
+package org.hswebframework.web.commons.bean
+
+import org.hibernate.validator.constraints.NotBlank
+import org.hswebframework.web.validator.group.CreateGroup
+import org.hswebframework.web.validator.group.UpdateGroup
+
+/**
+ * @author zhouhao
+ * @since 3.0.2
+ */
+class BeanValidatorTest extends spock.lang.Specification {
+
+    def "测试初始化验证器"() {
+        given: "初始化"
+        def validator = BeanValidator.getValidator();
+        expect: "成功"
+        null != validator
+    }
+
+
+    def doValidate(TestBean bean, Class group) {
+        try {
+            bean.tryValidate(group);
+            return null;
+        } catch (Exception e) {
+            return e.message;
+        }
+    }
+
+    def "测试group验证"() {
+        expect: "验证多个group"
+        doValidate(new TestBean(name: name), group as Class) == message
+        where:
+        name | group             | message
+        null | CreateGroup.class | "姓名不能为空"
+        ""   | CreateGroup.class | "姓名不能为空"
+        null | UpdateGroup.class | null
+        ""   | UpdateGroup.class | "长度必须在2-20之间"
+        "张三" | UpdateGroup.class | null
+    }
+
+
+}

+ 20 - 0
hsweb-commons/hsweb-commons-bean/src/test/groovy/org/hswebframework/web/commons/bean/TestBean.java

@@ -0,0 +1,20 @@
+package org.hswebframework.web.commons.bean;
+
+import lombok.Data;
+import org.hibernate.validator.constraints.Length;
+import org.hibernate.validator.constraints.NotBlank;
+import org.hswebframework.web.validator.group.CreateGroup;
+import org.hswebframework.web.validator.group.UpdateGroup;
+
+/**
+ * @author zhouhao
+ * @since 3.0.2
+ */
+@Data
+public class TestBean implements ValidateBean {
+
+    @NotBlank(groups = CreateGroup.class, message = "姓名不能为空")
+    @Length(min = 2, max = 20, message = "长度必须在2-20之间", groups = UpdateGroup.class)
+    private String name;
+
+}

+ 2 - 1
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/test/resources/application.yml

@@ -22,4 +22,5 @@ spring:
       hibernate:
         ddl-auto: update
 mybatis:
-  mapper-locations: org/hswebframework/web/dao/test/*.xml
+  mapper-locations: org/hswebframework/web/dao/test/*.xml
+  dynamic-datasource: true