@Override public <T> Set<ConstraintViolation<T>> validateConstructorReturnValue(Constructor<? extends T> constructor, T t, Class<?>... classes) { return delegate.validateConstructorReturnValue(constructor, t, classes); } }
Object createdObject = ctx.getTarget(); violations = validator.forExecutables().validateConstructorReturnValue( ctx.getConstructor(), createdObject
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedForReturnValueCausesException() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = null; getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedAsSingleGroupCausesException() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = new Customer(); getExecutableValidator().validateConstructorReturnValue( constructor, returnValue, (Class<?>) null ); }
@Test(expectedExceptions = ConstraintDeclarationException.class) @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "a") public void testGroupConversionWithoutValidAnnotationOnConstructorReturnValue() throws Exception { UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue object = new UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue(); Constructor<UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue> constructor = UserWithGroupConversionButWithoutValidAnnotationOnConstructorReturnValue.class .getConstructor(); getExecutableValidator().validateConstructorReturnValue( constructor, object ); }
@Test(expectedExceptions = ValidationException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "j") public void testUnexpectedType() throws Exception { Constructor<Email> constructor = Email.class.getConstructor(); Email returnValue = new Email(); getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedForGroupsCausesException() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = new Customer(); getExecutableValidator().validateConstructorReturnValue( constructor, returnValue, (Class<?>[]) null ); }
@Test(expectedExceptions = IllegalArgumentException.class) @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "l") public void testNullPassedForConstructorCausesException() throws Exception { Constructor<Customer> constructor = null; Customer returnValue = new Customer(); getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "d") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_VALIDATIONROUTINE_TYPEVALIDATORRESOLUTION, id = "m") public void testTargetedTypeIsConstructor() throws NoSuchMethodException, SecurityException { assertEquals( CustomConstraint.ValidatorForSubClassC.callCounter, 0, "The validate method of ValidatorForSubClassC should not have been called yet." ); getExecutableValidator().validateConstructorReturnValue( SubClassC.class.getConstructor(), new SubClassC() ); assertTrue( CustomConstraint.ValidatorForSubClassC.callCounter > 0, "The validate method of ValidatorForSubClassC should have been called." ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "k") public void testNoViolations() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor(); Customer returnValue = new Customer( "Bob" ); Set<ConstraintViolation<Customer>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertNumberOfViolations( violations, 0 ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_RETURNVALUECONSTRAINTS, id = "a") public void testReturnValueConstraintsAreDeclaredByAnnotatingConstructors() throws Exception { Constructor<?> constructor = CalendarService.class.getConstructor(); Object returnValue = new CalendarService(); Set<ConstraintViolation<Object>> constraintViolations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertNotNull( constraintViolations ); assertNumberOfViolations( constraintViolations, 1 ); assertCorrectConstraintTypes( constraintViolations, OnlineCalendarService.class ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "f") @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "g") @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "h") @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "i") public void testOneViolationForCascadedValidation() throws Exception { Item leaf = new Item( "foo" ); Object createdObject = new OrderLine( leaf ); Constructor<OrderLine> constructor = OrderLine.class.getConstructor( Item.class ); Set<ConstraintViolation<Object>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, createdObject ); assertNumberOfViolations( violations, 1 ); ConstraintViolation<Object> violation = violations.iterator().next(); assertEquals( violation.getLeafBean(), leaf ); assertEquals( violation.getInvalidValue(), "foo" ); assertNull( violation.getExecutableParameters() ); assertEquals( violation.getExecutableReturnValue(), createdObject ); } }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "m") public void testValidateConstructorReturnValueYieldsConstraintViolationIfValidateExecutableIsSetToNONEOnTypeLevel() throws Exception { Constructor<LineItem> constructor = LineItem.class.getConstructor( String.class ); LineItem createdObject = new LineItem( null ); Set<ConstraintViolation<LineItem>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, createdObject ); assertCorrectConstraintTypes( violations, ValidLineItem.class ); assertThat( violations ).containsOnlyPaths( pathWith().constructor( LineItem.class ).returnValue() ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "m") public void testValidateConstructorReturnValueYieldsConstraintViolationIfValidateExecutableIsSetToNONEOnExecutableLevel() throws Exception { Constructor<WarehouseItem> constructor = WarehouseItem.class.getConstructor( String.class ); WarehouseItem createdObject = new WarehouseItem( null ); Set<ConstraintViolation<WarehouseItem>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, createdObject ); assertCorrectConstraintTypes( violations, ValidWarehouseItem.class ); assertThat( violations ).containsOnlyPaths( pathWith().constructor( WarehouseItem.class ).returnValue() ); } }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "f") public void testReturnValueConstraintAddedToConstructorInSubClass() throws Exception { Constructor<?> constructor = CalendarServiceSubClass.class.getConstructor( String.class ); Object returnValue = new CalendarServiceSubClass(); Set<ConstraintViolation<Object>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); //only the constraint on the sub-type constructor should be validated assertCorrectConstraintTypes( violations, ValidCalendarServiceSubClass.class ); assertThat( violations ).containsOnlyPaths( pathWith() .constructor( CalendarServiceSubClass.class ) .returnValue() ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_CASCADEDVALIDATION, id = "a") public void testConstructorReturnValueIsMarkedAsCascaded() throws Exception { Constructor<?> constructor = CalendarEvent.class.getConstructor( String.class ); Object returnValue = new CalendarEvent( null, null ); Set<ConstraintViolation<Object>> constraintViolations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertNotNull( constraintViolations ); assertNumberOfViolations( constraintViolations, 1 ); assertCorrectConstraintTypes( constraintViolations, NotNull.class ); assertThat( constraintViolations ).containsOnlyPaths( pathWith() .constructor( CalendarEvent.class ) .returnValue() .property( "type" ) ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_INHERITANCE, id = "f") public void testReturnValueMarkedAsCascadedAtConstructorInSuperAndSubClass() throws Exception { Constructor<?> constructor = CalendarServiceSubClass.class.getConstructor( long.class ); Object returnValue = new CalendarServiceSubClass(); Set<ConstraintViolation<Object>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertCorrectConstraintTypes( violations, Min.class ); assertThat( violations ).containsOnlyPaths( pathWith() .constructor( CalendarServiceSubClass.class ) .returnValue() .property( "mode" ) ); }
@Test @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_GROUPSEQUENCE_GROUPCONVERSION, id = "c") public void testGroupConversionIsAppliedOnConstructorReturnValue() throws Exception { //given Constructor<User> constructor = User.class.getConstructor( Address.class ); User createdObject = new User( TestAddresses.withInvalidStreet1() ); //when Set<ConstraintViolation<User>> constraintViolations = getExecutableValidator() .validateConstructorReturnValue( constructor, createdObject ); //then assertNumberOfViolations( constraintViolations, 1 ); assertThat( constraintViolations ).containsOnlyPaths( pathWith() .constructor( User.class ) .returnValue() .property( "mainAddress" ) .property( "street1" ) ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "k") public void testTwoConstraintsOfSameType() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor( CharSequence.class ); Customer returnValue = new Customer(); Set<ConstraintViolation<Customer>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertNumberOfViolations( violations, 2 ); assertCorrectConstraintTypes( violations, ValidCustomer.class, ValidCustomer.class ); assertThat( violations ).containsOnlyPaths( pathWith() .constructor( Customer.class ) .returnValue(), pathWith() .constructor( Customer.class ) .returnValue() ); }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_VALIDATORAPI_METHODLEVELVALIDATIONMETHODS, id = "k") public void testTwoViolations() throws Exception { Constructor<Customer> constructor = Customer.class.getConstructor( String.class ); Customer returnValue = new Customer(); Set<ConstraintViolation<Customer>> violations = getExecutableValidator().validateConstructorReturnValue( constructor, returnValue ); assertNumberOfViolations( violations, 2 ); assertCorrectConstraintTypes( violations, ValidCustomer.class, ValidBusinessCustomer.class ); assertThat( violations ).containsOnlyPaths( pathWith() .constructor( Customer.class ) .returnValue(), pathWith() .constructor( Customer.class ) .returnValue() ); }