use of javax.validation.Validator in project wildfly by wildfly.
the class BootStrapValidationTestCase method testSafeHTML.
/**
* Ensure that including a cross-site script attack triggers the @SafeHtml validation rule
*/
@Test
public void testSafeHTML() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
assertNotNull(configuration);
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
Employee emp = new Employee();
// create employee
emp.setFirstName("Joe");
emp.setLastName("Cocker");
emp.setEmail("none@jboss.org");
emp.setWebsite("<script> Cross-site scripting http://en.wikipedia.org/wiki/Joe_Cocker <script/>.");
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
}
use of javax.validation.Validator in project wildfly by wildfly.
the class ConstraintValidationTestCase method testObjectGraphValidation.
@Test
public void testObjectGraphValidation() throws NamingException {
Validator validator = (Validator) new InitialContext().lookup("java:comp/Validator");
// create first passenger
UserBean user1 = new UserBean("MADHUMITA", "SADHUKHAN");
user1.setEmail("madhu@gmail.com");
user1.setAddress("REDHAT Brno");
// create second passenger
UserBean user2 = new UserBean("Mickey", "Mouse");
user2.setAddress("");
List<UserBean> passengers = new ArrayList<UserBean>();
passengers.add(user1);
passengers.add(user2);
// Create a Car
Car car = new Car("CET5678", passengers);
car.setModel("SKODA Octavia");
final Set<ConstraintViolation<Car>> errorresult = validator.validate(car);
Iterator<ConstraintViolation<Car>> it1 = errorresult.iterator();
String message = "";
while (it1.hasNext()) {
ConstraintViolation<Car> cts = it1.next();
String mess = cts.getMessage();
if (mess.contains("Please get a valid address")) {
message = mess;
}
}
assertEquals(2, errorresult.size());
assertTrue(message.contains("Please get a valid address"));
}
use of javax.validation.Validator in project wildfly by wildfly.
the class ExpressionLanguageTestCase method testValidationUsingExpressionLanguage.
@Test
public void testValidationUsingExpressionLanguage() {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean());
assertEquals(1, violations.size());
assertEquals("'Bob' is too short, it should at least be 5 characters long.", violations.iterator().next().getMessage());
}
use of javax.validation.Validator in project wildfly by wildfly.
the class LazyValidatorFactoryTestCase method testHibernateValidatorIsUsedAsProviderByDefault.
@Test
public void testHibernateValidatorIsUsedAsProviderByDefault() {
HibernateValidatorFactory hibernateValidatorFactory = validatorFactory.unwrap(HibernateValidatorFactory.class);
assertNotNull("LazyValidatorFactory should delegate to the HV factory by default", hibernateValidatorFactory);
Validator validator = validatorFactory.getValidator();
assertNotNull("LazyValidatorFactory should provide a validator", validator);
}
use of javax.validation.Validator in project wildfly by wildfly.
the class LazyValidatorFactoryTestCase method testSpecificProviderCanBeConfiguredInValidationXml.
@Test
@WithContextClassLoader(TestClassLoader.class)
public void testSpecificProviderCanBeConfiguredInValidationXml() {
Validator validator = validatorFactory.getValidator();
assertNotNull("LazyValidatorFactory should provide a validator", validator);
assertTrue("Validator should be of type created by XML-configured provider", validator instanceof MyValidatorImpl);
}
Aggregations