Search in sources :

Example 11 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class DefaultLocaleMessageInterpolationTest method testNoDefaultLocaleDefinedStillWorking.

@Test
public void testNoDefaultLocaleDefinedStillWorking() {
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();
    MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator();
    assertThat(messageInterpolator.interpolate("{jakarta.validation.constraints.AssertFalse.message}", new TestContext())).isEqualTo("must be false");
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 12 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class LocaleResolverTest method testLanguageRangeSupport.

@Test
public void testLanguageRangeSupport() throws NoSuchMethodException, SecurityException {
    ValidatorFactory validatorFactory = getValidatorFactoryWithInitializedLocales(Locale.FRANCE, new Locale("es", "ES"));
    MessageInterpolator messageInterpolator = validatorFactory.getMessageInterpolator();
    StaticFieldLocaleResolver.acceptLanguage = "fr-FR,fr;q=0.9";
    assertThat(messageInterpolator.interpolate("{jakarta.validation.constraints.AssertFalse.message}", new TestContext())).isEqualTo("doit avoir la valeur faux");
}
Also used : Locale(java.util.Locale) ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 13 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class PredefinedScopeValidatorFactoryTest method customizingMessageInterpolator.

@Test
public void customizingMessageInterpolator() {
    ValidatorFactory validatorFactory = getValidatorFactory();
    Validator validator = validatorFactory.getValidator();
    Set<ConstraintViolation<Bean>> violations = validator.validate(new Bean(null, null));
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("must not be null"));
    validator = validatorFactory.usingContext().messageInterpolator(new MessageInterpolator() {

        @Override
        public String interpolate(String messageTemplate, Context context, Locale locale) {
            return "another string";
        }

        @Override
        public String interpolate(String messageTemplate, Context context) {
            return "another string";
        }
    }).getValidator();
    violations = validator.validate(new Bean(null, null));
    assertThat(violations).containsOnlyViolations(violationOf(NotNull.class).withMessage("another string"));
}
Also used : Locale(java.util.Locale) ValidatorFactory(jakarta.validation.ValidatorFactory) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) PredefinedScopeHibernateValidator(org.hibernate.validator.PredefinedScopeHibernateValidator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 14 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class UniqueElementsValidatorTest method testMessageContainsDuplicatedValue.

@Test
public void testMessageContainsDuplicatedValue() {
    HibernateValidatorConfiguration configuration = ValidatorUtil.getConfiguration();
    MessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator(new AggregateResourceBundleLocator(Arrays.asList("org/hibernate/validator/test/internal/constraintvalidators/hv/UniqueElementsMessages"), configuration.getDefaultResourceBundleLocator(), getClass().getClassLoader()));
    Validator validator = configuration.messageInterpolator(messageInterpolator).buildValidatorFactory().getValidator();
    String duplicate = "seeme";
    List<Object> fails = Arrays.asList(duplicate, duplicate);
    Set<ConstraintViolation<AnnotationContainer>> violations = validator.validate(new AnnotationContainer(fails));
    assertThat(violations).containsOnlyViolations(violationOf(UniqueElements.class));
    assertTrue(violations.stream().anyMatch(cv -> cv.getMessage().contains(duplicate)));
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) Arrays(java.util.Arrays) ConstraintViolationAssert.assertThat(org.hibernate.validator.testutil.ConstraintViolationAssert.assertThat) Validator(jakarta.validation.Validator) ConstraintViolationAssert.assertNoViolations(org.hibernate.validator.testutil.ConstraintViolationAssert.assertNoViolations) Set(java.util.Set) ValidatorUtil(org.hibernate.validator.testutils.ValidatorUtil) Test(org.testng.annotations.Test) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) AggregateResourceBundleLocator(org.hibernate.validator.resourceloading.AggregateResourceBundleLocator) UniqueElements(org.hibernate.validator.constraints.UniqueElements) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) MessageInterpolator(jakarta.validation.MessageInterpolator) ConstraintViolationAssert.violationOf(org.hibernate.validator.testutil.ConstraintViolationAssert.violationOf) Assert.assertTrue(org.testng.Assert.assertTrue) Assertions(org.assertj.core.api.Assertions) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) HibernateConstraintViolation(org.hibernate.validator.engine.HibernateConstraintViolation) Collections(java.util.Collections) UniqueElements(org.hibernate.validator.constraints.UniqueElements) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) AggregateResourceBundleLocator(org.hibernate.validator.resourceloading.AggregateResourceBundleLocator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ConstraintViolation(jakarta.validation.ConstraintViolation) HibernateConstraintViolation(org.hibernate.validator.engine.HibernateConstraintViolation) Validator(jakarta.validation.Validator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Example 15 with MessageInterpolator

use of jakarta.validation.MessageInterpolator in project hibernate-validator by hibernate.

the class ConfigurationReuseHibernateValidatorTest method testMessageInterpolatorChange.

@Test
public void testMessageInterpolatorChange() {
    Configuration<?> config = Validation.byDefaultProvider().configure();
    MessageInterpolator interpolator1 = new MessageInterpolatorImpl("One");
    MessageInterpolator interpolator2 = new MessageInterpolatorImpl("Two");
    ValidatorFactory factory1 = config.messageInterpolator(interpolator1).buildValidatorFactory();
    ValidatorFactory factory2 = config.messageInterpolator(interpolator2).buildValidatorFactory();
    assertSame(factory1.getMessageInterpolator(), interpolator1);
    assertSame(factory2.getMessageInterpolator(), interpolator2);
}
Also used : ValidatorFactory(jakarta.validation.ValidatorFactory) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test)

Aggregations

MessageInterpolator (jakarta.validation.MessageInterpolator)33 ValidatorFactory (jakarta.validation.ValidatorFactory)14 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)13 Test (org.testng.annotations.Test)11 Test (org.junit.jupiter.api.Test)8 Locale (java.util.Locale)7 Validator (jakarta.validation.Validator)5 ValidatorContext (jakarta.validation.ValidatorContext)5 ConstraintViolation (jakarta.validation.ConstraintViolation)4 ConstraintValidatorFactory (jakarta.validation.ConstraintValidatorFactory)3 GenericBootstrap (jakarta.validation.bootstrap.GenericBootstrap)3 ProviderSpecificBootstrap (jakarta.validation.bootstrap.ProviderSpecificBootstrap)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Method (java.lang.reflect.Method)3 MessageSource (cn.taketoday.context.MessageSource)2 Resource (cn.taketoday.core.io.Resource)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 PredefinedScopeHibernateValidator (org.hibernate.validator.PredefinedScopeHibernateValidator)2