use of javax.validation.ConstraintViolationException in project wildfly by wildfly.
the class JPABeanValidationTestCase method testFailingBeanValidationNullAddress.
/**
* Test that a bean validation error is thrown
*
* @throws Exception
*/
@Test
public void testFailingBeanValidationNullAddress() throws Exception {
SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
try {
sfsb1.createEmployee("name", null, 2);
fail("should of thrown validation error for null address in Employee entity");
} catch (Throwable throwable) {
ConstraintViolationException constraintViolationException = null;
// find the ConstraintViolationException
while (throwable != null && !(throwable instanceof ConstraintViolationException)) {
throwable = throwable.getCause();
}
// should be null or instanceof ConstraintViolationException
constraintViolationException = (ConstraintViolationException) throwable;
assertTrue("expected ConstraintViolationException but got " + constraintViolationException, constraintViolationException instanceof ConstraintViolationException);
}
}
use of javax.validation.ConstraintViolationException in project spring-framework by spring-projects.
the class MethodValidationInterceptor method invoke.
@Override
@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?>[] groups = determineValidationGroups(invocation);
// Standard Bean Validation 1.1 API
ExecutableValidator execVal = this.validator.forExecutables();
Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<Object>> result;
try {
result = execVal.validateParameters(invocation.getThis(), 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(), invocation.getThis().getClass()));
result = execVal.validateParameters(invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
Object returnValue = invocation.proceed();
result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
return returnValue;
}
use of javax.validation.ConstraintViolationException in project dubbo by alibaba.
the class ValidationTest method testValidation.
@Test
public void testValidation() {
ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
providerContext.start();
try {
ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
consumerContext.start();
try {
ValidationService validationService = (ValidationService) consumerContext.getBean("validationService");
// Save OK
ValidationParameter parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
try {
parameter = new ValidationParameter();
parameter.setName("l");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// Delete OK
validationService.delete(2, "abc");
// Delete Error
try {
validationService.delete(2, "a");
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
// Delete Error
try {
validationService.delete(0, "abc");
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(2, null);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(0, null);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(2, violations.size());
}
} finally {
consumerContext.stop();
consumerContext.close();
}
} finally {
providerContext.stop();
providerContext.close();
}
}
use of javax.validation.ConstraintViolationException in project dubbo by alibaba.
the class ValidationTest method testValidation.
@Test
public void testValidation() {
ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
service.setApplication(new ApplicationConfig("validation-provider"));
service.setRegistry(new RegistryConfig("N/A"));
service.setProtocol(new ProtocolConfig("dubbo", 29582));
service.setInterface(ValidationService.class.getName());
service.setRef(new ValidationServiceImpl());
service.setValidation(String.valueOf(true));
service.export();
try {
ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
reference.setApplication(new ApplicationConfig("validation-consumer"));
reference.setInterface(ValidationService.class);
reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
ValidationService validationService = reference.get();
try {
// Save OK
ValidationParameter parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
try {
parameter = new ValidationParameter();
parameter.setName("l");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// verify save group, save error
try {
parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// and Update
try {
parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.relatedQuery(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertEquals(violations.size(), 2);
}
// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertTrue(violations.size() == 3);
Assert.assertNotNull(violations);
}
// Delete OK
validationService.delete(2, "abc");
// Delete Error
try {
validationService.delete(2, "a");
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
// Delete Error
try {
validationService.delete(0, "abc");
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(2, null);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(0, null);
Assert.fail();
} catch (ConstraintViolationException ve) {
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(2, violations.size());
}
} finally {
reference.destroy();
}
} finally {
service.unexport();
}
}
use of javax.validation.ConstraintViolationException in project spring-data-mongodb by spring-projects.
the class ValidatingMongoEventListener method onBeforeSave.
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeSave(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void onBeforeSave(BeforeSaveEvent<Object> event) {
LOG.debug("Validating object: {}", event.getSource());
Set violations = validator.validate(event.getSource());
if (!violations.isEmpty()) {
LOG.info("During object: {} validation violations found: {}", event.getSource(), violations);
throw new ConstraintViolationException(violations);
}
}
Aggregations