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