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() ); } }
private Validator getValidatorWithValueExtractors() { return TestUtil.getConfigurationUnderTest() .addValueExtractor( new ValueHolderExtractor() ) .addValueExtractor( new UnwrapByDefaultWrapperValueExtractor() ) .addValueExtractor( new UnwrapByDefaultIntegerWrapperValueExtractor() ) .buildValidatorFactory() .getValidator(); }
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() ); } }
@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 ) ); }
@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() ); }
@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 ) ); }
@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() ); }
@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() ); }
private static Validator getValidatorWithValueExtractor() { return getConfigurationUnderTest() .addValueExtractor( new CustomContainerValueExtractor() ) .buildValidatorFactory() .getValidator(); }
@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 ); }
@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() ); }
@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." ); }
@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 ) ); }
@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 ); }
@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(); }
@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(); }
@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" ) ); }
@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 ) ); }
@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 ) ); }
@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 ) ); }