Skip to content

Commit dd5df4b

Browse files
rgoersppkarwasz
authored andcommitted
Backport part of #2062
This commit includes fixes for: - Spring 33450 - Spring shutdown fails due to IllegalStateException (#2062) - [LOG4J2-3618] Fix property source comparator and review suggestions from #2454.
1 parent 4a60373 commit dd5df4b

File tree

6 files changed

+213
-75
lines changed

6 files changed

+213
-75
lines changed

log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java

+72-16
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import static java.time.temporal.ChronoUnit.MINUTES;
2424
import static java.time.temporal.ChronoUnit.NANOS;
2525
import static java.time.temporal.ChronoUnit.SECONDS;
26+
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.junit.jupiter.api.Assertions.assertEquals;
2728
import static org.junit.jupiter.api.Assertions.assertNotNull;
2829
import static org.junit.jupiter.api.Assertions.assertNull;
2930
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
3032

3133
import java.nio.charset.Charset;
3234
import java.nio.charset.StandardCharsets;
@@ -36,7 +38,6 @@
3638
import java.util.Map;
3739
import java.util.Properties;
3840
import java.util.stream.Stream;
39-
import org.assertj.core.api.Assertions;
4041
import org.junit.jupiter.api.BeforeEach;
4142
import org.junit.jupiter.api.Test;
4243
import org.junit.jupiter.api.parallel.ResourceAccessMode;
@@ -45,19 +46,20 @@
4546
import org.junit.jupiter.params.ParameterizedTest;
4647
import org.junit.jupiter.params.provider.Arguments;
4748
import org.junit.jupiter.params.provider.MethodSource;
49+
import org.junitpioneer.jupiter.Issue;
4850
import org.junitpioneer.jupiter.ReadsSystemProperty;
4951

50-
public class PropertiesUtilTest {
52+
class PropertiesUtilTest {
5153

5254
private final Properties properties = new Properties();
5355

5456
@BeforeEach
55-
public void setUp() throws Exception {
57+
void setUp() throws Exception {
5658
properties.load(ClassLoader.getSystemResourceAsStream("PropertiesUtilTest.properties"));
5759
}
5860

5961
@Test
60-
public void testExtractSubset() {
62+
void testExtractSubset() {
6163
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "a"));
6264
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "b."));
6365
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "c.1"));
@@ -67,7 +69,7 @@ public void testExtractSubset() {
6769
}
6870

6971
@Test
70-
public void testPartitionOnCommonPrefix() {
72+
void testPartitionOnCommonPrefix() {
7173
final Map<String, Properties> parts = PropertiesUtil.partitionOnCommonPrefixes(properties);
7274
assertEquals(4, parts.size());
7375
assertHasAllProperties(parts.get("a"));
@@ -85,7 +87,7 @@ private static void assertHasAllProperties(final Properties properties) {
8587
}
8688

8789
@Test
88-
public void testGetCharsetProperty() {
90+
void testGetCharsetProperty() {
8991
final Properties p = new Properties();
9092
p.setProperty("e.1", StandardCharsets.US_ASCII.name());
9193
p.setProperty("e.2", "wrong-charset-name");
@@ -130,8 +132,8 @@ static Stream<Arguments> should_properly_parse_duration() {
130132

131133
@ParameterizedTest
132134
@MethodSource
133-
void should_properly_parse_duration(final Duration expected, final String value) {
134-
Assertions.assertThat(PropertiesUtil.parseDuration(value)).isEqualTo(expected);
135+
void should_properly_parse_duration(final Duration expected, final CharSequence value) {
136+
assertThat(PropertiesUtil.parseDuration(value)).isEqualTo(expected);
135137
}
136138

137139
static List<String> should_throw_on_invalid_duration() {
@@ -142,29 +144,29 @@ static List<String> should_throw_on_invalid_duration() {
142144

143145
@ParameterizedTest
144146
@MethodSource
145-
void should_throw_on_invalid_duration(final String value) {
147+
void should_throw_on_invalid_duration(final CharSequence value) {
146148
assertThrows(IllegalArgumentException.class, () -> PropertiesUtil.parseDuration(value));
147149
}
148150

149151
@Test
150152
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
151-
public void testGetMappedProperty_sun_stdout_encoding() {
153+
void testGetMappedProperty_sun_stdout_encoding() {
152154
final PropertiesUtil pu = new PropertiesUtil(System.getProperties());
153155
final Charset expected = System.console() == null ? Charset.defaultCharset() : StandardCharsets.UTF_8;
154156
assertEquals(expected, pu.getCharsetProperty("sun.stdout.encoding"));
155157
}
156158

157159
@Test
158160
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
159-
public void testGetMappedProperty_sun_stderr_encoding() {
161+
void testGetMappedProperty_sun_stderr_encoding() {
160162
final PropertiesUtil pu = new PropertiesUtil(System.getProperties());
161163
final Charset expected = System.console() == null ? Charset.defaultCharset() : StandardCharsets.UTF_8;
162164
assertEquals(expected, pu.getCharsetProperty("sun.err.encoding"));
163165
}
164166

165167
@Test
166168
@ResourceLock(Resources.SYSTEM_PROPERTIES)
167-
public void testNonStringSystemProperties() {
169+
void testNonStringSystemProperties() {
168170
final Object key1 = "1";
169171
final Object key2 = new Object();
170172
System.getProperties().put(key1, new Object());
@@ -180,14 +182,32 @@ public void testNonStringSystemProperties() {
180182

181183
@Test
182184
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
183-
public void testPublish() {
185+
void testPublish() {
184186
final Properties props = new Properties();
185-
final PropertiesUtil util = new PropertiesUtil(props);
187+
new PropertiesUtil(props);
186188
final String value = System.getProperty("Application");
187189
assertNotNull(value, "System property was not published");
188190
assertEquals("Log4j", value);
189191
}
190192

193+
@Test
194+
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
195+
@Issue("https://github.com/spring-projects/spring-boot/issues/33450")
196+
void testBadPropertySource() {
197+
final String key = "testKey";
198+
final Properties props = new Properties();
199+
props.put(key, "test");
200+
final PropertiesUtil util = new PropertiesUtil(props);
201+
final ErrorPropertySource source = new ErrorPropertySource();
202+
util.addPropertySource(source);
203+
try {
204+
assertEquals("test", util.getStringProperty(key));
205+
assertTrue(source.exceptionThrown);
206+
} finally {
207+
util.removePropertySource(source);
208+
}
209+
}
210+
191211
private static final String[][] data = {
192212
{null, "org.apache.logging.log4j.level"},
193213
{null, "Log4jAnotherProperty"},
@@ -209,7 +229,7 @@ public void testPublish() {
209229
*/
210230
@Test
211231
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
212-
public void testResolvesOnlyLog4jProperties() {
232+
void testResolvesOnlyLog4jProperties() {
213233
final PropertiesUtil util = new PropertiesUtil("Jira3413Test.properties");
214234
for (final String[] pair : data) {
215235
assertEquals(pair[0], util.getStringProperty(pair[1]));
@@ -222,7 +242,7 @@ public void testResolvesOnlyLog4jProperties() {
222242
*/
223243
@Test
224244
@ReadsSystemProperty
225-
public void testLog4jProperty() {
245+
void testLog4jProperty() {
226246
final Properties props = new Properties();
227247
final String incorrect = "log4j2.";
228248
final String correct = "not.starting.with.log4j";
@@ -231,4 +251,40 @@ public void testLog4jProperty() {
231251
final PropertiesUtil util = new PropertiesUtil(props);
232252
assertEquals(correct, util.getStringProperty(correct));
233253
}
254+
255+
@Test
256+
void should_support_multiple_sources_with_same_priority() {
257+
final int priority = 2003;
258+
final String key1 = "propertySource1";
259+
final Properties props1 = new Properties();
260+
props1.put(key1, "props1");
261+
final String key2 = "propertySource2";
262+
final Properties props2 = new Properties();
263+
props2.put(key2, "props2");
264+
final PropertiesUtil util = new PropertiesUtil(new PropertiesPropertySource(props1, priority));
265+
util.addPropertySource(new PropertiesPropertySource(props2, priority));
266+
assertThat(util.getStringProperty(key1)).isEqualTo("props1");
267+
assertThat(util.getStringProperty(key2)).isEqualTo("props2");
268+
}
269+
270+
private static class ErrorPropertySource implements PropertySource {
271+
public boolean exceptionThrown = false;
272+
273+
@Override
274+
public int getPriority() {
275+
return Integer.MIN_VALUE;
276+
}
277+
278+
@Override
279+
public String getProperty(final String key) {
280+
exceptionThrown = true;
281+
throw new IllegalStateException("Test");
282+
}
283+
284+
@Override
285+
public boolean containsProperty(final String key) {
286+
exceptionThrown = true;
287+
throw new IllegalStateException("Test");
288+
}
289+
}
234290
}

0 commit comments

Comments
 (0)