new file: javaxval/.classpath
new file: javaxval/.project new file: javaxval/pom.xml new file: javaxval/src/main/java/sample/model/User.java new file: javaxval/src/test/java/sample/ValidationTest.java
This commit is contained in:
parent
a5375cb90c
commit
5c1ce1c74a
36
javaxval/.classpath
Normal file
36
javaxval/.classpath
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
23
javaxval/.project
Normal file
23
javaxval/.project
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>javaxval</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
51
javaxval/pom.xml
Normal file
51
javaxval/pom.xml
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<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.sample</groupId>
|
||||||
|
<artifactId>javaxval</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.validation</groupId>
|
||||||
|
<artifactId>validation-api</artifactId>
|
||||||
|
<version>1.1.0.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>5.2.1.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator-annotation-processor</artifactId>
|
||||||
|
<version>5.2.1.Final</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.el</groupId>
|
||||||
|
<artifactId>javax.el-api</artifactId>
|
||||||
|
<version>2.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.web</groupId>
|
||||||
|
<artifactId>javax.el</artifactId>
|
||||||
|
<version>2.2.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
73
javaxval/src/main/java/sample/model/User.java
Normal file
73
javaxval/src/main/java/sample/model/User.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package sample.model;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Max;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
import org.hibernate.validator.constraints.Email;
|
||||||
|
import org.hibernate.validator.constraints.NotEmpty;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
@NotNull(message = "First name cannot be null")
|
||||||
|
@NotEmpty(message = "First name cannot be empty")
|
||||||
|
private String fname;
|
||||||
|
|
||||||
|
@NotNull(message = "Last name cannot be null")
|
||||||
|
@NotEmpty(message = "Last name cannot be empty")
|
||||||
|
private String lname;
|
||||||
|
|
||||||
|
@Min(value = 18, message = "Age must be greater than or equal to 18")
|
||||||
|
@Max(value = 150, message = "Age must be less than or equal to 150")
|
||||||
|
private int age;
|
||||||
|
|
||||||
|
@NotNull(message = "Last name cannot be null")
|
||||||
|
@NotEmpty(message = "Last name cannot be empty")
|
||||||
|
private String gender;
|
||||||
|
|
||||||
|
@NotNull(message = "Email Address is compulsory")
|
||||||
|
@NotEmpty(message = "Email Address is compulsory")
|
||||||
|
@Email(message = "Email Address is not a valid format")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFname() {
|
||||||
|
return fname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFname(String fname) {
|
||||||
|
this.fname = fname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLname() {
|
||||||
|
return lname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLname(String lname) {
|
||||||
|
this.lname = lname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(String gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
102
javaxval/src/test/java/sample/ValidationTest.java
Normal file
102
javaxval/src/test/java/sample/ValidationTest.java
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package sample;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import sample.model.User;
|
||||||
|
|
||||||
|
public class ValidationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validation_when_fname_is_null() {
|
||||||
|
System.out
|
||||||
|
.println("\n---------------validation_when_fname_is_null--------------");
|
||||||
|
|
||||||
|
// [1]
|
||||||
|
User user = new User();
|
||||||
|
user.setLname("Last");
|
||||||
|
user.setEmail("first.last@gmail.com");
|
||||||
|
user.setGender("male");
|
||||||
|
user.setAge(29);
|
||||||
|
|
||||||
|
// [2]
|
||||||
|
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||||
|
// [3]
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
// [4]
|
||||||
|
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||||
|
|
||||||
|
// [5]
|
||||||
|
Iterator<ConstraintViolation<User>> iter = violations.iterator();
|
||||||
|
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
// [6]
|
||||||
|
ConstraintViolation<User> cv = iter.next();
|
||||||
|
|
||||||
|
// [7]
|
||||||
|
System.out.println(cv.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(violations.isEmpty(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validation_when_empty_lname() {
|
||||||
|
System.out
|
||||||
|
.println("\n---------------validation_when_empty_lname--------------");
|
||||||
|
User user = new User();
|
||||||
|
user.setFname("First");
|
||||||
|
user.setLname("");
|
||||||
|
user.setEmail("first.last@gmail.com");
|
||||||
|
user.setGender("male");
|
||||||
|
user.setAge(29);
|
||||||
|
|
||||||
|
// validate the input
|
||||||
|
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||||
|
|
||||||
|
Iterator<ConstraintViolation<User>> iter = violations.iterator();
|
||||||
|
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
ConstraintViolation<User> cv = iter.next();
|
||||||
|
System.out.println(cv.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(violations.isEmpty(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validation_when_email_is_invalid() {
|
||||||
|
System.out
|
||||||
|
.println("\n---------------validation_when_email_is_invalid--------------");
|
||||||
|
User user = new User();
|
||||||
|
user.setFname("First");
|
||||||
|
user.setLname("Last");
|
||||||
|
user.setEmail("firstgmail.com");
|
||||||
|
user.setGender("male");
|
||||||
|
user.setAge(29);
|
||||||
|
|
||||||
|
// validate the input
|
||||||
|
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||||
|
|
||||||
|
Iterator<ConstraintViolation<User>> iter = violations.iterator();
|
||||||
|
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
ConstraintViolation<User> cv = iter.next();
|
||||||
|
System.out.println(cv.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertEquals(violations.isEmpty(), false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user