org.springframework.context.support.StaticMessageSource Java Examples

The following examples show how to use org.springframework.context.support.StaticMessageSource. 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: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String name, @Nullable TargetSource customTargetSource) {
	if (StaticMessageSource.class.equals(beanClass)) {
		return DO_NOT_PROXY;
	}
	else if (name.endsWith("ToBeProxied")) {
		boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
		if ((this.proxyFactoryBean && isFactoryBean) || (this.proxyObject && !isFactoryBean)) {
			return new Object[] {this.testInterceptor};
		}
		else {
			return DO_NOT_PROXY;
		}
	}
	else {
		return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
	}
}
 
Example #3
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 #4
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 #5
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String name, TargetSource customTargetSource) {
	if (StaticMessageSource.class.equals(beanClass)) {
		return DO_NOT_PROXY;
	}
	else if (name.endsWith("ToBeProxied")) {
		boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
		if ((this.proxyFactoryBean && isFactoryBean) || (this.proxyObject && !isFactoryBean)) {
			return new Object[] {this.testInterceptor};
		}
		else {
			return DO_NOT_PROXY;
		}
	}
	else {
		return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
	}
}
 
Example #6
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 #7
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String name, @Nullable TargetSource customTargetSource) {
	if (StaticMessageSource.class.equals(beanClass)) {
		return DO_NOT_PROXY;
	}
	else if (name.endsWith("ToBeProxied")) {
		boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
		if ((this.proxyFactoryBean && isFactoryBean) || (this.proxyObject && !isFactoryBean)) {
			return new Object[] {this.testInterceptor};
		}
		else {
			return DO_NOT_PROXY;
		}
	}
	else {
		return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
	}
}
 
Example #8
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 #9
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 #10
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();
    }
 
Example #11
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #12
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #13
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 #14
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #15
Source File: SimpleWebApplicationContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
public DummyThemeSource() {
	this.messageSource = new StaticMessageSource();
	this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message");
	this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}");
}
 
Example #16
Source File: SimpleWebApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public DummyThemeSource() {
	this.messageSource = new StaticMessageSource();
	this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message");
	this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}");
}
 
Example #17
Source File: SimpleWebApplicationContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public DummyThemeSource() {
	this.messageSource = new StaticMessageSource();
	this.messageSource.addMessage("themetest", Locale.ENGLISH, "theme test message");
	this.messageSource.addMessage("themetestArgs", Locale.ENGLISH, "theme test message {0}");
}
 
Example #18
Source File: HibernateDatastore.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
protected void configureValidatorRegistry(HibernateConnectionSourceSettings settings, HibernateMappingContext mappingContext) {
    StaticMessageSource messageSource = new StaticMessageSource();
    ValidatorRegistry defaultValidatorRegistry = createValidatorRegistry(messageSource);
    configureValidatorRegistry(settings, mappingContext, defaultValidatorRegistry, messageSource);
}