@Override public <T> Set<ConstraintViolation<T>> validateConstructorParameters(Constructor<? extends T> constructor, Object[] objects, Class<?>... classes) { return delegate.validateConstructorParameters(constructor, objects, classes); }
public void validateConstructorInvocation(InvocationContext ctx) throws Exception { ExecutableValidator executableValidator = validator.forExecutables(); Set<? extends ConstraintViolation<?>> violations = executableValidator.validateConstructorParameters( ctx.getConstructor(), ctx.getParameters()
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorParameter() throws Exception { Constructor<UserWithGroupConversionButWithoutValidAnnotationConstructorParameter> constructor = UserWithGroupConversionButWithoutValidAnnotationConstructorParameter.class .getConstructor( List.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameters ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "i") public void testNullPassedForParameterValuesCausesException() throws Exception { Constructor<User> constructor = User.class.getConstructor( String.class ); Object[] parameterValues = null; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorParameter() throws Exception { Constructor<UserWithContainerElementGroupConversionButWithoutValidAnnotationConstructorParameter> constructor = UserWithContainerElementGroupConversionButWithoutValidAnnotationConstructorParameter.class .getConstructor( List.class ); Object[] parameters = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameters ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "i") public void testNullPassedAsSingleGroupCausesException() throws Exception { Constructor<User> constructor = User.class.getConstructor( String.class ); Object[] parameterValues = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameterValues, (Class<?>) null ); }
@Test(expectedExceptions = ValidationException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "g") public void testUnexpectedType() throws Exception { Constructor<Address> constructor = Address.class.getConstructor( String.class ); Object[] parameterValues = new Object[] { "S" }; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "i") public void testNullPassedForGroupsCausesException() throws Exception { Constructor<User> constructor = User.class.getConstructor( String.class ); Object[] parameterValues = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameterValues, (Class<?>[]) null ); }
@Override public void validateParameters(final Constructor constructor, final Object[] parameters) { final ExecutableValidator executableValidator = validatorFactory.getValidator().forExecutables(); final Set<ConstraintViolation<?>> violations = executableValidator.validateConstructorParameters(constructor, parameters); if (!violations.isEmpty()) { throw new ConstraintViolationException(violations); } } }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "i") public void testNullPassedForConstructorCausesException() throws Exception { Constructor<User> constructor = null; Object[] parameterValues = new Object[] { null }; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_CASCADEDVALIDATION, id = "b") public void testPassingNullToCascadedConstructorParameterCausesNoViolation() throws Exception { Constructor<?> constructor = CalendarEvent.class.getConstructor( User.class ); Object[] parameterValues = new Object[1]; Set<ConstraintViolation<Object>> constraintViolations = getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); assertNotNull( constraintViolations ); assertNumberOfViolations( constraintViolations, 0 ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "f") public void testParameterConstraintAddedToConstructorInSubClass() throws Exception { Constructor<?> constructor = CalendarServiceSubClass.class.getConstructor( int.class ); Object[] parameterValues = new Object[] { 4 }; Set<ConstraintViolation<Object>> violations = getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); assertCorrectConstraintTypes( violations, Min.class ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "e") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "m") public void testTargetedTypeIsConstructorParameter() throws NoSuchMethodException, SecurityException { assertEquals( CustomConstraint.ValidatorForSubClassF.callCounter, 0, "The validate method of ValidatorForSubClassF should not have been called yet." ); getExecutableValidator().validateConstructorParameters( SubClassFService.class.getConstructor( SubClassF.class ), new Object[]{ new SubClassF() } ); assertTrue( CustomConstraint.ValidatorForSubClassF.callCounter > 0, "The validate method of ValidatorForSubClassF should have been called." ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "h") public void testNoViolations() throws Exception { Constructor<User> constructor = User.class.getConstructor( String.class, CharSequence.class ); Object[] parameterValues = new Object[] { "Bob", "Smith" }; Set<ConstraintViolation<User>> violations = getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); assertNumberOfViolations( violations, 0 ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "c") @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "e") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_CROSSPARAMETERCONSTRAINTS, id = "b") public void testConstraintTargetParametersOnConstructorWithoutParametersCausesException() throws Exception { Constructor<?> constructor = Baz.class.getConstructor(); Object[] parameterValues = new Object[0]; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); fail( "Usage of ConstraintTarget.PARAMETERS not allowed for constructors without parameters. Expected exception wasn't thrown." ); }
/** * Validates constructor parameters. * @param ctr Constructor at pointcut * @param args Parameters of the method */ private void validateConstructor(final Constructor<Object> ctr, final Object... args) { this.checkForViolations( this.validator .forExecutables() .validateConstructorParameters( ctr, args ) ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS, id = "a") public void testConstructorParameterConstraintsAreDeclaredByAnnotatingParameters() throws Exception { Constructor<?> constructor = CalendarService.class.getConstructor( String.class ); Object[] parameterValues = new Object[1]; Set<ConstraintViolation<Object>> constraintViolations = getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); assertNotNull( constraintViolations ); assertNumberOfViolations( constraintViolations, 1 ); assertCorrectConstraintTypes( constraintViolations, NotNull.class ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "c") @SpecAssertion(section = Sections.CONSTRAINTSDEFINITIONIMPLEMENTATION_CONSTRAINTDEFINITION_PROPERTIES_VALIDATIONAPPLIESTO, id = "d") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_CROSSPARAMETERCONSTRAINTS, id = "c") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_RETURNVALUECONSTRAINTS, id = "b") public void testConstraintTargetImplicitOnConstructorWithParametersCausesException() throws Exception { Constructor<?> constructor = Bar.class.getConstructor( Date.class, Date.class ); Object[] parameterValues = new Object[2]; getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); fail( "Usage of ConstraintTarget.IMPLICIT not allowed for constructors with parameters. Expected exception wasn't thrown." ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "m") public void testValidateConstructorParametersYieldsConstraintViolationIfValidateExecutableIsSetToNONEOnTypeLevel() throws Exception { Constructor<LineItem> constructor = LineItem.class.getConstructor( String.class ); Object[] parameterValues = new Object[] { null }; Set<ConstraintViolation<LineItem>> violations = getExecutableValidator().validateConstructorParameters( constructor, parameterValues ); assertCorrectConstraintTypes( violations, NotNull.class ); assertThat( violations ).containsOnlyPaths( pathWith().constructor( LineItem.class ).parameter( "name", 0 ) ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_REQUIREMENTS_GRAPHVALIDATION, id = "l") public void cascading_on_container_element_of_constructor_parameter_is_applied() throws NoSuchMethodException, SecurityException { Set<ConstraintViolation<BarService>> constraintViolations = getExecutableValidator() .validateConstructorParameters( BarService.class.getConstructor( List.class ), new Object[]{ Arrays.asList( new Bar( 2 ) ) } ); assertThat( constraintViolations ).containsOnlyViolations( violationOf( Min.class ) .withPropertyPath( pathWith() .constructor( BarService.class ) .parameter( "bars", 0 ) .property( "number", true, null, 0, List.class, 0 ) ) ); }