change getProperty(key, default) and add unit test

This commit is contained in:
Armin Schrenk
2023-06-30 10:57:12 +02:00
parent 79eea4814e
commit ab048508a8
2 changed files with 16 additions and 7 deletions

View File

@@ -29,12 +29,8 @@ public class SubstitutingProperties extends PropertiesDecorator {
@Override
public String getProperty(String key, String defaultValue) {
var value = delegate.getProperty(key, defaultValue);
if (key.startsWith("cryptomator.") && value != null) {
return process(value);
} else {
return value;
}
var result = getProperty(key);
return result != null ? result : defaultValue;
}
//visible for testing

View File

@@ -70,7 +70,7 @@ public class SubstitutingPropertiesTest {
@ParameterizedTest
@DisplayName("Properties not starting with \"cryptomator.\" are not processed")
@ValueSource(strings = {"example.foo","cryptomatorSomething.foo","org.cryptomator.foo","cryPtoMAtor.foo"})
@ValueSource(strings = {"example.foo", "cryptomatorSomething.foo", "org.cryptomator.foo", "cryPtoMAtor.foo"})
public void testNoProcessingOnNotCryptomator(String propKey) {
var props = new Properties();
props.setProperty(propKey, "someValue");
@@ -92,6 +92,19 @@ public class SubstitutingPropertiesTest {
inTest.getProperty("cryptomator.prop");
Mockito.verify(inTest).process("someValue");
}
@Test
@DisplayName("Default value is not processed")
public void testNoProcessingDefault() {
var props = Mockito.mock(Properties.class);
Mockito.when(props.getProperty("cryptomator.prop")).thenReturn(null);
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of()));
Mockito.doReturn("someValue").when(inTest).process(Mockito.anyString());
var result = inTest.getProperty("cryptomator.prop", "a default");
Assertions.assertEquals("a default", result);
Mockito.verify(inTest, Mockito.never()).process(Mockito.any());
}
}
}