From b77409fd4c556b10c8ceb0337924749a5fc5f754 Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Mon, 9 Sep 2024 07:27:35 +0200 Subject: [PATCH 1/9] feat (excluded sources) : start working on #1 --- .../insee/test/ExcludedPropertySourcesTest.java | 17 +++++++++++++++++ .../fr/insee/test/WebAppIntegrationTest.java | 5 +++++ src/test/resources/application.properties | 3 ++- .../additionalPropsInClasspath.properties | 0 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java create mode 100644 src/test/resources/otherProps/additionalPropsInClasspath.properties diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java new file mode 100644 index 0000000..18eb2b2 --- /dev/null +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -0,0 +1,17 @@ +package fr.insee.test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Configuration; + +@SpringBootTest(classes = ExcludedPropertySourcesTest.class,properties = { + "properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],/secrets/secret.properties,commandLineArgs,Inlined\\ Test\\ Properties", + "spring.config.additional-location="} +) +@Configuration +class ExcludedPropertySourcesTest { + + void contextLoad_shouldNotPrintExcludedPropertySources() { + + } + +} diff --git a/src/test/java/fr/insee/test/WebAppIntegrationTest.java b/src/test/java/fr/insee/test/WebAppIntegrationTest.java index 6beaa53..af04440 100644 --- a/src/test/java/fr/insee/test/WebAppIntegrationTest.java +++ b/src/test/java/fr/insee/test/WebAppIntegrationTest.java @@ -34,4 +34,9 @@ void contextLoads() { ); } + @Test + void excludedPropertySourcesTest(){ + + } + } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 3a9b6d3..280ca86 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,2 +1,3 @@ fr.insee.test = ok -fr.insee.secret = SECRET \ No newline at end of file +fr.insee.secret = SECRET +spring.config.import=classpath:otherProps/additionalPropsInClasspath.properties \ No newline at end of file diff --git a/src/test/resources/otherProps/additionalPropsInClasspath.properties b/src/test/resources/otherProps/additionalPropsInClasspath.properties new file mode 100644 index 0000000..e69de29 From 2e569ac58249383265b63da9681c3fd17d1db3d1 Mon Sep 17 00:00:00 2001 From: Fabrice B Date: Mon, 9 Sep 2024 17:52:39 +0200 Subject: [PATCH 2/9] feat (excluded sources) : continue working on #1 --- .../test/ExcludedPropertySourcesTest.java | 41 +++++++++++++++++++ src/test/resources/application.properties | 4 +- src/test/resources/nonExcludedFile.properties | 3 ++ .../additionalPropsInClasspath.properties | 2 + 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/nonExcludedFile.properties diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index 18eb2b2..4671bc3 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -1,7 +1,15 @@ package fr.insee.test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; +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; @SpringBootTest(classes = ExcludedPropertySourcesTest.class,properties = { "properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],/secrets/secret.properties,commandLineArgs,Inlined\\ Test\\ Properties", @@ -10,8 +18,41 @@ @Configuration class ExcludedPropertySourcesTest { + static Path externalPropertiesPath; + + @BeforeAll + static void createTempPropertyFileInFileSystem() throws IOException { + externalPropertiesPath=Files.createTempFile("inFileSystem", "properties"); + fr.insee.shared = inFileSystem + fr.insee.specific.inFileSystem = inFileSystem + fr.insee.sharedWithExternal = inFileSystem + } + + @DynamicPropertySource + static void setProperties(DynamicPropertyRegistry registry) { + registry.add("spring.config.additional-location", () -> "file:"+externalPropertiesPath.toAbsolutePath()); + } + + void contextLoad_shouldNotPrintExcludedPropertySources() { + non exclu: + props internes + + exclus: + application.properties + classpath:otherProps/additionalPropsInClasspath.properties + commandLineArgs + Inlined\ Test\ Properties + externalPropertiesPath + @DynamicPropertySource + + + } + + @AfterAll + static void deleteTempPropertyFileInFileSystem() throws IOException { + Files.delete(externalPropertiesPath); } } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 280ca86..037f353 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,3 +1,5 @@ fr.insee.test = ok fr.insee.secret = SECRET -spring.config.import=classpath:otherProps/additionalPropsInClasspath.properties \ No newline at end of file +fr.insee.shared = application.properties +fr.insee.specific.applicationproperties = application.properties +spring.config.import=classpath:otherProps/additionalPropsInClasspath.properties,classpath:nonExcludedFile.properties diff --git a/src/test/resources/nonExcludedFile.properties b/src/test/resources/nonExcludedFile.properties new file mode 100644 index 0000000..dbb7e43 --- /dev/null +++ b/src/test/resources/nonExcludedFile.properties @@ -0,0 +1,3 @@ +fr.insee.shared = nonExcludedFile +fr.insee.specific.nonExcludedFile = nonExcludedFile +fr.insee.sharedWithExternal = nonExcludedFile \ No newline at end of file diff --git a/src/test/resources/otherProps/additionalPropsInClasspath.properties b/src/test/resources/otherProps/additionalPropsInClasspath.properties index e69de29..f5f0b00 100644 --- a/src/test/resources/otherProps/additionalPropsInClasspath.properties +++ b/src/test/resources/otherProps/additionalPropsInClasspath.properties @@ -0,0 +1,2 @@ +fr.insee.shared = additionalPropsInClasspath +fr.insee.specific.additionalPropsInClasspath = additionalPropsInClasspath \ No newline at end of file From f6f0e32e5ecdc9381cb11c16e15f763551b82fee Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Tue, 10 Sep 2024 07:46:42 +0200 Subject: [PATCH 3/9] feat (excluded sources) : continue working on #1 --- .../test/ExcludedPropertySourcesTest.java | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index 4671bc3..16cf944 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -1,19 +1,29 @@ 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 java.sql.SQLOutput; -@SpringBootTest(classes = ExcludedPropertySourcesTest.class,properties = { - "properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],/secrets/secret.properties,commandLineArgs,Inlined\\ Test\\ Properties", - "spring.config.additional-location="} +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(classes = ExcludedPropertySourcesTest.class, properties = { + "properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\\ Test\\ Properties", + "fr.insee.shared = inlineTestProperties", + "fr.insee.specific.inlineTestProperties = inlineTestProperties"}, + args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args"} ) @Configuration class ExcludedPropertySourcesTest { @@ -22,31 +32,47 @@ class ExcludedPropertySourcesTest { @BeforeAll static void createTempPropertyFileInFileSystem() throws IOException { - externalPropertiesPath=Files.createTempFile("inFileSystem", "properties"); + System.out.println("#################### TEST AND FIX spring.config.additional-location does not work if declared in @DynamicPropertySource"); + externalPropertiesPath = Files.createTempFile("inFileSystem", ".properties"); + Files.writeString(externalPropertiesPath, """ fr.insee.shared = inFileSystem fr.insee.specific.inFileSystem = inFileSystem fr.insee.sharedWithExternal = inFileSystem + """); + + System.setProperty("spring.config.additional-location", "file:" + externalPropertiesPath.toAbsolutePath()); } @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { - registry.add("spring.config.additional-location", () -> "file:"+externalPropertiesPath.toAbsolutePath()); + registry.add("fr.insee.shared", ()->"dynamicPropertySource"); + registry.add("fr.insee.specific.dynamicPropertySource",()->"dynamicPropertySource"); } - void contextLoad_shouldNotPrintExcludedPropertySources() { + @Test + void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment environment) { - non exclu: - props internes + //GIVEN context and prop properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\ Test\ Properties + // excluded @DynamicPropertySource + //not excluded : nonExcluded.properties, externalfile + //WHEN context loads + //THEN + String logOutput = ((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().toString(); - exclus: - application.properties - classpath:otherProps/additionalPropsInClasspath.properties - commandLineArgs - Inlined\ Test\ Properties - externalPropertiesPath - @DynamicPropertySource + System.out.println(logOutput); + assertThat(environment.getProperty("properties.logger.sources-ignored")).hasToString("systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined Test Properties"); + assertThat(logOutput).contains("fr.insee.shared = dynamicPropertySource") + .doesNotContain("fr.insee.specific.dynamicPropertySource") + .contains("fr.insee.specific.nonExcludedFile = nonExcludedFile") + .doesNotContain("fr.insee.sharedWithExternal = nonExcludedFile") + .contains("fr.insee.sharedWithExternal = inFileSystem") + .contains("fr.insee.specific.inFileSystem = inFileSystem"); + 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"); } From d4e3b4de7455c4082467ac89a60b9a612e885ff8 Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Thu, 12 Sep 2024 07:46:24 +0200 Subject: [PATCH 4/9] test (excluded sources) : continue working on #1 --- README.md | 18 +++++++++ .../test/ExcludedPropertySourcesTest.java | 39 ++++++++++++------- .../IssueWithDynamicPropertySourceTest.java | 31 +++++++++++++++ src/test/resources/application.properties | 1 + .../additionalPropsInClasspath.properties | 1 + 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java create mode 100644 src/test/resources/spring/issue/additionalPropsInClasspath.properties diff --git a/README.md b/README.md index 011bf11..b7b5fbf 100644 --- a/README.md +++ b/README.md @@ -188,3 +188,21 @@ When a property is displayed, there is a filter which avoid to leak secrets in l one of the token in `properties.logger.with-hidden-values` will be displayed with the value `******` instead of the actual value. The default value of `properties.logger.with-hidden-values` contains the most common tokens for secrets but you should add the specific ones you may have in your application. + + +## Warnings + +### Properties from `@DynamicPropertySource` methods will not be displayed + +The Properties-Logger library cannot access to the property source of `@DynamicPropertySource` methods because +the properties logger library runs before the `@DynamicPropertySource` methods are executed. Therefore, **neither the properties +keys exclusively defined in `@DynamicPropertySource` methods, neither the values defined in this methods, whatever the +property key is, will be displayed** even if `@DynamicPropertySource` methods property source is not excluded. + +`@DynamicPropertySource` methods are a specific mecanism dedicated to test which let you define or overrride properties values +very late so it is not in the scope of the Properties Logger library which aims to display properties at the earliest time +in the application lifecycle. It is strongly recommended to not define in other place properties from `@DynamicPropertySource` +methods. If you have no other choice (make sure you had a complete look to +[the numerous ways to override properties in Spring Boot](https://docs.spring.io/spring-boot/reference/features/external-config.html) ) +that defining `@DynamicPropertySource` methods to override properties in a test and you want to check the value of the +overrided property, consider logging it yourself or write an assertion in your test \ No newline at end of file diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index 16cf944..2f67963 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -15,12 +15,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.sql.SQLOutput; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = ExcludedPropertySourcesTest.class, properties = { - "properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\\ Test\\ Properties", "fr.insee.shared = inlineTestProperties", "fr.insee.specific.inlineTestProperties = inlineTestProperties"}, args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args"} @@ -32,13 +30,14 @@ class ExcludedPropertySourcesTest { @BeforeAll static void createTempPropertyFileInFileSystem() throws IOException { - System.out.println("#################### TEST AND FIX spring.config.additional-location does not work if declared in @DynamicPropertySource"); externalPropertiesPath = Files.createTempFile("inFileSystem", ".properties"); - Files.writeString(externalPropertiesPath, """ - fr.insee.shared = inFileSystem - fr.insee.specific.inFileSystem = inFileSystem - fr.insee.sharedWithExternal = inFileSystem - """); + String content = """ + fr.insee.shared = inFileSystem + fr.insee.specific.inFileSystem = inFileSystem + fr.insee.sharedWithExternal = inFileSystem + properties.logger.sources-ignored = systemProperties, systemEnvironment,application.properties,otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\\ Test\\ Properties,""" + + externalPropertiesPath.toAbsolutePath(); + Files.writeString(externalPropertiesPath, content); System.setProperty("spring.config.additional-location", "file:" + externalPropertiesPath.toAbsolutePath()); } @@ -54,21 +53,35 @@ static void setProperties(DynamicPropertyRegistry registry) { void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment environment) { //GIVEN context and prop properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\ Test\ Properties - // excluded @DynamicPropertySource - //not excluded : nonExcluded.properties, externalfile + //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/additionalPropsInClasspath.properties,commandLineArgs,Inlined Test Properties"); - assertThat(logOutput).contains("fr.insee.shared = dynamicPropertySource") + assertThat(environment.getProperty("properties.logger.sources-ignored")).hasToString("systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined Test Properties,"+ externalPropertiesPath.toAbsolutePath()); + assertThat(environment.getProperty("fr.insee.shared")).hasToString("dynamicPropertySource"); + 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") - .contains("fr.insee.specific.inFileSystem = 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/additionalPropsInClasspath.properties]") + .doesNotContain("- commandLineArgs") + .doesNotContain("Config resource 'file ["+externalPropertiesPath+"]'") + .doesNotContain("- Inlined Test Properties") + .doesNotContain("- systemProperties") + .doesNotContain("- systemEnvironment") + .contains("class path resource [nonExcludedFile.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"); diff --git a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java new file mode 100644 index 0000000..fe1b8f2 --- /dev/null +++ b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java @@ -0,0 +1,31 @@ +package spring.issue; + +import org.junit.jupiter.api.Test; +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.additional-location=classpath:spring/issue/additionalPropsInClasspath.properties" +} +) +@Configuration +class IssueWithDynamicPropertySourceTest { + + @DynamicPropertySource + static void setProperties(DynamicPropertyRegistry registry) { + registry.add("spring.config.additional-location", () -> "classpath:spring/issue/additionalPropsInClasspath.properties"); + } + + @Test + void propertyFromAddtionalLocationShouldBeLoaded(@Autowired Environment environment) { + assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:spring/issue/additionalPropsInClasspath.properties"); + assertThat(environment.getProperty("property.in.addtional.file")).hasToString("ok"); + } + +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 037f353..cd38736 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -3,3 +3,4 @@ fr.insee.secret = SECRET fr.insee.shared = application.properties fr.insee.specific.applicationproperties = application.properties spring.config.import=classpath:otherProps/additionalPropsInClasspath.properties,classpath:nonExcludedFile.properties +properties.logger.prefix-for-properties=debug, trace, info, logging, spring, server, management, springdoc, properties, fr \ No newline at end of file diff --git a/src/test/resources/spring/issue/additionalPropsInClasspath.properties b/src/test/resources/spring/issue/additionalPropsInClasspath.properties new file mode 100644 index 0000000..4aaae2b --- /dev/null +++ b/src/test/resources/spring/issue/additionalPropsInClasspath.properties @@ -0,0 +1 @@ +property.in.addtional.file=ok \ No newline at end of file From b06f23ab03b3870fcef90117d97f8efb7ff5dd25 Mon Sep 17 00:00:00 2001 From: Fabrice B Date: Thu, 12 Sep 2024 09:38:41 +0200 Subject: [PATCH 5/9] feat(ignore property sources) --- .../java/fr/insee/boot/PropertiesLogger.java | 2 +- .../insee/test/ExcludedPropertySourcesTest.java | 17 +++++++++-------- .../IssueWithDynamicPropertySourceTest.java | 6 +++--- src/test/resources/application.properties | 1 - .../application.properties} | 0 ...sspath.properties => application.properties} | 0 6 files changed, 13 insertions(+), 13 deletions(-) rename src/test/resources/{nonExcludedFile.properties => nonExcluded/application.properties} (100%) rename src/test/resources/otherProps/{additionalPropsInClasspath.properties => application.properties} (100%) diff --git a/src/main/java/fr/insee/boot/PropertiesLogger.java b/src/main/java/fr/insee/boot/PropertiesLogger.java index 9782e4b..1bfc2d9 100644 --- a/src/main/java/fr/insee/boot/PropertiesLogger.java +++ b/src/main/java/fr/insee/boot/PropertiesLogger.java @@ -72,7 +72,7 @@ private boolean willBeProcessed(PropertySource propertySource, Set ig } private boolean isNotIgnored(PropertySource propertySource, Set ignoredPropertySources) { - if (ignoredPropertySources.contains(propertySource.getName())) { + if (ignoredPropertySources.stream().anyMatch(propertySource.getName()::contains)) { log.trace(() -> propertySource + " is listed to be ignored"); return false; } diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index 2f67963..e5696af 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -21,7 +21,7 @@ @SpringBootTest(classes = ExcludedPropertySourcesTest.class, properties = { "fr.insee.shared = inlineTestProperties", "fr.insee.specific.inlineTestProperties = inlineTestProperties"}, - args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args"} + args = { "--fr.insee.shared=args", "--fr.insee.specific.args=args","--spring.config.additional-location=classpath:/otherProps/,classpath:/nonExcluded/"} ) @Configuration class ExcludedPropertySourcesTest { @@ -35,11 +35,11 @@ static void createTempPropertyFileInFileSystem() throws IOException { fr.insee.shared = inFileSystem fr.insee.specific.inFileSystem = inFileSystem fr.insee.sharedWithExternal = inFileSystem - properties.logger.sources-ignored = systemProperties, systemEnvironment,application.properties,otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\\ Test\\ Properties,""" - + externalPropertiesPath.toAbsolutePath(); + 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.additional-location", "file:" + externalPropertiesPath.toAbsolutePath()); + System.setProperty("spring.config.import=", "file:" + externalPropertiesPath.toAbsolutePath()); } @DynamicPropertySource @@ -52,7 +52,7 @@ static void setProperties(DynamicPropertyRegistry registry) { @Test void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment environment) { - //GIVEN context and prop properties.logger.sources-ignored= systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined\ Test\ Properties + //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 @@ -60,8 +60,9 @@ void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment e System.out.println(logOutput); - assertThat(environment.getProperty("properties.logger.sources-ignored")).hasToString("systemProperties, systemEnvironment,[application.properties],otherProps/additionalPropsInClasspath.properties,commandLineArgs,Inlined Test Properties,"+ externalPropertiesPath.toAbsolutePath()); + 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") @@ -74,13 +75,13 @@ void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment e .doesNotContain("fr.insee.specific.inFileSystem") .doesNotContain("fr.insee.specific.inlineTestProperties") .doesNotContain("class path resource [application.properties]") - .doesNotContain("class path resource [otherProps/additionalPropsInClasspath.properties]") + .doesNotContain("class path resource [otherProps/application.properties]") .doesNotContain("- commandLineArgs") .doesNotContain("Config resource 'file ["+externalPropertiesPath+"]'") .doesNotContain("- Inlined Test Properties") .doesNotContain("- systemProperties") .doesNotContain("- systemEnvironment") - .contains("class path resource [nonExcludedFile.properties]") + .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"); diff --git a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java index fe1b8f2..599f788 100644 --- a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java +++ b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java @@ -11,7 +11,7 @@ import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = IssueWithDynamicPropertySourceTest.class ,properties = { - //"spring.config.additional-location=classpath:spring/issue/additionalPropsInClasspath.properties" + //"spring.config.additional-location=classpath:spring/issue/application.properties" } ) @Configuration @@ -19,12 +19,12 @@ class IssueWithDynamicPropertySourceTest { @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { - registry.add("spring.config.additional-location", () -> "classpath:spring/issue/additionalPropsInClasspath.properties"); + registry.add("spring.config.additional-location", () -> "classpath:spring/issue/application.properties"); } @Test void propertyFromAddtionalLocationShouldBeLoaded(@Autowired Environment environment) { - assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:spring/issue/additionalPropsInClasspath.properties"); + assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:spring/issue/application.properties"); assertThat(environment.getProperty("property.in.addtional.file")).hasToString("ok"); } diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index cd38736..5a37662 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -2,5 +2,4 @@ fr.insee.test = ok fr.insee.secret = SECRET fr.insee.shared = application.properties fr.insee.specific.applicationproperties = application.properties -spring.config.import=classpath:otherProps/additionalPropsInClasspath.properties,classpath:nonExcludedFile.properties properties.logger.prefix-for-properties=debug, trace, info, logging, spring, server, management, springdoc, properties, fr \ No newline at end of file diff --git a/src/test/resources/nonExcludedFile.properties b/src/test/resources/nonExcluded/application.properties similarity index 100% rename from src/test/resources/nonExcludedFile.properties rename to src/test/resources/nonExcluded/application.properties diff --git a/src/test/resources/otherProps/additionalPropsInClasspath.properties b/src/test/resources/otherProps/application.properties similarity index 100% rename from src/test/resources/otherProps/additionalPropsInClasspath.properties rename to src/test/resources/otherProps/application.properties From 033be824f230663d04243ad484461448ffef9824 Mon Sep 17 00:00:00 2001 From: Fabrice B Date: Thu, 12 Sep 2024 09:40:01 +0200 Subject: [PATCH 6/9] pom(upgrade minor version) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 248c3ff..a00ba74 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ fr.insee boot-properties-logger-starter - 1.0.1 + 1.1.0 jar From 0c3d5b830af06bb06d2a8422092dcebbde1a5239 Mon Sep 17 00:00:00 2001 From: Fabrice B Date: Thu, 12 Sep 2024 17:51:39 +0200 Subject: [PATCH 7/9] feat(excluded properties) WIP --- .../fr/insee/test/ExcludedPropertySourcesTest.java | 2 ++ .../java/fr/insee/test/WebAppIntegrationTest.java | 11 ++++++++--- .../issue/IssueWithDynamicPropertySourceTest.java | 5 +++-- ...sInClasspath.properties => application.properties} | 0 4 files changed, 13 insertions(+), 5 deletions(-) rename src/test/resources/spring/issue/{additionalPropsInClasspath.properties => application.properties} (100%) diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index e5696af..27bcd65 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -93,6 +93,8 @@ void contextLoad_shouldNotPrintExcludedPropertySources(@Autowired Environment e @AfterAll static void deleteTempPropertyFileInFileSystem() throws IOException { Files.delete(externalPropertiesPath); + System.clearProperty("spring.config.import"); + ((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0); } } diff --git a/src/test/java/fr/insee/test/WebAppIntegrationTest.java b/src/test/java/fr/insee/test/WebAppIntegrationTest.java index af04440..a556fb2 100644 --- a/src/test/java/fr/insee/test/WebAppIntegrationTest.java +++ b/src/test/java/fr/insee/test/WebAppIntegrationTest.java @@ -1,10 +1,15 @@ package fr.insee.test; import fr.insee.boot.PropertiesLogger; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; + +import java.io.BufferedReader; +import java.io.InputStreamReader; import static org.assertj.core.api.Assertions.assertThat; @@ -34,9 +39,9 @@ void contextLoads() { ); } - @Test - void excludedPropertySourcesTest(){ - + @AfterAll + static void clearLogStub(){ + ((Slf4jStub) LoggerFactory.getLogger(PropertiesLogger.class)).getStringBuilder().setLength(0); } } diff --git a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java index 599f788..580b313 100644 --- a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java +++ b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java @@ -11,6 +11,7 @@ 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" } ) @@ -19,12 +20,12 @@ class IssueWithDynamicPropertySourceTest { @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { - registry.add("spring.config.additional-location", () -> "classpath:spring/issue/application.properties"); + registry.add("spring.config.additional-location", () -> "classpath:/spring/issue/"); } @Test void propertyFromAddtionalLocationShouldBeLoaded(@Autowired Environment environment) { - assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:spring/issue/application.properties"); + assertThat(environment.getProperty("spring.config.additional-location")).hasToString("classpath:/spring/issue/"); assertThat(environment.getProperty("property.in.addtional.file")).hasToString("ok"); } diff --git a/src/test/resources/spring/issue/additionalPropsInClasspath.properties b/src/test/resources/spring/issue/application.properties similarity index 100% rename from src/test/resources/spring/issue/additionalPropsInClasspath.properties rename to src/test/resources/spring/issue/application.properties From f8f9c0b75bef0db7167ab33f0b507263ea2f9737 Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Thu, 12 Sep 2024 21:13:34 +0200 Subject: [PATCH 8/9] test (excluded sources) : finish #1 --- .../fr/insee/test/ExcludedPropertySourcesTest.java | 2 +- .../java/fr/insee/test/WebAppIntegrationTest.java | 8 +++----- .../issue/IssueWithDynamicPropertySourceTest.java | 11 +++++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java index 27bcd65..b985b67 100644 --- a/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java +++ b/src/test/java/fr/insee/test/ExcludedPropertySourcesTest.java @@ -39,7 +39,7 @@ static void createTempPropertyFileInFileSystem() throws IOException { + externalPropertiesPath.toAbsolutePath()+"]"; Files.writeString(externalPropertiesPath, content); - System.setProperty("spring.config.import=", "file:" + externalPropertiesPath.toAbsolutePath()); + System.setProperty("spring.config.import", "file:" + externalPropertiesPath.toAbsolutePath()); } @DynamicPropertySource diff --git a/src/test/java/fr/insee/test/WebAppIntegrationTest.java b/src/test/java/fr/insee/test/WebAppIntegrationTest.java index a556fb2..3ee29b8 100644 --- a/src/test/java/fr/insee/test/WebAppIntegrationTest.java +++ b/src/test/java/fr/insee/test/WebAppIntegrationTest.java @@ -6,16 +6,12 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.ClassPathResource; - -import java.io.BufferedReader; -import java.io.InputStreamReader; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(classes = WebAppIntegrationTest.class, properties = { "properties.logger.sources-ignored = systemEnvironment", - "properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr" + "properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr", }) @Configuration class WebAppIntegrationTest { @@ -35,6 +31,8 @@ void contextLoads() { properties.logger.prefix-for-properties = info, logging, spring, server, management, properties, springdoc, fr%n\ fr.insee.test = ok%n\ fr.insee.secret = ******%n\ + fr.insee.shared = application.properties%n\ + fr.insee.specific.applicationproperties = application.properties%n\ ================================================================================%n""").formatted() ); } diff --git a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java index 580b313..576b08f 100644 --- a/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java +++ b/src/test/java/spring/issue/IssueWithDynamicPropertySourceTest.java @@ -1,6 +1,11 @@ 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; @@ -24,9 +29,15 @@ static void setProperties(DynamicPropertyRegistry registry) { } @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); + } + } From b7b1b23b25489e34cc186e04c8deede57a84edf1 Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Thu, 12 Sep 2024 21:28:33 +0200 Subject: [PATCH 9/9] refactor(workflows (verify PR)) --- .github/workflows/check-pr.yml | 20 ++++++++++++++++++++ .github/workflows/deploy-snapshot.yml | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/check-pr.yml diff --git a/.github/workflows/check-pr.yml b/.github/workflows/check-pr.yml new file mode 100644 index 0000000..0d15fb9 --- /dev/null +++ b/.github/workflows/check-pr.yml @@ -0,0 +1,20 @@ +name: 'Build running test' + +on: + pull_request + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + # overrides the file ~/.m2/settings.xml with data to publish to maven pkg server with id github + overwrite-settings: true + - name: Build + run: | + mvn -B verify diff --git a/.github/workflows/deploy-snapshot.yml b/.github/workflows/deploy-snapshot.yml index ba9a453..5427d27 100644 --- a/.github/workflows/deploy-snapshot.yml +++ b/.github/workflows/deploy-snapshot.yml @@ -1,4 +1,4 @@ -name: 'Build and Deploy snapshot' +name: 'Build and Deploy' permissions: packages: write