@ -26,8 +26,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir ;
import org.junit.jupiter.api.io.TempDir ;
import org.springframework.boot.convert.ApplicationConversionService ;
import org.springframework.boot.convert.ApplicationConversionService ;
import org.springframework.boot.env. VolumeMountDirectory PropertySource.Option;
import org.springframework.boot.env. ConfigTree PropertySource.Option;
import org.springframework.boot.env. VolumeMountDirectory PropertySource.Value;
import org.springframework.boot.env. ConfigTree PropertySource.Value;
import org.springframework.boot.origin.TextResourceOrigin ;
import org.springframework.boot.origin.TextResourceOrigin ;
import org.springframework.core.convert.ConversionService ;
import org.springframework.core.convert.ConversionService ;
import org.springframework.core.convert.support.ConfigurableConversionService ;
import org.springframework.core.convert.support.ConfigurableConversionService ;
@ -40,32 +40,31 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatIllegalStateException ;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException ;
/ * *
/ * *
* Tests for { @link VolumeMountDirectory PropertySource} .
* Tests for { @link ConfigTree PropertySource} .
*
*
* @author Phillip Webb
* @author Phillip Webb
* /
* /
class VolumeMountDirectory PropertySourceTests {
class ConfigTree PropertySourceTests {
@TempDir
@TempDir
Path directory ;
Path directory ;
@Test
@Test
void createWhenNameIsNullThrowsException ( ) {
void createWhenNameIsNullThrowsException ( ) {
assertThatIllegalArgumentException ( )
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new ConfigTreePropertySource ( null , this . directory ) )
. isThrownBy ( ( ) - > new VolumeMountDirectoryPropertySource ( null , this . directory ) )
. withMessageContaining ( "name must contain" ) ;
. withMessageContaining ( "name must contain" ) ;
}
}
@Test
@Test
void createWhenSourceIsNullThrowsException ( ) {
void createWhenSourceIsNullThrowsException ( ) {
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new VolumeMountDirectory PropertySource( "test" , null ) )
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new ConfigTree PropertySource( "test" , null ) )
. withMessage ( "Property source must not be null" ) ;
. withMessage ( "Property source must not be null" ) ;
}
}
@Test
@Test
void createWhenSourceDoesNotExistThrowsException ( ) {
void createWhenSourceDoesNotExistThrowsException ( ) {
Path missing = this . directory . resolve ( "missing" ) ;
Path missing = this . directory . resolve ( "missing" ) ;
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new VolumeMountDirectory PropertySource( "test" , missing ) )
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new ConfigTree PropertySource( "test" , missing ) )
. withMessage ( "Directory '" + missing + "' does not exist" ) ;
. withMessage ( "Directory '" + missing + "' does not exist" ) ;
}
}
@ -73,45 +72,45 @@ class VolumeMountDirectoryPropertySourceTests {
void createWhenSourceIsFileThrowsException ( ) throws Exception {
void createWhenSourceIsFileThrowsException ( ) throws Exception {
Path file = this . directory . resolve ( "file" ) ;
Path file = this . directory . resolve ( "file" ) ;
FileCopyUtils . copy ( "test" . getBytes ( StandardCharsets . UTF_8 ) , file . toFile ( ) ) ;
FileCopyUtils . copy ( "test" . getBytes ( StandardCharsets . UTF_8 ) , file . toFile ( ) ) ;
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new VolumeMountDirectory PropertySource( "test" , file ) )
assertThatIllegalArgumentException ( ) . isThrownBy ( ( ) - > new ConfigTree PropertySource( "test" , file ) )
. withMessage ( "File '" + file + "' is not a directory" ) ;
. withMessage ( "File '" + file + "' is not a directory" ) ;
}
}
@Test
@Test
void getPropertyNamesFromFlatReturnsPropertyNames ( ) throws Exception {
void getPropertyNamesFromFlatReturnsPropertyNames ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "a" , "b" , "c" ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "a" , "b" , "c" ) ;
}
}
@Test
@Test
void getPropertyNamesFromNestedReturnsPropertyNames ( ) throws Exception {
void getPropertyNamesFromNestedReturnsPropertyNames ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getNestedPropertySource ( ) ;
ConfigTree PropertySource propertySource = getNestedPropertySource ( ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "c" , "fa.a" , "fa.b" , "fb.a" , "fb.fa.a" ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "c" , "fa.a" , "fa.b" , "fb.a" , "fb.fa.a" ) ;
}
}
@Test
@Test
void getPropertyNamesWhenLowercaseReturnsPropertyNames ( ) throws Exception {
void getPropertyNamesWhenLowercaseReturnsPropertyNames ( ) throws Exception {
addProperty ( "SpRiNg" , "boot" ) ;
addProperty ( "SpRiNg" , "boot" ) ;
VolumeMountDirectoryPropertySource propertySource = new VolumeMountDirectory PropertySource( "test" ,
ConfigTreePropertySource propertySource = new ConfigTree PropertySource( "test" , this . directory ,
this . directory , Option . USE_LOWERCASE_NAMES ) ;
Option . USE_LOWERCASE_NAMES ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "spring" ) ;
assertThat ( propertySource . getPropertyNames ( ) ) . containsExactly ( "spring" ) ;
}
}
@Test
@Test
void getPropertyFromFlatReturnsFileContent ( ) throws Exception {
void getPropertyFromFlatReturnsFileContent ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
assertThat ( propertySource . getProperty ( "b" ) ) . hasToString ( "B" ) ;
assertThat ( propertySource . getProperty ( "b" ) ) . hasToString ( "B" ) ;
}
}
@Test
@Test
void getPropertyFromFlatWhenMissingReturnsNull ( ) throws Exception {
void getPropertyFromFlatWhenMissingReturnsNull ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
assertThat ( propertySource . getProperty ( "missing" ) ) . isNull ( ) ;
assertThat ( propertySource . getProperty ( "missing" ) ) . isNull ( ) ;
}
}
@Test
@Test
void getPropertyFromFlatWhenFileDeletedThrowsException ( ) throws Exception {
void getPropertyFromFlatWhenFileDeletedThrowsException ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
Path b = this . directory . resolve ( "b" ) ;
Path b = this . directory . resolve ( "b" ) ;
Files . delete ( b ) ;
Files . delete ( b ) ;
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - > propertySource . getProperty ( "b" ) . toString ( ) )
assertThatIllegalStateException ( ) . isThrownBy ( ( ) - > propertySource . getProperty ( "b" ) . toString ( ) )
@ -120,7 +119,7 @@ class VolumeMountDirectoryPropertySourceTests {
@Test
@Test
void getOriginFromFlatReturnsOrigin ( ) throws Exception {
void getOriginFromFlatReturnsOrigin ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
TextResourceOrigin origin = ( TextResourceOrigin ) propertySource . getOrigin ( "b" ) ;
TextResourceOrigin origin = ( TextResourceOrigin ) propertySource . getOrigin ( "b" ) ;
assertThat ( origin . getResource ( ) . getFile ( ) ) . isEqualTo ( this . directory . resolve ( "b" ) . toFile ( ) ) ;
assertThat ( origin . getResource ( ) . getFile ( ) ) . isEqualTo ( this . directory . resolve ( "b" ) . toFile ( ) ) ;
assertThat ( origin . getLocation ( ) . getLine ( ) ) . isEqualTo ( 0 ) ;
assertThat ( origin . getLocation ( ) . getLine ( ) ) . isEqualTo ( 0 ) ;
@ -129,7 +128,7 @@ class VolumeMountDirectoryPropertySourceTests {
@Test
@Test
void getOriginFromFlatWhenMissingReturnsNull ( ) throws Exception {
void getOriginFromFlatWhenMissingReturnsNull ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getFlatPropertySource ( ) ;
ConfigTree PropertySource propertySource = getFlatPropertySource ( ) ;
assertThat ( propertySource . getOrigin ( "missing" ) ) . isNull ( ) ;
assertThat ( propertySource . getOrigin ( "missing" ) ) . isNull ( ) ;
}
}
@ -147,13 +146,13 @@ class VolumeMountDirectoryPropertySourceTests {
@Test
@Test
void getPropertyFromNestedReturnsFileContent ( ) throws Exception {
void getPropertyFromNestedReturnsFileContent ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getNestedPropertySource ( ) ;
ConfigTree PropertySource propertySource = getNestedPropertySource ( ) ;
assertThat ( propertySource . getProperty ( "fb.fa.a" ) ) . hasToString ( "BAA" ) ;
assertThat ( propertySource . getProperty ( "fb.fa.a" ) ) . hasToString ( "BAA" ) ;
}
}
@Test
@Test
void getPropertyWhenNotAlwaysReadIgnoresUpdates ( ) throws Exception {
void getPropertyWhenNotAlwaysReadIgnoresUpdates ( ) throws Exception {
VolumeMountDirectory PropertySource propertySource = getNestedPropertySource ( ) ;
ConfigTree PropertySource propertySource = getNestedPropertySource ( ) ;
Value v1 = propertySource . getProperty ( "fa.b" ) ;
Value v1 = propertySource . getProperty ( "fa.b" ) ;
Value v2 = propertySource . getProperty ( "fa.b" ) ;
Value v2 = propertySource . getProperty ( "fa.b" ) ;
assertThat ( v1 ) . isSameAs ( v2 ) ;
assertThat ( v1 ) . isSameAs ( v2 ) ;
@ -167,8 +166,8 @@ class VolumeMountDirectoryPropertySourceTests {
@Test
@Test
void getPropertyWhenAlwaysReadReflectsUpdates ( ) throws Exception {
void getPropertyWhenAlwaysReadReflectsUpdates ( ) throws Exception {
addNested ( ) ;
addNested ( ) ;
VolumeMountDirectoryPropertySource propertySource = new VolumeMountDirectory PropertySource( "test" ,
ConfigTreePropertySource propertySource = new ConfigTree PropertySource( "test" , this . directory ,
this . directory , Option . ALWAYS_READ ) ;
Option . ALWAYS_READ ) ;
Value v1 = propertySource . getProperty ( "fa.b" ) ;
Value v1 = propertySource . getProperty ( "fa.b" ) ;
Value v2 = propertySource . getProperty ( "fa.b" ) ;
Value v2 = propertySource . getProperty ( "fa.b" ) ;
assertThat ( v1 ) . isNotSameAs ( v2 ) ;
assertThat ( v1 ) . isNotSameAs ( v2 ) ;
@ -183,21 +182,21 @@ class VolumeMountDirectoryPropertySourceTests {
@Test
@Test
void getPropertyWhenLowercaseReturnsValue ( ) throws Exception {
void getPropertyWhenLowercaseReturnsValue ( ) throws Exception {
addProperty ( "SpRiNg" , "boot" ) ;
addProperty ( "SpRiNg" , "boot" ) ;
VolumeMountDirectoryPropertySource propertySource = new VolumeMountDirectory PropertySource( "test" ,
ConfigTreePropertySource propertySource = new ConfigTree PropertySource( "test" , this . directory ,
this . directory , Option . USE_LOWERCASE_NAMES ) ;
Option . USE_LOWERCASE_NAMES ) ;
assertThat ( propertySource . getProperty ( "spring" ) ) . hasToString ( "boot" ) ;
assertThat ( propertySource . getProperty ( "spring" ) ) . hasToString ( "boot" ) ;
}
}
private VolumeMountDirectory PropertySource getFlatPropertySource ( ) throws IOException {
private ConfigTree PropertySource getFlatPropertySource ( ) throws IOException {
addProperty ( "a" , "A" ) ;
addProperty ( "a" , "A" ) ;
addProperty ( "b" , "B" ) ;
addProperty ( "b" , "B" ) ;
addProperty ( "c" , "C" ) ;
addProperty ( "c" , "C" ) ;
return new VolumeMountDirectory PropertySource( "test" , this . directory ) ;
return new ConfigTree PropertySource( "test" , this . directory ) ;
}
}
private VolumeMountDirectory PropertySource getNestedPropertySource ( ) throws IOException {
private ConfigTree PropertySource getNestedPropertySource ( ) throws IOException {
addNested ( ) ;
addNested ( ) ;
return new VolumeMountDirectory PropertySource( "test" , this . directory ) ;
return new ConfigTree PropertySource( "test" , this . directory ) ;
}
}
private void addNested ( ) throws IOException {
private void addNested ( ) throws IOException {