-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1921 Create a tester library for constraint validators with depend…
…ency injection
- Loading branch information
Showing
9 changed files
with
385 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...renceguide/chapter06/customvalidatorwithdependency/CustomValidatorWithDependencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
import static org.easymock.EasyMock.eq; | ||
import static org.easymock.EasyMock.expect; | ||
import static org.easymock.EasyMock.isA; | ||
import static org.easymock.EasyMock.mock; | ||
import static org.easymock.EasyMock.replay; | ||
import static org.easymock.EasyMock.verify; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
|
||
import org.hibernate.validator.testutil.PreconfiguredValidatorsValidatorFactory; | ||
|
||
import org.junit.Test; | ||
|
||
@SuppressWarnings("unused") | ||
//tag::field[] | ||
public class CustomValidatorWithDependencyTest { | ||
|
||
@Test | ||
public void mockCustomValidatorWithDependency() { | ||
ZipCodeValidator zipCodeValidator = mock( ZipCodeValidator.class ); | ||
|
||
expect( zipCodeValidator.isValid( eq( "1234" ), isA( ConstraintValidatorContext.class ) ) ) | ||
.andStubReturn( true ); | ||
zipCodeValidator.initialize( isA( ZipCode.class ) ); | ||
|
||
replay( zipCodeValidator ); | ||
|
||
ValidatorFactory validatorFactory = PreconfiguredValidatorsValidatorFactory.builder() | ||
.defaultValidators( Map.of( ZipCodeValidator.class, zipCodeValidator ) ) | ||
.build(); | ||
|
||
Validator validator = validatorFactory.getValidator(); | ||
|
||
Person person = new Person( "1234" ); | ||
|
||
Set<ConstraintViolation<Person>> constraintViolations = validator.validate( person ); | ||
|
||
assertEquals( 0, constraintViolations.size() ); | ||
|
||
verify( zipCodeValidator ); | ||
} | ||
} | ||
//end::field[] |
17 changes: 17 additions & 0 deletions
17
...rg/hibernate/validator/referenceguide/chapter06/customvalidatorwithdependency/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
//tag::include[] | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
public class Person { | ||
|
||
@ZipCode | ||
private String zipCode; | ||
|
||
public Person(String zipCode) { | ||
this.zipCode = zipCode; | ||
} | ||
} | ||
//end::include[] |
35 changes: 35 additions & 0 deletions
35
...g/hibernate/validator/referenceguide/chapter06/customvalidatorwithdependency/ZipCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
//tag::include[] | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE_USE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
//tag::include[] | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, TYPE_USE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = ZipCodeValidator.class) | ||
@Documented | ||
public @interface ZipCode { | ||
|
||
String message() default "{org.hibernate.validator.referenceguide.chapter06." + | ||
"customvalidatorwithdependency.ZipCode.message}"; | ||
|
||
Class<?>[] groups() default { }; | ||
|
||
Class<? extends Payload>[] payload() default { }; | ||
} | ||
//end::include[] |
9 changes: 9 additions & 0 deletions
9
...e/validator/referenceguide/chapter06/customvalidatorwithdependency/ZipCodeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
public interface ZipCodeRepository { | ||
boolean isExist(String zipCode); | ||
} |
29 changes: 29 additions & 0 deletions
29
...te/validator/referenceguide/chapter06/customvalidatorwithdependency/ZipCodeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
//tag::include[] | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
//end::include[] | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
//tag::include[] | ||
public class ZipCodeValidator implements ConstraintValidator<ZipCode, String> { | ||
|
||
@Inject | ||
public ZipCodeRepository zipCodeRepository; | ||
|
||
@Override | ||
public boolean isValid(String zipCode, ConstraintValidatorContext constraintContext) { | ||
if ( zipCode == null ) { | ||
return true; | ||
} | ||
|
||
return zipCodeRepository.isExist( zipCode ); | ||
} | ||
} | ||
//end::include[] |
66 changes: 66 additions & 0 deletions
66
...c/main/java/org/hibernate/validator/testutil/PreconfiguredConstraintValidatorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.validator.testutil; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorFactory; | ||
|
||
public class PreconfiguredConstraintValidatorFactory implements ConstraintValidatorFactory { | ||
|
||
private final Map<Class<? extends ConstraintValidator>, ConstraintValidator<?, ?>> defaultValidators; | ||
private final ConstraintValidatorFactory delegated; | ||
|
||
private PreconfiguredConstraintValidatorFactory(Builder builder) { | ||
this.defaultValidators = builder.defaultValidators; | ||
this.delegated = builder.delegated; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { | ||
if ( defaultValidators.containsKey( key ) ) { | ||
return (T) defaultValidators.get( key ); | ||
} | ||
|
||
return delegated.getInstance( key ); | ||
} | ||
|
||
@Override | ||
public void releaseInstance(ConstraintValidator<?, ?> instance) { | ||
delegated.releaseInstance( instance ); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private ConstraintValidatorFactory delegated; | ||
private final Map<Class<? extends ConstraintValidator>, ConstraintValidator<?, ?>> defaultValidators = new HashMap<>(); | ||
|
||
private Builder() { | ||
} | ||
|
||
public Builder defaultValidators( | ||
Map<Class<? extends ConstraintValidator>, ConstraintValidator<?, ?>> validators) { | ||
this.defaultValidators.putAll( validators ); | ||
return this; | ||
} | ||
|
||
public Builder delegated( | ||
ConstraintValidatorFactory delegated) { | ||
this.delegated = delegated; | ||
return this; | ||
} | ||
|
||
public PreconfiguredConstraintValidatorFactory build() { | ||
return new PreconfiguredConstraintValidatorFactory( this ); | ||
} | ||
} | ||
} |
Oops, something went wrong.