mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-20 03:31:27 +00:00
ensure logging for Substituting properties
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package org.cryptomator.common;
|
||||
|
||||
import org.jetbrains.annotations.VisibleForTesting;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@@ -13,10 +13,12 @@ public class SubstitutingProperties extends PropertiesDecorator {
|
||||
private static final Pattern TEMPLATE = Pattern.compile("@\\{(\\w+)}");
|
||||
|
||||
private final Map<String, String> env;
|
||||
private final Logger logger;
|
||||
|
||||
public SubstitutingProperties(Properties props, Map<String, String> systemEnvironment) {
|
||||
public SubstitutingProperties(Properties props, Map<String, String> systemEnvironment, Logger logger) {
|
||||
super(props);
|
||||
this.env = systemEnvironment;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,7 +46,7 @@ public class SubstitutingProperties extends PropertiesDecorator {
|
||||
case "localappdata" -> resolveFrom("LOCALAPPDATA", Source.ENV);
|
||||
case "userhome" -> resolveFrom("user.home", Source.PROPS);
|
||||
default -> {
|
||||
LoggerFactory.getLogger(SubstitutingProperties.class).warn("Unknown variable {} in property value {}.", match.group(), value);
|
||||
logger.warn("Unknown variable {} in property value {}.", match.group(), value);
|
||||
yield match.group();
|
||||
}
|
||||
});
|
||||
@@ -56,7 +58,7 @@ public class SubstitutingProperties extends PropertiesDecorator {
|
||||
case PROPS -> delegate.getProperty(key);
|
||||
};
|
||||
if (val == null) {
|
||||
LoggerFactory.getLogger(SubstitutingProperties.class).warn("Variable {} used for substitution not found in {}. Replaced with empty string.", key, src);
|
||||
logger.warn("Variable {} used for substitution not found in {}. Replaced with empty string.", key, src);
|
||||
return "";
|
||||
} else {
|
||||
return Matcher.quoteReplacement(val);
|
||||
|
||||
@@ -36,7 +36,7 @@ public class Cryptomator {
|
||||
|
||||
static {
|
||||
var adminProps = AdminPropertiesFactory.create();
|
||||
var lazyProcessedProps = new SubstitutingProperties(adminProps, System.getenv());
|
||||
var lazyProcessedProps = new SubstitutingProperties(adminProps, System.getenv(), EventualLogger.INSTANCE);
|
||||
System.setProperties(lazyProcessedProps);
|
||||
CRYPTOMATOR_COMPONENT = DaggerCryptomatorComponent.factory().create(STARTUP_TIME);
|
||||
LOG = LoggerFactory.getLogger(Cryptomator.class);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.cryptomator.common;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -8,12 +9,14 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.Mockito;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SubstitutingPropertiesTest {
|
||||
|
||||
Logger logger = Mockito.mock(Logger.class);
|
||||
SubstitutingProperties inTest;
|
||||
|
||||
@Nested
|
||||
@@ -28,7 +31,7 @@ public class SubstitutingPropertiesTest {
|
||||
@{@{appdir}},@{foobar}
|
||||
Replacing several @{appdir} with @{appdir}., Replacing several foobar with foobar.""")
|
||||
public void test(String propertyValue, String expected) {
|
||||
SubstitutingProperties inTest = new SubstitutingProperties(Mockito.mock(Properties.class), Map.of("APPDIR", "foobar"));
|
||||
SubstitutingProperties inTest = new SubstitutingProperties(Mockito.mock(Properties.class), Map.of("APPDIR", "foobar"), logger);
|
||||
var result = inTest.process(propertyValue);
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
@@ -39,7 +42,7 @@ public class SubstitutingPropertiesTest {
|
||||
var props = new Properties();
|
||||
props.setProperty("user.home", "OneUponABit");
|
||||
|
||||
inTest = new SubstitutingProperties(props, Map.of());
|
||||
inTest = new SubstitutingProperties(props, Map.of(), logger);
|
||||
var result = inTest.process("@{userhome}");
|
||||
Assertions.assertEquals("OneUponABit", result);
|
||||
}
|
||||
@@ -48,7 +51,7 @@ public class SubstitutingPropertiesTest {
|
||||
@ParameterizedTest(name = "Token \"{0}\" replaced with content of {1}")
|
||||
@CsvSource(value = {"appdir, APPDIR, foobar", "appdata, APPDATA, bazbaz", "localappdata, LOCALAPPDATA, boboAlice"})
|
||||
public void testEnvSubstitutions(String token, String envName, String expected) {
|
||||
inTest = new SubstitutingProperties(new Properties(), Map.of(envName, expected));
|
||||
inTest = new SubstitutingProperties(new Properties(), Map.of(envName, expected), logger);
|
||||
var result = inTest.process("@{" + token + "}");
|
||||
Assertions.assertEquals(expected, result);
|
||||
}
|
||||
@@ -62,7 +65,7 @@ public class SubstitutingPropertiesTest {
|
||||
@Test
|
||||
@DisplayName("Undefined properties are not processed")
|
||||
public void testNoProcessingOnNull() {
|
||||
inTest = Mockito.spy(new SubstitutingProperties(new Properties(), Map.of()));
|
||||
inTest = Mockito.spy(new SubstitutingProperties(new Properties(), Map.of(), logger));
|
||||
|
||||
var result = inTest.getProperty("some.prop");
|
||||
Assertions.assertNull(result);
|
||||
@@ -75,7 +78,7 @@ public class SubstitutingPropertiesTest {
|
||||
public void testNoProcessingOnNotCryptomator(String propKey) {
|
||||
var props = new Properties();
|
||||
props.setProperty(propKey, "someValue");
|
||||
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of()));
|
||||
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of(), logger));
|
||||
|
||||
var result = inTest.getProperty("some.prop");
|
||||
Assertions.assertNull(result);
|
||||
@@ -87,7 +90,7 @@ public class SubstitutingPropertiesTest {
|
||||
public void testProcessing() {
|
||||
var props = new Properties();
|
||||
props.setProperty("cryptomator.prop", "someValue");
|
||||
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of()));
|
||||
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of(), logger));
|
||||
Mockito.doReturn("someValue").when(inTest).process(Mockito.anyString());
|
||||
|
||||
inTest.getProperty("cryptomator.prop");
|
||||
@@ -99,7 +102,7 @@ public class SubstitutingPropertiesTest {
|
||||
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()));
|
||||
inTest = Mockito.spy(new SubstitutingProperties(props, Map.of(), logger));
|
||||
Mockito.doReturn("someValue").when(inTest).process(Mockito.anyString());
|
||||
|
||||
var result = inTest.getProperty("cryptomator.prop", "a default");
|
||||
@@ -120,7 +123,7 @@ public class SubstitutingPropertiesTest {
|
||||
var delegate = Mockito.mock(Properties.class);
|
||||
Mockito.doReturn("/home").when(delegate).getProperty("user.home");
|
||||
Mockito.doReturn(raw).when(delegate).getProperty(key);
|
||||
var inTest = new SubstitutingProperties(delegate, Map.of());
|
||||
var inTest = new SubstitutingProperties(delegate, Map.of(), logger);
|
||||
|
||||
var result = inTest.getProperty(key);
|
||||
|
||||
@@ -142,7 +145,7 @@ public class SubstitutingPropertiesTest {
|
||||
var env = Map.of("APPDATA", "C:\\Users\\JimFang\\AppData\\Roaming", //
|
||||
"LOCALAPPDATA", "C:\\Users\\JimFang\\AppData\\Local", //
|
||||
"APPDIR", "/squashfs1337/usr");
|
||||
var inTest = new SubstitutingProperties(delegate, env);
|
||||
var inTest = new SubstitutingProperties(delegate, env, logger);
|
||||
|
||||
var result = inTest.getProperty(key);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user