for (ConstraintViolation<Object> violation : violations) { String msg = "property " + violation.getPropertyPath(); msg += " in " + violation.getRootBeanClass(); msg += " " + violation.getMessage(); sortedViolations.add(msg);
ConstraintViolation<?> v = it.next(); sb.append(v.getRootBeanClass().getSimpleName()); sb.append("."); sb.append(v.getPropertyPath());
StringBuilder path = new StringBuilder(); try { Class<?> beanClazz = violation.getRootBeanClass(); final Iterator<Path.Node> iter = violation.getPropertyPath().iterator(); while (iter.hasNext()) {
@Test public void testToOneAssocNotValidated() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); PowerSupply ps = new PowerSupply(); ps.setPosition( "1" ); ps.setPower( new BigDecimal( 350 ) ); screen.setPowerSupply( ps ); try { s.persist( screen ); s.flush(); fail( "Associated objects should not be validated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next(); assertEquals( PowerSupply.class, constraintViolation.getRootBeanClass() ); } tx.rollback(); s.close(); }
@Test public void testEmbeddedCollection() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); PowerSupply ps = new PowerSupply(); screen.setPowerSupply( ps ); DisplayConnector conn = new DisplayConnector(); conn.setNumber( 0 ); screen.getConnectors().add( conn ); try { s.persist( screen ); s.flush(); fail( "Collection of embedded objects should be validated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next(); assertEquals( Screen.class, constraintViolation.getRootBeanClass() ); // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though assertEquals( "connectors[].number", constraintViolation.getPropertyPath().toString() ); } tx.rollback(); s.close(); }
@Test public void testEmbedded() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); PowerSupply ps = new PowerSupply(); screen.setPowerSupply( ps ); Button button = new Button(); button.setName( null ); button.setSize( 3 ); screen.setStopButton( button ); try { s.persist( screen ); s.flush(); fail( "@NotNull on embedded property is not evaluated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next(); assertEquals( Screen.class, cv.getRootBeanClass() ); // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though assertEquals( "stopButton.name", cv.getPropertyPath().toString() ); } tx.rollback(); s.close(); }
@Test public void testCollectionAssocNotValidated() { Session s = openSession(); Transaction tx = s.beginTransaction(); Screen screen = new Screen(); screen.setStopButton( new Button() ); screen.getStopButton().setName( "STOOOOOP" ); PowerSupply ps = new PowerSupply(); screen.setPowerSupply( ps ); Color c = new Color(); c.setName( "Blue" ); s.persist( c ); c.setName( null ); screen.getDisplayColors().add( c ); try { s.persist( screen ); s.flush(); fail( "Associated objects should not be validated" ); } catch ( ConstraintViolationException e ) { assertEquals( 1, e.getConstraintViolations().size() ); final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next(); assertEquals( Color.class, constraintViolation.getRootBeanClass() ); } tx.rollback(); s.close(); }
assertEquals( 1, e.getConstraintViolations().size() ); final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next(); assertEquals( Display.class, constraintViolation.getRootBeanClass() );
private ViolationExpectation(ConstraintViolation<?> violation, ViolationExpectationPropertiesToTest propertiesToTest) { this.constraintType = violation.getConstraintDescriptor().getAnnotation().annotationType(); if ( propertiesToTest.testRootBeanClass ) { withRootBeanClass( violation.getRootBeanClass() ); } if ( propertiesToTest.testMessage ) { withMessage( violation.getMessage() ); } if ( propertiesToTest.testInvalidValue ) { withInvalidValue( violation.getInvalidValue() ); } if ( propertiesToTest.testPropertyPath ) { withPropertyPath( new PathExpectation( violation.getPropertyPath() ) ); } }
getMethod(v.getRootBeanClass(), getterName); return ConstraintType.Type.PROPERTY;
if (cv1.getRootBeanClass() != null ? !compareClass(cv1.getRootBeanClass(), cv2.getRootBeanClass()) : cv2.getRootBeanClass() != null)
/** * @see ConstraintViolation#getRootBeanClass */ public Class<T> getRootBeanClass() { return constraintViolation.getRootBeanClass(); }
/** * Asserts that the error message and the root bean class of the given violation are equal to the expected message * and root bean class. * * @param violation The violation to verify. * @param rootBeanClass The expected root bean class. */ public static void assertConstraintViolation(ConstraintViolation<?> violation, Class<?> rootBeanClass) { assertEquals( violation.getRootBeanClass(), rootBeanClass, "Wrong root bean type" ); }
@Override public ViolationContent apply(ConstraintViolation input) { return new ViolationContent(input.getMessage(), input.getPropertyPath(), input.getRootBeanClass()); } };
@Override public ViolationContent apply(ConstraintViolation input) { return new ViolationContent(input.getMessage(), input.getPropertyPath(), input.getRootBeanClass()); } };
public static <T> String getErrorMessage(ConstraintViolation<T> violation) { String classDeclarationPath = getDeclarationPath(violation.getRootBeanClass()); String propertyPath = violation.getPropertyPath() + ""; if (isBlank(propertyPath)) { return format("[%s - %s]", classDeclarationPath, violation.getMessage()); } else { return format("[%s.%s - %s]", classDeclarationPath, propertyPath, violation.getMessage()); } }
protected String buildErrorMessage(ConstraintViolation<?> violation) { return "Value " + (violation.getInvalidValue() != null ? "'" + violation.getInvalidValue().toString() + "'" : "(null)") + " of " + violation.getRootBeanClass().getSimpleName() + "." + violation.getPropertyPath() + ": " + violation.getMessage(); }
@ExceptionHandler({ ConstraintViolationException.class }) public ResponseEntity<Object> handleConstraintViolation(final ConstraintViolationException ex, final WebRequest request) { logger.info(ex.getClass().getName()); // final List<String> errors = new ArrayList<String>(); for (final ConstraintViolation<?> violation : ex.getConstraintViolations()) { errors.add(violation.getRootBeanClass().getName() + " " + violation.getPropertyPath() + ": " + violation.getMessage()); } final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors); return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); }
/** * Utility method for adding error of ConstraintViolation. Usually when * a @Validated validation fails. * * @param cv * the ConstraintViolation */ private void addValidationError(ConstraintViolation<?> cv) { this.addValidationError(cv.getRootBeanClass().getSimpleName(), ((PathImpl) cv.getPropertyPath()).getLeafNode().asString(), cv.getInvalidValue(), cv.getMessage()); }
/** * Utility method for adding error of ConstraintViolation. Usually when * a @Validated validation fails. * * @param cv * the ConstraintViolation */ private void addValidationError(ConstraintViolation<?> cv) { this.addValidationError(cv.getRootBeanClass().getSimpleName(), ((PathImpl) cv.getPropertyPath()).getLeafNode().asString(), cv.getInvalidValue(), cv.getMessage()); }