Aggiunto l'esempio del capitolo sulla Bean Validation
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Address {
|
||||
|
||||
@NotNull
|
||||
private String street1;
|
||||
private String street2;
|
||||
@NotNull
|
||||
private String city;
|
||||
private String state;
|
||||
@NotNull
|
||||
@ZipCode
|
||||
private String zipCode;
|
||||
private String coutry;
|
||||
|
||||
public Address(@NotNull String street1, @NotNull String city, String state,
|
||||
@NotNull String zipCode, String coutry) {
|
||||
this.street1 = street1;
|
||||
this.city = city;
|
||||
this.state = state;
|
||||
this.zipCode = zipCode;
|
||||
this.coutry = coutry;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class BeanValidationMain
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.util.Date;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Past;
|
||||
import javax.validation.constraints.Size;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
@NotNull
|
||||
@Size(min = 2)
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
@Email
|
||||
private String email;
|
||||
private String phoneNumber;
|
||||
@Past
|
||||
private Date dateOfBirth;
|
||||
private Address deliveryAddress;
|
||||
|
||||
public Customer(
|
||||
@NotNull @Size(min = 2) String firstName, String lastName, String email) {
|
||||
this(firstName, lastName, email, null, null, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import javax.validation.ReportAsSingleViolation;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Size(min = 7)
|
||||
@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\."
|
||||
+ "[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
|
||||
+ "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
|
||||
@ReportAsSingleViolation
|
||||
@Constraint(validatedBy = {})
|
||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
|
||||
ElementType.CONSTRUCTOR, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Email {
|
||||
|
||||
String message() default "{it.plague.jeedemo.Email.message}";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
|
||||
ElementType.CONSTRUCTOR, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface List {
|
||||
|
||||
Email[] value();
|
||||
}
|
||||
}
|
||||
14
beanvalidationdemo/src/main/java/it/plague/jeedemo/USA.java
Normal file
14
beanvalidationdemo/src/main/java/it/plague/jeedemo/USA.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.inject.Qualifier;
|
||||
|
||||
@Qualifier
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
|
||||
public @interface USA {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Constraint(validatedBy = ZipCodeValidator.class)
|
||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
|
||||
ElementType.CONSTRUCTOR, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ZipCode {
|
||||
|
||||
String message() default "{it.plague.jeedemo.beanvalidation.ZipCode.message}";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,
|
||||
ElementType.CONSTRUCTOR, ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface List {
|
||||
|
||||
ZipCode[] value();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
public class ZipCodeChecker {
|
||||
|
||||
public boolean isZipCodeValid(String value) {
|
||||
// TODO it's not important for this example, here could you do anything do you want
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.inject.Inject;
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
public class ZipCodeValidator implements ConstraintValidator<ZipCode, String> {
|
||||
|
||||
/**
|
||||
* FIXME
|
||||
* this attribute not work, because <code>@Produce</code>
|
||||
* not defined
|
||||
*/
|
||||
@Inject
|
||||
@USA
|
||||
private ZipCodeChecker checker;
|
||||
|
||||
private Pattern zipPattern = Pattern.compile("\\d{5}(-\\d{5})?");
|
||||
|
||||
@Override
|
||||
public void initialize(ZipCode zipCode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
Matcher m = zipPattern.matcher(value);
|
||||
if (!m.matches()) {
|
||||
return false;
|
||||
}
|
||||
return checker.isZipCodeValid(value);
|
||||
}
|
||||
}
|
||||
6
beanvalidationdemo/src/main/resources/META-INF/beans.xml
Normal file
6
beanvalidationdemo/src/main/resources/META-INF/beans.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/
|
||||
XMLSchema-instance" xsi:schemeLocation="http://java.sun.com/xml/ns/javaee http://
|
||||
java.sun.com/xml/ns/javaee/beans_1_0.xsd" version="1.1" bean-discovery-mode="all">
|
||||
<!-- This is empty on purpose. -->
|
||||
</beans>
|
||||
@@ -0,0 +1 @@
|
||||
it.plague.jeedemo.Email.message=invalid email address
|
||||
@@ -0,0 +1,23 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AddressIT {
|
||||
|
||||
@Test
|
||||
public void shouldRaiseConstraintViolationCauseInvalidZipCode() {
|
||||
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = vf.getValidator();
|
||||
Address address = new Address("233 Spring Street", "New York", "NY", "DUMMY", "USA");
|
||||
Set<ConstraintViolation<Address>> violations = validator.validate(address);
|
||||
assertEquals(1, violations.size());
|
||||
vf.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package it.plague.jeedemo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CustomerIT {
|
||||
|
||||
private static ValidatorFactory vf;
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
vf = Validation.buildDefaultValidatorFactory();
|
||||
validator = vf.getValidator();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void close() {
|
||||
vf.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRaiseNoConstraintViolation() {
|
||||
Customer customer = new Customer("John", "Smith", "jsmith@gmail.com");
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(0, violations.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRaiseConstraintViolationCauseInvalidEmail() {
|
||||
Customer customer = new Customer("John", "Smith", "DummyEmail");
|
||||
Set<ConstraintViolation<Customer>> violations = validator.validate(customer);
|
||||
assertEquals(1, violations.size());
|
||||
assertEquals("invalid email address", violations.iterator().next().getMessage());
|
||||
assertEquals("DummyEmail", violations.iterator().next().getInvalidValue());
|
||||
assertEquals("{it.plague.jeedemo.Email.message}",
|
||||
violations.iterator().next().getMessageTemplate());
|
||||
}
|
||||
}
|
||||
6
beanvalidationdemo/src/test/resources/META-INF/beans.xml
Normal file
6
beanvalidationdemo/src/test/resources/META-INF/beans.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/
|
||||
XMLSchema-instance" xsi:schemeLocation="http://java.sun.com/xml/ns/javaee http://
|
||||
java.sun.com/xml/ns/javaee/beans_1_0.xsd" version="1.1" bean-discovery-mode="all">
|
||||
<!-- This is empty on purpose. -->
|
||||
</beans>
|
||||
Reference in New Issue
Block a user