1. Micha Kops
  2. bean-validation-tutorial

Commits

Micha Kops  committed 18bfe0a

initial import

  • Participants
  • Branches default

Comments (0)

Files changed (6)

File pom.xml

View file
  • Ignore whitespace
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>bean-validation-examples</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+
+	<dependencies>
+		<dependency>
+			<groupId>javax.validation</groupId>
+			<artifactId>validation-api</artifactId>
+			<version>1.0.0.GA</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-validator</artifactId>
+			<version>4.0.2.GA</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-api</artifactId>
+			<version>1.6.1</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+			<version>1.6.1</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.8.2</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.0.2</version>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+	<repositories>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2</url>
+		</repository>
+		<repository>
+			<id>JBoss repository</id>
+			<url>http://repository.jboss.com/maven2/</url>
+		</repository>
+	</repositories>
+</project>

File src/main/java/com/hascode/tutorial/bean/UserBean.java

View file
  • Ignore whitespace
+package com.hascode.tutorial.bean;
+
+import java.util.Date;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Past;
+import javax.validation.constraints.Size;
+
+import com.hascode.tutorial.constraint.CustomerNumber;
+
+public class UserBean {
+	@NotNull
+	@Size(min = 2)
+	private String	firstName;
+
+	@NotNull
+	@Size(min = 2)
+	private String	lastName;
+
+	@NotNull
+	@Past
+	private Date	birthday;
+
+	@NotNull
+	@CustomerNumber
+	private String	customerNumber;
+
+	public String getFirstName() {
+		return firstName;
+	}
+
+	public void setFirstName(String firstName) {
+		this.firstName = firstName;
+	}
+
+	public String getLastName() {
+		return lastName;
+	}
+
+	public void setLastName(String lastName) {
+		this.lastName = lastName;
+	}
+
+	public Date getBirthday() {
+		return birthday;
+	}
+
+	public void setBirthday(Date birthday) {
+		this.birthday = birthday;
+	}
+
+	public String getCustomerNumber() {
+		return customerNumber;
+	}
+
+	public void setCustomerNumber(String customerNumber) {
+		this.customerNumber = customerNumber;
+	}
+}

File src/main/java/com/hascode/tutorial/constraint/CustomerNumber.java

View file
  • Ignore whitespace
+package com.hascode.tutorial.constraint;
+
+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;
+
+@Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = CustomerNumberValidator.class)
+public @interface CustomerNumber {
+	String message() default "The given customer number is invalid";
+
+	Class<?>[] groups() default {};
+
+	Class<? extends Payload>[] payload() default {};
+
+}

File src/main/java/com/hascode/tutorial/constraint/CustomerNumberValidator.java

View file
  • Ignore whitespace
+package com.hascode.tutorial.constraint;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class CustomerNumberValidator implements ConstraintValidator<CustomerNumber, String> {
+
+	@Override
+	public void initialize(CustomerNumber number) {
+	}
+
+	@Override
+	public boolean isValid(String customerNumber, ConstraintValidatorContext ctx) {
+		if (customerNumber == null)
+			return false;
+
+		if (customerNumber.matches("^[a-zA-Z]{2}-\\d+$"))
+			return true;
+		return false;
+	}
+
+}

File src/main/resources/messages.properties

View file
  • Ignore whitespace
+com.hascode.tutorial.validation.customernumber.invalid=The given customer number is invalid

File src/test/java/com/hascode/tutorial/bean/UserBeanTest.java

View file
  • Ignore whitespace
+package com.hascode.tutorial.bean;
+
+import java.util.Date;
+import java.util.Set;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+
+import junit.framework.Assert;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class UserBeanTest {
+	private static Validator	validator;
+
+	@BeforeClass
+	public static void setUp() {
+		validator = Validation.buildDefaultValidatorFactory().getValidator();
+	}
+
+	@Test
+	public void testCreateUser() {
+		UserBean user = new UserBean();
+		Set<ConstraintViolation<UserBean>> violations = validator.validate(user);
+		Assert.assertEquals(5, violations.size()); // 5x
+													// "must not be null error"
+		user.setFirstName("Howard Phillips");
+		user.setLastName("Lovecraft");
+		user.setBirthday(new Date(600000000));
+		user.setCustomerNumber("XXX");
+		violations = validator.validate(user);
+		/**
+		 * birthday is not a past date and customer number does not match the
+		 * specified format
+		 */
+		Assert.assertTrue("there are still some errors", (violations.size() > 0));
+		user.setCustomerNumber("EN-123456");
+		violations = validator.validate(user);
+		Assert.assertEquals(0, violations.size()); // finally everything
+													// validates
+
+	}
+}