Skip to content

Commit 3d82e3a

Browse files
authored
Merge pull request #4 from mulderbaba/PAYARA-1941-Create-a-bean-validation-2-sample
Bean Validation 2 samples with hibernate-validator on board
2 parents 06eda27 + 7a412c0 commit 3d82e3a

File tree

10 files changed

+315
-1
lines changed

10 files changed

+315
-1
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ env:
99
- TESTFOLDER=jpa
1010
- TESTFOLDER=jsf
1111
- TESTFOLDER=jsonb
12+
- TESTFOLDER=validation
1213

1314
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
1415

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@
9292
-->
9393
<modules>
9494
<module>test-utils</module>
95-
9695
<module>servlet</module>
9796
<module>security</module>
9897
<module>jsf</module>
9998
<module>cdi</module>
10099
<module>jpa</module>
101100
<module>jsonb</module>
101+
<module>validation</module>
102102
</modules>
103103

104104
<dependencyManagement>

validation/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Java EE 8 Samples: Bean Validation 2#
2+
3+
The [JSR 380](https://www.jcp.org/en/jsr/detail?id=380) specifies the next version of Bean Validation, version 2.
4+
5+
## Samples ##
6+
7+
- constraints:
8+
Testing new features coming with Bean Validation 2 like java.time API validation support, repeatable annotations and type annotations applied on Lists and Maps.

validation/constraints/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>validation</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>constraints</artifactId>
11+
<name>Java EE 8 Samples: Bean Validation 2: Support for java.time API</name>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>javax.validation</groupId>
16+
<artifactId>validation-api</artifactId>
17+
<version>2.0.0.Final</version>
18+
</dependency>
19+
</dependencies>
20+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.javaee8.validation;
2+
3+
import javax.validation.constraints.NotEmpty;
4+
5+
/**
6+
* @author mertcaliskan
7+
*/
8+
public class Address {
9+
10+
@NotEmpty
11+
private String detail;
12+
13+
public String getDetail() {
14+
return detail;
15+
}
16+
17+
public void setDetail(String detail) {
18+
this.detail = detail;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.javaee8.validation;
2+
3+
/**
4+
* @author mertcaliskan
5+
*/
6+
public interface Admin {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee8.validation;
2+
3+
import javax.validation.constraints.NotNull;
4+
import javax.validation.constraints.Size;
5+
6+
/**
7+
* @author mertcaliskan
8+
*/
9+
public class Country {
10+
11+
@NotNull
12+
@Size(min = 2, max = 2)
13+
private String countryCode;
14+
15+
public String getCountryCode() {
16+
return countryCode;
17+
}
18+
19+
public void setCountryCode(String countryCode) {
20+
this.countryCode = countryCode;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.javaee8.validation;
2+
3+
import javax.validation.Valid;
4+
import javax.validation.constraints.Email;
5+
import javax.validation.constraints.NotNull;
6+
import javax.validation.constraints.Past;
7+
import javax.validation.constraints.Size;
8+
import javax.validation.groups.Default;
9+
import java.time.LocalDate;
10+
import java.time.Year;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.Optional;
15+
16+
/**
17+
* @author mertcaliskan
18+
*/
19+
class Person {
20+
21+
@Past
22+
private Year yearOfBirth;
23+
24+
private Optional<@Past LocalDate> marriageAnniversary;
25+
26+
private List<@NotNull @Email String> emails;
27+
28+
@Size(min = 8, groups = Default.class)
29+
@Size(min = 12, groups = Admin.class)
30+
private String password;
31+
32+
private Map<@Valid Country, @Valid Address> addressMap = new HashMap<>();
33+
34+
35+
public Year getYearOfBirth() {
36+
return yearOfBirth;
37+
}
38+
39+
public void setYearOfBirth(Year yearOfBirth) {
40+
this.yearOfBirth = yearOfBirth;
41+
}
42+
43+
public Optional<LocalDate> getMarriageAnniversary() {
44+
return marriageAnniversary;
45+
}
46+
47+
public void setMarriageAnniversary(Optional<LocalDate> marriageAnniversary) {
48+
this.marriageAnniversary = marriageAnniversary;
49+
}
50+
51+
public List<String> getEmails() {
52+
return emails;
53+
}
54+
55+
public void setEmails(List<String> emails) {
56+
this.emails = emails;
57+
}
58+
59+
public String getPassword() {
60+
return password;
61+
}
62+
63+
public void setPassword(String password) {
64+
this.password = password;
65+
}
66+
67+
public Map<Country, Address> getAddressMap() {
68+
return addressMap;
69+
}
70+
71+
public void setAddressMap(Map<Country, Address> addressMap) {
72+
this.addressMap = addressMap;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package org.javaee8.validation;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
6+
import org.jboss.shrinkwrap.api.spec.WebArchive;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.validation.ConstraintViolation;
12+
import javax.validation.Validation;
13+
import javax.validation.Validator;
14+
import javax.validation.groups.Default;
15+
import java.time.*;
16+
import java.util.Arrays;
17+
import java.util.Optional;
18+
import java.util.Set;
19+
20+
import static org.hamcrest.CoreMatchers.*;
21+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
22+
import static org.junit.Assert.assertThat;
23+
24+
/**
25+
* @author mertcaliskan
26+
*/
27+
@RunWith(Arquillian.class)
28+
public class ConstraintViolationTest {
29+
30+
private Validator validator;
31+
32+
@Before
33+
public void setUpValidator() {
34+
validator = Validation
35+
.byDefaultProvider()
36+
.configure()
37+
.clockProvider(
38+
() -> Clock.fixed(
39+
Instant.parse("2017-01-01T00:00:00.00Z"),
40+
ZoneId.systemDefault()))
41+
.buildValidatorFactory()
42+
.getValidator();
43+
}
44+
45+
@Deployment
46+
public static WebArchive deploy() {
47+
return create(WebArchive.class)
48+
.addAsLibraries(
49+
create(JavaArchive.class).addClasses(Person.class,
50+
Admin.class,
51+
Country.class,
52+
Address.class));
53+
}
54+
55+
@Test
56+
public void validatingBirthDateFailsWithViolation() {
57+
Person person = new Person();
58+
person.setYearOfBirth(Year.of(2018));
59+
60+
Set<ConstraintViolation<Person>> violations = validator.validate(person);
61+
62+
assertThat(violations, is(not(nullValue())));
63+
assertThat(violations.size(), is(1));
64+
assertThat(violations.iterator().next().getMessage(),
65+
is("must be in the past"));
66+
}
67+
68+
public void validatingMarriageAnniversaryDateFailsWithViolation() {
69+
Person person = new Person();
70+
person.setMarriageAnniversary(Optional.of(LocalDate.MAX));
71+
72+
Set<ConstraintViolation<Person>> violations = validator.validate(person);
73+
74+
assertThat(violations, is(not(nullValue())));
75+
assertThat(violations.size(), is(1));
76+
assertThat(violations.iterator().next().getMessage(),
77+
is("must be in the past"));
78+
}
79+
80+
@Test
81+
public void validatingEmailsFailsWithViolation() {
82+
Person person = new Person();
83+
person.setEmails(Arrays.asList("[email protected]", "invalid_mail"));
84+
85+
Set<ConstraintViolation<Person>> violations = validator.validate(person);
86+
87+
assertThat(violations, is(not(nullValue())));
88+
assertThat(violations.size(), is(1));
89+
assertThat(violations.iterator().next().getMessage(),
90+
is("must be a well-formed email address"));
91+
}
92+
93+
@Test
94+
public void validatingPassswordWithDefaultGroupFailsWithViolation() {
95+
Person person = new Person();
96+
person.setPassword("1234567");
97+
98+
Set<ConstraintViolation<Person>> violations = validator.validate(person, Default.class);
99+
100+
assertThat(violations, is(not(nullValue())));
101+
assertThat(violations.size(), is(1));
102+
assertThat(violations.iterator().next().getMessage(),
103+
is("size must be between 8 and 2147483647"));
104+
}
105+
106+
@Test
107+
public void validatingPassswordWithAdminGroupFailsWithViolation() {
108+
Person person = new Person();
109+
person.setPassword("1234567");
110+
111+
Set<ConstraintViolation<Person>> violations = validator.validate(person, Admin.class);
112+
113+
assertThat(violations, is(not(nullValue())));
114+
assertThat(violations.size(), is(1));
115+
assertThat(violations.iterator().next().getMessage(),
116+
is("size must be between 12 and 2147483647"));
117+
}
118+
119+
@Test
120+
public void validatingAddressFailsWithViolation() {
121+
Person person = new Person();
122+
Country country = new Country();
123+
country.setCountryCode("ABC");
124+
Address address = new Address();
125+
address.setDetail("");
126+
person.getAddressMap().put(country, address);
127+
128+
Set<ConstraintViolation<Person>> violations = validator.validate(person);
129+
130+
assertThat(violations, is(not(nullValue())));
131+
assertThat(violations.size(), is(2));
132+
assertThat(violations.iterator().next().getMessage(),
133+
anyOf(is("must not be empty"), is("size must be between 2 and 2")));
134+
assertThat(violations.iterator().next().getMessage(),
135+
anyOf(is("must not be empty"), is("size must be between 2 and 2")));
136+
}
137+
}

validation/pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>samples-parent</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>validation</artifactId>
11+
<packaging>pom</packaging>
12+
<name>Java EE 8 Samples: Bean Validation 2</name>
13+
14+
<modules>
15+
<module>constraints</module>
16+
</modules>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee8</groupId>
21+
<artifactId>test-utils</artifactId>
22+
<version>${project.version}</version>
23+
</dependency>
24+
</dependencies>
25+
</project>

0 commit comments

Comments
 (0)