make process function reliable testable and adjust unit test

This commit is contained in:
Armin Schrenk
2023-06-29 12:35:59 +02:00
parent 01da51e6e7
commit ebc60c4fb3
4 changed files with 16 additions and 15 deletions

View File

@@ -29,11 +29,12 @@ public class LazyProcessedProperties extends Properties {
//Template and env _need_ to be instance variables, otherwise they might not be initialized at access time
private final Pattern template = Pattern.compile("@\\{(\\w+)}");
private final Map<String, String> env = System.getenv();
private final Map<String, String> env;
private final Properties delegate;
public LazyProcessedProperties(Properties props) {
public LazyProcessedProperties(Properties props, Map<String, String> systemEnvironment) {
this.delegate = props;
this.env = systemEnvironment;
}
@Override
@@ -56,6 +57,7 @@ public class LazyProcessedProperties extends Properties {
}
}
//visible for testing
String process(String value) {
return template.matcher(value).replaceAll(match -> //
switch (match.group(1)) {

View File

@@ -30,16 +30,16 @@ import java.util.concurrent.Executors;
public class Cryptomator {
private static final long STARTUP_TIME = System.currentTimeMillis();
// DaggerCryptomatorComponent gets generated by Dagger.
// Run Maven and include target/generated-sources/annotations in your IDE.
static {
var lazyProcessedProps = new LazyProcessedProperties(System.getProperties());
var lazyProcessedProps = new LazyProcessedProperties(System.getProperties(), System.getenv());
System.setProperties(lazyProcessedProps);
CRYPTOMATOR_COMPONENT = DaggerCryptomatorComponent.factory().create(STARTUP_TIME);
LOG = LoggerFactory.getLogger(Cryptomator.class);
}
// DaggerCryptomatorComponent gets generated by Dagger.
// Run Maven and include target/generated-sources/annotations in your IDE.
private static final CryptomatorComponent CRYPTOMATOR_COMPONENT;
private static final Logger LOG;

View File

@@ -59,7 +59,7 @@ public class LogbackConfigurator extends ContextAwareBase implements Configurato
@Override
public ExecutionStatus configure(LoggerContext context) {
//we need to preprocess those, because every other class has a dependency to logging, none are initialized yet
var processedProps = new LazyProcessedProperties(System.getProperties());
var processedProps = new LazyProcessedProperties(System.getProperties(), System.getenv());
var useCustomCfg = Optional.ofNullable(processedProps.getProperty("logback.configurationFile")).map(s -> Files.exists(Path.of(s))).orElse(false);
var logDir = Optional.ofNullable(processedProps.getProperty("cryptomator.logDir")).map(Path::of).orElse(null);

View File

@@ -1,22 +1,21 @@
package org.cryptomator.common;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.Map;
public class LazyProcessedPropertiesTest {
@ParameterizedTest
@CsvSource(value = """
org.example.@{mytest1}.test, org.example.@{mytest1}.test
@{only*word*digits*under_score\\},@{only*words*digits*under_score\\}
C:\\Users\\@{appdir}\\dir, C:\\Users\\\\dir
@{@{appdir}},@{}
Longer @{appdir} text with @{appdir}., Longer text with .
""")
@CsvSource(value = {"org.example.@{mytest1}.test, org.example.@{mytest1}.test", //
"@{only*words*digits*under_score\\},@{only*words*digits*under_score\\}", //
"C:\\Users\\@{appdir}\\dir, C:\\Users\\foobar\\dir", //
"@{@{appdir}},@{foobar}", //
"Longer @{appdir} text with @{appdir}., Longer foobar text with foobar."})
public void test(String propertyValue, String expected) {
LazyProcessedProperties inTest = new LazyProcessedProperties(System.getProperties());
LazyProcessedProperties inTest = new LazyProcessedProperties(System.getProperties(), Map.of("APPDIR", "foobar"));
var result = inTest.process(propertyValue);
Assertions.assertEquals(result, expected);
}