@ -18,17 +18,15 @@ package org.springframework.boot.autoconfigure.context;
import java.util.Locale ;
import java.util.Locale ;
import org.junit.After ;
import org.junit.Ignore ;
import org.junit.Ignore ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.beans.DirectFieldAccessor ;
import org.springframework.beans.DirectFieldAccessor ;
import org.springframework.boot. test.util.TestPropertyValue s;
import org.springframework.boot. autoconfigure.AutoConfiguration s;
import org.springframework. context.ConfigurableApplicationContext ;
import org.springframework. boot.test.context.runner.ApplicationContextRunner ;
import org.springframework.context.MessageSource ;
import org.springframework.context.MessageSource ;
import org.springframework.context.MessageSourceResolvable ;
import org.springframework.context.MessageSourceResolvable ;
import org.springframework.context.NoSuchMessageException ;
import org.springframework.context.NoSuchMessageException ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.context.annotation.PropertySource ;
import org.springframework.context.annotation.PropertySource ;
@ -41,95 +39,93 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
* @author Dave Syer
* @author Edd ú Mel é ndez
* @author Edd ú Mel é ndez
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Kedar Joshi
* /
* /
public class MessageSourceAutoConfigurationTests {
public class MessageSourceAutoConfigurationTests {
private AnnotationConfigApplicationContext context ;
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ( )
. withConfiguration ( AutoConfigurations . of (
@After
MessageSourceAutoConfiguration . class ) ) ;
public void closeContext ( ) {
if ( this . context ! = null ) {
this . context . close ( ) ;
}
}
@Test
@Test
public void testDefaultMessageSource ( ) throws Exception {
public void testDefaultMessageSource ( ) {
load ( ) ;
this . contextRunner . run ( ( context ) - >
assertThat ( this . context. getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
assertThat ( context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
. isEqualTo ( "Foo message" ) ;
. isEqualTo ( "Foo message" ) ) ;
}
}
@Test
@Test
public void testMessageSourceCreated ( ) throws Exception {
public void testMessageSourceCreated ( ) {
load ( "spring.messages.basename:test/messages" ) ;
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" )
assertThat ( this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
. run ( ( context ) - > assertThat ( context . getMessage (
. isEqualTo ( "bar" ) ;
"foo" , null , "Foo message" , Locale . UK ) ) . isEqualTo ( "bar" ) ) ;
}
}
@Test
@Test
public void testEncodingWorks ( ) throws Exception {
public void testEncodingWorks ( ) {
load ( "spring.messages.basename:test/swedish" ) ;
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/swedish" )
assertThat ( this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
. run ( ( context ) - > assertThat ( context . getMessage (
. isEqualTo ( "Some text with some swedish öäå!" ) ;
"foo" , null , "Foo message" , Locale . UK ) ) . isEqualTo (
"Some text with some swedish öäå!" ) ) ;
}
}
@Test
@Test
public void testMultipleMessageSourceCreated ( ) throws Exception {
public void testMultipleMessageSourceCreated ( ) {
load ( "spring.messages.basename:test/messages,test/messages2" ) ;
this . contextRunner . withPropertyValues (
assertThat ( this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
"spring.messages.basename:test/messages,test/messages2" ) . run ( ( context ) - > {
. isEqualTo ( "bar" ) ;
assertThat ( context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
assertThat ( this . context . getMessage ( "foo-foo" , null , "Foo-Foo message" , Locale . UK ) )
. isEqualTo ( "bar" ) ;
. isEqualTo ( "bar-bar" ) ;
assertThat ( context . getMessage ( "foo-foo" , null , "Foo-Foo message" , Locale . UK ) )
. isEqualTo ( "bar-bar" ) ;
} ) ;
}
}
@Test
@Test
public void testBadEncoding ( ) throws Exception {
public void testBadEncoding ( ) {
load ( "spring.messages.encoding:rubbish" ) ;
this . contextRunner . withPropertyValues ( "spring.messages.encoding:rubbish" )
// Bad encoding just means the messages are ignored
. run ( ( context ) - > {
assertThat ( this . context . getMessage ( "foo" , null , "blah" , Locale . UK ) )
// Bad encoding just means the messages are ignored
. isEqualTo ( "blah" ) ;
assertThat ( context . getMessage ( "foo" , null , "blah" , Locale . UK ) )
. isEqualTo ( "blah" ) ;
} ) ;
}
}
@Test
@Test
@Ignore ( "Expected to fail per gh-1075" )
@Ignore ( "Expected to fail per gh-1075" )
public void testMessageSourceFromPropertySourceAnnotation ( ) throws Exception {
public void testMessageSourceFromPropertySourceAnnotation ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
this . contextRunner . withUserConfiguration ( Config . class ) . run ( ( context ) - >
this . context . register ( Config . class , MessageSourceAutoConfiguration . class ,
assertThat ( context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
PropertyPlaceholderAutoConfiguration . class ) ;
. isEqualTo ( "bar" ) ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
. isEqualTo ( "bar" ) ;
}
}
@Test
@Test
public void testFallbackDefault ( ) throws Exception {
public void testFallbackDefault ( ) {
load ( "spring.messages.basename:test/messages" ) ;
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" )
assertThat ( isFallbackToSystemLocale ( this . context . getBean ( MessageSource . class ) ) )
. run ( ( context ) - > assertThat ( isFallbackToSystemLocale (
. isTrue ( ) ;
context . getBean ( MessageSource . class ) ) ) . isTrue ( ) ) ;
}
}
@Test
@Test
public void testFallbackTurnOff ( ) throws Exception {
public void testFallbackTurnOff ( ) {
load ( "spring.messages.basename:test/messages" ,
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" ,
"spring.messages.fallback-to-system-locale:false" ) ;
"spring.messages.fallback-to-system-locale:false" ) .run ( ( context ) - >
assertThat ( isFallbackToSystemLocale ( this . context. getBean ( MessageSource . class ) ) )
assertThat ( isFallbackToSystemLocale ( context. getBean ( MessageSource . class ) ) )
. isFalse ( ) ;
. isFalse ( ) ) ;
}
}
@Test
@Test
public void testFormatMessageDefault ( ) throws Exception {
public void testFormatMessageDefault ( ) {
load ( "spring.messages.basename:test/messages" ) ;
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" )
assertThat ( isAlwaysUseMessageFormat ( this . context . getBean ( MessageSource . class ) ) )
. run ( ( context ) - > assertThat ( isAlwaysUseMessageFormat (
. isFalse ( ) ;
context . getBean ( MessageSource . class ) ) ) . isFalse ( ) ) ;
}
}
@Test
@Test
public void testFormatMessageOn ( ) throws Exception {
public void testFormatMessageOn ( ) throws Exception {
load ( "spring.messages.basename:test/messages" ,
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" ,
"spring.messages.always-use-message-format:true" ) ;
"spring.messages.always-use-message-format:true" ) .run ( ( context ) - >
assertThat ( isAlwaysUseMessageFormat ( this . context. getBean ( MessageSource . class ) ) )
assertThat ( isAlwaysUseMessageFormat ( context. getBean ( MessageSource . class ) ) )
. isTrue ( ) ;
. isTrue ( ) ) ;
}
}
private boolean isFallbackToSystemLocale ( MessageSource messageSource ) {
private boolean isFallbackToSystemLocale ( MessageSource messageSource ) {
@ -143,37 +139,40 @@ public class MessageSourceAutoConfigurationTests {
}
}
@Test
@Test
public void existingMessageSourceIsPreferred ( ) {
public void testUseCodeAsDefaultMessageDefault ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" )
this . context . register ( CustomMessageSource . class ,
. run ( ( context ) - > assertThat ( isUseCodeAsDefaultMessage (
MessageSourceAutoConfiguration . class ,
context . getBean ( MessageSource . class ) ) ) . isFalse ( ) ) ;
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getMessage ( "foo" , null , null , null ) ) . isEqualTo ( "foo" ) ;
}
}
@Test
@Test
public void existingMessageSourceInParentIsIgnored ( ) {
public void testUseCodeAsDefaultMessageOn ( ) {
try ( ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext ( ) ) {
this . contextRunner . withPropertyValues ( "spring.messages.basename:test/messages" ,
parent . refresh ( ) ;
"spring.messages.use-code-as-default-message:true" ) . run ( ( context ) - >
this . context = new AnnotationConfigApplicationContext ( ) ;
assertThat ( isUseCodeAsDefaultMessage (
this . context . setParent ( parent ) ;
context . getBean ( MessageSource . class ) ) ) . isTrue ( ) ) ;
TestPropertyValues . of ( "spring.messages.basename:test/messages" )
. applyTo ( this . context ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
assertThat ( this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) )
. isEqualTo ( "bar" ) ;
}
}
}
private void load ( String . . . environment ) {
private boolean isUseCodeAsDefaultMessage ( MessageSource messageSource ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
return ( boolean ) new DirectFieldAccessor ( messageSource )
TestPropertyValues . of ( environment ) . applyTo ( this . context ) ;
. getPropertyValue ( "useCodeAsDefaultMessage" ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
}
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
@Test
public void existingMessageSourceIsPreferred ( ) {
this . contextRunner . withUserConfiguration ( CustomMessageSource . class )
. run ( ( context ) - > assertThat ( context . getMessage ( "foo" , null , null , null ) )
. isEqualTo ( "foo" ) ) ;
}
@Test
public void existingMessageSourceInParentIsIgnored ( ) {
this . contextRunner . run ( ( parent ) - > {
this . contextRunner . withParent ( parent )
. withPropertyValues ( "spring.messages.basename:test/messages" )
. run ( ( context ) - > assertThat ( context . getMessage (
"foo" , null , "Foo message" , Locale . UK ) ) . isEqualTo ( "bar" ) ) ;
} ) ;
}
}
@Configuration
@Configuration