@Test @SpecAssertion(section = Sections.VALIDATIONAPI_MESSAGE_CUSTOMRESOLUTION, id = "g") public void testExceptionDuringMessageInterpolationIsWrappedIntoValidationException() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); ExceptionThrowingMessageInterpolator interpolator = new ExceptionThrowingMessageInterpolator(); Validator validator = factory.usingContext().messageInterpolator( interpolator ).getValidator(); try { validator.validate( new TestBeanWithPropertyConstraint( "Bob" ) ); fail( "Expected exception wasn't thrown." ); } catch ( ValidationException ve ) { assertEquals( ve.getCause(), interpolator.exception ); } }
@Test(expected = ConstraintDeclarationException.class) public void shouldHandleMissingStartDate() throws Throwable { MissingStartDate obj = new MissingStartDate(); obj.end = new Date(); try { validator.validate(obj); } catch (ValidationException e) { throw e.getCause(); } }
@Test(expected = ConstraintDeclarationException.class) public void shouldHandleMissingEndDate() throws Throwable { MissingEndDate obj = new MissingEndDate(); obj.start = new Date(); try { validator.validate(obj); } catch (ValidationException e) { throw e.getCause(); } }
@Test(expected = UnexpectedTypeException.class) public void shouldHandleWrongType() throws Throwable { WrongType obj = new WrongType(); obj.start = ""; obj.end = ""; try { validator.validate(obj); } catch (ValidationException e) { throw e.getCause(); } }
@Test(expectedExceptions = UnsupportedOperationException.class, expectedExceptionsMessageRegExp = "Exception in ParameterNameProvider") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_NAMINGPARAMETERS, id = "b") public void testExceptionInParameterNameProviderIsWrappedIntoValidationException() throws Throwable { Validator validator = TestUtil.getConfigurationUnderTest() .parameterNameProvider( new BrokenCustomParameterNameProvider() ) .buildValidatorFactory() .getValidator(); try { Object object = new User(); Method method = User.class.getMethod( "setNames", String.class, String.class ); Object[] parameters = new Object[] { null, null }; validator.forExecutables().validateParameters( object, method, parameters ); fail( "Expected exception wasn't thrown" ); } catch ( ValidationException e ) { throw e.getCause(); } }
private Object bindAndReply(Request request, Page page, Object instance) throws IOException { // bind request (sets request params, etc). binder.bind(request, instance); Object response = null; try { // call the appropriate handler. response = fireEvent(request, page, instance); } catch (ValidationException ve) { ConstraintViolationException cve = (ConstraintViolationException) ve.getCause(); Set<? extends ConstraintViolation<?>> scv = (Set<? extends ConstraintViolation<?>>) cve.getConstraintViolations(); List<String> errors = validationConvertor.to(scv); response = Reply.with(errors).as(Json.class).badRequest(); } return response; }
private Object bindAndReply(Request request, Page page, Object instance) throws IOException { // bind request (sets request params, etc). binder.bind(request, instance); Object response = null; try { // call the appropriate handler. response = fireEvent(request, page, instance); } catch (ValidationException ve) { ConstraintViolationException cve = (ConstraintViolationException) ve.getCause(); Set<? extends ConstraintViolation<?>> scv = (Set<? extends ConstraintViolation<?>>) cve.getConstraintViolations(); List<String> errors = validationConvertor.to(scv); response = Reply.with(errors).as(Json.class).badRequest(); } return response; }
@Post @As(Json.class) Reply<?> postPerson(@As(Json.class) Person person, Request request) { try { request.validate(person); } catch (ValidationException ve) { ConstraintViolationException cve = (ConstraintViolationException) ve.getCause(); Set<? extends ConstraintViolation<?>> scv = (Set<? extends ConstraintViolation<?>>) cve.getConstraintViolations(); return Reply.with(validationConverter.to(scv)).badRequest(); } return Reply.with(person); }