@ -16,12 +16,13 @@
package org.springframework.boot.env ;
import java.util. Map ;
import java.util. List ;
import java.util.Properties ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.springframework.boot.env.OriginTrackedPropertiesLoader.Document ;
import org.springframework.boot.origin.OriginTrackedValue ;
import org.springframework.boot.origin.TextResourceOrigin ;
import org.springframework.core.io.ClassPathResource ;
@ -40,47 +41,48 @@ class OriginTrackedPropertiesLoaderTests {
private ClassPathResource resource ;
private Map< String , OriginTrackedValue > properti es;
private List< Document > document es;
@BeforeEach
void setUp ( ) throws Exception {
String path = "test-properties.properties" ;
this . resource = new ClassPathResource ( path , getClass ( ) ) ;
this . properti es = new OriginTrackedPropertiesLoader ( this . resource ) . load ( ) ;
this . document es = new OriginTrackedPropertiesLoader ( this . resource ) . load ( ) ;
}
@Test
void compareToJavaProperties ( ) throws Exception {
Properties java = PropertiesLoaderUtils . loadProperties ( this . resource ) ;
Properties ours = new Properties ( ) ;
new OriginTrackedPropertiesLoader ( this . resource ) . load ( false ) . forEach ( ( k , v ) - > ours . put ( k , v . getValue ( ) ) ) ;
new OriginTrackedPropertiesLoader ( this . resource ) . load ( false ) . get ( 0 ) . asMap ( )
. forEach ( ( k , v ) - > ours . put ( k , v . getValue ( ) ) ) ;
assertThat ( ours ) . isEqualTo ( java ) ;
}
@Test
void getSimpleProperty ( ) {
OriginTrackedValue value = this . properties . ge t( "test" ) ;
OriginTrackedValue value = getFromFirs t( "test" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "properties" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "11:6" ) ;
}
@Test
void getSimplePropertyWithColonSeparator ( ) {
OriginTrackedValue value = this . properties . ge t( "test-colon-separator" ) ;
OriginTrackedValue value = getFromFirs t( "test-colon-separator" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "my-property" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "15:23" ) ;
}
@Test
void getPropertyWithSeparatorSurroundedBySpaces ( ) {
OriginTrackedValue value = this . properties . ge t( "blah" ) ;
OriginTrackedValue value = getFromFirs t( "blah" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "hello world" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "2:12" ) ;
}
@Test
void getUnicodeProperty ( ) {
OriginTrackedValue value = this . properties . ge t( "test-unicode" ) ;
OriginTrackedValue value = getFromFirs t( "test-unicode" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "properties&test" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "12:14" ) ;
}
@ -95,165 +97,169 @@ class OriginTrackedPropertiesLoaderTests {
@Test
void getEscapedProperty ( ) {
OriginTrackedValue value = this . properties . ge t( "test=property" ) ;
OriginTrackedValue value = getFromFirs t( "test=property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "helloworld" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "14:15" ) ;
}
@Test
void getPropertyWithTab ( ) {
OriginTrackedValue value = this . properties . ge t( "test-tab-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-tab-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo\tbar" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "16:19" ) ;
}
@Test
void getPropertyWithBang ( ) {
OriginTrackedValue value = this . properties . ge t( "test-bang-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-bang-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo!" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "34:20" ) ;
}
@Test
void getPropertyWithValueComment ( ) {
OriginTrackedValue value = this . properties . ge t( "test-property-value-comment" ) ;
OriginTrackedValue value = getFromFirs t( "test-property-value-comment" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo !bar #foo" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "36:29" ) ;
}
@Test
void getPropertyWithMultilineImmediateBang ( ) {
OriginTrackedValue value = this . properties . ge t( "test-multiline-immediate-bang" ) ;
OriginTrackedValue value = getFromFirs t( "test-multiline-immediate-bang" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "!foo" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "39:1" ) ;
}
@Test
void getPropertyWithCarriageReturn ( ) {
OriginTrackedValue value = this . properties . ge t( "test-return-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-return-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo\rbar" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "17:22" ) ;
}
@Test
void getPropertyWithNewLine ( ) {
OriginTrackedValue value = this . properties . ge t( "test-newline-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-newline-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo\nbar" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "18:23" ) ;
}
@Test
void getPropertyWithFormFeed ( ) {
OriginTrackedValue value = this . properties . ge t( "test-form-feed-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-form-feed-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo\fbar" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "19:25" ) ;
}
@Test
void getPropertyWithWhiteSpace ( ) {
OriginTrackedValue value = this . properties . ge t( "test-whitespace-property" ) ;
OriginTrackedValue value = getFromFirs t( "test-whitespace-property" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo bar" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "20:32" ) ;
}
@Test
void getCommentedOutPropertyShouldBeNull ( ) {
assertThat ( this . properties . ge t( "commented-property" ) ) . isNull ( ) ;
assertThat ( this . properties . ge t( "#commented-property" ) ) . isNull ( ) ;
assertThat ( this . properties . ge t( "commented-two" ) ) . isNull ( ) ;
assertThat ( this . properties . ge t( "!commented-two" ) ) . isNull ( ) ;
assertThat ( getFromFirs t( "commented-property" ) ) . isNull ( ) ;
assertThat ( getFromFirs t( "#commented-property" ) ) . isNull ( ) ;
assertThat ( getFromFirs t( "commented-two" ) ) . isNull ( ) ;
assertThat ( getFromFirs t( "!commented-two" ) ) . isNull ( ) ;
}
@Test
void getMultiline ( ) {
OriginTrackedValue value = this . properties . ge t( "test-multiline" ) ;
OriginTrackedValue value = getFromFirs t( "test-multiline" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "ab\\c" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "21:17" ) ;
}
@Test
void getImmediateMultiline ( ) {
OriginTrackedValue value = this . properties . ge t( "test-multiline-immediate" ) ;
OriginTrackedValue value = getFromFirs t( "test-multiline-immediate" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "32:1" ) ;
}
@Test
void getPropertyWithWhitespaceAfterKey ( ) {
OriginTrackedValue value = this . properties . ge t( "bar" ) ;
OriginTrackedValue value = getFromFirs t( "bar" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "foo=baz" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "3:7" ) ;
}
@Test
void getPropertyWithSpaceSeparator ( ) {
OriginTrackedValue value = this . properties . ge t( "hello" ) ;
OriginTrackedValue value = getFromFirs t( "hello" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "world" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "4:9" ) ;
}
@Test
void getPropertyWithBackslashEscaped ( ) {
OriginTrackedValue value = this . properties . ge t( "proper\\ty" ) ;
OriginTrackedValue value = getFromFirs t( "proper\\ty" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "test" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "5:11" ) ;
}
@Test
void getPropertyWithEmptyValue ( ) {
OriginTrackedValue value = this . properties . ge t( "foo" ) ;
OriginTrackedValue value = getFromFirs t( "foo" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "7:0" ) ;
}
@Test
void getPropertyWithBackslashEscapedInValue ( ) {
OriginTrackedValue value = this . properties . ge t( "bat" ) ;
OriginTrackedValue value = getFromFirs t( "bat" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "a\\" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "7:7" ) ;
}
@Test
void getPropertyWithSeparatorInValue ( ) {
OriginTrackedValue value = this . properties . ge t( "bling" ) ;
OriginTrackedValue value = getFromFirs t( "bling" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "a=b" ) ;
assertThat ( getLocation ( value ) ) . isEqualTo ( "8:9" ) ;
}
@Test
void getListProperty ( ) {
OriginTrackedValue apple = this . properties . ge t( "foods[0]" ) ;
OriginTrackedValue apple = getFromFirs t( "foods[0]" ) ;
assertThat ( getValue ( apple ) ) . isEqualTo ( "Apple" ) ;
assertThat ( getLocation ( apple ) ) . isEqualTo ( "24:9" ) ;
OriginTrackedValue orange = this . properties . ge t( "foods[1]" ) ;
OriginTrackedValue orange = getFromFirs t( "foods[1]" ) ;
assertThat ( getValue ( orange ) ) . isEqualTo ( "Orange" ) ;
assertThat ( getLocation ( orange ) ) . isEqualTo ( "25:1" ) ;
OriginTrackedValue strawberry = this . properties . ge t( "foods[2]" ) ;
OriginTrackedValue strawberry = getFromFirs t( "foods[2]" ) ;
assertThat ( getValue ( strawberry ) ) . isEqualTo ( "Strawberry" ) ;
assertThat ( getLocation ( strawberry ) ) . isEqualTo ( "26:1" ) ;
OriginTrackedValue mango = this . properties . ge t( "foods[3]" ) ;
OriginTrackedValue mango = getFromFirs t( "foods[3]" ) ;
assertThat ( getValue ( mango ) ) . isEqualTo ( "Mango" ) ;
assertThat ( getLocation ( mango ) ) . isEqualTo ( "27:1" ) ;
}
@Test
void getPropertyWithISO88591Character ( ) {
OriginTrackedValue value = this . properties . ge t( "test-iso8859-1-chars" ) ;
OriginTrackedValue value = getFromFirs t( "test-iso8859-1-chars" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "æ×ÈÅÞßáñÀÿ" ) ;
}
@Test
void getPropertyWithTrailingSpace ( ) {
OriginTrackedValue value = this . properties . ge t( "test-with-trailing-space" ) ;
OriginTrackedValue value = getFromFirs t( "test-with-trailing-space" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "trailing " ) ;
}
@Test
void getPropertyWithEscapedTrailingSpace ( ) {
OriginTrackedValue value = this . properties . ge t( "test-with-escaped-trailing-space" ) ;
OriginTrackedValue value = getFromFirs t( "test-with-escaped-trailing-space" ) ;
assertThat ( getValue ( value ) ) . isEqualTo ( "trailing " ) ;
}
private OriginTrackedValue getFromFirst ( String key ) {
return this . documentes . get ( 0 ) . asMap ( ) . get ( key ) ;
}
private Object getValue ( OriginTrackedValue value ) {
return ( value ! = null ) ? value . getValue ( ) : null ;
}