diff --git a/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java b/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java index 36c7dd247..12d1c50da 100644 --- a/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java +++ b/src/main/java/org/cryptomator/launcher/AdminPropertiesSetter.java @@ -92,7 +92,7 @@ class AdminPropertiesSetter { return systemProps; } - private static Map loadAdminProperties(Path adminPropertiesPath) { + static Map loadAdminProperties(Path adminPropertiesPath) { try (var in = Files.newInputStream(adminPropertiesPath, StandardOpenOption.READ)) { var map = JSON.readValue(in, new TypeReference>() {}); if (map == null) { diff --git a/src/main/java/org/cryptomator/launcher/BufferedLog.java b/src/main/java/org/cryptomator/launcher/BufferedLog.java index 0facf7734..68eb75e26 100644 --- a/src/main/java/org/cryptomator/launcher/BufferedLog.java +++ b/src/main/java/org/cryptomator/launcher/BufferedLog.java @@ -15,7 +15,7 @@ class BufferedLog { record Entry(String className, String message, List messageInput) {} synchronized static void log(String className, String message, List messageInput) { - logMessages.add(new BufferedLog.Entry(className, message, messageInput)); + logMessages.add(new Entry(className, message, messageInput)); } synchronized static void flushTo(Logger log) { diff --git a/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java b/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java new file mode 100644 index 000000000..31b6be658 --- /dev/null +++ b/src/test/java/org/cryptomator/launcher/AdminPropertiesSetterTest.java @@ -0,0 +1,98 @@ +package org.cryptomator.launcher; + +import com.fasterxml.jackson.databind.json.JsonMapper; +import org.hamcrest.MatcherAssert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.mockito.Answers; +import org.mockito.Mockito; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.hasEntry; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; + +public class AdminPropertiesSetterTest { + + private static final Map NO_STRING_CONFIG = Map.of("list", List.of("a", "b", "c"), // + "map", Map.of("a", 1, "b", 2)); + + private static final Map CONFIG = Map.of("kack", "dudel", // + "list", List.of("a", "b", "c"), // + "map", Map.of("a", 1, "b", 2)); + + @Test + @DisplayName("Loading valid JSON") + void loadValidJson(@TempDir Path path) throws IOException { + try (var adminPropSetterMock = Mockito.mockStatic(AdminPropertiesSetter.class)) { + adminPropSetterMock.when(() -> AdminPropertiesSetter.log(anyString(), any())).thenAnswer(Answers.RETURNS_DEFAULTS); + adminPropSetterMock.when(() -> AdminPropertiesSetter.loadAdminProperties(any())).thenCallRealMethod(); + var configPath = path.resolve("config.json"); + setupValidJson(configPath); + + var result = AdminPropertiesSetter.loadAdminProperties(configPath); + Assertions.assertAll(CONFIG.entrySet().stream().map((e) -> // + () -> MatcherAssert.assertThat(result, hasEntry(e.getKey(), e.getValue())))); + } + } + + @Test + @DisplayName("Loading not existing file") + void loadNotExistingFile(@TempDir Path path) { + try (var adminPropSetterMock = Mockito.mockStatic(AdminPropertiesSetter.class)) { + adminPropSetterMock.when(() -> AdminPropertiesSetter.log(anyString(), any())).thenAnswer(Answers.RETURNS_DEFAULTS); + adminPropSetterMock.when(() -> AdminPropertiesSetter.loadAdminProperties(any())).thenCallRealMethod(); + var configPath = path.resolve("config.json"); + + var result = AdminPropertiesSetter.loadAdminProperties(configPath); + MatcherAssert.assertThat(result, anEmptyMap()); + adminPropSetterMock.verify(() -> AdminPropertiesSetter.log(anyString(), any())); + } + } + + @Test + @DisplayName("Loading empty file") + void loadEmptyFile(@TempDir Path path) throws IOException { + try (var adminPropSetterMock = Mockito.mockStatic(AdminPropertiesSetter.class)) { + adminPropSetterMock.when(() -> AdminPropertiesSetter.log(anyString(), any())).thenAnswer(Answers.RETURNS_DEFAULTS); + adminPropSetterMock.when(() -> AdminPropertiesSetter.loadAdminProperties(any())).thenCallRealMethod(); + var configPath = path.resolve("config.json"); + Files.createFile(configPath); + + var result = AdminPropertiesSetter.loadAdminProperties(configPath); + MatcherAssert.assertThat(result, anEmptyMap()); + adminPropSetterMock.verify(() -> AdminPropertiesSetter.log(anyString(), any())); + } + } + + void setupValidJson(Path p) throws IOException { + var json = JsonMapper.builder().build(); + try (var out = Files.newOutputStream(p, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { + json.writerWithDefaultPrettyPrinter().writeValue(out, CONFIG); + } + } + + @Test + @DisplayName("Keys with non-String values are ignored") + void ignoreValues(@TempDir Path path) { + try (var adminPropSetterMock = Mockito.mockStatic(AdminPropertiesSetter.class)) { + adminPropSetterMock.when(() -> AdminPropertiesSetter.log(anyString(), any())).thenAnswer(Answers.RETURNS_DEFAULTS); + adminPropSetterMock.when(() -> AdminPropertiesSetter.loadAdminProperties(any())).thenReturn(NO_STRING_CONFIG); + adminPropSetterMock.when(AdminPropertiesSetter::adjustSystemProperties).thenCallRealMethod(); + + AdminPropertiesSetter.adjustSystemProperties(); + adminPropSetterMock.verify(() -> AdminPropertiesSetter.log(anyString(), any()), Mockito.times(NO_STRING_CONFIG.size())); + } + } + + +}