Basic examples on cdi and beans validation
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class class_with_builtin_constraints {
|
||||
|
||||
|
||||
|
||||
@NotNull @Size(min = 4, max = 50)
|
||||
private String title;
|
||||
@NotNull
|
||||
private Float price;
|
||||
|
||||
|
||||
@Pattern(regexp = "[A-Z][a-z]{1,}")
|
||||
private String musicCompany;
|
||||
|
||||
@Max(value = 5)
|
||||
private Integer numberOfCDs;
|
||||
|
||||
@NotNull @DecimalMin("5.8")
|
||||
public Float calculatePrice(@DecimalMin("1.4") Float rate) {
|
||||
return price * rate;
|
||||
}
|
||||
@DecimalMin("9.99")
|
||||
public Float calculateVAT() {
|
||||
return price * 0.196f;
|
||||
}
|
||||
|
||||
@AssertTrue
|
||||
public Boolean CheckStringLength (@NotNull String a) {
|
||||
return a.length() > 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
42
test2_constraints_validation/general_class.java
Normal file
42
test2_constraints_validation/general_class.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
|
||||
@general_class_constraint
|
||||
public class general_class {
|
||||
|
||||
|
||||
private String title;
|
||||
|
||||
private Double price;
|
||||
|
||||
@PostConstruct private void ps (){
|
||||
title = "AAA";
|
||||
price = 121.0;
|
||||
}
|
||||
|
||||
public Boolean CheckStringLength ( String a) {
|
||||
return a.length() > 0;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
24
test2_constraints_validation/general_class_constraint.java
Normal file
24
test2_constraints_validation/general_class_constraint.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
|
||||
@Constraint(validatedBy = {general_class_validator.class})
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER,TYPE})
|
||||
@Retention(RUNTIME)
|
||||
public @interface general_class_constraint {
|
||||
|
||||
String message() default "This is a sample of constraint for a class";
|
||||
Class<?>[] groups() default {};
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
23
test2_constraints_validation/general_class_validator.java
Normal file
23
test2_constraints_validation/general_class_validator.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class general_class_validator implements ConstraintValidator<general_class_constraint,general_class> {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize(general_class_constraint general_class_constraint) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(general_class general_class, ConstraintValidatorContext constraintValidatorContext) {
|
||||
if (general_class.getPrice() > 0 && general_class.getTitle().length() > 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
19
test2_constraints_validation/general_class_wrapper.java
Normal file
19
test2_constraints_validation/general_class_wrapper.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class general_class_wrapper {
|
||||
|
||||
@Inject @general_class_constraint
|
||||
private general_class g;
|
||||
|
||||
@PostConstruct
|
||||
private void ps (){
|
||||
g.setPrice(10.0);
|
||||
g.setTitle("Artur");
|
||||
}
|
||||
}
|
||||
15
test2_constraints_validation/substring_class.java
Normal file
15
test2_constraints_validation/substring_class.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class substring_class {
|
||||
|
||||
|
||||
@substring_constraint (xg_attribute_1 = "xgiovio" )
|
||||
String a = "arxgiovio";
|
||||
|
||||
@substring_constraint (xg_attribute_1 = "mario" )
|
||||
String b = "xgarturomario";
|
||||
|
||||
}
|
||||
25
test2_constraints_validation/substring_constraint.java
Normal file
25
test2_constraints_validation/substring_constraint.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
|
||||
@Constraint(validatedBy = {substring_validator.class})
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface substring_constraint {
|
||||
|
||||
String message() default "This string must contain the xg_attribute_1 attribute ";
|
||||
Class<?>[] groups() default {};
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
String xg_attribute_1() default "";
|
||||
}
|
||||
23
test2_constraints_validation/substring_validator.java
Normal file
23
test2_constraints_validation/substring_validator.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class substring_validator implements ConstraintValidator<substring_constraint,String> {
|
||||
|
||||
String xg_attribute_1;
|
||||
|
||||
|
||||
@Override
|
||||
public void initialize(substring_constraint substring_constraint) {
|
||||
this.xg_attribute_1 = substring_constraint.xg_attribute_1();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return s.contains(this.xg_attribute_1);
|
||||
}
|
||||
}
|
||||
15
test2_constraints_validation/xg_substring_class.java
Normal file
15
test2_constraints_validation/xg_substring_class.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class xg_substring_class {
|
||||
|
||||
|
||||
@xg_substring_constraint
|
||||
String a = "arxgiovio";
|
||||
|
||||
@xg_substring_constraint
|
||||
String b = "xgarturo";
|
||||
|
||||
}
|
||||
22
test2_constraints_validation/xg_substring_constraint.java
Normal file
22
test2_constraints_validation/xg_substring_constraint.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.*;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
|
||||
@Constraint(validatedBy = {xg_substring_validator.class})
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
|
||||
@Retention(RUNTIME)
|
||||
public @interface xg_substring_constraint {
|
||||
|
||||
String message() default "This string doesn't contain xg";
|
||||
Class<?>[] groups() default {};
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
20
test2_constraints_validation/xg_substring_validator.java
Normal file
20
test2_constraints_validation/xg_substring_validator.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package test2_constraints_validation;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* Created by Giovanni on 29/04/2015.
|
||||
*/
|
||||
public class xg_substring_validator implements ConstraintValidator<xg_substring_constraint,String> {
|
||||
|
||||
@Override
|
||||
public void initialize(xg_substring_constraint xg_substring_constraint) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
|
||||
return s.contains("xg");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user