本文整理了Java中javax.validation.Configuration.addValueExtractor()
方法的一些代码示例,展示了Configuration.addValueExtractor()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.addValueExtractor()
方法的具体详情如下:
包路径:javax.validation.Configuration
类名称:Configuration
方法名:addValueExtractor
[英]Adds a value extractor. Has priority over any extractor for the same type and type parameter detected through the service loader or given in the XML configuration.
[中]添加值提取器。对于通过服务加载器检测到的或XML配置中给定的相同类型和类型参数,具有高于任何提取器的优先级。
代码示例来源:origin: hibernate/hibernate-validator
private void addValueExtractorBeans(Configuration<?> config) {
Map<ValueExtractorDescriptor.Key, ValueExtractorDescriptor> valueExtractorDescriptors = createValidationXmlValueExtractors( config ).stream()
.collect( Collectors.toMap( ValueExtractorDescriptor::getKey, Function.identity() ) );
// We only add the service loader value extractors if we don't already have a value extractor defined in the XML configuration
// as the XML configuration has precedence over the service loader value extractors.
for ( ValueExtractorDescriptor serviceLoaderValueExtractorDescriptor : createServiceLoaderValueExtractors() ) {
valueExtractorDescriptors.putIfAbsent( serviceLoaderValueExtractorDescriptor.getKey(), serviceLoaderValueExtractorDescriptor );
}
for ( ValueExtractorDescriptor valueExtractorDescriptor : valueExtractorDescriptors.values() ) {
config.addValueExtractor( valueExtractorDescriptor.getValueExtractor() );
}
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
private Validator getValidatorWithValueExtractors() {
return TestUtil.getConfigurationUnderTest()
.addValueExtractor( new ValueHolderExtractor() )
.addValueExtractor( new UnwrapByDefaultWrapperValueExtractor() )
.addValueExtractor( new UnwrapByDefaultIntegerWrapperValueExtractor() )
.buildValidatorFactory()
.getValidator();
}
代码示例来源:origin: org.hibernate.validator/hibernate-validator-cdi
private void addValueExtractorBeans(Configuration<?> config) {
Map<ValueExtractorDescriptor.Key, ValueExtractorDescriptor> valueExtractorDescriptors = createValidationXmlValueExtractors( config ).stream()
.collect( Collectors.toMap( ValueExtractorDescriptor::getKey, Function.identity() ) );
// We only add the service loader value extractors if we don't already have a value extractor defined in the XML configuration
// as the XML configuration has precedence over the service loader value extractors.
for ( ValueExtractorDescriptor serviceLoaderValueExtractorDescriptor : createServiceLoaderValueExtractors() ) {
valueExtractorDescriptors.putIfAbsent( serviceLoaderValueExtractorDescriptor.getKey(), serviceLoaderValueExtractorDescriptor );
}
for ( ValueExtractorDescriptor valueExtractorDescriptor : valueExtractorDescriptors.values() ) {
config.addValueExtractor( valueExtractorDescriptor.getValueExtractor() );
}
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ConstraintDeclarationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_ALGORITHM_CONSTRAINTS, id = "e")
public void parallelValueExtractorDefinitionsCausesException() {
Validator validator = Validation.byDefaultProvider()
.configure()
.addValueExtractor( new IWrapper21ValueExtractor0() )
.addValueExtractor( new IWrapper211ValueExtractor0() )
.addValueExtractor( new IWrapper212ValueExtractor0() )
.addValueExtractor( new IWrapper22ValueExtractor0() )
.addValueExtractor( new IWrapper221ValueExtractor0() )
.buildValidatorFactory()
.getValidator();
validator.validate( new ContainerElementEntity2( null, null ) );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ConstraintDeclarationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_IMPLICITUNWRAPPING, id = "c")
public void validate_forced_unwrapping_having_two_type_parameters_and_two_maximally_specific_value_extractors_raises_exception() {
Validator validator = TestUtil.getConfigurationUnderTest()
.addValueExtractor( new WrapperWithTwoTypeArgumentsFirstValueExtractor() )
.addValueExtractor( new WrapperWithTwoTypeArgumentsSecondValueExtractor() )
.buildValidatorFactory()
.getValidator();
validator.validate( new BeanWithWrapperWithTwoTypeArgumentsAndForcedUnwrapping() );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ConstraintDeclarationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_ALGORITHM_CASCADED, id = "f")
public void parallelValueExtractorDefinitionsCausesException() {
Validator validator = Validation.byDefaultProvider()
.configure()
.addValueExtractor( new IWrapper211ValueExtractor0() )
.addValueExtractor( new IWrapper212ValueExtractor0() )
.addValueExtractor( new Wrapper2ValueExtractor1() )
.buildValidatorFactory()
.getValidator();
validator.validate( new CascadingEntity2( null ) );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ConstraintDeclarationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_IMPLICITUNWRAPPING, id = "c")
public void validate_implicit_unwrapping_having_two_type_parameters_and_two_maximally_specific_value_extractors_marked_with_unwrap_by_default_raises_exception() {
Validator validator = TestUtil.getConfigurationUnderTest()
.addValueExtractor( new UnwrapByDefaultWrapperWithTwoTypeArgumentsFirstValueExtractor() )
.addValueExtractor( new UnwrapByDefaultWrapperWithTwoTypeArgumentsSecondValueExtractor() )
.buildValidatorFactory()
.getValidator();
validator.validate( new BeanWithWrapperWithTwoTypeArguments() );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ValueExtractorDeclarationException.class)
@SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_CONFIGURATION, id = "h")
public void configuringMultipleExtractorsForSameTypeAndTypeUseCausesException() throws Exception {
Validation.byDefaultProvider()
.configure()
.addValueExtractor( new ReferenceValueExtractor0() )
.addValueExtractor( new ReferenceValueExtractor1() );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
private static Validator getValidatorWithValueExtractor() {
return getConfigurationUnderTest()
.addValueExtractor( new CustomContainerValueExtractor() )
.buildValidatorFactory()
.getValidator();
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "b")
public void valueExtractorNotInvokedIfContainerIsNull() {
StringContainerHolder containerHolder = new StringContainerHolder( null );
Validator validator = Validation.byDefaultProvider().configure()
.addValueExtractor( new ContainerValueExtractorCountCalls() )
.buildValidatorFactory()
.getValidator();
validator.validate( containerHolder );
assertEquals( ContainerValueExtractorCountCalls.callCounter, 0 );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ValidationException.class)
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "i")
@SpecAssertion(section = Sections.EXCEPTION, id = "a")
public void exception_in_value_extractor_is_wrapped() {
Validator validator = TestUtil.getConfigurationUnderTest()
.addValueExtractor( new ThrowsExceptionInExtractValuesValueHolderExtractor() )
.buildValidatorFactory()
.getValidator();
validator.validate( new Entity() );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "h")
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "m")
public void testTargetedTypeIsTypeArgumentForNonGenericContainerWithValueExtractorWithExtractedType() {
assertEquals(
CustomConstraint.ValidatorForSubClassI.callCounter,
0,
"The validate method of ValidatorForSubClassI should not have been called yet." );
TestUtil.getConfigurationUnderTest()
.addValueExtractor( new SubClassIContainerValueExtractor() )
.buildValidatorFactory().getValidator()
.validate( new SubClassIHolder() );
assertTrue(
CustomConstraint.ValidatorForSubClassI.callCounter > 0,
"The validate method of ValidatorForSubClassI should have been called." );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ConstraintDeclarationException.class)
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_ALGORITHM_CASCADED, id = "c")
public void customGenericTypeWithCascadingButNoValueExtractorThrowsException() {
Validator validator = Validation.byDefaultProvider()
.configure()
.addValueExtractor( new IWrapper111ValueExtractor1() )
.buildValidatorFactory()
.getValidator();
validator.validate( new CascadingEntity1( null ) );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "a")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "a")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "c")
public void instanceAndValueReceiverPassedToExtractValues() {
Container<String> container = new Container<String>( null );
StringContainerHolder containerHolder = new StringContainerHolder( container );
Validator validator = Validation.byDefaultProvider().configure()
.addValueExtractor( new ContainerValueExtractorCompareInstance( container ) )
.buildValidatorFactory()
.getValidator();
validator.validate( containerHolder );
assertEquals( ContainerValueExtractorCompareInstance.callCounter, 1 );
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ValueExtractorDefinitionException.class)
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "b")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "f")
@SpecAssertion(section = Sections.EXCEPTION_VALUEEXTRACTORDEFINITION, id = "a")
public void noExtractedValueThrowsException() {
Validation.byDefaultProvider().configure()
.addValueExtractor( new NoExtractedValueValueExtractor() )
.buildValidatorFactory()
.getValidator();
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test(expectedExceptions = ValueExtractorDefinitionException.class)
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "b")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "f")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXAMPLES, id = "a")
@SpecAssertion(section = Sections.EXCEPTION_VALUEEXTRACTORDEFINITION, id = "a")
public void severalExtractedValuesThrowException() {
Validation.byDefaultProvider().configure()
.addValueExtractor( new SeveralExtractedValuesValueExtractor() )
.buildValidatorFactory()
.getValidator();
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "h")
public void nullNodeName() {
Container<ContainerElement> container = new Container<ContainerElement>( CustomConstraint.INSTANCE );
ContainerHolder containerHolder = new ContainerHolder( container );
Validator validator = Validation.byDefaultProvider().configure()
.addValueExtractor( new NullNodeNameContainerValueExtractor() )
.buildValidatorFactory()
.getValidator();
Set<ConstraintViolation<ContainerHolder>> violations = validator.validate( containerHolder );
assertThat( violations ).containsOnlyPaths(
pathWith().property( "container" )
);
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_IMPLICITUNWRAPPING, id = "c")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "k")
public void validate_implicit_unwrapping_having_two_type_parameters_and_only_one_maximally_specific_value_extractor_marked_with_unwrap_by_default_is_ok() {
Validator validator = TestUtil.getConfigurationUnderTest()
.addValueExtractor( new UnwrapByDefaultWrapperWithTwoTypeArgumentsFirstValueExtractor() )
.addValueExtractor( new WrapperWithTwoTypeArgumentsSecondValueExtractor() )
.buildValidatorFactory()
.getValidator();
Set<ConstraintViolation<BeanWithWrapperWithTwoTypeArguments>> constraintViolations = validator.validate( new BeanWithWrapperWithTwoTypeArguments() );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Min.class )
.withPropertyPath( pathWith()
.property( "wrapper" )
.containerElement( "first", false, null, null, WrapperWithTwoTypeArguments.class, 0 )
)
.withInvalidValue( 5L )
);
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_VALUEEXTRACTORRESOLUTION_IMPLICITUNWRAPPING, id = "c")
@SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "k")
public void validate_implicit_unwrapping_having_two_type_parameters_and_only_one_maximally_specific_value_extractor_is_ok() {
Validator validator = TestUtil.getConfigurationUnderTest()
.addValueExtractor( new UnwrapByDefaultWrapperWithTwoTypeArgumentsFirstValueExtractor() )
.buildValidatorFactory()
.getValidator();
Set<ConstraintViolation<BeanWithWrapperWithTwoTypeArguments>> constraintViolations = validator.validate( new BeanWithWrapperWithTwoTypeArguments() );
assertThat( constraintViolations ).containsOnlyViolations(
violationOf( Min.class )
.withPropertyPath( pathWith()
.property( "wrapper" )
.containerElement( "first", false, null, null, WrapperWithTwoTypeArguments.class, 0 )
)
.withInvalidValue( 5L )
);
}
代码示例来源:origin: org.hibernate.beanvalidation.tck/beanvalidation-tck-tests
@Test
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "d")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION, id = "g")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "a")
@SpecAssertion(section = Sections.VALUEEXTRACTORDEFINITION_EXTRACTEDVALUE, id = "c")
public void iterableValue() {
Container<ContainerElement> container = new Container<ContainerElement>( CustomConstraint.INSTANCE );
ContainerHolder containerHolder = new ContainerHolder( container );
Validator validator = Validation.byDefaultProvider().configure()
.addValueExtractor( new IterableValueContainerValueExtractor() )
.buildValidatorFactory()
.getValidator();
Set<ConstraintViolation<ContainerHolder>> violations = validator.validate( containerHolder );
assertThat( violations ).containsOnlyPaths(
pathWith()
.property( "container" )
.containerElement( "<node name>", true, null, null, Container.class, 0 )
);
}
内容来源于网络,如有侵权,请联系作者删除!