Browse Source

change getProperty(key, default) and add unit test

Armin Schrenk 1 year ago
parent
commit
ab048508a8

+ 2 - 6
src/main/java/org/cryptomator/common/SubstitutingProperties.java

@@ -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

+ 14 - 1
src/test/java/org/cryptomator/common/SubstitutingPropertiesTest.java

@@ -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());
+		}
 	}
 
 }