-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from FBibonne/1-manage-excluded-sources-as-desc…
…ribed-in-readme 1 manage excluded sources as described in readme
- Loading branch information
Showing
10 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package fr.insee.test; | ||
|
||
import fr.insee.boot.PropertiesLogger; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = ExcludedPropertySourcesTest.class, properties = { | ||
"fr.insee.shared = inlineTestProperties", | ||
"fr.insee.specific.inlineTestProperties = inlineTestProperties"}, | ||
args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args","--spring.config.additional-location=classpath:/otherProps/,classpath:/nonExcluded/"} | ||
) | ||
@Configuration | ||
class ExcludedPropertySourcesTest { | ||
|
||
static Path externalPropertiesPath; | ||
|
||
@BeforeAll | ||
static void createTempPropertyFileInFileSystem() throws IOException { | ||
externalPropertiesPath = Files.createTempFile("inFileSystem", ".properties"); | ||
String content = """ | ||
fr.insee.shared = inFileSystem | ||
fr.insee.specific.inFileSystem = inFileSystem | ||
fr.insee.sharedWithExternal = inFileSystem | ||
properties.logger.sources-ignored = systemProperties, systemEnvironment,[application.properties],[otherProps/application.properties],commandLineArgs,Inlined\\ Test\\ Properties,[""" | ||
+ externalPropertiesPath.toAbsolutePath()+"]"; | ||
Files.writeString(externalPropertiesPath, content); | ||
|
||
System.setProperty("spring.config.import", "file:" + externalPropertiesPath.toAbsolutePath()); | ||
} | ||
|
||
@DynamicPropertySource | ||
static void setProperties(DynamicPropertyRegistry registry) { | ||
registry.add("fr.insee.shared", ()->"dynamicPropertySource"); | ||
registry.add("fr.insee.specific.dynamicPropertySource",()->"dynamicPropertySource"); | ||
} | ||
|
||
|
||
@Test | ||
void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment environment) { | ||
|
||
//GIVEN context and prop properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/application.properties,commandLineArgs,Inlined\ Test\ Properties | ||
//not excluded : nonExcluded.properties, | ||
//WHEN context loads | ||
//THEN | ||
String logOutput = ((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().toString(); | ||
|
||
System.out.println(logOutput); | ||
|
||
assertThat(environment.getProperty("properties.logger.sources-ignored")).hasToString("systemProperties, systemEnvironment,[application.properties],[otherProps/application.properties],commandLineArgs,Inlined Test Properties,["+ externalPropertiesPath.toAbsolutePath()+"]"); | ||
assertThat(environment.getProperty("fr.insee.shared")).hasToString("dynamicPropertySource"); | ||
assertThat(environment.getProperty("fr.insee.specific.nonExcludedFile")).hasToString("nonExcludedFile"); | ||
assertThat(logOutput).contains("fr.insee.shared = inlineTestProperties") | ||
.doesNotContain("fr.insee.specific.dynamicPropertySource") | ||
.contains("fr.insee.specific.nonExcludedFile = nonExcludedFile") | ||
.doesNotContain("fr.insee.sharedWithExternal = nonExcludedFile") | ||
.contains("fr.insee.sharedWithExternal = inFileSystem") | ||
.doesNotContain("fr.insee.specific.inFileSystem = inFileSystem") | ||
.doesNotContain("fr.insee.specific.applicationproperties") | ||
.doesNotContain("fr.insee.specific.additionalPropsInClasspath") | ||
.doesNotContain("fr.insee.specific.args") | ||
.doesNotContain("fr.insee.specific.inFileSystem") | ||
.doesNotContain("fr.insee.specific.inlineTestProperties") | ||
.doesNotContain("class path resource [application.properties]") | ||
.doesNotContain("class path resource [otherProps/application.properties]") | ||
.doesNotContain("- commandLineArgs") | ||
.doesNotContain("Config resource 'file ["+externalPropertiesPath+"]'") | ||
.doesNotContain("- Inlined Test Properties") | ||
.doesNotContain("- systemProperties") | ||
.doesNotContain("- systemEnvironment") | ||
.doesNotContain("class path resource [application.properties]") | ||
; | ||
assertThat(environment.getProperty("fr.insee.specific.inFileSystem")).hasToString("inFileSystem"); | ||
assertThat(environment.getProperty("fr.insee.specific.applicationproperties")).hasToString("application.properties"); | ||
assertThat(environment.getProperty("fr.insee.specific.additionalPropsInClasspath")).hasToString("additionalPropsInClasspath"); | ||
assertThat(environment.getProperty("fr.insee.specific.args")).hasToString("args"); | ||
|
||
} | ||
|
||
@AfterAll | ||
static void deleteTempPropertyFileInFileSystem() throws IOException { | ||
Files.delete(externalPropertiesPath); | ||
System.clearProperty("spring.config.import"); | ||
((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package spring.issue; | ||
|
||
import fr.insee.boot.PropertiesLogger; | ||
import fr.insee.test.Slf4jStub; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = IssueWithDynamicPropertySourceTest.class ,properties = { | ||
"spring.config.import=", | ||
//"spring.config.additional-location=classpath:spring/issue/application.properties" | ||
} | ||
) | ||
@Configuration | ||
class IssueWithDynamicPropertySourceTest { | ||
|
||
@DynamicPropertySource | ||
static void setProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.config.additional-location", () -> "classpath:/spring/issue/"); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
void propertyFromAddtionalLocationShouldBeLoaded(@Autowired Environment environment) { | ||
assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:/spring/issue/"); | ||
assertThat(environment.getProperty("property.in.addtional.file")).hasToString("ok"); | ||
} | ||
|
||
@AfterAll | ||
static void clearLogStub(){ | ||
((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
fr.insee.test = ok | ||
fr.insee.secret = SECRET | ||
fr.insee.secret = SECRET | ||
fr.insee.shared = application.properties | ||
fr.insee.specific.applicationproperties = application.properties | ||
properties.logger.prefix-for-properties=debug, trace, info, logging, spring, server, management, springdoc, properties, fr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fr.insee.shared = nonExcludedFile | ||
fr.insee.specific.nonExcludedFile = nonExcludedFile | ||
fr.insee.sharedWithExternal = nonExcludedFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fr.insee.shared = additionalPropsInClasspath | ||
fr.insee.specific.additionalPropsInClasspath = additionalPropsInClasspath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
property.in.addtional.file=ok |