Skip to content

Commit bef509e

Browse files
committed
wip
1 parent be25a7b commit bef509e

7 files changed

Lines changed: 166 additions & 16 deletions

File tree

src/test/java/com/github/jinahya/assertj/validation/example/user/JuniorValidator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,7 +26,7 @@
2626
class JuniorValidator
2727
implements ConstraintValidator<Junior, User> {
2828

29-
static final int MAX_AGE_EXCLUSIVE = 60;
29+
private static final int MAX_AGE_EXCLUSIVE = UserConstants.MAX_AGE_FOR_JUNIOR_EXCLUSIVE;
3030

3131
@Override
3232
public void initialize(final Junior junior) {
@@ -38,6 +38,6 @@ public boolean isValid(final User value, final ConstraintValidatorContext contex
3838
if (value == null) {
3939
return true;
4040
}
41-
return value.age < MAX_AGE_EXCLUSIVE;
41+
return value.getAge() < MAX_AGE_EXCLUSIVE;
4242
}
4343
}

src/test/java/com/github/jinahya/assertj/validation/example/user/Registration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -33,8 +33,8 @@
3333
@Setter(AccessLevel.PACKAGE)
3434
@Getter(AccessLevel.PACKAGE)
3535
@ToString
36-
@AllArgsConstructor
37-
@NoArgsConstructor
36+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
37+
@NoArgsConstructor(access = AccessLevel.PACKAGE)
3838
abstract class Registration {
3939

4040
@Valid

src/test/java/com/github/jinahya/assertj/validation/example/user/SeniorValidator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,7 +26,7 @@
2626
class SeniorValidator
2727
implements ConstraintValidator<Senior, User> {
2828

29-
static final int MIN_AGE_INCLUSIVE = JuniorValidator.MAX_AGE_EXCLUSIVE;
29+
private static final int MIN_AGE_INCLUSIVE = UserConstants.MIN_AGE_FOR_SENIOR_INCLUSIVE;
3030

3131
@Override
3232
public void initialize(final Senior junior) {
@@ -38,6 +38,6 @@ public boolean isValid(final User value, final ConstraintValidatorContext contex
3838
if (value == null) {
3939
return true;
4040
}
41-
return value.age >= MIN_AGE_INCLUSIVE;
41+
return value.getAge() >= MIN_AGE_INCLUSIVE;
4242
}
4343
}

src/test/java/com/github/jinahya/assertj/validation/example/user/User.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -72,9 +72,9 @@ static User newInstance(final boolean validName, final boolean validAge) {
7272
}
7373

7474
@NotBlank
75-
String name;
75+
private String name;
7676

7777
@Max(MAX_AGE)
7878
@PositiveOrZero
79-
int age;
79+
private int age;
8080
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.jinahya.assertj.validation.example.user;
2+
3+
/*-
4+
* #%L
5+
* assertj-bean-validation
6+
* %%
7+
* Copyright (C) 2021 - 2022 Jinahya, Inc.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import org.assertj.core.api.Condition;
24+
25+
final class UserConditions {
26+
27+
public static final Condition<User> JUNIOR = new Condition<>(
28+
v -> v.getAge() < UserConstants.MAX_AGE_FOR_JUNIOR_EXCLUSIVE,
29+
"a junior whose `age` is less than [%d]",
30+
UserConstants.MAX_AGE_FOR_JUNIOR_EXCLUSIVE
31+
);
32+
33+
public static final Condition<User> SENIOR = new Condition<>(
34+
v -> v.getAge() >= UserConstants.MIN_AGE_FOR_SENIOR_INCLUSIVE,
35+
"a senior whose 'age' is greater than or equal to [%d]",
36+
UserConstants.MIN_AGE_FOR_SENIOR_INCLUSIVE
37+
);
38+
39+
private UserConditions() {
40+
throw new AssertionError("instantiation is not allowed");
41+
}
42+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.github.jinahya.assertj.validation.example.user;
2+
3+
/*-
4+
* #%L
5+
* assertj-bean-validation
6+
* %%
7+
* Copyright (C) 2021 - 2022 Jinahya, Inc.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import lombok.extern.slf4j.Slf4j;
24+
import org.junit.jupiter.api.Nested;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static com.github.jinahya.assertj.validation.example.user.UserConditions.JUNIOR;
28+
import static com.github.jinahya.assertj.validation.example.user.UserConditions.SENIOR;
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
32+
@Slf4j
33+
class UserConditionsTest {
34+
35+
@Nested
36+
class JuniorTest {
37+
38+
@Test
39+
void _Pass_Junior() {
40+
final User user = User.newInstance(true, true);
41+
user.setAge(UserConstants.MAX_AGE_FOR_JUNIOR_EXCLUSIVE - 1);
42+
assertThat(user).is(JUNIOR);
43+
}
44+
45+
@Test
46+
void _Fail_Junior() {
47+
final User user = User.newInstance(true, true);
48+
user.setAge(UserConstants.MAX_AGE_FOR_JUNIOR_EXCLUSIVE);
49+
final var assertion = assertThat(user);
50+
assertThatThrownBy(() -> assertion.is(JUNIOR))
51+
.isInstanceOf(AssertionError.class)
52+
.satisfies(ar -> {
53+
log.debug("message: {}", ar.getMessage());
54+
});
55+
}
56+
}
57+
58+
@Nested
59+
class SeniorTest {
60+
61+
@Test
62+
void _Pass_Senior() {
63+
final User user = User.newInstance(true, true);
64+
user.setAge(UserConstants.MIN_AGE_FOR_SENIOR_INCLUSIVE);
65+
assertThat(user).is(SENIOR);
66+
}
67+
68+
@Test
69+
void _Fail_Senior() {
70+
final User user = User.newInstance(true, true);
71+
user.setAge(UserConstants.MIN_AGE_FOR_SENIOR_INCLUSIVE - 1);
72+
final var assertion = assertThat(user);
73+
assertThatThrownBy(() -> assertion.is(SENIOR))
74+
.isInstanceOf(AssertionError.class)
75+
.satisfies(ar -> {
76+
log.debug("message: {}", ar.getMessage());
77+
});
78+
}
79+
}
80+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.jinahya.assertj.validation.example.user;
2+
3+
/*-
4+
* #%L
5+
* assertj-bean-validation
6+
* %%
7+
* Copyright (C) 2021 - 2022 Jinahya, Inc.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
final class UserConstants {
24+
25+
static final int MAX_AGE_FOR_JUNIOR_EXCLUSIVE = 60;
26+
27+
static final int MIN_AGE_FOR_SENIOR_INCLUSIVE = MAX_AGE_FOR_JUNIOR_EXCLUSIVE;
28+
}

0 commit comments

Comments
 (0)