Skip to content

Commit 2e0bb46

Browse files
committed
Refactor Log4j2LoggingSystemPropertiesTests to verify System.setProperty path
Signed-off-by: hojooo <[email protected]>
1 parent 16b17d8 commit 2e0bb46

File tree

1 file changed

+66
-63
lines changed

1 file changed

+66
-63
lines changed

core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemPropertiesTests.java

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@
1616

1717
package org.springframework.boot.logging.log4j2;
1818

19-
import java.util.LinkedHashMap;
20-
import java.util.Map;
21-
import java.util.function.BiConsumer;
19+
import java.util.HashSet;
20+
import java.util.Set;
2221

2322
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
2525

26+
import org.springframework.boot.convert.ApplicationConversionService;
2627
import org.springframework.boot.logging.LogFile;
27-
import org.springframework.core.env.Environment;
28-
import org.springframework.core.env.MapPropertySource;
29-
import org.springframework.core.env.StandardEnvironment;
28+
import org.springframework.boot.logging.LoggingSystemProperty;
29+
import org.springframework.core.convert.support.ConfigurableConversionService;
30+
import org.springframework.mock.env.MockEnvironment;
3031
import org.springframework.util.unit.DataSize;
3132

3233
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,86 +39,88 @@
3839
*/
3940
class Log4j2LoggingSystemPropertiesTests {
4041

41-
private final Map<String, String> systemProperties = new LinkedHashMap<>();
42+
private Set<Object> systemPropertyNames;
4243

43-
private final BiConsumer<String, String> systemPropertySetter = this.systemProperties::put;
44+
private MockEnvironment environment;
45+
46+
@BeforeEach
47+
void captureSystemPropertyNames() {
48+
for (LoggingSystemProperty property : LoggingSystemProperty.values()) {
49+
System.getProperties().remove(property.getEnvironmentVariableName());
50+
}
51+
this.systemPropertyNames = new HashSet<>(System.getProperties().keySet());
52+
this.environment = new MockEnvironment();
53+
this.environment
54+
.setConversionService((ConfigurableConversionService) ApplicationConversionService.getSharedInstance());
55+
}
4456

4557
@AfterEach
46-
void clearSystemProperties() {
47-
this.systemProperties.clear();
58+
void restoreSystemProperties() {
59+
System.getProperties().keySet().retainAll(this.systemPropertyNames);
4860
}
4961

5062
@Test
5163
void appliesLog4j2RollingPolicyProperties() {
52-
Environment environment = new StandardEnvironment();
53-
addPropertiesToEnvironment(environment, "logging.log4j2.rollingpolicy.max-file-size", "50MB",
54-
"logging.log4j2.rollingpolicy.clean-history-on-start", "true",
55-
"logging.log4j2.rollingpolicy.max-history", "30", "logging.log4j2.rollingpolicy.total-size-cap", "10GB",
56-
"logging.log4j2.rollingpolicy.file-name-pattern", "test.%d{yyyy-MM-dd}.%i.log",
57-
"logging.log4j2.rollingpolicy.strategy", "time", "logging.log4j2.rollingpolicy.time-based.interval",
58-
"2", "logging.log4j2.rollingpolicy.time-based.modulate", "true",
59-
"logging.log4j2.rollingpolicy.cron.schedule", "0 0 0 * * ?");
60-
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(null);
61-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE",
62-
String.valueOf(DataSize.ofMegabytes(50).toBytes()));
63-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_CLEAN_HISTORY_ON_START", "true");
64-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "30");
65-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_TOTAL_SIZE_CAP",
66-
String.valueOf(DataSize.ofGigabytes(10).toBytes()));
67-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_FILE_NAME_PATTERN",
68-
"test.%d{yyyy-MM-dd}.%i.log");
69-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_STRATEGY", "time");
70-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_TIME_INTERVAL", "2");
71-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_TIME_MODULATE", "true");
72-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_CRON_SCHEDULE", "0 0 0 * * ?");
64+
this.environment.setProperty("logging.log4j2.rollingpolicy.max-file-size", "50MB");
65+
this.environment.setProperty("logging.log4j2.rollingpolicy.clean-history-on-start", "true");
66+
this.environment.setProperty("logging.log4j2.rollingpolicy.max-history", "30");
67+
this.environment.setProperty("logging.log4j2.rollingpolicy.total-size-cap", "10GB");
68+
this.environment.setProperty("logging.log4j2.rollingpolicy.file-name-pattern", "test.%d{yyyy-MM-dd}.%i.log");
69+
this.environment.setProperty("logging.log4j2.rollingpolicy.strategy", "time");
70+
this.environment.setProperty("logging.log4j2.rollingpolicy.time-based.interval", "2");
71+
this.environment.setProperty("logging.log4j2.rollingpolicy.time-based.modulate", "true");
72+
this.environment.setProperty("logging.log4j2.rollingpolicy.cron.schedule", "0 0 0 * * ?");
73+
74+
new Log4j2LoggingSystemProperties(this.environment).apply(null);
75+
76+
assertThat(System.getProperties())
77+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE", String.valueOf(DataSize.ofMegabytes(50).toBytes()))
78+
.containsEntry("LOG4J2_ROLLINGPOLICY_CLEAN_HISTORY_ON_START", "true")
79+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "30")
80+
.containsEntry("LOG4J2_ROLLINGPOLICY_TOTAL_SIZE_CAP", String.valueOf(DataSize.ofGigabytes(10).toBytes()))
81+
.containsEntry("LOG4J2_ROLLINGPOLICY_FILE_NAME_PATTERN", "test.%d{yyyy-MM-dd}.%i.log")
82+
.containsEntry("LOG4J2_ROLLINGPOLICY_STRATEGY", "time")
83+
.containsEntry("LOG4J2_ROLLINGPOLICY_TIME_INTERVAL", "2")
84+
.containsEntry("LOG4J2_ROLLINGPOLICY_TIME_MODULATE", "true")
85+
.containsEntry("LOG4J2_ROLLINGPOLICY_CRON_SCHEDULE", "0 0 0 * * ?");
7386
}
7487

7588
@Test
7689
void appliesLog4j2RollingPolicyPropertiesWithDefaults() {
77-
Environment environment = new StandardEnvironment();
78-
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(null);
79-
// Should not set any rolling policy properties when not configured
80-
assertThat(this.systemProperties.keySet()).noneMatch((key) -> key.startsWith("LOG4J2_ROLLINGPOLICY"));
90+
new Log4j2LoggingSystemProperties(this.environment).apply(null);
91+
assertThat(System.getProperties().keySet())
92+
.noneMatch((key) -> ((String) key).startsWith("LOG4J2_ROLLINGPOLICY"));
8193
}
8294

8395
@Test
8496
void appliesDeprecatedProperties() {
85-
Environment environment = new StandardEnvironment();
86-
addPropertiesToEnvironment(environment, "logging.file.max-size", "20MB", "logging.file.max-history", "15");
87-
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(null);
88-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE",
89-
String.valueOf(DataSize.ofMegabytes(20).toBytes()));
90-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "15");
97+
this.environment.setProperty("logging.file.max-size", "20MB");
98+
this.environment.setProperty("logging.file.max-history", "15");
99+
new Log4j2LoggingSystemProperties(this.environment).apply(null);
100+
assertThat(System.getProperties())
101+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE", String.valueOf(DataSize.ofMegabytes(20).toBytes()))
102+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "15");
91103
}
92104

93105
@Test
94106
void newPropertiesOverrideDeprecatedProperties() {
95-
Environment environment = new StandardEnvironment();
96-
addPropertiesToEnvironment(environment, "logging.log4j2.rollingpolicy.max-file-size", "100MB",
97-
"logging.file.max-size", "20MB", "logging.log4j2.rollingpolicy.max-history", "50",
98-
"logging.file.max-history", "15");
99-
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(null);
100-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE",
101-
String.valueOf(DataSize.ofMegabytes(100).toBytes()));
102-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "50");
107+
this.environment.setProperty("logging.log4j2.rollingpolicy.max-file-size", "100MB");
108+
this.environment.setProperty("logging.file.max-size", "20MB");
109+
this.environment.setProperty("logging.log4j2.rollingpolicy.max-history", "50");
110+
this.environment.setProperty("logging.file.max-history", "15");
111+
new Log4j2LoggingSystemProperties(this.environment).apply(null);
112+
assertThat(System.getProperties())
113+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE", String.valueOf(DataSize.ofMegabytes(100).toBytes()))
114+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_HISTORY", "50");
103115
}
104116

105117
@Test
106118
void appliesWithLogFile() {
107-
Environment environment = new StandardEnvironment();
108-
addPropertiesToEnvironment(environment, "logging.log4j2.rollingpolicy.max-file-size", "25MB");
109-
LogFile logFile = LogFile.get(environment);
110-
new Log4j2LoggingSystemProperties(environment, this.systemPropertySetter).apply(logFile);
111-
assertThat(this.systemProperties).containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE",
112-
String.valueOf(DataSize.ofMegabytes(25).toBytes()));
113-
}
114-
115-
private void addPropertiesToEnvironment(Environment environment, String... pairs) {
116-
Map<String, Object> map = new LinkedHashMap<>();
117-
for (int i = 0; i < pairs.length; i += 2) {
118-
map.put(pairs[i], pairs[i + 1]);
119-
}
120-
((StandardEnvironment) environment).getPropertySources().addFirst(new MapPropertySource("test", map));
119+
this.environment.setProperty("logging.log4j2.rollingpolicy.max-file-size", "25MB");
120+
LogFile logFile = LogFile.get(this.environment);
121+
new Log4j2LoggingSystemProperties(this.environment).apply(logFile);
122+
assertThat(System.getProperties())
123+
.containsEntry("LOG4J2_ROLLINGPOLICY_MAX_FILE_SIZE", String.valueOf(DataSize.ofMegabytes(25).toBytes()));
121124
}
122125

123126
}

0 commit comments

Comments
 (0)