use of javax.validation.ValidatorFactory in project hibernate-orm by hibernate.
the class BeanValidationProvidedFactoryTest method configure.
@Override
protected void configure(Configuration cfg) {
super.configure(cfg);
final MessageInterpolator messageInterpolator = new MessageInterpolator() {
public String interpolate(String s, Context context) {
return "Oops";
}
public String interpolate(String s, Context context, Locale locale) {
return interpolate(s, context);
}
};
final javax.validation.Configuration<?> configuration = Validation.byDefaultProvider().configure();
configuration.messageInterpolator(messageInterpolator);
ValidatorFactory vf = configuration.buildValidatorFactory();
cfg.getProperties().put("javax.persistence.validation.factory", vf);
cfg.setProperty("javax.persistence.validation.mode", "AUTO");
}
use of javax.validation.ValidatorFactory in project hibernate-orm by hibernate.
the class BeanValidationEventListener method initialize.
public void initialize(Map settings, ClassLoaderService classLoaderService) {
if (!initialized) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
init(factory, settings, classLoaderService);
}
}
use of javax.validation.ValidatorFactory in project wildfly by wildfly.
the class BootStrapValidationTestCase method testCustomConstraintValidatorFactory.
@Test
public void testCustomConstraintValidatorFactory() {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
assertNotNull(configuration);
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
Employee emp = new Employee();
// create employee
emp.setEmpId("M1234");
emp.setFirstName("MADHUMITA");
emp.setLastName("SADHUKHAN");
emp.setEmail("madhu@redhat.com");
Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
assertEquals("Created by default factory", constraintViolations.iterator().next().getMessage());
// get a new factory using a custom configuration
configuration.constraintValidatorFactory(new CustomConstraintValidatorFactory(configuration.getDefaultConstraintValidatorFactory()));
factory = configuration.buildValidatorFactory();
validator = factory.getValidator();
constraintViolations = validator.validate(emp);
assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
assertEquals("Created by custom factory", constraintViolations.iterator().next().getMessage());
}
use of javax.validation.ValidatorFactory 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.ValidatorFactory in project wildfly by wildfly.
the class JSFBeanValidationFactoryProcessor method deploy.
@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
// Get the CDI-enabled ValidatorFactory and add it to the servlet context
ValidatorFactory validatorFactory = deploymentUnit.getAttachment(BeanValidationAttachments.VALIDATOR_FACTORY);
if (validatorFactory != null) {
deploymentUnit.addToAttachmentList(ServletContextAttribute.ATTACHMENT_KEY, new ServletContextAttribute(VALIDATOR_FACTORY_KEY, validatorFactory));
}
}
Aggregations