@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 2014 the original author or authors .
* Copyright 2012 - 2015 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure;
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure;
import java.util.Locale ;
import org.junit.After ;
import org.junit.Ignore ;
import org.junit.Test ;
import org.springframework.boot.test.EnvironmentTestUtils ;
@ -26,72 +27,59 @@ import org.springframework.context.annotation.Configuration;
@@ -26,72 +27,59 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertTrue ;
/ * *
* Tests for { @link MessageSourceAutoConfiguration } .
*
* @author Dave Syer
* @author Eddú Meléndez
* @author Stephane Nicoll
* /
public class MessageSourceAutoConfigurationTests {
private AnnotationConfigApplicationContext context ;
@After
public void closeContext ( ) {
if ( context ! = null ) {
context . close ( ) ;
}
}
@Test
public void testDefaultMessageSource ( ) throws Exception {
this . context = new AnnotationConfigApplicationContext ( ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
load ( ) ;
assertEquals ( "Foo message" ,
this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
}
@Test
public void testMessageSourceCreated ( ) throws Exception {
this . context = new AnnotationConfigApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.messages.basename:test/messages" ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
load ( "spring.messages.basename:test/messages" ) ;
assertEquals ( "bar" ,
this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
}
@Test
public void testEncodingWorks ( ) throws Exception {
this . context = new AnnotationConfigApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.messages.basename:test/swedish" ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
load ( "spring.messages.basename:test/swedish" ) ;
assertEquals ( "Some text with some swedish öäå!" ,
this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
}
@Test
public void testMultipleMessageSourceCreated ( ) throws Exception {
this . context = new AnnotationConfigApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.messages.basename:test/messages,test/messages2" ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
assertEquals ( "bar" ,
this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
load ( "spring.messages.basename:test/messages,test/messages2" ) ;
assertEquals ( "bar" , this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
assertEquals ( "bar-bar" ,
this . context . getMessage ( "foo-foo" , null , "Foo-Foo message" , Locale . UK ) ) ;
}
@Test
public void testBadEncoding ( ) throws Exception {
this . context = new AnnotationConfigApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context ,
"spring.messages.encoding:rubbish" ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
load ( "spring.messages.encoding:rubbish" ) ;
// Bad encoding just means the messages are ignored
assertEquals ( "blah" , this . context . getMessage ( "foo" , null , "blah" , Locale . UK ) ) ;
}
@ -107,6 +95,30 @@ public class MessageSourceAutoConfigurationTests {
@@ -107,6 +95,30 @@ public class MessageSourceAutoConfigurationTests {
this . context . getMessage ( "foo" , null , "Foo message" , Locale . UK ) ) ;
}
@Test
public void testFallbackDefault ( ) throws Exception {
load ( "spring.messages.basename:test/messages" ) ;
assertTrue ( this . context . getBean ( MessageSourceAutoConfiguration . class )
. isFallbackToSystemLocale ( ) ) ;
}
@Test
public void testFallbackTurnOff ( ) throws Exception {
load ( "spring.messages.basename:test/messages" ,
"spring.messages.fallback-to-system-locale:false" ) ;
assertFalse ( this . context . getBean ( MessageSourceAutoConfiguration . class )
. isFallbackToSystemLocale ( ) ) ;
}
private void load ( String . . . environment ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
EnvironmentTestUtils . addEnvironment ( this . context , environment ) ;
this . context . register ( MessageSourceAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ) ;
this . context . refresh ( ) ;
}
@Configuration
@PropertySource ( "classpath:/switch-messages.properties" )
protected static class Config {