Skip to content

Commit 769b924

Browse files
[LOG4J2-3805] NamedInstantPatternTest#compatibilityOfLegacyPattern fails in timezones with minute offsets (e.g., GMT+05:30) (#3888)
* [LOG4J2-3805] Skip compatibilityOfLegacyPattern test for fractional timezone offsets The compatibilityOfLegacyPattern test fails in environments where the system default timezone has a non-zero minute offset (e.g., Asia/Kolkata, Asia/Kathmandu). Root cause: - SimpleDateFormat's X pattern truncates fractional offsets (e.g., +05:30 → +05). - DateTimeFormatter's X pattern preserves the minutes (+05:30). Since Log4j intentionally follows DateTimeFormatter’s behavior, the test should not assert equivalence in such environments. This change adds an assumption to skip the test when the system timezone offset is not a whole hour. This ensures deterministic builds for contributors in all regions, while still verifying correctness in whole-hour zones. Closes #3805 * LOG4J2-3885: Add test for ISO8601_OFFSET_DATE_TIME_HH with system default zone Ensure legacy and modern formatting produce consistent results when the system default zone offset is a whole hour. Skip the test otherwise using assumptions.
1 parent e84655e commit 769b924

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/pattern/NamedInstantPatternTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package org.apache.logging.log4j.core.pattern;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assumptions.assumeThat;
2021

2122
import java.time.Instant;
23+
import java.time.ZoneId;
24+
import java.time.ZoneOffset;
2225
import org.apache.logging.log4j.core.time.MutableInstant;
2326
import org.apache.logging.log4j.core.util.internal.instant.InstantPatternFormatter;
2427
import org.junit.jupiter.params.ParameterizedTest;
@@ -29,6 +32,15 @@ class NamedInstantPatternTest {
2932
@ParameterizedTest
3033
@EnumSource(NamedInstantPattern.class)
3134
void compatibilityOfLegacyPattern(NamedInstantPattern namedPattern) {
35+
if (namedPattern == NamedInstantPattern.ISO8601_OFFSET_DATE_TIME_HH) {
36+
ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
37+
assumeThat(offset.getTotalSeconds() % 3600 == 0)
38+
.withFailMessage(
39+
"Skipping test: ISO8601_OFFSET_DATE_TIME_HH requires a whole-hour offset, but system offset is %s",
40+
offset)
41+
.isTrue();
42+
}
43+
3244
InstantPatternFormatter legacyFormatter = InstantPatternFormatter.newBuilder()
3345
.setPattern(namedPattern.getLegacyPattern())
3446
.setLegacyFormattersEnabled(true)
@@ -40,6 +52,8 @@ void compatibilityOfLegacyPattern(NamedInstantPattern namedPattern) {
4052
Instant javaTimeInstant = Instant.now();
4153
MutableInstant instant = new MutableInstant();
4254
instant.initFromEpochSecond(javaTimeInstant.getEpochSecond(), javaTimeInstant.getNano());
43-
assertThat(legacyFormatter.format(instant)).isEqualTo(formatter.format(instant));
55+
String legacy = legacyFormatter.format(instant);
56+
String modern = formatter.format(instant);
57+
assertThat(legacy).isEqualTo(modern);
4458
}
4559
}

0 commit comments

Comments
 (0)