以上连接的这片文章resources提供了框架类的源代码和使用的一些方法,
没有提供一个实例,看着类文档才写出了下面这个真正的测试实例。
/**
* 使用annotations进行javabean私有属性有效性检查的测试实例
* 参见其api文档(resource部分第一项第二部分)的resources部分的javadoc中
* 使用annotations进行javabean私有属性有效性检查的测试实例
* 参见其api文档(resource部分第一项第二部分)的resources部分的javadoc中
* 的validateobject类
*/
*/
package com.sun.aus.constraints.samples;
import com.sun.aus.constraints.annotation.*;
import com.sun.aus.constraints.annotation.*;
import com.sun.aus.constraints.validation.*;
import java.util.Iterator;
import java.util.Set;
import java.beans.IntrospectionException;
import java.lang.annotation.*;
import com.sun.aus.constraints.annotation.*;
import com.sun.aus.constraints.validation.*;
import java.util.Iterator;
import java.util.Set;
import java.beans.IntrospectionException;
import java.lang.annotation.*;
public class Address {
private String street;
public String getStreet() {
return street;
}
return street;
}
@MinLength(10)
@MaxLength(20)
public void setStreet(String street) {
this.street = street;
}
@MaxLength(20)
public void setStreet(String street) {
this.street = street;
}
public static void main(String args[]) {
String aPossibleValue = "123sdf";
ConstrainedClass addressClass = ConstrainedClass
.getConstrainedClass(Address.class);
try {
PropertyValidationErrors errors = ValidatableObject
.validateProperty(addressClass, "street", aPossibleValue);
if (errors == null) {
System.out.println("valid");
} else {
Iterator ite = errors.getConstraintViolations().iterator();
while (ite.hasNext()) {
ConstraintViolation conv = (ConstraintViolation) ite.next();
System.out.println(conv.getMessage());
}
}
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String aPossibleValue = "123sdf";
ConstrainedClass addressClass = ConstrainedClass
.getConstrainedClass(Address.class);
try {
PropertyValidationErrors errors = ValidatableObject
.validateProperty(addressClass, "street", aPossibleValue);
if (errors == null) {
System.out.println("valid");
} else {
Iterator ite = errors.getConstraintViolations().iterator();
while (ite.hasNext()) {
ConstraintViolation conv = (ConstraintViolation) ite.next();
System.out.println(conv.getMessage());
}
}
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}