Merge pull request #268 from mnkartik/master
Update the codebase as per the documented standards
This commit is contained in:
commit
cbeef91784
|
@ -0,0 +1,6 @@
|
|||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
target/
|
||||
bin/
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
.classpath
|
||||
.project
|
||||
.settings/
|
||||
target/
|
||||
|
||||
|
|
@ -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>org.baeldung</groupId>
|
||||
<artifactId>javaxval</artifactId>
|
||||
<version>0.1-SNAPSHOT</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>
|
|
@ -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>org.baeldung</groupId>
|
||||
<artifactId>javaxval</artifactId>
|
||||
<version>0.1-SNAPSHOT</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>
|
|
@ -0,0 +1,56 @@
|
|||
package org.baeldung;
|
||||
|
||||
import javax.validation.constraints.AssertTrue;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class User {
|
||||
|
||||
@NotNull(message = "Name cannot be null")
|
||||
private String name;
|
||||
|
||||
@AssertTrue
|
||||
private boolean working;
|
||||
|
||||
@Size(min = 10, max = 200, message = "Number of characters should be in between 10 and 200 inclusive")
|
||||
private String aboutMe;
|
||||
|
||||
@Min(value = 18, message = "Age should not be less than 18")
|
||||
@Max(value = 150, message = "Age should not be more than 150")
|
||||
private int age;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public boolean isWorking() {
|
||||
return working;
|
||||
}
|
||||
|
||||
public void setWorking(boolean working) {
|
||||
this.working = working;
|
||||
}
|
||||
|
||||
public String getAboutMe() {
|
||||
return aboutMe;
|
||||
}
|
||||
|
||||
public void setAboutMe(String aboutMe) {
|
||||
this.aboutMe = aboutMe;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package org.baeldung;
|
||||
|
||||
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;
|
||||
|
||||
public class ValidationTest {
|
||||
|
||||
@Test
|
||||
public void ifNameIsNull_nameValidationFails() {
|
||||
User user = new User();
|
||||
user.setWorking(true);
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifSizeNotInRange_aboutMeValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifWorkingIsFalse_workingValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(50);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifAgeNotRange_ageValidationFails() {
|
||||
User user = new User();
|
||||
user.setName("MyName");
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(8);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void ifFnameNullAgeNotRangeAndWorkingIsFalse_validationFailsWithThreeErrors() {
|
||||
User user = new User();
|
||||
user.setAboutMe("Its all about me!!");
|
||||
user.setAge(300);
|
||||
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
Set<ConstraintViolation<User>> violations = validator.validate(user);
|
||||
Assert.assertEquals(violations.isEmpty(), false);
|
||||
Assert.assertEquals(violations.size(), 3);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue