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");
}
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");
}
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"));
}
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)));
}
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);
}
Aggregations