Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1339 Add support for non parameterized type in value extraction
- Loading branch information
Showing
10 changed files
with
417 additions
and
5 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
.../java/org/hibernate/validator/internal/engine/cascading/OptionalDoubleValueExtractor.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,27 @@ | ||
/* | ||
* 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.internal.engine.cascading; | ||
|
||
import java.util.OptionalDouble; | ||
|
||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.UnwrapByDefault; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
/** | ||
* @author Guillaume Smet | ||
*/ | ||
@UnwrapByDefault | ||
public class OptionalDoubleValueExtractor implements ValueExtractor<@ExtractedValue(type = Double.class) OptionalDouble> { | ||
|
||
static final ValueExtractorDescriptor DESCRIPTOR = new ValueExtractorDescriptor( new OptionalDoubleValueExtractor() ); | ||
|
||
@Override | ||
public void extractValues(OptionalDouble originalValue, ValueReceiver receiver) { | ||
receiver.value( null, originalValue.isPresent() ? originalValue.getAsDouble() : null ); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ain/java/org/hibernate/validator/internal/engine/cascading/OptionalIntValueExtractor.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,27 @@ | ||
/* | ||
* 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.internal.engine.cascading; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.UnwrapByDefault; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
/** | ||
* @author Guillaume Smet | ||
*/ | ||
@UnwrapByDefault | ||
public class OptionalIntValueExtractor implements ValueExtractor<@ExtractedValue(type = Integer.class) OptionalInt> { | ||
|
||
static final ValueExtractorDescriptor DESCRIPTOR = new ValueExtractorDescriptor( new OptionalIntValueExtractor() ); | ||
|
||
@Override | ||
public void extractValues(OptionalInt originalValue, ValueReceiver receiver) { | ||
receiver.value( null, originalValue.isPresent() ? originalValue.getAsInt() : null ); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...in/java/org/hibernate/validator/internal/engine/cascading/OptionalLongValueExtractor.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,27 @@ | ||
/* | ||
* 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.internal.engine.cascading; | ||
|
||
import java.util.OptionalLong; | ||
|
||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.UnwrapByDefault; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
/** | ||
* @author Guillaume Smet | ||
*/ | ||
@UnwrapByDefault | ||
public class OptionalLongValueExtractor implements ValueExtractor<@ExtractedValue(type = Long.class) OptionalLong> { | ||
|
||
static final ValueExtractorDescriptor DESCRIPTOR = new ValueExtractorDescriptor( new OptionalLongValueExtractor() ); | ||
|
||
@Override | ||
public void extractValues(OptionalLong originalValue, ValueReceiver receiver) { | ||
receiver.value( null, originalValue.isPresent() ? originalValue.getAsLong() : null ); | ||
} | ||
} |
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
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
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
182 changes: 182 additions & 0 deletions
182
...e/validator/test/internal/engine/valuehandling/NonGenericContainerValueExtractorTest.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,182 @@ | ||
/* | ||
* 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.test.internal.engine.valuehandling; | ||
|
||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertCorrectConstraintTypes; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertNumberOfViolations; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat; | ||
import static org.hibernate.validator.testutil.ConstraintViolationAssert.pathWith; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.OptionalInt; | ||
import java.util.OptionalLong; | ||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Valid; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.valueextraction.ExtractedValue; | ||
import javax.validation.valueextraction.UnwrapByDefault; | ||
import javax.validation.valueextraction.ValueExtractor; | ||
|
||
import org.hibernate.validator.HibernateValidator; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* @author Guillaume Smet | ||
*/ | ||
public class NonGenericContainerValueExtractorTest { | ||
|
||
private Validator validator; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
validator = Validation.byProvider( HibernateValidator.class ) | ||
.configure() | ||
.addValueExtractor( new FooContainerValueExtractor() ) | ||
.buildValidatorFactory() | ||
.getValidator(); | ||
} | ||
|
||
@Test | ||
public void testValueExtraction() { | ||
Set<ConstraintViolation<Bean>> constraintViolations = validator.validate( Bean.valid() ); | ||
assertNumberOfViolations( constraintViolations, 0 ); | ||
|
||
constraintViolations = validator.validate( Bean.nullFoo() ); | ||
assertNumberOfViolations( constraintViolations, 1 ); | ||
assertCorrectConstraintTypes( constraintViolations, NotNull.class ); | ||
assertThat( constraintViolations ).containsOnlyPaths( | ||
pathWith() | ||
.property( "map" ) | ||
.containerElement( "<map value>", true, "key", null, Map.class, 1 ) | ||
); | ||
|
||
// check that cascaded validation is working correctly | ||
constraintViolations = validator.validate( Bean.invalid() ); | ||
assertNumberOfViolations( constraintViolations, 3 ); | ||
assertCorrectConstraintTypes( constraintViolations, Min.class, NotBlank.class, Max.class ); | ||
assertThat( constraintViolations ).containsOnlyPaths( | ||
pathWith() | ||
.property( "map" ) | ||
.property( "foo", true, "key", null, Map.class, 1 ) | ||
.property( "property" ), | ||
pathWith() | ||
.property( "map" ) | ||
.property( "foo", true, "key", null, Map.class, 1 ) | ||
.property( "optionalInt" ), | ||
pathWith() | ||
.property( "map" ) | ||
.property( "foo", true, "key", null, Map.class, 1 ) | ||
.property( "bar" ) | ||
.property( "optionalLong" ) | ||
); | ||
} | ||
|
||
@UnwrapByDefault | ||
private static class FooContainerValueExtractor implements ValueExtractor<@ExtractedValue(type = Foo.class) FooContainer> { | ||
|
||
@Override | ||
public void extractValues(FooContainer originalValue, ValueReceiver receiver) { | ||
receiver.value( null, originalValue.get() ); | ||
} | ||
} | ||
|
||
private static class Bean { | ||
|
||
private final Map<String, @NotNull @Valid FooContainer> map = new HashMap<>(); | ||
|
||
private Bean(FooContainer container) { | ||
map.put( "key", container ); | ||
} | ||
|
||
private static Bean valid() { | ||
return new Bean( FooContainer.valid() ); | ||
} | ||
|
||
private static Bean invalid() { | ||
return new Bean( FooContainer.invalid() ); | ||
} | ||
|
||
private static Bean nullFoo() { | ||
return new Bean( new FooContainer( null ) ); | ||
} | ||
} | ||
|
||
private static class FooContainer { | ||
|
||
@Valid | ||
private final Foo foo; | ||
|
||
private FooContainer(Foo foo) { | ||
this.foo = foo; | ||
} | ||
|
||
public Foo get() { | ||
return foo; | ||
} | ||
|
||
private static FooContainer valid() { | ||
return new FooContainer( Foo.valid() ); | ||
} | ||
|
||
private static FooContainer invalid() { | ||
return new FooContainer( Foo.invalid() ); | ||
} | ||
} | ||
|
||
private static class Foo { | ||
|
||
@NotBlank | ||
private final String property; | ||
|
||
@Min(value = 5) | ||
private final OptionalInt optionalInt; | ||
|
||
@Valid | ||
private Bar bar; | ||
|
||
private Foo(String property, OptionalInt optionalInt, Bar bar) { | ||
this.property = property; | ||
this.optionalInt = optionalInt; | ||
this.bar = bar; | ||
} | ||
|
||
private static Foo valid() { | ||
return new Foo( | ||
"value", | ||
OptionalInt.of( 6 ), | ||
new Bar( OptionalLong.of( 6 ) ) | ||
); | ||
} | ||
|
||
private static Foo invalid() { | ||
return new Foo( | ||
"", | ||
OptionalInt.of( 4 ), | ||
new Bar( OptionalLong.of( 18 ) ) | ||
); | ||
} | ||
} | ||
|
||
private static class Bar { | ||
|
||
@Max(value = 16) | ||
private final OptionalLong optionalLong; | ||
|
||
private Bar(OptionalLong optionalLong) { | ||
this.optionalLong = optionalLong; | ||
} | ||
} | ||
} |
Oops, something went wrong.