From 6b81b95ef6919945cdcb4e9281db412f0aab90d6 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Sat, 22 Sep 2018 17:29:16 -0300 Subject: [PATCH] Initial Commit --- javaxval/pom.xml | 127 ++++++++++-------- .../appplication/Application.java | 14 ++ .../entities/UserNotBlank.java | 22 +++ .../entities/UserNotEmpty.java | 22 +++ .../entities/UserNotNull.java | 22 +++ .../test/UserNotBlankUnitTest.java | 54 ++++++++ .../test/UserNotEmptyUnitTest.java | 47 +++++++ .../test/UserNotNullUnitTest.java | 47 +++++++ 8 files changed, 300 insertions(+), 55 deletions(-) create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/appplication/Application.java create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotBlank.java create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotEmpty.java create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotNull.java create mode 100644 javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotBlankUnitTest.java create mode 100644 javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotEmptyUnitTest.java create mode 100644 javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotNullUnitTest.java diff --git a/javaxval/pom.xml b/javaxval/pom.xml index 4d27b3e0c9..63cb4c1d1d 100644 --- a/javaxval/pom.xml +++ b/javaxval/pom.xml @@ -1,56 +1,73 @@ - - 4.0.0 - com.baeldung - javaxval - 0.1-SNAPSHOT - - - 1.1.0.Final - 5.3.4.Final - 3.0.0 - 2.2.6 - - - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - - javax.validation - validation-api - ${validation-api.version} - - - - org.hibernate - hibernate-validator - ${hibernate-validator.version} - - - - org.hibernate - hibernate-validator-annotation-processor - ${hibernate-validator.version} - - - - javax.el - javax.el-api - ${javax.el-api.version} - - - - org.glassfish.web - javax.el - ${javax.el.version} - - - - + + 4.0.0 + com.baeldung + javaxval + 0.1-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + javax.validation + validation-api + ${validation-api.version} + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + org.hibernate + hibernate-validator-annotation-processor + ${hibernate-validator.version} + + + javax.el + javax.el-api + ${javax.el-api.version} + + + org.glassfish.web + javax.el + ${javax.el.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-test + ${org.springframework.version} + + + junit + junit + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + 2.0.1.Final + 6.0.7.Final + 3.0.0 + 2.2.6 + 5.0.2.RELEASE + 4.12 + 3.11.1 + \ No newline at end of file diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/appplication/Application.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/appplication/Application.java new file mode 100644 index 0000000000..c9f2ab6f98 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/appplication/Application.java @@ -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())); + } +} diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotBlank.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotBlank.java new file mode 100644 index 0000000000..2ea6a3af56 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotBlank.java @@ -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 + "}"; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotEmpty.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotEmpty.java new file mode 100644 index 0000000000..39e34b63d3 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotEmpty.java @@ -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 + "}"; + } +} diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotNull.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotNull.java new file mode 100644 index 0000000000..598c9ba9f9 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/entities/UserNotNull.java @@ -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 + "}"; + } +} diff --git a/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotBlankUnitTest.java b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotBlankUnitTest.java new file mode 100644 index 0000000000..081ceadfbe --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotBlankUnitTest.java @@ -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> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(0); + } + + @Test + public void whenBlankName_thenOneConstraintViolation() { + UserNotBlank user = new UserNotBlank(" "); + Set> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + public void whenEmptyName_thenOneConstraintViolation() { + UserNotBlank user = new UserNotBlank(""); + Set> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + public void whenNullName_thenOneConstraintViolation() { + UserNotBlank user = new UserNotBlank(null); + Set> 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}"); + } +} diff --git a/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotEmptyUnitTest.java b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotEmptyUnitTest.java new file mode 100644 index 0000000000..1ef914607e --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotEmptyUnitTest.java @@ -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> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(0); + } + + @Test + public void whenEmptyName_thenOneConstraintViolation() { + UserNotEmpty user = new UserNotEmpty(""); + Set> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + public void whenNullName_thenOneConstraintViolation() { + UserNotEmpty user = new UserNotEmpty(null); + Set> 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}"); + } +} diff --git a/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotNullUnitTest.java b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotNullUnitTest.java new file mode 100644 index 0000000000..252d71a4cb --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javabeanconstraints/test/UserNotNullUnitTest.java @@ -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> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(0); + } + + @Test + public void whenNullName_thenOneConstraintViolation() { + UserNotNull user = new UserNotNull(null); + Set> violations = validator.validate(user); + assertThat(violations.size()).isEqualTo(1); + } + + @Test + public void whenEmptyName_thenNoConstraintViolations() { + UserNotNull user = new UserNotNull(""); + Set> 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}"); + } +}