Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import static java.time.temporal.ChronoUnit.MINUTES;
import static java.time.temporal.ChronoUnit.NANOS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -36,7 +38,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceAccessMode;
Expand All @@ -45,19 +46,20 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junitpioneer.jupiter.Issue;
import org.junitpioneer.jupiter.ReadsSystemProperty;

public class PropertiesUtilTest {
class PropertiesUtilTest {

private final Properties properties = new Properties();

@BeforeEach
public void setUp() throws Exception {
void setUp() throws Exception {
properties.load(ClassLoader.getSystemResourceAsStream("PropertiesUtilTest.properties"));
}

@Test
public void testExtractSubset() {
void testExtractSubset() {
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "a"));
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "b."));
assertHasAllProperties(PropertiesUtil.extractSubset(properties, "c.1"));
Expand All @@ -67,7 +69,7 @@ public void testExtractSubset() {
}

@Test
public void testPartitionOnCommonPrefix() {
void testPartitionOnCommonPrefix() {
final Map<String, Properties> parts = PropertiesUtil.partitionOnCommonPrefixes(properties);
assertEquals(4, parts.size());
assertHasAllProperties(parts.get("a"));
Expand All @@ -85,7 +87,7 @@ private static void assertHasAllProperties(final Properties properties) {
}

@Test
public void testGetCharsetProperty() {
void testGetCharsetProperty() {
final Properties p = new Properties();
p.setProperty("e.1", StandardCharsets.US_ASCII.name());
p.setProperty("e.2", "wrong-charset-name");
Expand Down Expand Up @@ -130,8 +132,8 @@ static Stream<Arguments> should_properly_parse_duration() {

@ParameterizedTest
@MethodSource
void should_properly_parse_duration(final Duration expected, final String value) {
Assertions.assertThat(PropertiesUtil.parseDuration(value)).isEqualTo(expected);
void should_properly_parse_duration(final Duration expected, final CharSequence value) {
assertThat(PropertiesUtil.parseDuration(value)).isEqualTo(expected);
}

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

@ParameterizedTest
@MethodSource
void should_throw_on_invalid_duration(final String value) {
void should_throw_on_invalid_duration(final CharSequence value) {
assertThrows(IllegalArgumentException.class, () -> PropertiesUtil.parseDuration(value));
}

@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
public void testGetMappedProperty_sun_stdout_encoding() {
void testGetMappedProperty_sun_stdout_encoding() {
final PropertiesUtil pu = new PropertiesUtil(System.getProperties());
final Charset expected = System.console() == null ? Charset.defaultCharset() : StandardCharsets.UTF_8;
assertEquals(expected, pu.getCharsetProperty("sun.stdout.encoding"));
}

@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
public void testGetMappedProperty_sun_stderr_encoding() {
void testGetMappedProperty_sun_stderr_encoding() {
final PropertiesUtil pu = new PropertiesUtil(System.getProperties());
final Charset expected = System.console() == null ? Charset.defaultCharset() : StandardCharsets.UTF_8;
assertEquals(expected, pu.getCharsetProperty("sun.err.encoding"));
}

@Test
@ResourceLock(Resources.SYSTEM_PROPERTIES)
public void testNonStringSystemProperties() {
void testNonStringSystemProperties() {
final Object key1 = "1";
final Object key2 = new Object();
System.getProperties().put(key1, new Object());
Expand All @@ -180,14 +182,32 @@ public void testNonStringSystemProperties() {

@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
public void testPublish() {
void testPublish() {
final Properties props = new Properties();
final PropertiesUtil util = new PropertiesUtil(props);
new PropertiesUtil(props);
final String value = System.getProperty("Application");
assertNotNull(value, "System property was not published");
assertEquals("Log4j", value);
}

@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
@Issue("https://github.com/spring-projects/spring-boot/issues/33450")
void testBadPropertySource() {
final String key = "testKey";
final Properties props = new Properties();
props.put(key, "test");
final PropertiesUtil util = new PropertiesUtil(props);
final ErrorPropertySource source = new ErrorPropertySource();
util.addPropertySource(source);
try {
assertEquals("test", util.getStringProperty(key));
assertTrue(source.exceptionThrown);
} finally {
util.removePropertySource(source);
}
}

private static final String[][] data = {
{null, "org.apache.logging.log4j.level"},
{null, "Log4jAnotherProperty"},
Expand All @@ -209,7 +229,7 @@ public void testPublish() {
*/
@Test
@ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ)
public void testResolvesOnlyLog4jProperties() {
void testResolvesOnlyLog4jProperties() {
final PropertiesUtil util = new PropertiesUtil("Jira3413Test.properties");
for (final String[] pair : data) {
assertEquals(pair[0], util.getStringProperty(pair[1]));
Expand All @@ -222,7 +242,7 @@ public void testResolvesOnlyLog4jProperties() {
*/
@Test
@ReadsSystemProperty
public void testLog4jProperty() {
void testLog4jProperty() {
final Properties props = new Properties();
final String incorrect = "log4j2.";
final String correct = "not.starting.with.log4j";
Expand All @@ -231,4 +251,40 @@ public void testLog4jProperty() {
final PropertiesUtil util = new PropertiesUtil(props);
assertEquals(correct, util.getStringProperty(correct));
}

@Test
void should_support_multiple_sources_with_same_priority() {
final int priority = 2003;
final String key1 = "propertySource1";
final Properties props1 = new Properties();
props1.put(key1, "props1");
final String key2 = "propertySource2";
final Properties props2 = new Properties();
props2.put(key2, "props2");
final PropertiesUtil util = new PropertiesUtil(new PropertiesPropertySource(props1, priority));
util.addPropertySource(new PropertiesPropertySource(props2, priority));
assertThat(util.getStringProperty(key1)).isEqualTo("props1");
assertThat(util.getStringProperty(key2)).isEqualTo("props2");
}

private static class ErrorPropertySource implements PropertySource {
public boolean exceptionThrown = false;

@Override
public int getPriority() {
return Integer.MIN_VALUE;
}

@Override
public String getProperty(final String key) {
exceptionThrown = true;
throw new IllegalStateException("Test");
}

@Override
public boolean containsProperty(final String key) {
exceptionThrown = true;
throw new IllegalStateException("Test");
}
}
}
Loading