Java Code Examples for org.springframework.context.support.StaticMessageSource#addMessage()

The following examples show how to use org.springframework.context.support.StaticMessageSource#addMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRejectWithoutDefaultMessage() {
	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));
}
 
Example 2
Source File: ResponseStatusExceptionResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void statusCodeAndReasonMessage() {
	Locale locale = Locale.CHINESE;
	LocaleContextHolder.setLocale(locale);
	try {
		StaticMessageSource messageSource = new StaticMessageSource();
		messageSource.addMessage("gone.reason", locale, "Gone reason message");
		exceptionResolver.setMessageSource(messageSource);

		StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException();
		exceptionResolver.resolveException(request, response, null, ex);
		assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 3
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRejectWithoutDefaultMessage() {
	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));
}
 
Example 4
Source File: ResponseStatusExceptionResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void statusCodeAndReasonMessage() {
	Locale locale = Locale.CHINESE;
	LocaleContextHolder.setLocale(locale);
	try {
		StaticMessageSource messageSource = new StaticMessageSource();
		messageSource.addMessage("gone.reason", locale, "Gone reason message");
		exceptionResolver.setMessageSource(messageSource);

		StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException();
		exceptionResolver.resolveException(request, response, null, ex);
		assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 5
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 6
Source File: ResponseStatusExceptionResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void statusCodeAndReasonMessage() {
	Locale locale = Locale.CHINESE;
	LocaleContextHolder.setLocale(locale);
	try {
		StaticMessageSource messageSource = new StaticMessageSource();
		messageSource.addMessage("gone.reason", locale, "Gone reason message");
		exceptionResolver.setMessageSource(messageSource);

		StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException();
		exceptionResolver.resolveException(request, response, null, ex);
		assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 7
Source File: ErrorsMessageDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
static MessageSource createMessageSource() {
    StaticMessageSource messageSource = new StaticMessageSource();
    messageSource.addMessage("user.properties.not.null", Locale.getDefault(), "User 所有属性不能为空");
    messageSource.addMessage("id.required", Locale.getDefault(), "the id of User must not be null.");
    messageSource.addMessage("name.required", Locale.getDefault(), "the name of User must not be null.");
    return messageSource;
}
 
Example 8
Source File: AddSoRPersonFlowTests.java    From openregistry with Apache License 2.0 5 votes vote down vote up
protected void setUp() {

        this.personService = mock(PersonService.class);

        final StaticMessageSource staticMessageSource = new StaticMessageSource();
        staticMessageSource.addMessage("personAddedFinalConfirm", Locale.getDefault(), "test");
        staticMessageSource.addMessage("roleAdded", Locale.getDefault(), "test");
        staticMessageSource.addMessage("errorCode", Locale.getDefault(), "test");

        this.messageContext = new DefaultMessageContext(staticMessageSource);
        this.personSearchAction = new PersonSearchAction(this.personService);
        this.personSearchAction.setPreferredPersonIdentifierType("NETID");
        this.referenceRepository = new MockReferenceRepository();
        this.reconciliationCriteriaFactory = new MockReconciliationCriteriaFactory();
    }