Polish "Fix potential NPE in GraylogExtendedLogFormatProperties"

See gh-43863
This commit is contained in:
Stéphane Nicoll 2025-01-19 08:24:28 +01:00
parent 9de517281e
commit 7c52938168
2 changed files with 11 additions and 2 deletions

View File

@ -32,7 +32,7 @@ import org.springframework.util.StringUtils;
*/
public record GraylogExtendedLogFormatProperties(String host, Service service) {
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE);
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, null);
public GraylogExtendedLogFormatProperties(String host, Service service) {
this.host = host;

View File

@ -46,7 +46,7 @@ class GraylogExtendedLogFormatPropertiesTests {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.structured.gelf.host", "spring");
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", Service.NONE));
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service(null)));
}
@Test
@ -58,6 +58,15 @@ class GraylogExtendedLogFormatPropertiesTests {
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
}
@Test
void getBindsFromEnvironmentWhenVersionIsPresentAndHostIsMissingUsesApplicationName() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.application.name", "spring");
environment.setProperty("logging.structured.gelf.service.version", "1.2.3");
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
}
@Test
void getWhenNoServiceNameUsesApplicationName() {
MockEnvironment environment = new MockEnvironment();