Search in sources :

Example 1 with MessageInterpolator

use of javax.validation.MessageInterpolator in project wildfly by wildfly.

the class MessageInterpolationValidationTestCase method testCustomMessageInterpolation.

@Test
public void testCustomMessageInterpolation() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    final MessageInterpolator messageInterpolator = new CustomMessageInterpolator();
    configuration.messageInterpolator(messageInterpolator);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    // create employee
    Employee emp = new Employee();
    emp.setEmail("MADHUMITA");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals(CustomMessageInterpolator.MESSAGE, constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) MessageInterpolator(javax.validation.MessageInterpolator) Test(org.junit.Test)

Example 2 with MessageInterpolator

use of javax.validation.MessageInterpolator in project spring-boot by spring-projects.

the class MessageInterpolatorFactoryWithoutElIntegrationTests method getObjectShouldUseFallback.

@Test
public void getObjectShouldUseFallback() {
    MessageInterpolator interpolator = new MessageInterpolatorFactory().getObject();
    assertThat(interpolator).isInstanceOf(ParameterMessageInterpolator.class);
}
Also used : MessageInterpolator(javax.validation.MessageInterpolator) ParameterMessageInterpolator(org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator) Test(org.junit.Test)

Example 3 with MessageInterpolator

use of javax.validation.MessageInterpolator 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");
}
Also used : Locale(java.util.Locale) ValidatorFactory(javax.validation.ValidatorFactory) MessageInterpolator(javax.validation.MessageInterpolator)

Example 4 with MessageInterpolator

use of javax.validation.MessageInterpolator in project spring-boot by spring-projects.

the class MessageInterpolatorFactoryTests method getObjectShouldReturnResourceBundleMessageInterpolator.

@Test
public void getObjectShouldReturnResourceBundleMessageInterpolator() {
    MessageInterpolator interpolator = new MessageInterpolatorFactory().getObject();
    assertThat(interpolator).isInstanceOf(ResourceBundleMessageInterpolator.class);
}
Also used : MessageInterpolator(javax.validation.MessageInterpolator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.junit.Test)

Example 5 with MessageInterpolator

use of javax.validation.MessageInterpolator in project tomee by apache.

the class ValidatorBuilder method getConfig.

@SuppressWarnings("unchecked")
private static Configuration<?> getConfig(final ValidationInfo info) {
    Configuration<?> target = null;
    final Thread thread = Thread.currentThread();
    final ClassLoader classLoader = thread.getContextClassLoader();
    String providerClassName = info.providerClassName;
    if (providerClassName == null) {
        providerClassName = SystemInstance.get().getOptions().get(VALIDATION_PROVIDER_KEY, (String) null);
    }
    if (providerClassName != null) {
        try {
            @SuppressWarnings({ "unchecked", "rawtypes" }) final Class clazz = classLoader.loadClass(providerClassName);
            target = Validation.byProvider(clazz).configure();
            logger.info("Using " + providerClassName + " as validation provider.");
        } catch (final ClassNotFoundException e) {
            logger.warning("Unable to load provider class " + providerClassName, e);
        } catch (final ValidationException ve) {
            logger.warning("Unable create validator factory with provider " + providerClassName + " (" + ve.getMessage() + ")." + " Default one will be used.");
        }
    }
    if (target == null) {
        // force to use container provider to ignore any conflicting configuration
        thread.setContextClassLoader(ValidatorBuilder.class.getClassLoader());
        target = Validation.byDefaultProvider().configure();
        thread.setContextClassLoader(classLoader);
    }
    final Set<ExecutableType> types = new HashSet<>();
    for (final String type : info.validatedTypes) {
        types.add(ExecutableType.valueOf(type));
    }
    final Map<String, String> props = new HashMap<>();
    for (final Map.Entry<Object, Object> entry : info.propertyTypes.entrySet()) {
        final PropertyType property = new PropertyType();
        property.setName((String) entry.getKey());
        property.setValue((String) entry.getValue());
        props.put(property.getName(), property.getValue());
        if (logger.isDebugEnabled()) {
            logger.debug("Found property '" + property.getName() + "' with value '" + property.getValue());
        }
        target.addProperty(property.getName(), property.getValue());
    }
    final OpenEjbBootstrapConfig bootstrapConfig = new OpenEjbBootstrapConfig(providerClassName, info.constraintFactoryClass, info.messageInterpolatorClass, info.traversableResolverClass, info.parameterNameProviderClass, new HashSet<>(info.constraintMappings), info.executableValidationEnabled, types, props);
    final OpenEjbConfig config = new OpenEjbConfig(bootstrapConfig, target);
    target.ignoreXmlConfiguration();
    final String messageInterpolatorClass = info.messageInterpolatorClass;
    if (messageInterpolatorClass != null) {
        try {
            @SuppressWarnings("unchecked") final Class<MessageInterpolator> clazz = (Class<MessageInterpolator>) classLoader.loadClass(messageInterpolatorClass);
            target.messageInterpolator(newInstance(config, clazz));
        } catch (final Exception e) {
            logger.warning("Unable to set " + messageInterpolatorClass + " as message interpolator.", e);
        }
        logger.info("Using " + messageInterpolatorClass + " as message interpolator.");
    }
    final String traversableResolverClass = info.traversableResolverClass;
    if (traversableResolverClass != null) {
        try {
            @SuppressWarnings("unchecked") final Class<TraversableResolver> clazz = (Class<TraversableResolver>) classLoader.loadClass(traversableResolverClass);
            target.traversableResolver(newInstance(config, clazz));
        } catch (final Exception e) {
            logger.warning("Unable to set " + traversableResolverClass + " as traversable resolver.", e);
        }
        logger.info("Using " + traversableResolverClass + " as traversable resolver.");
    }
    final String constraintFactoryClass = info.constraintFactoryClass;
    if (constraintFactoryClass != null) {
        try {
            @SuppressWarnings("unchecked") final Class<ConstraintValidatorFactory> clazz = (Class<ConstraintValidatorFactory>) classLoader.loadClass(constraintFactoryClass);
            target.constraintValidatorFactory(newInstance(config, clazz));
        } catch (final Exception e) {
            logger.warning("Unable to set " + constraintFactoryClass + " as constraint factory.", e);
        }
        logger.info("Using " + constraintFactoryClass + " as constraint factory.");
    }
    for (final String mappingFileName : info.constraintMappings) {
        if (logger.isDebugEnabled()) {
            logger.debug("Opening input stream for " + mappingFileName);
        }
        final InputStream in = classLoader.getResourceAsStream(mappingFileName);
        if (in == null) {
            logger.warning("Unable to open input stream for mapping file " + mappingFileName + ". It will be ignored");
        } else {
            target.addMapping(in);
        }
    }
    if (info.parameterNameProviderClass != null) {
        try {
            final Class<ParameterNameProvider> clazz = (Class<ParameterNameProvider>) classLoader.loadClass(info.parameterNameProviderClass);
            target.parameterNameProvider(newInstance(config, clazz));
        } catch (final Exception e) {
            logger.warning("Unable to set " + info.parameterNameProviderClass + " as parameter name provider.", e);
        }
        logger.info("Using " + info.parameterNameProviderClass + " as parameter name provider.");
    }
    return config;
}
Also used : ExecutableType(javax.validation.executable.ExecutableType) ValidationException(javax.validation.ValidationException) HashMap(java.util.HashMap) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) PropertyType(org.apache.openejb.jee.bval.PropertyType) HashSet(java.util.HashSet) InputStream(java.io.InputStream) NamingException(javax.naming.NamingException) ValidationException(javax.validation.ValidationException) TraversableResolver(javax.validation.TraversableResolver) ParameterNameProvider(javax.validation.ParameterNameProvider) HashMap(java.util.HashMap) Map(java.util.Map) MessageInterpolator(javax.validation.MessageInterpolator)

Aggregations

MessageInterpolator (javax.validation.MessageInterpolator)8 Test (org.junit.Test)4 ValidatorFactory (javax.validation.ValidatorFactory)3 Validator (javax.validation.Validator)2 ExecutableType (javax.validation.executable.ExecutableType)2 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Map (java.util.Map)1 NamingException (javax.naming.NamingException)1 BootstrapConfiguration (javax.validation.BootstrapConfiguration)1 ConstraintValidatorFactory (javax.validation.ConstraintValidatorFactory)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ParameterNameProvider (javax.validation.ParameterNameProvider)1 TraversableResolver (javax.validation.TraversableResolver)1 ValidationException (javax.validation.ValidationException)1 ValidatorContext (javax.validation.ValidatorContext)1 PropertyType (org.apache.openejb.jee.bval.PropertyType)1