use of javax.validation.ConstraintViolation in project camel by apache.
the class CMConfigurationTest method testMaxNumberOfPartsZero.
@Test
public void testMaxNumberOfPartsZero() throws Exception {
final CMConfiguration configuration = new CMConfiguration();
configuration.setProductToken(UUID.randomUUID().toString());
configuration.setDefaultFrom("DefaultFrom");
configuration.setDefaultMaxNumberOfParts(0);
configuration.setTestConnectionOnStartup(false);
final Set<ConstraintViolation<CMConfiguration>> constraintViolations = validator.validate(configuration);
Assert.isTrue(1 == constraintViolations.size());
}
use of javax.validation.ConstraintViolation in project camel by apache.
the class BeanValidatorRouteTest method validateShouldFailWithImpliciteDefaultGroup.
@Test
public void validateShouldFailWithImpliciteDefaultGroup() throws Exception {
if (isPlatform("aix")) {
// cannot run on aix
return;
}
final String url = "bean-validator://x";
final Car car = createCar("BMW", null);
try {
template.requestBody(url, car);
fail("should throw exception");
} catch (CamelExecutionException e) {
assertIsInstanceOf(BeanValidationException.class, e.getCause());
BeanValidationException exception = (BeanValidationException) e.getCause();
Set<ConstraintViolation<Object>> constraintViolations = exception.getConstraintViolations();
assertEquals(1, constraintViolations.size());
ConstraintViolation<Object> constraintViolation = constraintViolations.iterator().next();
assertEquals("licensePlate", constraintViolation.getPropertyPath().toString());
assertEquals(null, constraintViolation.getInvalidValue());
assertEquals("may not be null", constraintViolation.getMessage());
}
car.setLicensePlate("D-A");
Exchange exchange = template.request(url, new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(car);
}
});
assertNotNull(exchange);
}
use of javax.validation.ConstraintViolation in project dubbo by alibaba.
the class JValidator method validate.
public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception {
String methodClassName = clazz.getName() + "_" + toUpperMethoName(methodName);
Class<?> methodClass = null;
try {
methodClass = Class.forName(methodClassName, false, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
}
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();
Method method = clazz.getMethod(methodName, parameterTypes);
Object parameterBean = getMethodParameterBean(clazz, method, arguments);
if (parameterBean != null) {
if (methodClass != null) {
violations.addAll(validator.validate(parameterBean, Default.class, clazz, methodClass));
} else {
violations.addAll(validator.validate(parameterBean, Default.class, clazz));
}
}
for (Object arg : arguments) {
validate(violations, arg, clazz, methodClass);
}
if (violations.size() > 0) {
throw new ConstraintViolationException("Failed to validate service: " + clazz.getName() + ", method: " + methodName + ", cause: " + violations, violations);
}
}
use of javax.validation.ConstraintViolation in project spring-petclinic by spring-projects.
the class ValidatorTests method shouldNotValidateWhenFirstNameEmpty.
@Test
public void shouldNotValidateWhenFirstNameEmpty() {
LocaleContextHolder.setLocale(Locale.ENGLISH);
Person person = new Person();
person.setFirstName("");
person.setLastName("smith");
Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
assertThat(constraintViolations.size()).isEqualTo(1);
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
assertThat(violation.getMessage()).isEqualTo("may not be empty");
}
use of javax.validation.ConstraintViolation in project ORCID-Source by ORCID.
the class ManagePasswordOptionsValidationFormTest method testPasswordsMatchValidation.
@Test
public void testPasswordsMatchValidation() throws Exception {
String eightMixedCharactersDigitsAndSymbol = "p4s]sW[rd";
String incorrectRetype = "p4s]sT]rd";
ManagePasswordOptionsForm form = new ManagePasswordOptionsForm();
form.setPassword(eightMixedCharactersDigitsAndSymbol);
form.setRetypedPassword(incorrectRetype);
Set<ConstraintViolation<ManagePasswordOptionsForm>> violations = validator.validate(form);
Map<String, String> allErrorValues = retrieveErrorKeyAndMessage(violations);
String passwordFormatError = allErrorValues.get("password");
String retypedPasswordFormatError = allErrorValues.get("retypedPassword");
allErrorValues = retrieveErrorKeyAndMessage(violations);
// the passwords themselves should be ok as they conform to the format,
// problem is they don't match
assertNull(passwordFormatError);
assertNull(retypedPasswordFormatError);
Set<String> fieldLevelErrors = retrieveErrorValuesOnly(violations);
assertTrue(fieldLevelErrors.contains("The password and confirm password field must match"));
// now match them
form.setRetypedPassword("p4s]sW[rd");
violations = validator.validate(form);
allErrorValues = retrieveErrorKeyAndMessage(violations);
passwordFormatError = allErrorValues.get("password");
retypedPasswordFormatError = allErrorValues.get("retypedPassword");
allErrorValues = retrieveErrorKeyAndMessage(violations);
assertNull(passwordFormatError);
assertNull(retypedPasswordFormatError);
fieldLevelErrors = retrieveErrorValuesOnly(violations);
assertFalse(fieldLevelErrors.contains("The password and confirmed password must match"));
}
Aggregations