Search in sources :

Example 1 with HibernateMessageInterpolatorContext

use of org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext in project hibernate-validator by hibernate.

the class ElTermResolver method bindContextValues.

private ValueExpression bindContextValues(String messageTemplate, MessageInterpolator.Context messageInterpolatorContext, ELContext elContext) {
    // bind the validated value
    ValueExpression valueExpression = expressionFactory.createValueExpression(messageInterpolatorContext.getValidatedValue(), Object.class);
    elContext.getVariableMapper().setVariable(VALIDATED_VALUE_NAME, valueExpression);
    // bind a formatter instantiated with proper locale
    valueExpression = expressionFactory.createValueExpression(new FormatterWrapper(locale), FormatterWrapper.class);
    elContext.getVariableMapper().setVariable(RootResolver.FORMATTER, valueExpression);
    // map the parameters provided by the annotation values and the parameters + expression variables explicitly
    // added to the context
    addVariablesToElContext(elContext, messageInterpolatorContext.getConstraintDescriptor().getAttributes());
    if (messageInterpolatorContext instanceof HibernateMessageInterpolatorContext) {
        addVariablesToElContext(elContext, ((HibernateMessageInterpolatorContext) messageInterpolatorContext).getExpressionVariables());
    }
    return expressionFactory.createValueExpression(elContext, messageTemplate, String.class);
}
Also used : HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) ValueExpression(jakarta.el.ValueExpression)

Example 2 with HibernateMessageInterpolatorContext

use of org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext 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)

Example 3 with HibernateMessageInterpolatorContext

use of org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext in project hibernate-validator by hibernate.

the class ParameterTermResolverTest method createHibernateContextWithConstraintDescriptorAttr.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MessageInterpolator.Context createHibernateContextWithConstraintDescriptorAttr(String attrName, Object attrValue) {
    final Map<String, Object> attrs = createConstraintDescriptorAttr(attrName, attrValue);
    final HibernateMessageInterpolatorContext context = EasyMock.mock(HibernateMessageInterpolatorContext.class);
    final ConstraintDescriptor constraintDescriptor = EasyMock.mock(ConstraintDescriptor.class);
    EasyMock.expect(context.getMessageParameters()).andStubReturn(attrs);
    EasyMock.expect(context.getConstraintDescriptor()).andStubReturn(constraintDescriptor);
    EasyMock.expect(constraintDescriptor.getAttributes()).andStubReturn(attrs);
    EasyMock.replay(context);
    EasyMock.replay(constraintDescriptor);
    return context;
}
Also used : HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor)

Aggregations

HibernateMessageInterpolatorContext (org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext)3 ValueExpression (jakarta.el.ValueExpression)1 MessageInterpolator (jakarta.validation.MessageInterpolator)1 Context (jakarta.validation.MessageInterpolator.Context)1 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)1 MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)1 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)1 BeforeTest (org.testng.annotations.BeforeTest)1 Test (org.testng.annotations.Test)1