diff --git a/src/main/java/de/mediathekview/mlib/config/ConfigManager.java b/src/main/java/de/mediathekview/mlib/config/ConfigManager.java index fca52fcc..2e5e1af7 100644 --- a/src/main/java/de/mediathekview/mlib/config/ConfigManager.java +++ b/src/main/java/de/mediathekview/mlib/config/ConfigManager.java @@ -38,15 +38,21 @@ protected void initializeConfigAfterRead(final T config) { // Do something after the configuration is read } + /* + * check if the given file exists in FS or + * try to read a resource from filesystem + */ public String getResourcePath(String resourceName) { try { - ClassLoader classLoader = getClass().getClassLoader(); - URL resourceUrl = classLoader.getResource(resourceName); - if (resourceUrl != null) { + if (new java.io.File(resourceName).exists()) { + return resourceName; + } else { + ClassLoader classLoader = getClass().getClassLoader(); + URL resourceUrl = classLoader.getResource(resourceName); + if (resourceUrl != null) { Path resourcePath = Paths.get(resourceUrl.toURI()); return resourcePath.toString(); - } else { - return resourceName; + } } } catch(Exception e) {} return null; diff --git a/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java b/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java index c1c3af27..5b35de4a 100644 --- a/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java +++ b/src/test/java/de/mediathekview/mlib/config/ConfigManagerTest.java @@ -22,7 +22,7 @@ */ @TestMethodOrder(org.junit.jupiter.api.MethodOrderer.OrderAnnotation.class) class ConfigManagerTest { - private final String TEST_CONFIG_FILE_NAME = "TestConfig.yaml"; + private static final String TEST_CONFIG_FILE_NAME = "TestConfig.yaml"; class TestConfigManager extends ConfigManager { @@ -33,7 +33,7 @@ private TestConfigManager() { @Override protected String getConfigFileName() { - return (TEST_CONFIG_FILE_NAME); + return TEST_CONFIG_FILE_NAME; } @Override @@ -53,13 +53,38 @@ void testGetConfigFileName() { void testGetConfigClass() { assertThat(new TestConfigManager().getConfigClass()).isEqualTo(TestConfigDTO.class); } - + @Test @Order(3) void testReadClasspathConfig() { final TestConfigDTO classpathConfig = new TestConfigManager().getConfig(); - assertThat(classpathConfig.getValueWithDefault()).isEqualTo("TestValue"); - assertThat(classpathConfig.getValueWithoutDefault()).isEqualTo("Some other test value"); + assertThat(classpathConfig.getValueWithDefault()).isEqualTo("Hello World!"); + assertThat(classpathConfig.getValueWithoutDefault()).isEqualTo("Not the default, sorry!"); + } + + @Test + @Order(4) + void testReadFileConfig() throws IOException { + + writeTempTestFileConfig(); + + final TestConfigDTO fileConfig = new TestConfigManager().getConfig(); + assertThat(fileConfig.getValueWithDefault()).isEqualTo("FileConfig"); + assertThat(fileConfig.getValueWithoutDefault()).isEqualTo("Some other test value"); + } + + @BeforeEach + void deleteExistingFiles() throws IOException { + Files.deleteIfExists(Paths.get(TEST_CONFIG_FILE_NAME)); + } + + private void writeTempTestFileConfig() throws IOException { + final Path tempConfigPath = Paths.get("./" + TEST_CONFIG_FILE_NAME); + + Files.write( + tempConfigPath, + Arrays.asList("valueWithDefault: FileConfig", "valueWithoutDefault: Some other test value"), + StandardOpenOption.CREATE, + StandardOpenOption.TRUNCATE_EXISTING); } - }