Initial Commit
This commit is contained in:
parent
deb79bbd83
commit
6b81b95ef6
127
javaxval/pom.xml
127
javaxval/pom.xml
|
@ -1,56 +1,73 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>javaxval</artifactId>
|
<artifactId>javaxval</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<parent>
|
||||||
<validation-api.version>1.1.0.Final</validation-api.version>
|
<groupId>com.baeldung</groupId>
|
||||||
<hibernate-validator.version>5.3.4.Final</hibernate-validator.version>
|
<artifactId>parent-modules</artifactId>
|
||||||
<javax.el-api.version>3.0.0</javax.el-api.version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<javax.el.version>2.2.6</javax.el.version>
|
</parent>
|
||||||
</properties>
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
<parent>
|
<groupId>javax.validation</groupId>
|
||||||
<groupId>com.baeldung</groupId>
|
<artifactId>validation-api</artifactId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<version>${validation-api.version}</version>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
</dependency>
|
||||||
</parent>
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
<dependencies>
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>${hibernate-validator.version}</version>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>javax.validation</groupId>
|
<dependency>
|
||||||
<artifactId>validation-api</artifactId>
|
<groupId>org.hibernate</groupId>
|
||||||
<version>${validation-api.version}</version>
|
<artifactId>hibernate-validator-annotation-processor</artifactId>
|
||||||
</dependency>
|
<version>${hibernate-validator.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>javax.el</groupId>
|
||||||
<artifactId>hibernate-validator</artifactId>
|
<artifactId>javax.el-api</artifactId>
|
||||||
<version>${hibernate-validator.version}</version>
|
<version>${javax.el-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
<dependency>
|
<groupId>org.glassfish.web</groupId>
|
||||||
<groupId>org.hibernate</groupId>
|
<artifactId>javax.el</artifactId>
|
||||||
<artifactId>hibernate-validator-annotation-processor</artifactId>
|
<version>${javax.el.version}</version>
|
||||||
<version>${hibernate-validator.version}</version>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
<dependency>
|
<artifactId>spring-context</artifactId>
|
||||||
<groupId>javax.el</groupId>
|
<version>${org.springframework.version}</version>
|
||||||
<artifactId>javax.el-api</artifactId>
|
</dependency>
|
||||||
<version>${javax.el-api.version}</version>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
<dependency>
|
<version>${org.springframework.version}</version>
|
||||||
<groupId>org.glassfish.web</groupId>
|
</dependency>
|
||||||
<artifactId>javax.el</artifactId>
|
<dependency>
|
||||||
<version>${javax.el.version}</version>
|
<groupId>junit</groupId>
|
||||||
</dependency>
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
</dependencies>
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<validation-api.version>2.0.1.Final</validation-api.version>
|
||||||
|
<hibernate-validator.version>6.0.7.Final</hibernate-validator.version>
|
||||||
|
<javax.el-api.version>3.0.0</javax.el-api.version>
|
||||||
|
<javax.el.version>2.2.6</javax.el.version>
|
||||||
|
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<assertj.version>3.11.1</assertj.version>
|
||||||
|
</properties>
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
package org.baeldung.javabeanconstraints.appplication;
|
||||||
|
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import org.baeldung.javabeanconstraints.entities.UserNotBlank;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
UserNotBlank user = new UserNotBlank(" ");
|
||||||
|
validator.validate(user).stream().forEach(violation -> System.out.println(violation.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package org.baeldung.javabeanconstraints.entities;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
public class UserNotBlank {
|
||||||
|
|
||||||
|
@NotBlank(message = "Name is mandatory")
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public UserNotBlank(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "name=" + name + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package org.baeldung.javabeanconstraints.entities;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
public class UserNotEmpty {
|
||||||
|
|
||||||
|
@NotEmpty(message = "Name is mandatory")
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public UserNotEmpty(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "name=" + name + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package org.baeldung.javabeanconstraints.entities;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class UserNotNull {
|
||||||
|
|
||||||
|
@NotNull(message = "Name is mandatory")
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public UserNotNull(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "name=" + name + "}";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package org.baeldung.javabeanconstraints.test;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.baeldung.javabeanconstraints.entities.UserNotBlank;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UserNotBlankUnitTest {
|
||||||
|
|
||||||
|
private static Validator validator;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setupValidatorInstance() {
|
||||||
|
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotBlankName_thenNoConstraintViolations() {
|
||||||
|
UserNotBlank user = new UserNotBlank("John");
|
||||||
|
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenBlankName_thenOneConstraintViolation() {
|
||||||
|
UserNotBlank user = new UserNotBlank(" ");
|
||||||
|
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmptyName_thenOneConstraintViolation() {
|
||||||
|
UserNotBlank user = new UserNotBlank("");
|
||||||
|
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNullName_thenOneConstraintViolation() {
|
||||||
|
UserNotBlank user = new UserNotBlank(null);
|
||||||
|
Set<ConstraintViolation<UserNotBlank>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenToString_thenCorrect() {
|
||||||
|
UserNotBlank user = new UserNotBlank("John");
|
||||||
|
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package org.baeldung.javabeanconstraints.test;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.baeldung.javabeanconstraints.entities.UserNotEmpty;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UserNotEmptyUnitTest {
|
||||||
|
|
||||||
|
private static Validator validator;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setupValidatorInstance() {
|
||||||
|
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotEmptyName_thenNoConstraintViolations() {
|
||||||
|
UserNotEmpty user = new UserNotEmpty("John");
|
||||||
|
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmptyName_thenOneConstraintViolation() {
|
||||||
|
UserNotEmpty user = new UserNotEmpty("");
|
||||||
|
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNullName_thenOneConstraintViolation() {
|
||||||
|
UserNotEmpty user = new UserNotEmpty(null);
|
||||||
|
Set<ConstraintViolation<UserNotEmpty>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenToString_thenCorrect() {
|
||||||
|
UserNotEmpty user = new UserNotEmpty("John");
|
||||||
|
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package org.baeldung.javabeanconstraints.test;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import org.baeldung.javabeanconstraints.entities.UserNotNull;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class UserNotNullUnitTest {
|
||||||
|
|
||||||
|
private static Validator validator;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setupValidatorInstance() {
|
||||||
|
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotNullName_thenNoConstraintViolations() {
|
||||||
|
UserNotNull user = new UserNotNull("John");
|
||||||
|
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNullName_thenOneConstraintViolation() {
|
||||||
|
UserNotNull user = new UserNotNull(null);
|
||||||
|
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenEmptyName_thenNoConstraintViolations() {
|
||||||
|
UserNotNull user = new UserNotNull("");
|
||||||
|
Set<ConstraintViolation<UserNotNull>> violations = validator.validate(user);
|
||||||
|
assertThat(violations.size()).isEqualTo(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenToString_thenCorrect() {
|
||||||
|
UserNotNull user = new UserNotNull("John");
|
||||||
|
assertThat(user.toString()).isEqualTo("User{name=John}");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue