Search in sources :

Example 6 with StaticMessageSource

use of org.springframework.context.support.StaticMessageSource in project spring-boot by spring-projects.

the class RelaxedDataBinderTests method testRequiredFieldsValidation.

@Test
public void testRequiredFieldsValidation() throws Exception {
    TargetWithValidatedMap target = new TargetWithValidatedMap();
    BindingResult result = bind(target, "info[foo]: bar");
    assertThat(result.getErrorCount()).isEqualTo(2);
    for (FieldError error : result.getFieldErrors()) {
        System.err.println(new StaticMessageSource().getMessage(error, Locale.getDefault()));
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) StaticMessageSource(org.springframework.context.support.StaticMessageSource) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 7 with StaticMessageSource

use of org.springframework.context.support.StaticMessageSource in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryParameterizedTests method setupFactory.

private void setupFactory() throws IOException {
    this.factory.setTargetName(this.targetName);
    this.factory.setMessageSource(new StaticMessageSource());
}
Also used : StaticMessageSource(org.springframework.context.support.StaticMessageSource)

Example 8 with StaticMessageSource

use of org.springframework.context.support.StaticMessageSource in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method setupFactory.

private void setupFactory() throws IOException {
    this.factory = new PropertiesConfigurationFactory<>(Foo.class);
    this.factory.setValidator(this.validator);
    this.factory.setTargetName(this.targetName);
    this.factory.setIgnoreUnknownFields(this.ignoreUnknownFields);
    this.factory.setMessageSource(new StaticMessageSource());
}
Also used : StaticMessageSource(org.springframework.context.support.StaticMessageSource)

Example 9 with StaticMessageSource

use of org.springframework.context.support.StaticMessageSource in project spring-framework by spring-projects.

the class DataBinderTests method testRejectWithoutDefaultMessage.

@Test
public void testRejectWithoutDefaultMessage() throws Exception {
    TestBean tb = new TestBean();
    tb.setName("myName");
    tb.setAge(99);
    BeanPropertyBindingResult ex = new BeanPropertyBindingResult(tb, "tb");
    ex.reject("invalid");
    ex.rejectValue("age", "invalidField");
    StaticMessageSource ms = new StaticMessageSource();
    ms.addMessage("invalid", Locale.US, "general error");
    ms.addMessage("invalidField", Locale.US, "invalid field");
    assertEquals("general error", ms.getMessage(ex.getGlobalError(), Locale.US));
    assertEquals("invalid field", ms.getMessage(ex.getFieldError("age"), Locale.US));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Aggregations

StaticMessageSource (org.springframework.context.support.StaticMessageSource)9 Test (org.junit.Test)3 Locale (java.util.Locale)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1 BindingResult (org.springframework.validation.BindingResult)1 FieldError (org.springframework.validation.FieldError)1