Search in sources :

Example 6 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testDefaultInterpolation.

@Test
public void testDefaultInterpolation() {
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolatorContext messageInterpolatorContext = createMessageInterpolatorContext(notNullDescriptor);
    String expected = "must not be null";
    String actual = interpolator.interpolate(notNullDescriptor.getAnnotation().message(), messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
    // unknown token {}
    expected = "size must be between 0 and 2147483647";
    messageInterpolatorContext = createMessageInterpolatorContext(sizeDescriptor);
    actual = interpolator.interpolate(sizeDescriptor.getAnnotation().message(), messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
}
Also used : MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testUnknownTokenInterpolation.

@Test
public void testUnknownTokenInterpolation() {
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolatorContext messageInterpolatorContext = createMessageInterpolatorContext(notNullDescriptor);
    // unknown token {}
    String expected = "{bar}";
    String actual = interpolator.interpolate("{bar}", messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
}
Also used : MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project graphql-java-extended-validation by graphql-java.

the class ELDiscover method main.

public static void main(String[] args) throws Exception {
    elDirect();
    Map<String, Object> attributes = new HashMap<>();
    ConstraintAnnotationDescriptor.Builder<NotNull> constraintBuilder = new ConstraintAnnotationDescriptor.Builder<>(NotNull.class, attributes);
    ConstraintDescriptorImpl<NotNull> constraintDescriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, constraintBuilder.build(), ConstraintLocationKind.FIELD, ConstraintType.GENERIC);
    User user = new User();
    user.setAge(18);
    ResourceBundleMessageInterpolator messageInterpolator = new ResourceBundleMessageInterpolator();
    PathImpl rootPath = PathImpl.createRootPath();
    ExpressionLanguageFeatureLevel level = ExpressionLanguageFeatureLevel.BEAN_METHODS;
    MessageInterpolator.Context context = new MessageInterpolatorContext(constraintDescriptor, user, null, rootPath, Collections.emptyMap(), Collections.emptyMap(), level, true);
    print("${validatedValue.age}", messageInterpolator, context);
    print("${validatedValue}", messageInterpolator, context);
    print("[1,2,3,4].stream().sum()", messageInterpolator, context);
    print("${4.0>= 3}", messageInterpolator, context);
    print("${! empty validatedValue}", messageInterpolator, context);
    print("#{cube=(x->x*x*x);cube(2)}", messageInterpolator, context);
}
Also used : ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) NotNull(jakarta.validation.constraints.NotNull) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ExpressionLanguageFeatureLevel(org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator)

Example 9 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project graphql-java-extended-validation by graphql-java.

the class ResourceBundleMessageInterpolator method interpolateMessageImpl.

private String interpolateMessageImpl(String messageTemplate, Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
    Locale locale = validationEnvironment.getLocale() == null ? defaultLocale : validationEnvironment.getLocale();
    messageTemplate = loadMessageResource(messageTemplate, locale);
    MessageInterpolatorContext context = buildHibernateContext(messageParams, validationEnvironment);
    if (locale == null) {
        // let hibernate code do the local defaulting
        return hibernateInterpolator().interpolate(messageTemplate, context);
    } else {
        return hibernateInterpolator().interpolate(messageTemplate, context, locale);
    }
}
Also used : Locale(java.util.Locale) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext)

Example 10 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class MessageInterpolatorContextTest method testUnwrapToInterfaceTypesSucceeds.

@Test
public void testUnwrapToInterfaceTypesSucceeds() {
    Context context = new MessageInterpolatorContext(null, null, null, null, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), ExpressionLanguageFeatureLevel.BEAN_METHODS, false);
    MessageInterpolator.Context asMessageInterpolatorContext = context.unwrap(MessageInterpolator.Context.class);
    assertSame(asMessageInterpolatorContext, context);
    HibernateMessageInterpolatorContext asHibernateMessageInterpolatorContext = context.unwrap(HibernateMessageInterpolatorContext.class);
    assertSame(asHibernateMessageInterpolatorContext, context);
    Object asObject = context.unwrap(Object.class);
    assertSame(asObject, context);
}
Also used : Context(jakarta.validation.MessageInterpolator.Context) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) Context(jakarta.validation.MessageInterpolator.Context) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)16 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)13 BeforeTest (org.testng.annotations.BeforeTest)13 Test (org.testng.annotations.Test)13 MessageInterpolator (jakarta.validation.MessageInterpolator)5 HibernateMessageInterpolatorContext (org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext)5 Context (jakarta.validation.MessageInterpolator.Context)4 Path (jakarta.validation.Path)2 Locale (java.util.Locale)2 ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)2 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 ResultPath (graphql.execution.ResultPath)1 ConstraintViolation (jakarta.validation.ConstraintViolation)1 Validator (jakarta.validation.Validator)1 NotNull (jakarta.validation.constraints.NotNull)1 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)1 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)1 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)1 HashMap (java.util.HashMap)1