例子参考自官方文档,http://oval.sourceforge.net/u... 官方文档的xml配置有点过期了,一两个属性不合法。
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─honey
│ │ │ ├─collection
│ │ │ └─oval
│ │ └─resources
User
bean类,
package com.honey.oval;
public class User {
private String firstName;
private String lastName;
private String managerId;
private String userId;
public User() {
}
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void setPasswordExpirationDays(int days) {
}
}
OvalAppTest
测试类,
package com.honey;
import com.honey.oval.User;
import net.sf.oval.ConstraintViolation;
import net.sf.oval.configuration.xml.XMLConfigurer;
import net.sf.oval.guard.Guard;
import java.util.List;
public class OvalAppTest {
public static void main(String[] args) {
User user = new User("honey", "wang");
XMLConfigurer xmlConfigurer = new XMLConfigurer(ClassLoader.class.getResourceAsStream("/oval-config.xml"));
Guard guard = new Guard(xmlConfigurer);
List<ConstraintViolation> constraintViolationList = guard.validate(user);
System.out.println(constraintViolationList);
}
}
xml配置,src/resources/oval-config.xml
<?xml version="1.0" ?>
<oval
xmlns="http://oval.sf.net/oval-configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://oval.sf.net/oval-configuration http://oval.sourceforge.net/oval-configuration.xsd"
>
<!-- define a constraint set -->
<constraintSet id="user.userid">
<notNull />
<matchPattern matchAll="false">
<pattern pattern="^[a-z0-9]{8}$" flags="0" />
</matchPattern>
</constraintSet>
<!-- define checks for the acme.model.User class -->
<!-- overwrite=false means already defined checks for this class will not be removed -->
<class type="com.honey.oval.User" overwrite="false" applyFieldConstraintsToSetters="true">
<field name="firstName">
<length min="0" max="3" />
</field>
<field name="lastName">
<length min="0" max="5" />
</field>
<!-- overwrite=true means previously defined checks for this field will be overwritten by the checks defined here -->
<field name="managerId" overwrite="true">
<!-- use the checks defined for the constaint set "user.userid" -->
<assertConstraintSet id="user.userid" />
</field>
<field name="userId" overwrite="true">
<!-- use the checks defined for the constaint set "user.userid" -->
<assertConstraintSet id="user.userid" />
</field>
<!-- define constructor parameter checks -->
<constructor>
<!-- parameter1 -->
<parameter type="java.lang.String">
<notNull />
</parameter>
<!-- parameter 2 -->
<!-- the types of all parameters must be listed, even if no checks are defined -->
<parameter type="java.lang.String" />
</constructor>
<!-- define method parameter checks -->
<method name="setPasswordExpirationDays">
<!-- parameter 1 -->
<parameter type="int">
<notNull />
</parameter>
</method>
</class>
</oval>
输出结果,
[net.sf.oval.ConstraintViolation: com.honey.oval.User.managerId cannot be null, net.sf.oval.ConstraintViolation: com.honey.oval.User.firstName is not between 0 and 3 characters long, net.sf.oval.ConstraintViolation: com.honey.oval.User.userId cannot be null]