Skip to content

Commit 34f17bd

Browse files
committed
Add more testcases
1 parent bef509e commit 34f17bd

29 files changed

Lines changed: 970 additions & 363 deletions

README.md

Lines changed: 55 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,31 @@ An [AssertJ](https://joel-costigliola.github.io/assertj/) extension for [Bean-Va
1515
## Coordinates
1616

1717
```xml
18+
1819
<groupId>com.github.jinahya</groupId>
1920
<artifactId>assertj-bean-validation</artifactId>
2021
```
2122

2223
### Classifiers
2324

24-
classifier | `-release` | `javax.*` | `jakarta.*` |notes
25-
---------------------|------------|-----------|-------------|-----
26-
NONE | 8 | ✓ | |
27-
`jakarta` | 8 | | ✓ |
28-
`release-11` | 11 | ✓ | |
29-
`release-11-jakarta` | 11 | | ✓ |
30-
`release-17` | 17 | ✓ | |
31-
`release-17-jakarta` | 17 | | ✓ |
25+
classifier | `release` | `javax.*` | `jakarta.*` |notes
26+
---------------------|-----------|-----------|-------------|-----
27+
NONE | 8 | ✓ | |
28+
`jakarta` | 8 | | ✓ |
29+
`release-11` | 11 | ✓ | |
30+
`release-11-jakarta` | 11 | | ✓ |
31+
`release-17` | 17 | ✓ | |
32+
`release-17-jakarta` | 17 | | ✓ |
3233

3334
## Compatibilities
3435

35-
* Depends (as `provided`) on
36+
* Depends, in `provided` scope, on
3637
the [latest org.assertj:assertj-core](https://javadoc.io/doc/org.assertj/assertj-core/latest/index.html).
3738
* Targets **Java 8**.
3839

3940
### JDK
4041

41-
This module requires the **latest JDK**(currently, 18), especially with its test sources, for building itself.
42+
This module requires the **latest JDK**(currently, 19), especially with its test sources, for building itself.
4243

4344
## Usages
4445

@@ -50,16 +51,12 @@ class User {
5051
@Max(0x7F) @PositiveOrZero int age;
5152
}
5253

53-
class Registration {
54+
abstract class Registration {
5455
@Valid @NotNull User user;
5556
}
5657

57-
class SeniorRegistration
58-
extends Registration {
59-
@AssertTrue
60-
boolean isUserSenior() {
61-
return user == null || user.age >= 60;
62-
}
58+
class SeniorRegistration extends Registration {
59+
@Senior User getUser() { return super.getUser(); }
6360
}
6461
```
6562

@@ -69,160 +66,85 @@ Verifies that actual bean objects and/or their properties are valid.
6966

7067
```java
7168
class Test {
72-
73-
@Test
74-
void test() {
69+
@Test void test1() {
7570
assertThatBean(new User("Jane", 28)) // valid
76-
.isValid() // passes
77-
.hasValidProperty("name") // passes
78-
.hasValidProperty("age"); // passes
79-
}
80-
}
81-
```
82-
83-
You can debug by analyzing (or verifying) constraint violations populated while validating.
84-
85-
```java
86-
class Test {
87-
88-
@Test
89-
void test() {
90-
assertThatBean(new User("", 27)) // invalid; name is blank
91-
.isValid(cv -> { // should fail
92-
assertThat(cv.getInvalidValue()).isEqualTo(actual.getName());
93-
assertThat(cv.getPropertyPath())
94-
.isNotEmpty()
95-
.allSatisfy(n -> {
96-
assertThat(n.getName()).isEqualTo("name");
97-
});
98-
});
99-
assertThatBean(new User("John", 300)) // invalid; too old to be true
100-
.hasValidProperty("age", cv -> { // should fail
101-
assertThat(cv.getInvalidValue()).isEqualTo(actual.getAge());
102-
assertThat(cv.getPropertyPath())
103-
.isNotEmpty()
104-
.allSatisfy(n -> assertThat(n.getName()).isEqualTo("age"));
105-
});
71+
.isValid() // should pass
72+
.hasValidProperty("name") // should pass
73+
.hasValidProperty("age"); // should pass
10674
}
10775
}
10876
```
10977

110-
### Using extended assertion classes
111-
112-
You can work with your own (extended) assertion class.
113-
114-
```java
115-
class UserAssert
116-
extends AbstractBeanAssert<UserAssert, User> {
117-
118-
UserAssert(User actual) {
119-
super(actua, UserAssert.class);
120-
}
121-
122-
UserAssert isNamedJane() {
123-
return isNotNull()
124-
.is(new Condition<>(v -> "Jane".equalsIgnoreCase(v.getName()),
125-
"named Jane"));
126-
}
127-
}
128-
```
129-
130-
A number of static factory methods are prepared for extended assertion classes.
131-
132-
```java
133-
class Test {
134-
135-
@Test
136-
void test() {
137-
// specify your assert class along with specific actual class
138-
{
139-
final User actual = new User("Jane", 0);
140-
assertThatBean(UserAssert.class, User.class, actual)
141-
.isValid()
142-
.hasValidProperty("name")
143-
.hasValidProperty("age")
144-
.isNamedJane(); // should pass
145-
}
146-
// or emit the actual class
147-
{
148-
final User actual = new User("John", 1);
149-
assertThatBean(UserAssert.class, actual)
150-
.isValid()
151-
.hasValidProperty("name")
152-
.hasValidProperty("age")
153-
.isNamedJane(); // should fail
154-
}
155-
// or let it find whatever required
156-
{
157-
// tries to find a class named `UserAssert`
158-
// in the same package of `User` class
159-
final Object actual = new User("Jane", 0); // java.lang.Object
160-
assertThatVirtualBean(actual) // mind the name of the method
161-
.isValid()
162-
.hasValidProperty("name")
163-
.hasValidProperty("age")
164-
.isNamedJane();
165-
}
166-
}
167-
}
168-
```
169-
170-
### Verifying values against properties
78+
### Verifying a value against specific property
17179

17280
You can verify a value against specific property of specific bean type.
17381

17482
```java
17583
class Test {
176-
177-
@Test
178-
void test() {
84+
@Test void test2() {
17985
assertThatProperty("John").isValidFor(User.class, "name"); // should pass
180-
assertThatProperty(null).isValidFor(User.class, "name"); // should fail; @NotBlank
181-
assertThatProperty(" ").isValidFor(User.class, "name"); // should fail; @NotBlank
182-
assertThatProperty(31).isValidFor(User.class, "age"); // should pass
183-
assertThatProperty(-1).isValidFor(User.class, "age"); // should fail; @Min(0x00)
184-
assertThatProperty(297).isValidFor(User.class, "age"); // should fail; @Max(0x80)
86+
assertThatProperty( null).isValidFor(User.class, "name"); // should fail; @NotBlank
87+
assertThatProperty( "").isValidFor(User.class, "name"); // should fail; @NotBlank
88+
assertThatProperty( " ").isValidFor(User.class, "name"); // should fail; @NotBlank
89+
assertThatProperty( 31).isValidFor(User.class, "age"); // should pass
90+
assertThatProperty( -1).isValidFor(User.class, "age"); // should fail; @PositiveOrZero
91+
assertThatProperty( 297).isValidFor(User.class, "age"); // should fail; @Max(0x7F)
18592
}
18693
}
18794
```
18895

189-
Note that a bean value can also be validated against a property of another bean.
96+
Note that a bean value can also be validated against a property of another type.
19097

19198
```java
19299
class Test {
193-
194-
@Test
195-
void test() {
196-
assertThatBean(null).isValidFor(Registration.class, "user"); // should fail; @NotNull
100+
@Test void test3() {
101+
assertThatBean(new User("Jane", 21))
102+
.isValidFor(Registration.class, "user"); // should pass
103+
assertThatBean(null).isValidFor(Registration.class, "user"); // should fail; @NotNull
104+
assertThatBean(new User("John", 59))
105+
.isValidFor(SeniorRegistration.class, "user"); // should fail; age should be >= 60
197106
}
198107
}
199108
```
200109

201-
Note also that the `@Valid` is not honored by `Validator#validateProperty` method nor `Validator#validateValue` method.
110+
Note also that the `@Valid` annotation is not honored by `Validator#validateProperty` method nor `Validator#validateValue` method.
202111
See [6.1.1. Validation methods] (Jakarta Bean Validation specification).
203112

204113
```java
205114
class Test {
206-
207-
@Test
208-
void test() {
115+
@Test void test4() {
209116
User user = new User("John", 300); // not valid, obviously
210117
assertThatBean(user).isValid(); // so does fail
211118
assertThatBean(user).isValidFor(Registration.class, "user"); // DOES NOT FAIL!
212119
assertThatBean(null).isValidFor(Registration.class, "user"); // while fails
213-
assertThatBean(new Registration(user)).isValid(); // fails, after ...
120+
assertThatBean(new SeniorRegistration(user)).isValid(); // should fail
214121
}
215122
}
216123
```
217124

218-
### Using a `Validator` and/or targeting groups
125+
### Using a specific `Validator`
126+
127+
```java
128+
assertThatBean(user)
129+
.usingValidator(...) // null to reset
130+
.isValid();
131+
```
132+
133+
### Using validation groups
134+
135+
```java
136+
assertThatBean(user)
137+
.targetingGroups(..., ...) // null or empty to reset
138+
.isValid();
139+
```
219140

220-
You can configure a `Validator` or targeting groups for validating.
141+
### Consuming constraint violations
221142

222143
```java
223144
assertThatBean(user)
224-
.usingValidator(...) // null to reset
225-
.targetingGroups(...,...) // null or empty to reset
145+
.consumingViolations(cv -> {
146+
// ... debug
147+
})
226148
.isValid();
227149
```
228150

src/main/java/com/github/jinahya/assertj/validation/AbstractValidationAssert.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ protected AbstractValidationAssert(final ACTUAL actual, final Class<?> selfType)
5454
super(actual, selfType);
5555
}
5656

57-
@Override
58-
public SELF usingValidator(final Validator validator) {
59-
setValidator(validator);
60-
return myself;
61-
}
57+
// @Override
58+
// public SELF usingValidator(final Validator validator) {
59+
// setValidator(validator);
60+
// return myself;
61+
// }
6262

6363
@Override
6464
public SELF targetingGroups(final Class<?>... groups) {
@@ -78,22 +78,22 @@ public SELF consumingViolations(final Consumer<? super ConstraintViolation<?>> c
7878
* @return the validator configured to use.
7979
*/
8080
Validator getValidator() {
81-
if (validator == null) {
82-
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
83-
setValidator(factory.getValidator());
84-
}
81+
// if (validator == null) {
82+
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
83+
return factory.getValidator();
8584
}
86-
return validator;
85+
// }
86+
// return validator;
8787
}
8888

89-
/**
90-
* Replaces currently configured validator with specified value.
91-
*
92-
* @param validator new validator to use; may be {@code null}.
93-
*/
94-
void setValidator(final Validator validator) {
95-
this.validator = validator;
96-
}
89+
// /**
90+
// * Replaces currently configured validator with specified value.
91+
// *
92+
// * @param validator new validator to use; may be {@code null}.
93+
// */
94+
// void setValidator(final Validator validator) {
95+
// this.validator = validator;
96+
// }
9797

9898
/**
9999
* Returns currently configured groups targeted for validation.
@@ -126,10 +126,10 @@ void setConsumer(final Consumer<? super ConstraintViolation<?>> consumer) {
126126
this.consumer = consumer;
127127
}
128128

129-
/**
130-
* The validator being used.
131-
*/
132-
private Validator validator;
129+
// /**
130+
// * The validator being used.
131+
// */
132+
// private Validator validator;
133133

134134
/**
135135
* The targeting groups being used.

src/main/java/com/github/jinahya/assertj/validation/BeanAssert.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface BeanAssert<SELF extends BeanAssert<SELF, ACTUAL>, ACTUAL>
3636
/**
3737
* Verifies that the {@code actual} bean is valid.
3838
* <p>
39-
* {@snippet lang="java" id="example":
39+
* {@snippet lang = "java" id = "example":
4040
* class User {
4141
* @NotBlank String name;
4242
* @Max(0x7F) @PositiveOrZero int age;
@@ -53,7 +53,7 @@ public interface BeanAssert<SELF extends BeanAssert<SELF, ACTUAL>, ACTUAL>
5353
* // @end
5454
* }
5555
* }
56-
* }
56+
*}
5757
*
5858
* @return this assertion object.
5959
* @throws AssertionError when the {@code actual} is {@code null} or invalid.
@@ -64,7 +64,7 @@ public interface BeanAssert<SELF extends BeanAssert<SELF, ACTUAL>, ACTUAL>
6464
/**
6565
* Verifies that all constraints placed on the property of specified name, of {@code actual} bean, are validated.
6666
* <p>
67-
* {@snippet lang="java" id="example":
67+
* {@snippet lang = "java" id = "example":
6868
* class User {
6969
* @NotBlank String name;
7070
* @Max(0x7F) @PositiveOrZero int age;
@@ -84,7 +84,7 @@ public interface BeanAssert<SELF extends BeanAssert<SELF, ACTUAL>, ACTUAL>
8484
* // @end
8585
* }
8686
* }
87-
* }
87+
*}
8888
*
8989
* @param propertyName the name of the property to be verified as valid; not {@code null}.
9090
* @return this assertion object.

src/main/java/com/github/jinahya/assertj/validation/PropertyAssert.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface PropertyAssert<SELF extends PropertyAssert<SELF, ACTUAL>, ACTUA
3636
/**
3737
* Verifies that the {@code actual} value is valid for the property of specified name of specified bean type.
3838
* <p>
39-
* {@snippet lang="java" id="example":
39+
* {@snippet lang = "java" id = "example":
4040
* class User {
4141
* @NotBlank String name;
4242
* @Max(0x7F) @PositiveOrZero int age;
@@ -58,7 +58,7 @@ public interface PropertyAssert<SELF extends PropertyAssert<SELF, ACTUAL>, ACTUA
5858
* // @end
5959
* }
6060
* }
61-
* }
61+
*}
6262
*
6363
* @param beanType the bean type; not {@code null}.
6464
* @param propertyName the name of the property; must not be {@code null}.

0 commit comments

Comments
 (0)