-
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
374 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
50 changes: 50 additions & 0 deletions
50
...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,50 @@ | ||
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[] |
13 changes: 13 additions & 0 deletions
13
...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,13 @@ | ||
//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[] |
33 changes: 33 additions & 0 deletions
33
...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,33 @@ | ||
//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; | ||
|
||
//end::include[] | ||
|
||
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[] |
5 changes: 5 additions & 0 deletions
5
...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,5 @@ | ||
package org.hibernate.validator.referenceguide.chapter06.customvalidatorwithdependency; | ||
|
||
public interface ZipCodeRepository { | ||
boolean isExist(String zipCode); | ||
} |
25 changes: 25 additions & 0 deletions
25
...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,25 @@ | ||
//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[] |
68 changes: 68 additions & 0 deletions
68
...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,68 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.testutil; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
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.