use of jakarta.validation.ConstraintViolationException in project spring-framework by spring-projects.
the class MethodValidationInterceptor method invoke.
@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
// Avoid Validator invocation on FactoryBean.getObjectType/isSingleton
if (isFactoryBeanMetadataMethod(invocation.getMethod())) {
return invocation.proceed();
}
Class<?>[] groups = determineValidationGroups(invocation);
// Standard Bean Validation 1.1 API
ExecutableValidator execVal = this.validator.forExecutables();
Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<Object>> result;
Object target = invocation.getThis();
Assert.state(target != null, "Target must not be null");
try {
result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
} catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), target.getClass()));
result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
Object returnValue = invocation.proceed();
result = execVal.validateReturnValue(target, methodToValidate, returnValue, groups);
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
return returnValue;
}
use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testEmbedded.
@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();
}
use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testNonLazyAssocFieldWithConstraintsFailureExpected.
@Test
public void testNonLazyAssocFieldWithConstraintsFailureExpected() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
screen.setPowerSupply(null);
try {
s.persist(screen);
s.flush();
fail("@NotNull on a non lazy association is not evaluated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
}
tx.rollback();
s.close();
}
use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testEmbeddedCollection.
@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();
}
use of jakarta.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testToOneAssocNotValidated.
@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();
}
Aggregations